-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·589 lines (494 loc) · 17.7 KB
/
deploy.sh
File metadata and controls
executable file
·589 lines (494 loc) · 17.7 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
#!/usr/bin/env bash
# Consolidated deploy script for the multi-stack observability platform.
#
# Deploys from a clean release directory at /opt/observability-deploy/
# instead of the git working tree. Supports versioned releases with
# automatic backup and rollback.
#
# Usage:
# ./deploy.sh <stack> Deploy shared + stack (versioned release)
# ./deploy.sh shared Deploy shared services only
# ./deploy.sh status Show running services
#
# Available stacks: grafana-stack, openobserve
set -euo pipefail
SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEPLOY_ROOT="${DEPLOY_ROOT:-/opt/observability-deploy}"
RELEASE_ID="${RELEASE_ID:-$(date +%Y%m%d-%H%M%S)}"
RELEASE_DIR="$DEPLOY_ROOT/releases/$RELEASE_ID"
CURRENT_LINK="$DEPLOY_ROOT/current"
BACKUP_STATEFUL_VOLUMES="${BACKUP_STATEFUL_VOLUMES:-1}"
HEALTH_TIMEOUT_SECONDS="${HEALTH_TIMEOUT_SECONDS:-180}"
ENV_SOURCE="${ENV_SOURCE:-auto}"
KEEP_RELEASES="${KEEP_RELEASES:-5}"
KEEP_BACKUPS="${KEEP_BACKUPS:-10}"
ENABLE_DOCKER_PRUNE="${ENABLE_DOCKER_PRUNE:-1}"
DOCKER_PRUNE_UNTIL="${DOCKER_PRUNE_UNTIL:-336h}"
PRUNE_UNUSED_IMAGES="${PRUNE_UNUSED_IMAGES:-0}"
AVAILABLE_STACKS=("grafana-stack" "openobserve")
# ==================== COMPOSE HELPERS ====================
compose_shared() {
docker compose -p shared -f "$RELEASE_DIR/shared/docker-compose.yml" "$@"
}
compose_stack() {
docker compose -p "$STACK" -f "$RELEASE_DIR/$STACK/docker-compose.yml" "$@"
}
# ==================== UTILITY FUNCTIONS ====================
fix_service_volume_permissions() {
local compose_fn="$1"
local service_name="$2"
local mount_dest="$3"
local owner_uid="$4"
local owner_gid="$5"
local required_subdirs="$6"
local container_id
container_id="$($compose_fn ps --all -q "$service_name" | head -n1 || true)"
if [ -z "$container_id" ]; then
echo "WARN: missing container for service '$service_name'"
return 0
fi
local mount_info
mount_info="$(docker inspect "$container_id" \
--format "{{range .Mounts}}{{if eq .Destination \"$mount_dest\"}}{{.Type}}|{{.Name}}|{{.Source}}{{end}}{{end}}" \
2>/dev/null || true)"
if [ -z "$mount_info" ]; then
echo "WARN: missing mount '$mount_dest' for service '$service_name'"
return 0
fi
local mount_type mount_name mount_source
IFS='|' read -r mount_type mount_name mount_source <<< "$mount_info"
local mkdir_cmd="mkdir -p /target"
if [ -n "$required_subdirs" ]; then
for subdir in $required_subdirs; do
mkdir_cmd="$mkdir_cmd /target/$subdir"
done
fi
if [ "$mount_type" = "volume" ] && [ -n "$mount_name" ]; then
docker run --rm -v "$mount_name:/target" alpine sh -c \
"$mkdir_cmd && chown -R $owner_uid:$owner_gid /target && chmod -R 755 /target"
echo "OK: fixed volume '$mount_name' for $service_name"
return 0
fi
if [ "$(id -u)" -eq 0 ] && [ -n "$mount_source" ]; then
mkdir -p "$mount_source"
if [ -n "$required_subdirs" ]; then
for subdir in $required_subdirs; do
mkdir -p "$mount_source/$subdir"
done
fi
chown -R "$owner_uid:$owner_gid" "$mount_source"
chmod -R 755 "$mount_source"
echo "OK: fixed bind mount '$mount_source' for $service_name"
return 0
fi
echo "WARN: cannot chown bind mount '$mount_source' without root"
}
backup_service_mount() {
local compose_fn="$1"
local service_name="$2"
local mount_dest="$3"
local backup_name="$4"
local backup_dir="$DEPLOY_ROOT/backups/$RELEASE_ID"
local container_id
container_id="$($compose_fn ps --all -q "$service_name" | head -n1 || true)"
if [ -z "$container_id" ]; then
echo "WARN: cannot backup '$service_name' (container missing)"
return 0
fi
local mount_info
mount_info="$(docker inspect "$container_id" \
--format "{{range .Mounts}}{{if eq .Destination \"$mount_dest\"}}{{.Type}}|{{.Name}}|{{.Source}}{{end}}{{end}}" \
2>/dev/null || true)"
if [ -z "$mount_info" ]; then
echo "WARN: cannot backup '$service_name' mount '$mount_dest' (mount missing)"
return 0
fi
local mount_type mount_name mount_source
IFS='|' read -r mount_type mount_name mount_source <<< "$mount_info"
mkdir -p "$backup_dir"
if [ "$mount_type" = "volume" ] && [ -n "$mount_name" ]; then
docker run --rm -v "$mount_name:/source:ro" -v "$backup_dir:/backup" alpine sh -c \
"tar czf /backup/${backup_name}.tar.gz -C /source ."
echo "OK: backup '$service_name' → $backup_dir/${backup_name}.tar.gz"
return 0
fi
if [ -n "$mount_source" ] && [ -d "$mount_source" ]; then
tar czf "$backup_dir/${backup_name}.tar.gz" -C "$mount_source" .
echo "OK: backup '$service_name' → $backup_dir/${backup_name}.tar.gz"
return 0
fi
echo "WARN: cannot backup '$service_name' mount '$mount_dest'"
}
service_ready() {
local compose_fn="$1"
local service_name="$2"
local container_id
container_id="$($compose_fn ps --all -q "$service_name" | head -n1 || true)"
if [ -z "$container_id" ]; then
return 1
fi
local status health
status="$(docker inspect --format '{{.State.Status}}' "$container_id" 2>/dev/null || true)"
health="$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$container_id" 2>/dev/null || true)"
if [ "$status" != "running" ]; then
return 1
fi
# No healthcheck configured → running is good enough
if [ "$health" = "none" ] || [ "$health" = "healthy" ]; then
return 0
fi
return 1
}
wait_for_services() {
local compose_fn="$1"
local timeout="$2"
shift 2
local services=("$@")
local start_ts now_ts elapsed
start_ts="$(date +%s)"
while true; do
local pending=0
for svc in "${services[@]}"; do
if ! service_ready "$compose_fn" "$svc"; then
pending=1
fi
done
if [ "$pending" -eq 0 ]; then
return 0
fi
now_ts="$(date +%s)"
elapsed=$((now_ts - start_ts))
if [ "$elapsed" -ge "$timeout" ]; then
return 1
fi
sleep 3
done
}
cleanup_old_dirs() {
local base_dir="$1"
local keep_count="$2"
local label="$3"
local pruned=0
if [ "$keep_count" -lt 0 ]; then
echo "WARN: invalid keep count for $label: $keep_count"
return 0
fi
if [ ! -d "$base_dir" ]; then
return 0
fi
mapfile -t items < <(ls -1dt "$base_dir"/* 2>/dev/null || true)
if [ "${#items[@]}" -le "$keep_count" ]; then
echo "OK: $label retention satisfied (${#items[@]} <= $keep_count)"
return 0
fi
for item in "${items[@]:$keep_count}"; do
rm -rf "$item"
pruned=$((pruned + 1))
done
echo "OK: pruned $pruned old $label item(s)"
}
prune_docker_cache() {
if [ "$ENABLE_DOCKER_PRUNE" != "1" ]; then
echo "Skipping Docker prune (ENABLE_DOCKER_PRUNE=$ENABLE_DOCKER_PRUNE)"
return 0
fi
echo "Pruning dangling Docker images older than $DOCKER_PRUNE_UNTIL..."
docker image prune -f --filter "until=$DOCKER_PRUNE_UNTIL" || true
if [ "$PRUNE_UNUSED_IMAGES" = "1" ]; then
docker image prune -af --filter "until=$DOCKER_PRUNE_UNTIL" || true
fi
docker builder prune -f --filter "until=$DOCKER_PRUNE_UNTIL" || true
docker container prune -f --filter "until=$DOCKER_PRUNE_UNTIL" || true
}
resolve_env() {
local component_dir="$1"
local source_env="$SOURCE_DIR/$component_dir/.env"
local current_env="$CURRENT_LINK/$component_dir/.env"
local release_env="$RELEASE_DIR/$component_dir/.env"
local example_env="$RELEASE_DIR/$component_dir/.env.example"
if [ "$ENV_SOURCE" = "current" ]; then
if [ ! -f "$current_env" ]; then
echo "ERROR: ENV_SOURCE=current but no .env at $current_env"
exit 1
fi
cp "$current_env" "$release_env"
echo " $component_dir: using $current_env"
elif [ "$ENV_SOURCE" = "source" ]; then
if [ ! -f "$source_env" ]; then
echo "ERROR: ENV_SOURCE=source but no .env at $source_env"
exit 1
fi
cp "$source_env" "$release_env"
echo " $component_dir: using $source_env"
else
# auto: prefer current, then source, then .env.example
if [ -f "$current_env" ]; then
cp "$current_env" "$release_env"
echo " $component_dir: using $current_env"
elif [ -f "$source_env" ]; then
cp "$source_env" "$release_env"
echo " $component_dir: using $source_env"
elif [ -f "$example_env" ]; then
cp "$example_env" "$release_env"
echo " $component_dir: using .env.example (WARN: update secrets!)"
else
echo " $component_dir: no .env found (skipping)"
fi
fi
}
# ==================== STACK-SPECIFIC DEPLOY FUNCTIONS ====================
deploy_shared() {
echo ""
echo "==> Deploying shared services..."
compose_shared create caddy redis-autolog 2>/dev/null || true
if [ "$BACKUP_STATEFUL_VOLUMES" = "1" ]; then
echo "Backing up shared volumes..."
backup_service_mount compose_shared "redis-autolog" "/data" "shared-redis-autolog"
fi
compose_shared up -d
echo "Waiting for shared services..."
if ! wait_for_services compose_shared "$HEALTH_TIMEOUT_SECONDS" \
caddy redis-autolog autolog-service; then
echo "ERROR: shared services health check failed"
compose_shared ps
compose_shared logs --tail=60 caddy redis-autolog autolog-service || true
return 1
fi
echo "OK: shared services healthy"
}
deploy_grafana_stack() {
echo ""
echo "==> Deploying grafana-stack..."
# Fix file permissions for bind-mounted configs
find "$RELEASE_DIR/grafana-stack/grafana/provisioning" "$RELEASE_DIR/grafana-stack/grafana/dashboards" \
-type d -exec chmod a+rx {} + 2>/dev/null || true
find "$RELEASE_DIR/grafana-stack/grafana/provisioning" "$RELEASE_DIR/grafana-stack/grafana/dashboards" \
-type f \( -name "*.yml" -o -name "*.yaml" -o -name "*.json" \) -exec chmod a+r {} + 2>/dev/null || true
chmod a+r "$RELEASE_DIR"/grafana-stack/prometheus/*.yml \
"$RELEASE_DIR"/grafana-stack/prometheus/alerts/*.yml \
"$RELEASE_DIR"/grafana-stack/loki/loki.yml \
"$RELEASE_DIR"/grafana-stack/tempo/tempo.yml \
"$RELEASE_DIR"/grafana-stack/promtail/*.yml \
"$RELEASE_DIR"/grafana-stack/alertmanager/alertmanager.yml \
"$RELEASE_DIR"/grafana-stack/otel-collector/otel-collector-config.yml 2>/dev/null || true
echo "Creating stateful services..."
compose_stack create grafana prometheus loki tempo alertmanager >/dev/null
if [ "$BACKUP_STATEFUL_VOLUMES" = "1" ]; then
echo "Backing up grafana-stack volumes..."
backup_service_mount compose_stack "grafana" "/var/lib/grafana" "grafana-storage"
backup_service_mount compose_stack "prometheus" "/prometheus" "prometheus-storage"
backup_service_mount compose_stack "loki" "/loki" "loki-storage"
backup_service_mount compose_stack "tempo" "/tmp/tempo" "tempo-storage"
backup_service_mount compose_stack "alertmanager" "/alertmanager" "alertmanager-storage"
fi
echo "Fixing persistent volume permissions..."
fix_service_volume_permissions compose_stack "grafana" "/var/lib/grafana" "472" "472" ""
fix_service_volume_permissions compose_stack "prometheus" "/prometheus" "65534" "65534" ""
fix_service_volume_permissions compose_stack "loki" "/loki" "10001" "10001" "chunks rules compactor"
fix_service_volume_permissions compose_stack "tempo" "/tmp/tempo" "10001" "10001" "blocks wal generator"
fix_service_volume_permissions compose_stack "alertmanager" "/alertmanager" "65534" "65534" ""
echo "Starting services..."
compose_stack up -d grafana prometheus loki tempo alertmanager promtail
compose_stack up -d otel-collector
echo "Waiting for grafana-stack services..."
if ! wait_for_services compose_stack "$HEALTH_TIMEOUT_SECONDS" \
grafana prometheus loki tempo alertmanager otel-collector; then
echo "ERROR: grafana-stack health check failed"
compose_stack ps
compose_stack logs --tail=120 tempo prometheus alertmanager otel-collector || true
return 1
fi
echo "OK: grafana-stack services healthy"
}
deploy_openobserve() {
echo ""
echo "==> Deploying openobserve..."
chmod a+r "$RELEASE_DIR"/openobserve/otel-collector/otel-collector-config.yml 2>/dev/null || true
echo "Creating stateful services..."
compose_stack create openobserve >/dev/null
if [ "$BACKUP_STATEFUL_VOLUMES" = "1" ]; then
echo "Backing up openobserve volumes..."
backup_service_mount compose_stack "openobserve" "/data" "openobserve-data"
fi
echo "Starting services..."
compose_stack up -d openobserve
compose_stack up -d otel-collector-openobserve
echo "Waiting for openobserve services..."
if ! wait_for_services compose_stack "$HEALTH_TIMEOUT_SECONDS" \
openobserve otel-collector-openobserve; then
echo "ERROR: openobserve health check failed"
compose_stack ps
compose_stack logs --tail=120 openobserve otel-collector-openobserve || true
return 1
fi
echo "OK: openobserve services healthy"
}
# ==================== ROLLBACK ====================
rollback() {
if [ -n "$PREVIOUS_RELEASE" ] && [ -d "$PREVIOUS_RELEASE" ]; then
echo ""
echo "Rolling back to previous release: $PREVIOUS_RELEASE"
# Rollback shared
if [ -f "$PREVIOUS_RELEASE/shared/docker-compose.yml" ]; then
docker compose -p shared -f "$PREVIOUS_RELEASE/shared/docker-compose.yml" up -d || true
fi
# Rollback stack
if [ -n "${STACK:-}" ] && [ -f "$PREVIOUS_RELEASE/$STACK/docker-compose.yml" ]; then
docker compose -p "$STACK" -f "$PREVIOUS_RELEASE/$STACK/docker-compose.yml" up -d || true
fi
ln -sfn "$PREVIOUS_RELEASE" "$CURRENT_LINK"
echo "Rolled back to: $PREVIOUS_RELEASE"
else
echo "No previous release to roll back to"
fi
}
# ==================== USAGE ====================
usage() {
echo "Usage: $0 <target>"
echo ""
echo "Targets:"
echo " shared Deploy shared infrastructure only (Caddy, Redis, autolog-service)"
echo " grafana-stack Deploy shared + Grafana stack"
echo " openobserve Deploy shared + OpenObserve stack"
echo " status Show running services across all components"
echo ""
echo "Environment variables:"
echo " DEPLOY_ROOT Deploy directory (default: /opt/observability-deploy)"
echo " ENV_SOURCE auto|current|source (default: auto)"
echo " HEALTH_TIMEOUT_SECONDS Health check timeout (default: 180)"
echo " BACKUP_STATEFUL_VOLUMES 0|1 (default: 1)"
echo " ENABLE_DOCKER_PRUNE 0|1 (default: 1)"
echo ""
echo "Examples:"
echo " $0 openobserve # Deploy shared + openobserve"
echo " $0 grafana-stack # Deploy shared + grafana-stack"
echo " $0 shared # Deploy shared services only"
echo " DEPLOY_ROOT=/tmp/test $0 openobserve # Test deploy to /tmp"
exit 1
}
# ==================== MAIN ====================
if [ $# -lt 1 ]; then
usage
fi
TARGET="$1"
# Handle status command
if [ "$TARGET" = "status" ]; then
echo "=== Shared services ==="
if [ -L "$CURRENT_LINK" ] && [ -f "$CURRENT_LINK/shared/docker-compose.yml" ]; then
docker compose -p shared -f "$CURRENT_LINK/shared/docker-compose.yml" ps 2>/dev/null || echo "(not running)"
else
echo "(not deployed)"
fi
for stack in "${AVAILABLE_STACKS[@]}"; do
echo ""
echo "=== $stack ==="
if [ -L "$CURRENT_LINK" ] && [ -f "$CURRENT_LINK/$stack/docker-compose.yml" ]; then
docker compose -p "$stack" -f "$CURRENT_LINK/$stack/docker-compose.yml" ps 2>/dev/null || echo "(not running)"
else
echo "(not deployed)"
fi
done
echo ""
if [ -L "$CURRENT_LINK" ]; then
echo "Current release: $(readlink "$CURRENT_LINK")"
fi
exit 0
fi
# Validate target
STACK=""
if [ "$TARGET" = "shared" ]; then
STACK=""
elif printf '%s\n' "${AVAILABLE_STACKS[@]}" | grep -qx "$TARGET"; then
STACK="$TARGET"
else
echo "ERROR: unknown target '$TARGET'"
echo ""
usage
fi
# Prerequisites
if ! command -v docker >/dev/null 2>&1; then
echo "ERROR: docker is not installed"
exit 1
fi
if ! docker compose version >/dev/null 2>&1; then
echo "ERROR: docker compose plugin is not installed"
exit 1
fi
if ! command -v rsync >/dev/null 2>&1; then
echo "ERROR: rsync is required"
exit 1
fi
if [ "$ENV_SOURCE" != "auto" ] && [ "$ENV_SOURCE" != "current" ] && [ "$ENV_SOURCE" != "source" ]; then
echo "ERROR: ENV_SOURCE must be one of: auto, current, source"
exit 1
fi
echo "========================================="
echo " Deploying release $RELEASE_ID"
echo " Target: $TARGET"
echo " Deploy root: $DEPLOY_ROOT"
echo "========================================="
# Track previous release for rollback
PREVIOUS_RELEASE=""
if [ -L "$CURRENT_LINK" ]; then
PREVIOUS_RELEASE="$(readlink "$CURRENT_LINK")"
fi
# Create release directory and rsync repo
mkdir -p "$RELEASE_DIR"
mkdir -p "$DEPLOY_ROOT/releases"
echo ""
echo "Syncing source to release directory..."
rsync -a --delete \
--exclude '.git' \
--exclude '.env' \
--exclude '.claude' \
--exclude '.idea' \
"$SOURCE_DIR/" "$RELEASE_DIR/"
# Resolve .env files
echo ""
echo "Resolving environment files..."
resolve_env "shared"
if [ -n "$STACK" ]; then
resolve_env "$STACK"
fi
# Pull images
echo ""
echo "Pulling images..."
compose_shared pull
if [ -n "$STACK" ]; then
compose_stack pull
fi
# Deploy shared services (always)
if ! deploy_shared; then
rollback
exit 1
fi
# Deploy stack (if specified)
if [ -n "$STACK" ]; then
case "$STACK" in
grafana-stack)
if ! deploy_grafana_stack; then
rollback
exit 1
fi
;;
openobserve)
if ! deploy_openobserve; then
rollback
exit 1
fi
;;
esac
fi
# Finalize
ln -sfn "$RELEASE_DIR" "$CURRENT_LINK"
echo ""
echo "Cleaning up..."
cleanup_old_dirs "$DEPLOY_ROOT/releases" "$KEEP_RELEASES" "release"
cleanup_old_dirs "$DEPLOY_ROOT/backups" "$KEEP_BACKUPS" "backup"
prune_docker_cache
echo ""
echo "========================================="
echo " Deployment complete"
echo "========================================="
echo "Release: $RELEASE_DIR"
echo "Status: $0 status"