- Variant B: docker-compose.yml gets configurable host ports and health checks. Variant A: deploy/allinone image (nginx, supervisord, gunicorn, uvicorn, gatherer, OpenVPN) with an explicit database migration step before any service; docker-compose.allinone.yml. Same five volumes and paths in both variants. - deploy/docker: install.sh, update.sh (backup, build, stop, separate DB migration phase, start, automatic rollback), backup.sh, restore.sh, lib.sh. - Verified on Docker: install A/B, update with pre-versioned databases, rollback after an injected failure, backup/restore. - Docs: Scenario1_Containers guide, change summary, README, Index; .dockerignore, .gitignore additions. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
262 lines
12 KiB
Bash
Executable File
262 lines
12 KiB
Bash
Executable File
#!/bin/sh
|
|
# Update OpenVPN Monitor & Profiler in containers without touching the data.
|
|
#
|
|
# deploy/docker/update.sh [--variant a|b] [--project NAME] [--yes] [--no-pull] [--rollback]
|
|
#
|
|
# --variant, --project normally read from .ovpmon-deploy (written by install.sh); needed only for
|
|
# a stack that was started by hand
|
|
# --yes no confirmation questions
|
|
# --no-pull no "git pull --ff-only" and no refresh of the base images (offline hosts)
|
|
# --rollback restore the volumes from the newest backup and start the previous images
|
|
#
|
|
# Phases: 0 preflight, 1 BACKUP of the five volumes, 2 git pull, 3 BUILD (old stack still running),
|
|
# 4 STOP, 5 DATABASE MIGRATION (own step, nothing is started yet), 6 START and health check.
|
|
# A failure in phases 4-6 triggers an automatic rollback: the volumes are restored from the backup of
|
|
# this run and the previous images are started. Volumes are never removed.
|
|
# See DOCS/Deployment/Scenario1_Containers.md and DOCS/General/Database_Migrations.md
|
|
set -eu
|
|
|
|
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
|
|
. "$SCRIPT_DIR/lib.sh"
|
|
|
|
usage() {
|
|
sed -n '2,16p' "$0" | sed 's/^# \{0,1\}//'
|
|
}
|
|
|
|
ARG_VARIANT=""
|
|
ARG_PROJECT=""
|
|
ASSUME_YES=0
|
|
NO_PULL=0
|
|
DO_ROLLBACK=0
|
|
|
|
BACKUP_DIR=""
|
|
CURRENT_PHASE="start"
|
|
ROLLBACK_ARMED=0
|
|
STACK_WAS_RUNNING=0
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Rollback
|
|
# ---------------------------------------------------------------------------------------------
|
|
rollback_failed() {
|
|
log "!!! ROLLBACK FAILED: $1" >&2
|
|
log "!!! Your data is safe in the backup: $BACKUP_DIR" >&2
|
|
log "!!! Retry the rollback: $SCRIPT_DIR/update.sh --rollback (or only the data: $SCRIPT_DIR/restore.sh $BACKUP_DIR)" >&2
|
|
}
|
|
|
|
# Automatic rollback after a failure in phases 4-6 (called from the EXIT trap)
|
|
auto_rollback() {
|
|
printf '\n' >&2
|
|
log "!!! UPDATE FAILED in phase: $CURRENT_PHASE" >&2
|
|
log "!!! Automatic rollback: restore the volumes from $BACKUP_DIR and start the previous images" >&2
|
|
dc stop >/dev/null 2>&1 || true
|
|
if ! restore_volumes "$BACKUP_DIR" 0; then
|
|
rollback_failed "the volumes could not be restored"
|
|
return 0
|
|
fi
|
|
if ! start_previous "$BACKUP_DIR"; then
|
|
rollback_failed "the previous images could not be started"
|
|
return 0
|
|
fi
|
|
if ! wait_healthy; then
|
|
rollback_failed "the previous version did not become healthy"
|
|
return 0
|
|
fi
|
|
log "!!! ROLLED BACK: the previous version is running again on the data as it was before the update." >&2
|
|
log "!!! The update failed in phase '$CURRENT_PHASE'; see the messages above. Backup kept in: $BACKUP_DIR" >&2
|
|
}
|
|
|
|
on_exit() {
|
|
_rc=$?
|
|
set +e
|
|
trap '' INT TERM
|
|
if [ "$_rc" -ne 0 ]; then
|
|
if [ "$ROLLBACK_ARMED" = 1 ]; then
|
|
ROLLBACK_ARMED=0
|
|
auto_rollback
|
|
elif [ -n "$BACKUP_DIR" ]; then
|
|
log "update stopped in phase '$CURRENT_PHASE' before the running stack was changed. Backup: $BACKUP_DIR" >&2
|
|
fi
|
|
fi
|
|
release_lock
|
|
exit "$_rc"
|
|
}
|
|
|
|
manual_rollback() {
|
|
BACKUP_DIR=$(latest_backup)
|
|
[ -n "$BACKUP_DIR" ] || die "no complete backup found in $BACKUP_ROOT"
|
|
log "newest backup: $BACKUP_DIR"
|
|
[ -f "$BACKUP_DIR/MANIFEST" ] && sed 's/^/ /' "$BACKUP_DIR/MANIFEST"
|
|
[ -s "$BACKUP_DIR/images.tsv" ] || die "the backup has no recorded images (images.tsv): use restore.sh to restore the data only"
|
|
log "the volumes of project $PROJECT will be REPLACED by this backup; the current content is saved first to $BACKUP_ROOT/<timestamp>-pre-restore"
|
|
confirm "Restore all volumes from this backup and start the previous images?" || die "aborted by the user"
|
|
phase R1 "stop the stack"
|
|
dc stop
|
|
phase R2 "restore the volumes"
|
|
restore_volumes "$BACKUP_DIR" 1 || die "restore failed (see above). The backup is intact: $BACKUP_DIR"
|
|
phase R3 "start the previous images"
|
|
start_previous "$BACKUP_DIR" || die "the previous images could not be started (data is restored; see above)"
|
|
wait_healthy || die "the stack did not become healthy"
|
|
printf '\n'
|
|
log "=== ROLLBACK DONE: the data and images of $BACKUP_DIR are in use ==="
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Phase 5 helper: one component's database, one-off container from the NEW image, same volumes
|
|
# ---------------------------------------------------------------------------------------------
|
|
indent() { printf '%s\n' "$1" | sed 's/^/ | /'; }
|
|
|
|
migrate_component() { # NAME SERVICE DIR
|
|
local name svc dir rc out
|
|
name="$1"; svc="$2"; dir="$3"
|
|
log "--- $name database: check ---"
|
|
rc=0
|
|
out=$(dc run --rm --no-deps -T --entrypoint sh "$svc" -c "cd $dir && python migrate.py --check" 2>&1) || rc=$?
|
|
indent "$out"
|
|
case "$rc" in
|
|
0) log "$name: up to date, nothing to migrate"; return 0 ;;
|
|
2) log "$name: migrations pending: applying (a consistent backup of the database is made first)" ;;
|
|
*) die "$name: the check failed with exit code $rc (database newer than this version, damaged, or the check could not run)" ;;
|
|
esac
|
|
rc=0
|
|
out=$(dc run --rm --no-deps -T --entrypoint sh "$svc" -c "cd $dir && python migrate.py" 2>&1) || rc=$?
|
|
indent "$out"
|
|
[ "$rc" -eq 0 ] || die "$name: the migration failed with exit code $rc"
|
|
rc=0
|
|
out=$(dc run --rm --no-deps -T --entrypoint sh "$svc" -c "cd $dir && python migrate.py --check" 2>&1) || rc=$?
|
|
[ "$rc" -eq 0 ] || { indent "$out"; die "$name: the database is not up to date after the migration (exit code $rc)"; }
|
|
log "$name: migrated, database is up to date"
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
main() {
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--variant) [ $# -ge 2 ] || die "--variant needs a value"; ARG_VARIANT="$2"; shift 2 ;;
|
|
--project) [ $# -ge 2 ] || die "--project needs a value"; ARG_PROJECT="$2"; shift 2 ;;
|
|
--yes|-y) ASSUME_YES=1; shift ;;
|
|
--no-pull) NO_PULL=1; shift ;;
|
|
--rollback) DO_ROLLBACK=1; shift ;;
|
|
-h|--help) usage; exit 0 ;;
|
|
*) usage >&2; die "unknown option: $1" ;;
|
|
esac
|
|
done
|
|
|
|
trap on_exit EXIT
|
|
trap 'exit 130' INT
|
|
trap 'exit 143' TERM
|
|
need_docker
|
|
resolve_stack
|
|
acquire_lock
|
|
|
|
if [ "$DO_ROLLBACK" = 1 ]; then
|
|
manual_rollback
|
|
return 0
|
|
fi
|
|
|
|
# ----- Phase 0 -------------------------------------------------------------------------
|
|
CURRENT_PHASE="0 preflight"
|
|
phase 0 "preflight"
|
|
log "variant $VARIANT, project $PROJECT, compose file $(basename "$COMPOSE_FILE"), docker compose command: $DC_BIN"
|
|
check_env_file
|
|
dc config -q || die "the compose configuration is invalid (see the message above)"
|
|
check_other_variant_stopped
|
|
check_name_clash
|
|
stack_exists || die "no containers of variant $VARIANT found in project $PROJECT: nothing to update (install first, or check --variant / --project)"
|
|
if stack_running; then
|
|
STACK_WAS_RUNNING=1
|
|
else
|
|
warn "the stack is not running"
|
|
confirm "Continue anyway? (the stack will be started at the end)" || die "aborted by the user"
|
|
fi
|
|
[ -n "$(volume_name db_data)" ] || die "volume db_data of project '$PROJECT' not found: wrong --project? Nothing was changed"
|
|
mkdir -p "$BACKUP_ROOT"
|
|
chmod 700 "$BACKUP_ROOT" 2>/dev/null || true
|
|
check_free "$BACKUP_ROOT" "$MIN_FREE_BACKUP_MB" "backups"
|
|
check_free "$(docker_root_dir)" "$MIN_FREE_DOCKER_MB" "new images (docker data root)"
|
|
OLD_COMMIT=$(git_head)
|
|
log "current source version: $OLD_COMMIT"
|
|
log "plan: backup -> git pull -> build -> stop -> DATABASE MIGRATION -> start (automatic rollback after the stop)"
|
|
confirm "Update now?" || die "aborted by the user"
|
|
|
|
# ----- Phase 1: backup -----------------------------------------------------------------
|
|
CURRENT_PHASE="1 backup"
|
|
phase 1 "BACKUP of all volumes"
|
|
BACKUP_DIR="$BACKUP_ROOT/$(backup_stamp)"
|
|
[ ! -e "$BACKUP_DIR" ] || die "backup directory already exists: $BACKUP_DIR (retry in a second)"
|
|
mkdir -p "$BACKUP_DIR"
|
|
chmod 700 "$BACKUP_DIR"
|
|
log "backup directory: $BACKUP_DIR"
|
|
record_images "$BACKUP_DIR"
|
|
if ! make_backup "$BACKUP_DIR" update; then
|
|
die "the BACKUP FAILED. Nothing was changed, the update is refused. (The incomplete backup in $BACKUP_DIR is never used.)"
|
|
fi
|
|
log "backup complete and verified: $BACKUP_DIR"
|
|
|
|
# ----- Phase 2: sources ----------------------------------------------------------------
|
|
CURRENT_PHASE="2 git pull"
|
|
phase 2 "source update"
|
|
if [ "$NO_PULL" = 1 ]; then
|
|
log "skipped (--no-pull)"
|
|
elif ! git -C "$ROOT_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
|
log "not a git checkout: skipped (put the new sources in place yourself before running this script)"
|
|
else
|
|
git -C "$ROOT_DIR" pull --ff-only || die "git pull --ff-only failed. The stack was not touched. Fix the checkout, or use --no-pull"
|
|
log "source version: $OLD_COMMIT -> $(git_head)"
|
|
log "note: if deploy/docker/*.sh changed, the new versions are used from the next run"
|
|
fi
|
|
|
|
# ----- Phase 3: build ------------------------------------------------------------------
|
|
CURRENT_PHASE="3 build"
|
|
phase 3 "BUILD the new images (the old stack keeps running)"
|
|
dc config -q || die "the compose configuration is invalid after the source update"
|
|
log "tagging the current images as :previous"
|
|
tag_previous "$BACKUP_DIR"
|
|
if [ "$NO_PULL" = 1 ]; then
|
|
dc build || die "the build failed. The stack was not touched (still running the old version)"
|
|
else
|
|
dc build --pull || die "the build failed. The stack was not touched (still running the old version)"
|
|
fi
|
|
|
|
# ----- Phase 4: stop -------------------------------------------------------------------
|
|
CURRENT_PHASE="4 stop"
|
|
phase 4 "STOP the application containers (volumes are kept)"
|
|
ROLLBACK_ARMED=1 # from here on a failure restores the backup and the previous images
|
|
dc stop
|
|
if [ "$STACK_WAS_RUNNING" = 1 ]; then
|
|
log "final consistent snapshot of the volumes (everything is stopped, the databases are quiet)"
|
|
make_backup "$BACKUP_DIR" update || die "the final snapshot failed"
|
|
fi
|
|
|
|
# ----- Phase 5: database migration -----------------------------------------------------
|
|
CURRENT_PHASE="5 database migration"
|
|
phase 5 "DATABASE MIGRATION (own step; no service is started on an unmigrated database)"
|
|
log "one-off containers from the NEW images with the same volumes; reports below"
|
|
for t in $MIG_TARGETS; do # name:service:directory
|
|
_mname=${t%%:*}; _mrest=${t#*:}
|
|
migrate_component "$_mname" "${_mrest%%:*}" "${_mrest#*:}"
|
|
done
|
|
log "all databases are up to date"
|
|
|
|
# ----- Phase 6: start ------------------------------------------------------------------
|
|
CURRENT_PHASE="6 start"
|
|
phase 6 "START the new stack"
|
|
dc up -d --no-build
|
|
wait_healthy || die "the new stack did not become healthy"
|
|
|
|
# ----- Done ----------------------------------------------------------------------------
|
|
ROLLBACK_ARMED=0
|
|
CURRENT_PHASE="finish"
|
|
state_write "$VARIANT" "$PROJECT" "" "$(date -u '+%Y-%m-%dT%H:%M:%SZ')"
|
|
phase 7 "finish"
|
|
prune_backups
|
|
printf '\n'
|
|
log "=== UPDATE SUCCESSFUL: variant $VARIANT, project $PROJECT ==="
|
|
printf ' Source version: %s -> %s\n' "$OLD_COMMIT" "$(git_head)"
|
|
printf ' Backup: %s (the newest %s backups are kept)\n' "$BACKUP_DIR" "$KEEP_BACKUPS"
|
|
printf ' Previous images: tagged <name>:previous (rollback: %s --rollback)\n' "$0"
|
|
printf ' Check in the UI that the OpenVPN server is running: the containers were recreated and the\n'
|
|
printf ' profiler starts OpenVPN only on request.\n'
|
|
}
|
|
|
|
main "$@"; exit $?
|