#!/bin/sh # Update OpenVPN Monitor & Profiler (system services) without touching the data. # # deploy/native/update.sh [--prefix DIR] [--ui-dist FILE|DIR | --build-ui [--node-download]] [--source DIR] # [--no-pull] [--yes] [--rollback] # # --prefix install directory (default: from /etc/ovpmon/deploy.state, else /opt/OpenVPN-Monitoring-Simple) # --ui-dist UI tarball from build-ui.sh, or a directory with index.html: always installed (highest priority) # --build-ui build the UI now from APP_UI (Docker when it works, otherwise Node >= 20.19 on this host) and # install it; --node-download fetches a temporary official Node 20 for that (checksum verified, # glibc only, needs curl and xz) # Without these the committed build deploy/ui-dist of the source is installed automatically when # it differs from the installed UI (/.ui-source-hash); no UI source: the UI stays # --source checkout with the new version (default: the repository this script is in) # --no-pull no "git pull --ff-only" of the source checkout (offline hosts, sources already in place) # --yes no confirmation questions # --rollback restore code, databases, configuration, PKI and client profiles from the newest backup # # Phases: 0 preflight, 1 BACKUP (verified, /var/backups/ovpmon//), 2 git pull, 3 PREPARE the new # code, virtualenvs and UI next to the install dir (old code untouched), 4 STOP the application services (OpenVPN # keeps running) and swap the code in, 5 DATABASE MIGRATION as its own step with the new code (nothing is # started on an unmigrated database), 6 UI, nginx, helper, doas rules, service definitions, 7 START and health # checks. A failure after the stop triggers an automatic rollback from the backup of this run. # The PKI (easy-rsa), client profiles and databases are never replaced or deleted by an update. # See DOCS/Deployment/System_Services/README.md and DOCS/General/Database_Migrations.md set -eu SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) . "$SCRIPT_DIR/lib.sh" usage() { sed -n '2,/^set -eu$/{/^set -eu$/!p}' "$0" | sed 's/^# \{0,1\}//' } PREFIX="" UI_DIST="" UI_BUILD=0 UI_NODE_DL=0 UI_RESULT="kept as it is (no UI source)" SOURCE="" NO_PULL=0 ASSUME_YES=0 DO_ROLLBACK=0 FORCE=1 # an update never refuses on an untested OS version (install.sh decides that) BACKUP_DIR="" CURRENT_PHASE="start" ROLLBACK_ARMED=0 SWAP_BEGAN=0 STAMP=$(stamp) RESTORE_STAMP="$STAMP" STAGING="" HTTPS_PORT="" OLD_VERSION="unknown" NEW_VERSION="unknown" UI_STAGED=0 SERVICES_WERE_RUNNING=0 # --------------------------------------------------------------------------------------------- # Rollback # --------------------------------------------------------------------------------------------- rollback_failed() { log "!!! ROLLBACK FAILED: $1" >&2 log "!!! Your data is safe in the backup: $BACKUP_DIR" >&2 log "!!! Retry: $SCRIPT_DIR/update.sh --rollback (or restore everything: $SCRIPT_DIR/restore.sh $BACKUP_DIR)" >&2 } # Automatic rollback after a failure from phase 4 on (called from the EXIT trap) auto_rollback() { printf '\n' >&2 log "!!! UPDATE FAILED in phase: $CURRENT_PHASE" >&2 stop_app_services >/dev/null 2>&1 || true if [ "$SWAP_BEGAN" = 1 ]; then log "!!! Automatic rollback: code, databases, configuration and system files are restored from $BACKUP_DIR" >&2 RESTORE_STAMP="$STAMP" if ! restore_all "$BACKUP_DIR" "$PREFIX" auto; then rollback_failed "the restore from the backup failed (see above)" return 0 fi else log "!!! The code was not replaced yet: the old version is started again" >&2 fi svc_defs_reload || true if ! start_app_services; then rollback_failed "the previous version could not be started" return 0 fi if ! health_check "$HTTPS_PORT"; then rollback_failed "the previous version did not become healthy" return 0 fi log "!!! ROLLED BACK: the previous version ($OLD_VERSION) runs 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 system was changed. Backup: $BACKUP_DIR" >&2 fi fi if [ -n "$STAGING" ] && [ -f "$STAGING/.ovpmon-staging" ]; then rm -rf "$STAGING"; fi [ "$UI_STAGED" = 0 ] || rm -rf "$WEB_ROOT.new" [ -z "${TMPD:-}" ] || rm -rf "$TMPD" release_lock exit "$_rc" } manual_rollback() { local safe BACKUP_DIR=$(latest_backup) [ -n "$BACKUP_DIR" ] || die "no complete backup found in $BACKUP_ROOT" log "newest complete backup: $BACKUP_DIR" sed 's/^/ /' "$BACKUP_DIR/MANIFEST" [ "$(manifest_get "$BACKUP_DIR" prefix)" = "$PREFIX" ] || die "the backup belongs to install directory '$(manifest_get "$BACKUP_DIR" prefix)', not '$PREFIX' (use --prefix)" log "code, databases, configuration, PKI and client profiles will be REPLACED by this backup; the current state is saved first to $BACKUP_ROOT/-pre-restore" confirm "Restore from this backup and start the previous version?" || die "aborted by the user" phase R1 "stop the application services" stop_app_services || die "could not stop the services" if [ "${OVPMON_NO_SAFETY_BACKUP:-0}" != 1 ]; then phase R2 "safety backup of the current state" safe="$BACKUP_ROOT/$(stamp)-pre-restore" make_backup "$safe" pre-restore "$PREFIX" || die "the safety backup failed: nothing was changed" log "current state saved: $safe" fi phase R3 "restore from the backup" RESTORE_STAMP="$STAMP" restore_all "$BACKUP_DIR" "$PREFIX" full || die "the restore failed (see above). The backup is intact: $BACKUP_DIR" phase R4 "start" svc_defs_reload || true start_app_services || die "the services did not start after the restore" health_check "$HTTPS_PORT" || die "the health checks failed after the restore" printf '\n' log "=== ROLLBACK DONE: the state of $BACKUP_DIR is in use ===" } # --------------------------------------------------------------------------------------------- # Phase 5 helper: one component's database, with the NEW code, as the service user with its environment # --------------------------------------------------------------------------------------------- migrate_component() { # NAME DIR local name dir rc out name="$1"; dir="$2" log "--- $name database: check ---" rc=0 out=$(run_as_service_user "$dir" "$dir/venv/bin/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=$(run_as_service_user "$dir" "$dir/venv/bin/python" migrate.py 2>&1) || rc=$? indent "$out" [ "$rc" -eq 0 ] || die "$name: the migration failed with exit code $rc" rc=0 out=$(run_as_service_user "$dir" "$dir/venv/bin/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" } # Phase 4 helper: data that lives below a code directory moves to the new one (same file system, rename) preserve_data() { # OLD_DIR NEW_DIR NAME local old new item f old="$1"; new="$2" if [ "$3" = APP_PROFILER ]; then for item in easy-rsa client-config profiler.log; do if [ -e "$old/$item" ] && [ ! -e "$new/$item" ]; then mv "$old/$item" "$new/$item" || die "cannot move $item to the new code directory" log " kept $3/$item" fi done fi # config.ini is configuration: the current one stays, the shipped one is saved next to it if [ -f "$old/config.ini" ]; then if [ -f "$new/config.ini" ] && ! cmp -s "$old/config.ini" "$new/config.ini"; then mv "$new/config.ini" "$new/config.ini.dist" log " $3/config.ini kept (the shipped one of the new version is config.ini.dist)" else rm -f "$new/config.ini" fi mv "$old/config.ini" "$new/config.ini" fi # legacy data files that older installations kept next to the code (databases, logs) for f in "$old"/*.db "$old"/*.db-* "$old"/*.log; do [ -e "$f" ] || continue item=$(basename "$f") if [ ! -e "$new/$item" ]; then mv "$f" "$new/$item"; log " kept $3/$item"; fi done } detect_https_port() { [ -f "$NGINX_SITE" ] || return 0 sed -n 's/^[[:space:]]*listen[[:space:]][[:space:]]*\([0-9][0-9]*\)[[:space:]][[:space:]]*ssl.*/\1/p' "$NGINX_SITE" | head -n 1 } # apply_file RESULT_CODE LABEL: log what install_managed / install_doas did report() { case "$1" in 0) log " updated: $2"; CHANGES=$((CHANGES + 1)) ;; 1) log " unchanged: $2" ;; *) log " NOT touched (not managed by these scripts): $2" ;; esac } # --------------------------------------------------------------------------------------------- main() { while [ $# -gt 0 ]; do case "$1" in --prefix) [ $# -ge 2 ] || die "--prefix needs a value"; PREFIX="$2"; shift 2 ;; --ui-dist) [ $# -ge 2 ] || die "--ui-dist needs a value"; UI_DIST="$2"; shift 2 ;; --build-ui) UI_BUILD=1; shift ;; --node-download) UI_NODE_DL=1; shift ;; --source) [ $# -ge 2 ] || die "--source needs a value"; SOURCE="$2"; shift 2 ;; --no-pull) NO_PULL=1; shift ;; --yes|-y) ASSUME_YES=1; shift ;; --rollback) DO_ROLLBACK=1; shift ;; -h|--help) usage; exit 0 ;; *) usage >&2; die "unknown option: $1" ;; esac done [ "$UI_NODE_DL" = 0 ] || [ "$UI_BUILD" = 1 ] || die "--node-download only works together with --build-ui" [ -z "$UI_DIST" ] || [ "$UI_BUILD" = 0 ] || die "use either --ui-dist or --build-ui, not both" TMPD=$(mktemp -d /tmp/ovpmon-update.XXXXXX) || die "cannot create a temporary directory" trap on_exit EXIT trap 'exit 130' INT trap 'exit 143' TERM need_root check_os require_init acquire_lock [ -n "$PREFIX" ] || PREFIX=$(state_get OVPMON_PREFIX) [ -n "$PREFIX" ] || PREFIX="$DEFAULT_PREFIX" valid_prefix "$PREFIX" || die "invalid install directory '$PREFIX'" HTTPS_PORT=$(state_get OVPMON_HTTPS_PORT) [ -n "$HTTPS_PORT" ] || HTTPS_PORT=$(detect_https_port || true) SOURCE="${SOURCE:-$ROOT_DIR}" case "$UI_DIST" in ""|/*) ;; *) UI_DIST="$PWD/$UI_DIST" ;; esac if [ "$DO_ROLLBACK" = 1 ]; then [ -f "$ENV_FILE" ] || die "$ENV_FILE not found: nothing is installed" manual_rollback return 0 fi # ----- Phase 0 ------------------------------------------------------------------------- CURRENT_PHASE="0 preflight" phase 0 "preflight" log "install directory: $PREFIX; source: $SOURCE; backups: $BACKUP_ROOT" check_gnu_tar command -v python3 >/dev/null 2>&1 || die "python3 not found" command -v curl >/dev/null 2>&1 || die "curl not found" [ ! -f "$INCOMPLETE_MARK" ] || die "an unfinished install.sh run exists ($INCOMPLETE_MARK): run install.sh again to finish it, then update" [ -d "$PREFIX/APP_CORE" ] && [ -d "$PREFIX/APP_PROFILER" ] && [ -f "$ENV_FILE" ] \ || die "no installation found (need $PREFIX/APP_CORE, $PREFIX/APP_PROFILER and $ENV_FILE): wrong --prefix? Use install.sh for a new host" if [ -f "$STATE_FILE" ]; then log "installed version: $(state_get OVPMON_VERSION), installed $(state_get OVPMON_INSTALLED_AT)" else warn "no state file ($STATE_FILE): this installation was not made by install.sh; it is adopted and recorded after a successful update. Files that these scripts do not manage (init scripts, nginx site, doas file) are left as they are" fi OLD_VERSION=$(cat "$PREFIX/VERSION" 2>/dev/null || echo "unknown") for _f in APP_CORE/openvpn_api_v3.py APP_CORE/openvpn_gatherer_v3.py APP_CORE/migrate.py APP_CORE/requirements.txt \ APP_PROFILER/main.py APP_PROFILER/migrate.py APP_PROFILER/requirements.txt \ DOCS/General/privilege-separation/ovpmon-helper DOCS/General/privilege-separation/doas-ovpmon.conf \ deploy/native/nginx/ovpmon.conf.template; do [ -f "$SOURCE/$_f" ] || die "the source directory is not a complete checkout: $SOURCE/$_f is missing (--source)" done for _s in $SERVICES; do [ -f "$(unit_src "$SOURCE" "$_s")" ] || die "service definition missing in the source: $(unit_src "$SOURCE" "$_s")" done if [ "$(abs_dir "$SOURCE")" = "$(abs_dir "$PREFIX")" ]; then log "the source is the install directory itself (a checkout used as the install directory): git pull changes the live files, so a rollback is armed from phase 2" PULL_TOUCHES_LIVE=1 else PULL_TOUCHES_LIVE=0 fi if [ -n "$UI_DIST" ]; then check_ui_source "$UI_DIST" log "UI: $UI_DIST (--ui-dist), will be installed" elif [ "$UI_BUILD" = 1 ]; then ui_build_check log "UI: will be built from APP_UI after the source update (--build-ui) and installed" else log "UI: the committed build deploy/ui-dist of the source is installed after the source update when it differs from the installed UI" fi SERVICES_WERE_RUNNING=1 for _s in $SERVICES; do if svc_known "$_s"; then log " $_s: $(svc_state_word "$_s")" svc_active "$_s" || SERVICES_WERE_RUNNING=0 else log " $_s: no service definition yet (it will be installed)" SERVICES_WERE_RUNNING=0 fi done if [ "$SERVICES_WERE_RUNNING" = 0 ]; then warn "not all application services are running" confirm "Continue anyway? (the services will be started at the end)" || die "aborted by the user" fi log " OpenVPN ($OVPN_SERVICE): $(svc_state_word "$OVPN_SERVICE") (not touched by the update)" resolve_db_paths "$PREFIX" for _d in "$DB_MONITOR" "$DB_PROFILER"; do if [ -f "$_d" ]; then log "database: $_d ($(du -h "$_d" | awk '{print $1}'))"; else warn "database not found yet: $_d"; fi done mkdir -p "$BACKUP_ROOT" chmod 700 "$BACKUP_ROOT" _kb=$(du -sk "$PREFIX/APP_CORE" "$PREFIX/APP_PROFILER" "$DATA_DIR" "$ETC_DIR" /etc/openvpn "$WEB_ROOT" 2>/dev/null | awk '{ s += $1 } END { print s + 0 }') _need=$(( _kb / 1024 * 2 + 100 )) [ "$_need" -ge "$MIN_FREE_BACKUP_MB" ] || _need="$MIN_FREE_BACKUP_MB" check_free "$BACKUP_ROOT" "$_need" "the backup" check_free "$(dirname "$PREFIX")" "$MIN_FREE_PREFIX_MB" "the new code and virtualenvs" log "plan: backup -> git pull -> prepare (staging) -> stop -> swap code -> DATABASE MIGRATION -> files -> start (automatic rollback after the stop)" confirm "Update now?" || die "aborted by the user" # ----- Phase 1: backup ----------------------------------------------------------------- CURRENT_PHASE="1 backup" phase 1 "BACKUP (databases, configuration, PKI, client profiles, code, system files)" BACKUP_DIR="$BACKUP_ROOT/$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" if ! make_backup "$BACKUP_DIR" update "$PREFIX"; 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 "$SOURCE" 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 if [ "$PULL_TOUCHES_LIVE" = 1 ]; then ROLLBACK_ARMED=1; SWAP_BEGAN=1; fi # the live files change: restore them on failure git -C "$SOURCE" pull --ff-only || die "git pull --ff-only failed (local changes, diverged history, no network, or 'dubious ownership': git config --global --add safe.directory $SOURCE). The running system was not changed. Fix the checkout, or use --no-pull" fi NEW_VERSION=$(version_of "$SOURCE") log "source version: $OLD_VERSION -> $NEW_VERSION" if [ "$NO_PULL" = 0 ] && [ "$PULL_TOUCHES_LIVE" = 0 ]; then log "note: if deploy/native/*.sh changed, the new versions are used from the next run" fi # ----- Phase 3: prepare ---------------------------------------------------------------- CURRENT_PHASE="3 prepare" phase 3 "PREPARE the new code, virtualenvs and UI (the running system is not touched)" STAGING="${PREFIX}.new" if [ -e "$STAGING" ]; then [ -f "$STAGING/.ovpmon-staging" ] || die "$STAGING exists and was not created by this script: remove or rename it" rm -rf "$STAGING" fi mkdir -p "$STAGING" : > "$STAGING/.ovpmon-staging" copy_component "$SOURCE/APP_CORE" "$STAGING/APP_CORE" copy_component "$SOURCE/APP_PROFILER" "$STAGING/APP_PROFILER" make_venv "$STAGING/APP_CORE" gunicorn make_venv "$STAGING/APP_PROFILER" smoke_component "$STAGING/APP_CORE" core || die "the new APP_CORE virtualenv cannot import the application's dependencies" smoke_component "$STAGING/APP_PROFILER" profiler || die "the new APP_PROFILER virtualenv cannot import the application's dependencies" lock_down_code "$STAGING/APP_CORE" "$STAGING/APP_PROFILER" log "new code and virtualenvs are ready in $STAGING (dependencies installed and importable)" # the UI source is decided here, after the source update (git pull may have brought a new build) ui_resolve "$SOURCE" "$TMPD" if [ -z "$UI_SRC" ]; then log "no UI source found (no --ui-dist or --build-ui, no committed build in $SOURCE/deploy/ui-dist): the installed UI stays as it is" else _have=$(cat "$WEB_ROOT/$UI_HASH_FILE" 2>/dev/null || echo "none") _new=$(ui_hash_of "$UI_SRC") if [ "$UI_KIND" = repo ] && valid_hash "$_new" && [ "$_new" = "$_have" ]; then log "UI: $UI_ORIGIN; the installed UI comes from the same sources (hash $_new): not replaced" UI_RESULT="unchanged (same sources as the installed UI)" else log "UI: $UI_ORIGIN; installed UI: ${_have}, new: ${_new}" stage_ui "$UI_SRC" "$WEB_ROOT.new" UI_STAGED=1 UI_RESULT="replaced: $UI_ORIGIN" log "new UI files are ready in $WEB_ROOT.new" fi fi # ----- Phase 4: stop and swap ---------------------------------------------------------- CURRENT_PHASE="4 stop and swap" phase 4 "STOP the application services (OpenVPN keeps running) and swap the code in" ROLLBACK_ARMED=1 # from here on a failure restores the backup stop_app_services || die "could not stop the application services" log "final consistent snapshot (nothing writes to the databases now)" make_backup "$BACKUP_DIR" update "$PREFIX" 1 || die "the final snapshot failed" SWAP_BEGAN=1 for _c in APP_CORE APP_PROFILER; do mv "$PREFIX/$_c" "$PREFIX/$_c.old-$STAMP" || die "cannot move the old $_c aside" mv "$STAGING/$_c" "$PREFIX/$_c" || die "cannot move the new $_c into place" preserve_data "$PREFIX/$_c.old-$STAMP" "$PREFIX/$_c" "$_c" fix_venv_paths "$PREFIX/$_c" "$STAGING/$_c" "$PREFIX/$_c" log " $_c replaced (the old one is kept as $_c.old-$STAMP until the update succeeds)" done rm -rf "$STAGING" ensure_runtime_files "$PREFIX" lock_down_code "$PREFIX/APP_CORE" "$PREFIX/APP_PROFILER" compile_code "$PREFIX/APP_CORE" compile_code "$PREFIX/APP_PROFILER" smoke_component "$PREFIX/APP_CORE" core || die "the relocated APP_CORE virtualenv does not work" smoke_component "$PREFIX/APP_PROFILER" profiler || die "the relocated APP_PROFILER virtualenv does not work" log "PKI, client profiles and databases were not touched" # ----- Phase 5: database migration ----------------------------------------------------- CURRENT_PHASE="5 database migration" phase 5 "DATABASE MIGRATION (own step; no service is started on an unmigrated database)" log "run with the NEW code, as user $SERVICE_USER, with the environment of $ENV_FILE; reports below" migrate_component "profiler" "$PREFIX/APP_PROFILER" migrate_component "monitoring" "$PREFIX/APP_CORE" log "all databases are up to date" # ----- Phase 6: files ------------------------------------------------------------------ CURRENT_PHASE="6 files" phase 6 "UI, nginx site, helper, doas rules, service definitions" CHANGES=0 if [ "$UI_STAGED" = 1 ]; then swap_dir "$WEB_ROOT.new" "$WEB_ROOT" "$WEB_ROOT.old" UI_STAGED=0 log " UI files replaced (the previous ones are in the backup)" CHANGES=$((CHANGES + 1)) fi rc=0; install_helper "$SOURCE" || rc=$? report "$rc" "$HELPER_DEST" # helper.conf: written when missing (adopted hosts), kept when it is not ours helper_conf_text "$PREFIX" > "$TMPD/helper.conf" rc=0; install_managed "$TMPD/helper.conf" "$HELPER_CONF" 644 "deploy/native/lib.sh helper_conf_text" || rc=$? report "$rc" "$HELPER_CONF" rc=0; install_doas "$SOURCE" || rc=$? doas_target report "$rc" "doas rules ($DOAS_FILE)" if [ ! -f "$SYSCTL_FILE" ]; then printf '# %s\nnet.ipv4.ip_forward = 1\n' "$MARKER" > "$SYSCTL_FILE"; chmod 644 "$SYSCTL_FILE" log " added: $SYSCTL_FILE" fi # service definitions for _s in $SERVICES; do render_unit "$SOURCE" "$_s" "$PREFIX" "$TMPD/$_s" if [ "$INIT" = systemd ]; then _mode=644; else _mode=755; fi rc=0; install_managed "$TMPD/$_s" "$(unit_file "$_s")" "$_mode" "$(unit_src "$SOURCE" "$_s")" || rc=$? report "$rc" "$(unit_file "$_s")" done svc_defs_reload for _s in $SERVICES; do svc_enable "$_s" || warn "could not enable $_s at boot"; done # JWT key: the APIs refuse to start with a missing or weak key, so one is issued here, before the start. # The env file is part of the backup, a failed update restores the previous one. if ! _reason=$(secret_problem "$(env_get OVPMON_API_SECRET_KEY)"); then _new_key=$(openssl rand -hex 32 2>/dev/null || true) [ "${#_new_key}" -eq 64 ] || _new_key=$(od -An -N32 -tx1 /dev/urandom | tr -d ' \n') [ "${#_new_key}" -eq 64 ] || die "cannot generate a random JWT key" kv_set "$ENV_FILE" OVPMON_API_SECRET_KEY "$_new_key" 600 unset _new_key warn "OVPMON_API_SECRET_KEY in $ENV_FILE was $_reason: a new random key was issued. ALL USER SESSIONS ARE RESET: everyone has to sign in again" CHANGES=$((CHANGES + 1)) fi # environment: add the settings of this version when they are missing (existing values are never changed) for _kv in "OVPMON_RUNTIME=host" "OVPMON_OPENVPN_SERVICE=$OVPN_SERVICE" "OVPMON_OPENVPN_CONFIG=$OVPN_CONFIG"; do _k=${_kv%%=*} if [ -z "$(env_get "$_k")" ]; then kv_set "$ENV_FILE" "$_k" "${_kv#*=}" 600 log " added $_k to $ENV_FILE" fi done # OpenVPN prerequisites that may be missing on an adopted host [ -d "$(dirname "$OVPN_CONFIG")" ] || { mkdir -p "$(dirname "$OVPN_CONFIG")"; chmod 755 "$(dirname "$OVPN_CONFIG")"; } [ -d /etc/openvpn/scripts ] || ensure_dir /etc/openvpn/scripts root:root 755 # nginx site (only when it is ours and the port is known) if [ -n "$HTTPS_PORT" ]; then render_nginx "$SOURCE" "$HTTPS_PORT" "$TMPD/ovpmon.conf" rc=0; install_managed "$TMPD/ovpmon.conf" "$NGINX_SITE" 644 "deploy/native/nginx/ovpmon.conf.template" || rc=$? report "$rc" "$NGINX_SITE" if [ "$rc" = 0 ]; then nginx_apply || die "nginx did not accept the new site (it is restored from the backup)" log " nginx reloaded" fi else warn "no HTTPS port known (no state file and no nginx site found): the nginx site is not managed" fi sync_deploy_files "$SOURCE" "$PREFIX" echo "$NEW_VERSION" > "$PREFIX/VERSION" log "$CHANGES file(s) changed in this phase" # ----- Phase 7: start ------------------------------------------------------------------ CURRENT_PHASE="7 start" phase 7 "START the services and check them" start_app_services if svc_known nginx && ! svc_active nginx; then svc_start nginx >/dev/null 2>&1 || warn "nginx is not running"; fi health_check "$HTTPS_PORT" || die "the new version did not become healthy" doas_selftest && log "self-test: $SERVICE_USER can call the helper through doas" || warn "the helper self-test failed: starting OpenVPN from the UI may not work (see $DOAS_FILE and $HELPER_CONF)" # ----- Done ---------------------------------------------------------------------------- ROLLBACK_ARMED=0 CURRENT_PHASE="finish" phase 8 "finish" for _c in APP_CORE APP_PROFILER; do safe_remove_old "$PREFIX/$_c.old-$STAMP"; done rm -rf "$WEB_ROOT.old" state_set OVPMON_PREFIX "$PREFIX" [ -z "$HTTPS_PORT" ] || state_set OVPMON_HTTPS_PORT "$HTTPS_PORT" state_set OVPMON_OS "$OS_ID $OS_VERSION_ID" state_set OVPMON_VERSION "$NEW_VERSION" [ -n "$(state_get OVPMON_INSTALLED_AT)" ] || state_set OVPMON_INSTALLED_AT "adopted $(now_iso)" state_set OVPMON_LAST_UPDATE "$(now_iso)" prune_backups printf '\n' log "=== UPDATE SUCCESSFUL: $OLD_VERSION -> $NEW_VERSION ===" printf ' Install dir: %s\n' "$PREFIX" printf ' UI: %s\n' "$UI_RESULT" printf ' Backup: %s (the newest %s backups are kept)\n' "$BACKUP_DIR" "$KEEP_BACKUPS" printf ' Rollback: %s --rollback\n' "$0" printf ' OpenVPN was not restarted by the update: connected VPN users stayed connected.\n' printf ' Log of this run: %s\n' "$DEPLOY_LOG" } main "$@"; exit $?