Repository navigation
Expand file tree
/
Copy pathupgrade-lightstack.sh
More file actions
executable file
·600 lines (497 loc) · 17.1 KB
/
Copy pathupgrade-lightstack.sh
File metadata and controls
executable file
·600 lines (497 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#!/bin/bash
#
# Lightstack Upgrade Script
# Safely upgrades Lightstack to the latest version with backup and rollback capability
#
# Usage: sudo ./scripts/upgrade-lightstack.sh [--version VERSION] [--force] [--no-backup]
#
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Default configuration
LIGHTSTACK_DIR="/opt/lightstack"
BACKUP_DIR="/opt/lightstack-backups"
CONFIG_FILE="/opt/lightstack/config/lightstack.yaml"
SYSTEMD_SERVICE="lightstack.service"
TARGET_VERSION="latest"
FORCE_UPGRADE=false
NO_BACKUP=false
# Logging functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Banner
show_banner() {
cat << "EOF"
╔═══════════════════════════════════════════════════════════╗
║ ║
║ lightstack Upgrade Script ║
║ Safely upgrade to the latest version ║
║ ║
╚═══════════════════════════════════════════════════════════╝
EOF
echo ""
}
# Parse command line arguments
parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
--version)
TARGET_VERSION="$2"
shift 2
;;
--force)
FORCE_UPGRADE=true
shift
;;
--no-backup)
NO_BACKUP=true
shift
;;
--help|-h)
show_help
exit 0
;;
*)
log_error "Unknown option: $1"
show_help
exit 1
;;
esac
done
}
# Show help
show_help() {
cat << EOF
Usage: sudo ./scripts/upgrade-lightstack.sh [OPTIONS]
Options:
--version VERSION Upgrade to specific version (tag or branch)
Default: latest (main branch)
--force Force upgrade even if same version
--no-backup Skip backup creation (not recommended)
-h, --help Show this help message
Examples:
# Upgrade to latest version
sudo ./upgrade-lightstack.sh
# Upgrade to specific version
sudo ./upgrade-lightstack.sh --version v0.5.0
# Force upgrade
sudo ./upgrade-lightstack.sh --force
# Upgrade without backup (dangerous)
sudo ./upgrade-lightstack.sh --no-backup
Backup location: /opt/lightstack-backups/
Rollback: sudo ./upgrade-lightstack.sh --version <backup-tag>
EOF
}
# Check if running as root
check_root() {
if [ "$EUID" -ne 0 ]; then
log_error "This script must be run as root (use sudo)"
exit 1
fi
}
# Check if Lightstack is installed
check_installation() {
if [ ! -d "$LIGHTSTACK_DIR" ]; then
log_error "Lightstack not found at $LIGHTSTACK_DIR"
log_error "Please install Lightstack first or adjust LIGHTSTACK_DIR path"
log_error "Installed under the product's previous name? The install layout has to be"
log_error "renamed first — see docs/UPGRADING.md."
exit 1
fi
if [ ! -f "$LIGHTSTACK_DIR/bin/lightstack" ]; then
log_error "Lightstack binary not found at $LIGHTSTACK_DIR/bin/lightstack"
exit 1
fi
if [ ! -f "$CONFIG_FILE" ]; then
log_error "Configuration file not found at $CONFIG_FILE"
exit 1
fi
log_success "Lightstack installation found at $LIGHTSTACK_DIR"
}
# Get current version
get_current_version() {
cd "$LIGHTSTACK_DIR"
# Try to get version from git
if [ -d .git ]; then
CURRENT_VERSION=$(git describe --tags --always 2>/dev/null || echo "unknown")
CURRENT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
else
CURRENT_VERSION="unknown"
CURRENT_COMMIT="unknown"
fi
log_info "Current version: $CURRENT_VERSION (commit: $CURRENT_COMMIT)"
}
# Check if service is running
check_service_status() {
if systemctl is-active --quiet "$SYSTEMD_SERVICE"; then
SERVICE_RUNNING=true
log_info "Lightstack service is running"
else
SERVICE_RUNNING=false
log_warning "Lightstack service is not running"
fi
}
# Create backup
create_backup() {
if [ "$NO_BACKUP" = true ]; then
log_warning "Skipping backup (--no-backup flag)"
return
fi
log_info "Creating backup..."
# Create backup directory
mkdir -p "$BACKUP_DIR"
# Generate backup name with timestamp
BACKUP_NAME="lightstack-backup-$(date +%Y%m%d-%H%M%S)-$CURRENT_COMMIT"
BACKUP_PATH="$BACKUP_DIR/$BACKUP_NAME"
# Create backup directory
mkdir -p "$BACKUP_PATH"
# Backup binary
if [ -f "$LIGHTSTACK_DIR/bin/lightstack" ]; then
cp "$LIGHTSTACK_DIR/bin/lightstack" "$BACKUP_PATH/lightstack-binary"
fi
# Backup configuration
if [ -f "$CONFIG_FILE" ]; then
cp "$CONFIG_FILE" "$BACKUP_PATH/lightstack.yaml"
fi
# Backup git info
cd "$LIGHTSTACK_DIR"
if [ -d .git ]; then
git rev-parse HEAD > "$BACKUP_PATH/commit-hash.txt"
git describe --tags --always > "$BACKUP_PATH/version.txt" 2>/dev/null || true
fi
# Create backup metadata
cat > "$BACKUP_PATH/metadata.txt" <<EOF
Backup Date: $(date)
Version: $CURRENT_VERSION
Commit: $CURRENT_COMMIT
Service Status: $SERVICE_RUNNING
EOF
log_success "Backup created: $BACKUP_PATH"
# Keep only last 5 backups
cd "$BACKUP_DIR"
ls -t | tail -n +6 | xargs -r rm -rf
log_info "Kept last 5 backups, removed older ones"
}
# Stop Lightstack service
stop_service() {
if [ "$SERVICE_RUNNING" = true ]; then
log_info "Stopping Lightstack service..."
systemctl stop "$SYSTEMD_SERVICE"
# Wait for service to stop
for i in {1..10}; do
if ! systemctl is-active --quiet "$SYSTEMD_SERVICE"; then
log_success "Service stopped"
return
fi
sleep 1
done
log_error "Failed to stop service"
exit 1
fi
}
# Migrate a pre-rename node's local state (cloud-storage#575). The default state root
# moved with the product rename, so without this the upgraded binary writes to the new
# root and stops seeing the image bases, volumes, overlays, cloud-init ISOs and dnsmasq
# state already on this node. Runs with the service stopped, and is a no-op on any
# install that never ran under the old name.
migrate_legacy_state() {
# Prefer the copy next to this script — on a checkout install the tree has not been
# updated yet, so $LIGHTSTACK_DIR still holds the pre-upgrade version.
local migrate_script
migrate_script="$(dirname "${BASH_SOURCE[0]}")/migrate-legacy-state.sh"
if [ ! -x "$migrate_script" ]; then
migrate_script="$LIGHTSTACK_DIR/scripts/migrate-legacy-state.sh"
fi
if [ ! -x "$migrate_script" ]; then
log_warning "migrate-legacy-state.sh not found — skipping the legacy state check"
log_warning "If this node ran under the previous product name, see docs/UPGRADING.md"
return
fi
log_info "Checking for pre-rename node-local state..."
"$migrate_script" --apply
}
# Pull latest code
update_code() {
log_info "Updating code from repository..."
cd "$LIGHTSTACK_DIR"
# Ensure we're in a git repository
if [ ! -d .git ]; then
log_error "$LIGHTSTACK_DIR is not a git repository"
exit 1
fi
# Stash any local changes
if ! git diff-index --quiet HEAD --; then
log_warning "Local changes detected, stashing..."
git stash save "Auto-stash before upgrade $(date)"
fi
# Fetch latest changes
log_info "Fetching latest changes..."
git fetch origin
# Determine target
if [ "$TARGET_VERSION" = "latest" ]; then
TARGET="origin/main"
log_info "Upgrading to latest version (main branch)"
else
TARGET="$TARGET_VERSION"
log_info "Upgrading to version: $TARGET_VERSION"
fi
# Check if target exists
if ! git rev-parse "$TARGET" >/dev/null 2>&1; then
log_error "Version/branch '$TARGET' not found"
exit 1
fi
# Get new version info
NEW_COMMIT=$(git rev-parse "$TARGET" | cut -c1-7)
# Check if already at target version
if [ "$CURRENT_COMMIT" = "$NEW_COMMIT" ] && [ "$FORCE_UPGRADE" = false ]; then
log_warning "Already at version $TARGET (commit: $NEW_COMMIT)"
log_info "Use --force to rebuild anyway"
# Start service if it was running
if [ "$SERVICE_RUNNING" = true ]; then
systemctl start "$SYSTEMD_SERVICE"
fi
exit 0
fi
# Checkout target version
log_info "Checking out $TARGET..."
git checkout "$TARGET"
git pull origin "$(git rev-parse --abbrev-ref HEAD)" 2>/dev/null || true
log_success "Code updated to $TARGET (commit: $NEW_COMMIT)"
}
# Build new binary
build_binary() {
log_info "Building new Lightstack binary..."
cd "$LIGHTSTACK_DIR"
# Check if Go is installed
if ! command -v go &> /dev/null; then
log_error "Go compiler not found. Install Go first."
exit 1
fi
# Build binary
if make build > /tmp/lightstack-build.log 2>&1; then
log_success "Binary built successfully"
else
log_error "Build failed. See /tmp/lightstack-build.log for details"
cat /tmp/lightstack-build.log
exit 1
fi
# Verify binary
if [ ! -f "$LIGHTSTACK_DIR/bin/lightstack" ]; then
log_error "Binary not found after build"
exit 1
fi
# Get binary version info
BINARY_SIZE=$(du -h "$LIGHTSTACK_DIR/bin/lightstack" | cut -f1)
log_info "Binary size: $BINARY_SIZE"
}
# Run database migrations
run_migrations() {
log_info "Running database migrations..."
cd "$LIGHTSTACK_DIR"
# Check if migration is needed
if ./bin/lightstack migrate --config "$CONFIG_FILE" --dry-run 2>/dev/null | grep -q "No migrations to run"; then
log_info "No new migrations needed"
return
fi
# Run migrations
if ./bin/lightstack migrate --config "$CONFIG_FILE" > /tmp/lightstack-migrate.log 2>&1; then
log_success "Database migrations completed"
else
log_error "Migration failed. See /tmp/lightstack-migrate.log for details"
cat /tmp/lightstack-migrate.log
# Ask if should rollback
read -p "Rollback to previous version? [Y/n]: " ROLLBACK
if [[ "$ROLLBACK" =~ ^[Yy]?$ ]]; then
perform_rollback
fi
exit 1
fi
}
# Start Lightstack service
start_service() {
if [ "$SERVICE_RUNNING" = true ]; then
log_info "Starting Lightstack service..."
systemctl start "$SYSTEMD_SERVICE"
# Wait for service to start
sleep 5
if systemctl is-active --quiet "$SYSTEMD_SERVICE"; then
log_success "Service started successfully"
else
log_error "Service failed to start"
log_error "Check logs: journalctl -u $SYSTEMD_SERVICE -n 50"
exit 1
fi
fi
}
# Verify upgrade
verify_upgrade() {
log_info "Verifying upgrade..."
# Check service status
if [ "$SERVICE_RUNNING" = true ]; then
if ! systemctl is-active --quiet "$SYSTEMD_SERVICE"; then
log_error "Service is not running after upgrade"
return 1
fi
fi
# Wait for services to be ready
log_info "Waiting for services to be ready (10 seconds)..."
sleep 10
# Test API endpoints if service is running
if [ "$SERVICE_RUNNING" = true ]; then
# Source environment if exists
if [ -f /root/.lightstack-env ]; then
source /root/.lightstack-env
fi
# Test Keystone endpoint
if curl -sf "http://localhost:35357/v3" > /dev/null 2>&1; then
log_success "✓ Keystone API responding"
else
log_warning "✗ Keystone API not responding"
return 1
fi
# Test token authentication if OpenStack CLI is available
if command -v openstack &> /dev/null; then
if openstack token issue > /dev/null 2>&1; then
log_success "✓ Authentication working"
else
log_warning "✗ Authentication failed"
fi
fi
fi
log_success "Upgrade verification completed"
return 0
}
# Perform rollback
perform_rollback() {
log_warning "Starting rollback..."
# Find latest backup
if [ ! -d "$BACKUP_DIR" ]; then
log_error "No backups found at $BACKUP_DIR"
exit 1
fi
LATEST_BACKUP=$(ls -t "$BACKUP_DIR" | head -n1)
if [ -z "$LATEST_BACKUP" ]; then
log_error "No backup available for rollback"
exit 1
fi
log_info "Rolling back to: $LATEST_BACKUP"
# Stop service
stop_service
# Restore binary
if [ -f "$BACKUP_DIR/$LATEST_BACKUP/lightstack-binary" ]; then
cp "$BACKUP_DIR/$LATEST_BACKUP/lightstack-binary" "$LIGHTSTACK_DIR/bin/lightstack"
chmod +x "$LIGHTSTACK_DIR/bin/lightstack"
log_success "Binary restored"
fi
# Restore git state if commit hash exists
if [ -f "$BACKUP_DIR/$LATEST_BACKUP/commit-hash.txt" ]; then
BACKUP_COMMIT=$(cat "$BACKUP_DIR/$LATEST_BACKUP/commit-hash.txt")
cd "$LIGHTSTACK_DIR"
git checkout "$BACKUP_COMMIT" 2>/dev/null || log_warning "Could not restore git state"
fi
# Start service
start_service
log_success "Rollback completed"
}
# Show upgrade summary
show_summary() {
echo ""
echo "╔═══════════════════════════════════════════════════════════╗"
echo "║ ║"
echo "║ Lightstack Upgrade Complete! 🎉 ║"
echo "║ ║"
echo "╚═══════════════════════════════════════════════════════════╝"
echo ""
log_info "Upgrade Summary:"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo " Previous Version: $CURRENT_VERSION (commit: $CURRENT_COMMIT)"
cd "$LIGHTSTACK_DIR"
NEW_VERSION=$(git describe --tags --always 2>/dev/null || echo "unknown")
NEW_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
echo " New Version: $NEW_VERSION (commit: $NEW_COMMIT)"
echo ""
echo " Backup Location: $BACKUP_PATH"
echo " Configuration: $CONFIG_FILE"
echo " Service Status: $(systemctl is-active $SYSTEMD_SERVICE 2>/dev/null || echo "stopped")"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
log_info "Next Steps:"
echo " 1. Verify services: systemctl status lightstack.service"
echo " 2. Check logs: journalctl -u lightstack.service -f"
echo " 3. Test API: openstack token issue"
echo ""
log_info "Rollback (if needed):"
echo " systemctl stop lightstack.service"
echo " cp $BACKUP_PATH/lightstack-binary /opt/lightstack/bin/lightstack"
echo " systemctl start lightstack.service"
echo ""
}
# Cleanup old backups
cleanup_backups() {
if [ -d "$BACKUP_DIR" ]; then
BACKUP_COUNT=$(ls "$BACKUP_DIR" | wc -l)
if [ "$BACKUP_COUNT" -gt 5 ]; then
log_info "Cleaning up old backups (keeping last 5)..."
cd "$BACKUP_DIR"
ls -t | tail -n +6 | xargs -r rm -rf
fi
fi
}
# Main upgrade flow
main() {
show_banner
parse_args "$@"
check_root
check_installation
get_current_version
check_service_status
echo ""
log_info "Starting upgrade process..."
echo ""
# Create backup before any changes
create_backup
# Stop service
stop_service
# Migrate a pre-rename node's local state, if any
migrate_legacy_state
# Update code
update_code
# Build new binary
build_binary
# Run migrations
run_migrations
# Start service
start_service
# Verify upgrade
if verify_upgrade; then
show_summary
cleanup_backups
else
log_error "Upgrade verification failed"
log_warning "Service may be running but API verification failed"
log_info "Check logs: journalctl -u $SYSTEMD_SERVICE -n 100"
read -p "Rollback to previous version? [y/N]: " ROLLBACK
if [[ "$ROLLBACK" =~ ^[Yy]$ ]]; then
perform_rollback
fi
exit 1
fi
}
# Run main function
main "$@"