- install.sh always asks for the install directory (--prefix or a prompt with confirmation, no default); refuses system directories, the repository checkout itself, paths inside/containing it and non-empty directories (a resumed install is allowed). A preflight refusal says nothing was changed. - UI is part of the repository: committed build in deploy/ui-dist with SOURCE_HASH and a staleness guard (check-ui.sh, loud warning). install.sh and update.sh resolve the UI: --ui-dist FILE|DIR, --build-ui, committed build; install stops without a UI unless --no-ui. update.sh swaps the UI when its hash differs from the installed one (.ui-source-hash). - build-ui.sh: --docker, --local (host Node >= 20.19, temporary copy), --node-download (portable Node, SHA-256 verified), --publish. - Verified on Alpine 3.23: install without UI options installs the real UI; local build equals the Docker build; update skips an unchanged UI and warns on a stale build. --node-download is untested (needs network). - Docs: Scenario 2 guide, change summary, plan, README. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1283 lines
53 KiB
Bash
Executable File
1283 lines
53 KiB
Bash
Executable File
#!/bin/sh
|
|
# lib.sh: shared helpers of install.sh, update.sh, backup.sh and restore.sh (scenario 2, system services).
|
|
# Sourced, not executed. The caller sets SCRIPT_DIR (directory of the calling script) first:
|
|
#
|
|
# SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
|
|
# . "$SCRIPT_DIR/lib.sh"
|
|
#
|
|
# POSIX sh (dash, busybox ash, bash); `local` is used in functions (supported by all of them).
|
|
# Contains the OS adapter: Alpine (OpenRC), Debian and Ubuntu (systemd).
|
|
# Nothing here ever deletes a database, the PKI (APP_PROFILER/easy-rsa) or client configs
|
|
# (APP_PROFILER/client-config), and nothing touches the firewall.
|
|
|
|
ROOT_DIR=$(cd "$SCRIPT_DIR/../.." && pwd)
|
|
|
|
DEFAULT_PREFIX=/opt/OpenVPN-Monitoring-Simple
|
|
ETC_DIR=/etc/ovpmon
|
|
ENV_FILE="$ETC_DIR/env"
|
|
HELPER_CONF="$ETC_DIR/helper.conf"
|
|
STATE_FILE="$ETC_DIR/deploy.state"
|
|
INCOMPLETE_MARK="$ETC_DIR/.install-incomplete"
|
|
TLS_DIR="$ETC_DIR/tls"
|
|
HELPER_DEST=/usr/local/sbin/ovpmon-helper
|
|
DATA_DIR=/var/lib/ovpmon
|
|
APP_LOG_DIR=/var/log/ovpmon
|
|
OVPN_LOG_DIR=/var/log/openvpn
|
|
WEB_ROOT=/var/www/ovpmon
|
|
SYSCTL_FILE=/etc/sysctl.d/98-ovpmon-forward.conf
|
|
SERVICE_USER=ovpmon
|
|
# Application services in start order (stop order is the reverse)
|
|
SERVICES="ovpmon-api ovpmon-gatherer ovpmon-profiler"
|
|
# Marker line in files that install.sh/update.sh may replace; files without it are never overwritten
|
|
MARKER="managed by ovpmon deploy/native"
|
|
DOAS_BEGIN="# BEGIN ovpmon ($MARKER)"
|
|
DOAS_END="# END ovpmon"
|
|
|
|
BACKUP_ROOT="${OVPMON_BACKUP_DIR:-/var/backups/ovpmon}"
|
|
LOCK_DIR=/run/ovpmon-deploy.lock
|
|
DEPLOY_LOG="${OVPMON_DEPLOY_LOG:-/var/log/ovpmon-deploy.log}"
|
|
# Seconds to wait for the services to answer
|
|
HEALTH_TIMEOUT="${OVPMON_HEALTH_TIMEOUT:-90}"
|
|
# Minimum free space in MB: code, virtualenvs and staging (prefix file system), backups
|
|
MIN_FREE_PREFIX_MB="${OVPMON_MIN_FREE_MB:-1024}"
|
|
MIN_FREE_BACKUP_MB="${OVPMON_MIN_FREE_BACKUP_MB:-512}"
|
|
# How many backups update.sh keeps
|
|
KEEP_BACKUPS=5
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Output (every line also goes to $DEPLOY_LOG when it is writable)
|
|
# ---------------------------------------------------------------------------------------------
|
|
log() {
|
|
local line
|
|
line="$(date -u '+%Y-%m-%d %H:%M:%S') $*"
|
|
printf '%s\n' "$line"
|
|
if [ -n "$DEPLOY_LOG" ] && [ -d "$(dirname "$DEPLOY_LOG")" ]; then
|
|
( umask 077; printf '%s\n' "$line" >> "$DEPLOY_LOG" ) 2>/dev/null || true
|
|
fi
|
|
}
|
|
warn() { log "WARNING: $*" >&2; }
|
|
die() { log "ERROR: $*" >&2; exit 1; }
|
|
phase() { printf '\n'; log "=== PHASE $1: $2 ==="; }
|
|
indent() { printf '%s\n' "$1" | sed 's/^/ | /'; }
|
|
|
|
# confirm "question": 0 = yes. ASSUME_YES=1 (--yes) skips the question.
|
|
confirm() {
|
|
local ans
|
|
[ "${ASSUME_YES:-0}" = 1 ] && return 0
|
|
[ -t 0 ] || die "confirmation required but stdin is not a terminal: use --yes"
|
|
printf '%s [y/N] ' "$1"
|
|
read -r ans || return 1
|
|
case "$ans" in y|Y|yes|YES) return 0 ;; esac
|
|
return 1
|
|
}
|
|
|
|
need_root() {
|
|
[ "$(id -u)" = 0 ] || die "this script must run as root (it installs packages, users, services and root-owned files)"
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# OS adapter
|
|
# ---------------------------------------------------------------------------------------------
|
|
# detect_os: OS_ID (alpine|debian|ubuntu|...), OS_VERSION_ID, OS_LIKE, OS_TESTED (1/0)
|
|
detect_os() {
|
|
[ -r /etc/os-release ] || die "/etc/os-release not found: cannot detect the operating system"
|
|
OS_ID=$( . /etc/os-release; printf '%s' "${ID:-}" )
|
|
OS_VERSION_ID=$( . /etc/os-release; printf '%s' "${VERSION_ID:-}" )
|
|
OS_LIKE=$( . /etc/os-release; printf '%s' "${ID_LIKE:-}" )
|
|
OS_TESTED=0
|
|
case "$OS_ID:$OS_VERSION_ID" in
|
|
alpine:3.23|alpine:3.23.*|debian:12|debian:12.*|ubuntu:24.04) OS_TESTED=1 ;;
|
|
esac
|
|
}
|
|
|
|
# check_os: dies for unsupported systems; untested versions need FORCE=1 (--force)
|
|
check_os() {
|
|
detect_os
|
|
case "$OS_ID" in
|
|
alpine|debian|ubuntu) OS_FAMILY=$OS_ID ;;
|
|
*)
|
|
case " $OS_LIKE " in
|
|
*" debian "*|*" ubuntu "*)
|
|
[ "${FORCE:-0}" = 1 ] || die "operating system '$OS_ID' is a Debian derivative but not tested (tested: Alpine 3.23, Debian 12, Ubuntu 24.04). Use --force to continue at your own risk"
|
|
warn "untested Debian derivative '$OS_ID $OS_VERSION_ID': treated as Debian (--force)"
|
|
OS_FAMILY=debian ;;
|
|
*) die "unsupported operating system '$OS_ID' (supported: Alpine, Debian, Ubuntu)" ;;
|
|
esac
|
|
OS_TESTED=1 ;;
|
|
esac
|
|
if [ "$OS_TESTED" != 1 ]; then
|
|
warn "$OS_ID $OS_VERSION_ID is not a tested version (tested: Alpine 3.23, Debian 12, Ubuntu 24.04)"
|
|
[ "${FORCE:-0}" = 1 ] || die "continuing on an untested version needs --force"
|
|
warn "--force given: continuing at your own risk"
|
|
fi
|
|
set_os_vars
|
|
log "operating system: $OS_ID $OS_VERSION_ID (service manager: $INIT)"
|
|
}
|
|
|
|
# set_os_vars: everything that differs between the supported systems
|
|
set_os_vars() {
|
|
case "$OS_FAMILY" in
|
|
alpine)
|
|
INIT=openrc
|
|
PKG_LIST="python3 py3-pip nginx openvpn easy-rsa iptables bash doas openssl curl tar"
|
|
NGINX_CONF_DIR=/etc/nginx/http.d
|
|
NGINX_INCLUDE_RE='http\.d/\*\.conf'
|
|
OVPN_SERVICE=openvpn
|
|
OVPN_CONFIG=/etc/openvpn/server.conf
|
|
NOLOGIN=/sbin/nologin
|
|
UNIT_DIR=/etc/init.d
|
|
UNIT_SRC_DIR=DOCS/General/openrc
|
|
;;
|
|
debian|ubuntu)
|
|
INIT=systemd
|
|
PKG_LIST="python3 python3-venv python3-pip nginx openvpn easy-rsa iptables opendoas openssl curl ca-certificates tar iproute2"
|
|
NGINX_CONF_DIR=/etc/nginx/conf.d
|
|
NGINX_INCLUDE_RE='conf\.d/\*\.conf'
|
|
OVPN_SERVICE=openvpn-server@server
|
|
OVPN_CONFIG=/etc/openvpn/server/server.conf
|
|
NOLOGIN=/usr/sbin/nologin
|
|
UNIT_DIR=/etc/systemd/system
|
|
UNIT_SRC_DIR=DOCS/General/systemd
|
|
;;
|
|
esac
|
|
NGINX_SITE="$NGINX_CONF_DIR/ovpmon.conf"
|
|
EASYRSA_SRC=/usr/share/easy-rsa
|
|
}
|
|
|
|
# unit_file NAME: path of the installed init script / unit
|
|
unit_file() {
|
|
if [ "$INIT" = systemd ]; then echo "$UNIT_DIR/$1.service"; else echo "$UNIT_DIR/$1"; fi
|
|
}
|
|
# unit_src SRC_DIR NAME: path of the template in the source tree
|
|
unit_src() {
|
|
if [ "$INIT" = systemd ]; then echo "$1/$UNIT_SRC_DIR/$2.service"; else echo "$1/$UNIT_SRC_DIR/$2"; fi
|
|
}
|
|
|
|
# ---- packages --------------------------------------------------------------------------------
|
|
pkg_install() {
|
|
case "$OS_FAMILY" in
|
|
alpine)
|
|
apk update >/dev/null || warn "apk update failed: using the existing package index"
|
|
apk add --no-cache "$@" ;;
|
|
*)
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
apt-get update -q || warn "apt-get update failed: using the existing package lists"
|
|
apt-get install -y -q --no-install-recommends "$@" ;;
|
|
esac
|
|
}
|
|
|
|
# ---- service manager wrappers (OpenRC / systemd) ---------------------------------------------
|
|
require_init() {
|
|
if [ "$INIT" = systemd ]; then
|
|
command -v systemctl >/dev/null 2>&1 || die "systemctl not found"
|
|
[ -d /run/systemd/system ] || die "systemd is not the running init system on this host (/run/systemd/system is missing)"
|
|
else
|
|
command -v rc-service >/dev/null 2>&1 || die "rc-service not found (OpenRC is required on Alpine)"
|
|
fi
|
|
}
|
|
|
|
svc_defs_reload() { if [ "$INIT" = systemd ]; then systemctl daemon-reload; fi; }
|
|
|
|
svc_enable() {
|
|
if [ "$INIT" = systemd ]; then
|
|
systemctl enable "$1" >/dev/null 2>&1
|
|
else
|
|
if rc-update show default 2>/dev/null | awk -v s="$1" '$1 == s { f = 1 } END { exit !f }'; then return 0; fi
|
|
rc-update add "$1" default >/dev/null 2>&1
|
|
fi
|
|
}
|
|
svc_start() { if [ "$INIT" = systemd ]; then systemctl start "$1"; else rc-service "$1" start; fi; }
|
|
svc_stop() { if [ "$INIT" = systemd ]; then systemctl stop "$1"; else rc-service "$1" stop; fi; }
|
|
svc_restart() { if [ "$INIT" = systemd ]; then systemctl restart "$1"; else rc-service "$1" restart; fi; }
|
|
svc_reload() { if [ "$INIT" = systemd ]; then systemctl reload "$1"; else rc-service "$1" reload; fi; }
|
|
svc_active() {
|
|
if [ "$INIT" = systemd ]; then
|
|
systemctl is-active --quiet "$1" 2>/dev/null
|
|
else
|
|
rc-service "$1" status >/dev/null 2>&1
|
|
fi
|
|
}
|
|
svc_known() { # unit or init script is installed
|
|
if [ "$INIT" = systemd ]; then
|
|
[ -f "$UNIT_DIR/$1.service" ] || systemctl cat "$1" >/dev/null 2>&1
|
|
else
|
|
[ -x "/etc/init.d/$1" ]
|
|
fi
|
|
}
|
|
svc_state_word() { if svc_active "$1"; then echo active; else echo inactive; fi; }
|
|
|
|
# stop_app_services: ovpmon-profiler, -gatherer, -api (OpenVPN is not touched: VPN users stay connected)
|
|
stop_app_services() {
|
|
local s
|
|
for s in $(rev_words $SERVICES); do
|
|
if svc_known "$s"; then
|
|
log "stopping $s"
|
|
svc_stop "$s" >/dev/null 2>&1 || true
|
|
svc_active "$s" && { log "$s is still running after stop" >&2; return 1; }
|
|
fi
|
|
done
|
|
return 0
|
|
}
|
|
start_app_services() {
|
|
local s
|
|
for s in $SERVICES; do
|
|
log "starting $s"
|
|
svc_start "$s" >/dev/null 2>&1 || { log "$s failed to start" >&2; show_service_log "$s"; return 1; }
|
|
done
|
|
}
|
|
rev_words() { local w out=""; for w in "$@"; do out="$w $out"; done; printf '%s' "$out"; }
|
|
|
|
show_service_log() {
|
|
log "--- last log lines of $1 ---" >&2
|
|
if [ "$INIT" = systemd ]; then
|
|
journalctl -u "$1" -n 25 --no-pager 2>&1 | sed 's/^/ /' >&2 || true
|
|
else
|
|
tail -n 25 "$APP_LOG_DIR/$1.log" 2>&1 | sed 's/^/ /' >&2 || true
|
|
fi
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Generic helpers
|
|
# ---------------------------------------------------------------------------------------------
|
|
valid_port() {
|
|
case "$1" in
|
|
""|*[!0-9]*) return 1 ;;
|
|
esac
|
|
[ "$1" -ge 1 ] && [ "$1" -le 65535 ]
|
|
}
|
|
|
|
# valid_prefix PATH: absolute, plain characters only (it ends up in sed expressions and unit files)
|
|
valid_prefix() {
|
|
case "$1" in
|
|
/*) ;;
|
|
*) return 1 ;;
|
|
esac
|
|
case "$1" in
|
|
*[!A-Za-z0-9._/-]*|*..*|*//*|/) return 1 ;;
|
|
esac
|
|
return 0
|
|
}
|
|
|
|
abs_dir() { ( cd "$1" 2>/dev/null && pwd -P ); }
|
|
|
|
# port_in_use PORT tcp|udp: 0 = in use, 1 = free, 2 = cannot tell
|
|
port_in_use() {
|
|
local fl
|
|
case "$2" in tcp) fl=-ltn ;; *) fl=-lun ;; esac
|
|
if command -v ss >/dev/null 2>&1; then
|
|
ss $fl 2>/dev/null | awk -v p=":$1\$" '$4 ~ p { f = 1 } END { exit !f }' && return 0 || return 1
|
|
elif command -v netstat >/dev/null 2>&1; then
|
|
netstat $fl 2>/dev/null | awk -v p=":$1\$" '$4 ~ p { f = 1 } END { exit !f }' && return 0 || return 1
|
|
fi
|
|
return 2
|
|
}
|
|
|
|
# check_port PORT PROTO LABEL: dies if the port is busy
|
|
check_port() {
|
|
local rc
|
|
port_in_use "$1" "$2" && rc=0 || rc=$?
|
|
case "$rc" in
|
|
0) die "$3 port $1/$2 is already in use on this host" ;;
|
|
2) warn "cannot check whether port $1/$2 is free (no ss or netstat)" ;;
|
|
esac
|
|
return 0
|
|
}
|
|
|
|
free_mb() { df -Pk "$1" 2>/dev/null | awk 'NR == 2 { print int($4 / 1024) }'; }
|
|
|
|
# check_free DIR MIN_MB LABEL (DIR must exist; the nearest existing parent is used otherwise)
|
|
check_free() {
|
|
local f d
|
|
d="$1"
|
|
while [ ! -d "$d" ] && [ "$d" != / ]; do d=$(dirname "$d"); done
|
|
f=$(free_mb "$d")
|
|
if [ -z "$f" ]; then
|
|
warn "cannot determine free space for $3 ($d)"
|
|
elif [ "$f" -lt "$2" ]; then
|
|
die "not enough free space for $3: ${f} MB free in $d, at least $2 MB required (override: OVPMON_MIN_FREE_MB / OVPMON_MIN_FREE_BACKUP_MB)"
|
|
else
|
|
log "free space for $3: ${f} MB ($d)"
|
|
fi
|
|
}
|
|
|
|
acquire_lock() {
|
|
local pid
|
|
if mkdir "$LOCK_DIR" 2>/dev/null; then
|
|
echo $$ > "$LOCK_DIR/pid"
|
|
LOCK_HELD=1
|
|
return 0
|
|
fi
|
|
pid=$(cat "$LOCK_DIR/pid" 2>/dev/null || true)
|
|
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
|
|
die "another deploy script is running (pid $pid)"
|
|
fi
|
|
warn "removing stale lock $LOCK_DIR"
|
|
rm -rf "$LOCK_DIR"
|
|
mkdir "$LOCK_DIR" || die "cannot create lock $LOCK_DIR"
|
|
echo $$ > "$LOCK_DIR/pid"
|
|
LOCK_HELD=1
|
|
}
|
|
release_lock() {
|
|
if [ "${LOCK_HELD:-0}" = 1 ]; then
|
|
rm -rf "$LOCK_DIR"
|
|
LOCK_HELD=0
|
|
fi
|
|
}
|
|
|
|
stamp() { date -u '+%Y%m%dT%H%M%SZ'; }
|
|
now_iso() { date -u '+%Y-%m-%dT%H:%M:%SZ'; }
|
|
|
|
# ensure_dir PATH OWNER:GROUP MODE
|
|
ensure_dir() {
|
|
mkdir -p "$1"
|
|
chown "$2" "$1"
|
|
chmod "$3" "$1"
|
|
}
|
|
|
|
# version_of SRC_DIR: short git commit of the checkout, or "unknown"
|
|
version_of() {
|
|
git -C "$1" rev-parse --short HEAD 2>/dev/null || echo "unknown"
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Data files read as data, never sourced: /etc/ovpmon/env and /etc/ovpmon/deploy.state
|
|
# ---------------------------------------------------------------------------------------------
|
|
kv_get() { # FILE KEY
|
|
[ -f "$1" ] || return 0
|
|
sed -n "s/^[[:space:]]*$2=//p" "$1" | tail -n 1 | sed -e 's/^"\(.*\)"$/\1/' -e "s/^'\(.*\)'\$/\1/"
|
|
}
|
|
|
|
# kv_set FILE KEY VALUE [MODE]: replace or append, atomic (VALUE must not contain | & \ or newlines)
|
|
kv_set() {
|
|
local tmp
|
|
tmp="$1.tmp.$$"
|
|
( umask 077
|
|
if [ -f "$1" ] && grep -q "^$2=" "$1"; then
|
|
sed "s|^$2=.*|$2=$3|" "$1" > "$tmp"
|
|
else
|
|
{ [ -f "$1" ] && cat "$1"; printf '%s=%s\n' "$2" "$3"; } > "$tmp"
|
|
fi )
|
|
chmod "${4:-600}" "$tmp"
|
|
mv "$tmp" "$1"
|
|
}
|
|
|
|
env_get() { kv_get "$ENV_FILE" "$1"; }
|
|
state_get() { kv_get "$STATE_FILE" "$1"; }
|
|
state_set() { kv_set "$STATE_FILE" "$1" "$2" 644; }
|
|
|
|
env_remove_admin() {
|
|
local tmp
|
|
tmp="$ENV_FILE.tmp.$$"
|
|
( umask 077; grep -v -E '^[[:space:]]*(OVPMON_INITIAL_ADMIN_(USER|PASSWORD)=|# Initial administrator)' "$ENV_FILE" > "$tmp" || true )
|
|
chmod 600 "$tmp"
|
|
mv "$tmp" "$ENV_FILE"
|
|
}
|
|
|
|
# with_service_env CMD ARGS...: run a command with the variables of /etc/ovpmon/env exported
|
|
# (parsed as KEY=VALUE data; nothing in the file is executed)
|
|
with_service_env() {
|
|
(
|
|
if [ -f "$ENV_FILE" ]; then
|
|
while IFS= read -r _l || [ -n "$_l" ]; do
|
|
case "$_l" in ""|"#"*) continue ;; esac
|
|
case "$_l" in [A-Za-z_]*=*) ;; *) continue ;; esac
|
|
_k=${_l%%=*}
|
|
_v=${_l#*=}
|
|
case "$_k" in *[!A-Za-z0-9_]*) continue ;; esac
|
|
case "$_v" in \"*\") _v=${_v#\"}; _v=${_v%\"} ;; \'*\') _v=${_v#\'}; _v=${_v%\'} ;; esac
|
|
export "$_k=$_v"
|
|
done < "$ENV_FILE"
|
|
fi
|
|
"$@"
|
|
)
|
|
}
|
|
|
|
# shquote ARGS...: each argument single-quoted for sh
|
|
shquote() {
|
|
local a out="" q="'"
|
|
for a in "$@"; do
|
|
out="$out '$(printf '%s' "$a" | sed "s/'/'\\\\''/g")'"
|
|
done
|
|
printf '%s' "${out# }"
|
|
}
|
|
|
|
# run_as_service_user DIR CMD...: with the service environment, as user ovpmon, in DIR.
|
|
# The environment is loaded as root (the env file is root:root 600), then handed over (su -m).
|
|
# The command is passed to su as one quoted string: su itself must not see the command's own options.
|
|
run_as_service_user() {
|
|
local dir
|
|
dir="$1"; shift
|
|
with_service_env su -m -s /bin/sh "$SERVICE_USER" -c "cd $(shquote "$dir") && exec $(shquote "$@")"
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Database paths (as the services see them: env file, else the defaults of config.ini)
|
|
# ---------------------------------------------------------------------------------------------
|
|
# resolve_db_paths PREFIX: DB_MONITOR, DB_PROFILER (absolute)
|
|
resolve_db_paths() {
|
|
DB_MONITOR=$(env_get OVPMON_OPENVPN_MONITOR_DB_PATH)
|
|
DB_PROFILER=$(env_get OVPMON_PROFILER_DB_PATH)
|
|
[ -n "$DB_MONITOR" ] || DB_MONITOR="openvpn_monitor.db"
|
|
[ -n "$DB_PROFILER" ] || DB_PROFILER="ovpn_profiler.db"
|
|
case "$DB_MONITOR" in /*) ;; *) DB_MONITOR="$1/APP_CORE/$DB_MONITOR" ;; esac
|
|
case "$DB_PROFILER" in /*) ;; *) DB_PROFILER="$1/APP_PROFILER/$DB_PROFILER" ;; esac
|
|
}
|
|
|
|
# users_count DB: rows in the users table (read-only; empty if it cannot be read)
|
|
users_count() {
|
|
[ -f "$1" ] || return 0
|
|
python3 - "$1" 2>/dev/null <<'PY' | tail -n 1 | tr -d '\r '
|
|
import sqlite3, sys, urllib.parse
|
|
c = sqlite3.connect("file:" + urllib.parse.quote(sys.argv[1]) + "?mode=ro", uri=True)
|
|
print(c.execute("SELECT COUNT(*) FROM users").fetchone()[0])
|
|
PY
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Users and directories
|
|
# ---------------------------------------------------------------------------------------------
|
|
ensure_service_user() {
|
|
if getent passwd "$SERVICE_USER" >/dev/null 2>&1; then
|
|
log "user $SERVICE_USER exists"
|
|
return 0
|
|
fi
|
|
log "creating system user $SERVICE_USER"
|
|
case "$OS_FAMILY" in
|
|
alpine)
|
|
getent group "$SERVICE_USER" >/dev/null 2>&1 || addgroup -S "$SERVICE_USER"
|
|
adduser -S -D -H -h "$DATA_DIR" -s "$NOLOGIN" -G "$SERVICE_USER" "$SERVICE_USER" ;;
|
|
*)
|
|
getent group "$SERVICE_USER" >/dev/null 2>&1 || groupadd --system "$SERVICE_USER"
|
|
useradd --system --no-create-home --home-dir "$DATA_DIR" --shell "$NOLOGIN" -g "$SERVICE_USER" "$SERVICE_USER" ;;
|
|
esac
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Managed files: replaced only while they are ours (marker line) or absent
|
|
# ---------------------------------------------------------------------------------------------
|
|
# install_managed TMP DEST MODE: 0 = installed/replaced, 1 = identical (nothing to do),
|
|
# 2 = DEST exists without our marker and differs: left untouched (warning). TMP is removed.
|
|
install_managed() {
|
|
local tmp dest mode
|
|
tmp="$1"; dest="$2"; mode="$3"
|
|
if [ -f "$dest" ]; then
|
|
if cmp -s "$tmp" "$dest"; then rm -f "$tmp"; return 1; fi
|
|
if ! grep -q "$MARKER" "$dest"; then
|
|
warn "$dest was not created by these scripts and differs from the shipped version: left untouched (compare with ${4:-the shipped template} if you want the new one)"
|
|
rm -f "$tmp"
|
|
return 2
|
|
fi
|
|
fi
|
|
mkdir -p "$(dirname "$dest")"
|
|
cp "$tmp" "$dest.new.$$"
|
|
chown root:root "$dest.new.$$"
|
|
chmod "$mode" "$dest.new.$$"
|
|
mv "$dest.new.$$" "$dest"
|
|
rm -f "$tmp"
|
|
return 0
|
|
}
|
|
|
|
# render_unit SRC_DIR NAME PREFIX OUT: init script / unit with the install directory applied
|
|
render_unit() {
|
|
local src
|
|
src=$(unit_src "$1" "$2")
|
|
[ -f "$src" ] || die "service definition not found: $src"
|
|
sed -e "s#$DEFAULT_PREFIX#$3#g" "$src" > "$4"
|
|
}
|
|
|
|
# render_nginx SRC_DIR HTTPS_PORT OUT
|
|
render_nginx() {
|
|
local src
|
|
src="$1/deploy/native/nginx/ovpmon.conf.template"
|
|
[ -f "$src" ] || die "nginx template not found: $src"
|
|
sed -e "s#@HTTPS_PORT@#$2#g" -e "s#@WEB_ROOT@#$WEB_ROOT#g" \
|
|
-e "s#@TLS_CRT@#$TLS_DIR/ovpmon.crt#g" -e "s#@TLS_KEY@#$TLS_DIR/ovpmon.key#g" "$1/deploy/native/nginx/ovpmon.conf.template" > "$3"
|
|
}
|
|
|
|
# helper_conf_text PREFIX: content of /etc/ovpmon/helper.conf for this OS
|
|
helper_conf_text() {
|
|
cat <<EOF
|
|
# $MARKER; read by $HELPER_DEST (root side of the privilege separation)
|
|
# Strictly validated by the helper; the file must stay root:root and not group/world writable.
|
|
[helper]
|
|
install_dir = $1
|
|
service_manager = $INIT
|
|
service_unit = $OVPN_SERVICE
|
|
config_path = $OVPN_CONFIG
|
|
staged_path = $DATA_DIR/staging/server.conf
|
|
EOF
|
|
}
|
|
|
|
# install_helper SRC_DIR: /usr/local/sbin/ovpmon-helper (root:root 755), atomic. 0 = changed, 1 = same
|
|
install_helper() {
|
|
local src tmp
|
|
src="$1/DOCS/General/privilege-separation/ovpmon-helper"
|
|
[ -f "$src" ] || die "helper not found: $src"
|
|
# compile() instead of py_compile: the source tree may be read-only (no .pyc can be written)
|
|
python3 -c 'import sys; compile(open(sys.argv[1]).read(), sys.argv[1], "exec")' "$src" >/dev/null 2>&1 \
|
|
|| die "the helper has a syntax error: $src"
|
|
rm -rf "$(dirname "$src")/__pycache__"
|
|
if [ -f "$HELPER_DEST" ] && cmp -s "$src" "$HELPER_DEST"; then
|
|
chown root:root "$HELPER_DEST"; chmod 755 "$HELPER_DEST"
|
|
return 1
|
|
fi
|
|
mkdir -p "$(dirname "$HELPER_DEST")"
|
|
tmp="$HELPER_DEST.new.$$"
|
|
cp "$src" "$tmp"
|
|
chown root:root "$tmp"
|
|
chmod 755 "$tmp"
|
|
mv "$tmp" "$HELPER_DEST"
|
|
return 0
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# doas rules: Alpine with /etc/doas.d: own file /etc/doas.d/ovpmon.conf (as on the existing hosts);
|
|
# otherwise a marked block inside /etc/doas.conf (other rules are never touched)
|
|
# ---------------------------------------------------------------------------------------------
|
|
doas_target() {
|
|
if [ "$OS_FAMILY" = alpine ] && [ -d /etc/doas.d ]; then
|
|
DOAS_MODE=file
|
|
DOAS_FILE=/etc/doas.d/ovpmon.conf
|
|
else
|
|
DOAS_MODE=block
|
|
DOAS_FILE=/etc/doas.conf
|
|
fi
|
|
}
|
|
|
|
# doas_rules SRC_DIR: the rules (no comments, no blank lines)
|
|
doas_rules() {
|
|
local f
|
|
f="$1/DOCS/General/privilege-separation/doas-ovpmon.conf"
|
|
[ -f "$f" ] || die "doas rules not found: $f"
|
|
grep -v -E '^[[:space:]]*(#|$)' "$f"
|
|
}
|
|
|
|
doas_check_file() { # a candidate configuration must parse
|
|
if command -v doas >/dev/null 2>&1; then
|
|
doas -C "$1" >/dev/null 2>&1 || return 1
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
# doas_block_text SRC_DIR: the marked block
|
|
doas_block_text() {
|
|
printf '%s\n' "$DOAS_BEGIN"
|
|
doas_rules "$1"
|
|
printf '%s\n' "$DOAS_END"
|
|
}
|
|
|
|
# doas_current_block: our block as it is in the active file (empty if none)
|
|
doas_current_block() {
|
|
[ -f "$DOAS_FILE" ] || return 0
|
|
awk -v b="$DOAS_BEGIN" -v e="$DOAS_END" '$0 == b { p = 1 } p { print } $0 == e { p = 0 }' "$DOAS_FILE"
|
|
}
|
|
|
|
# install_doas SRC_DIR: 0 = changed, 1 = unchanged, 2 = refused (foreign file differs)
|
|
install_doas() {
|
|
local tmp new
|
|
doas_target
|
|
tmp="$DOAS_FILE.new.$$"
|
|
if [ "$DOAS_MODE" = file ]; then
|
|
{ printf '# %s\n' "$MARKER"; doas_rules "$1"; } > "$tmp"
|
|
if [ -f "$DOAS_FILE" ]; then
|
|
if cmp -s "$tmp" "$DOAS_FILE"; then rm -f "$tmp"; return 1; fi
|
|
if ! grep -q "$MARKER" "$DOAS_FILE"; then
|
|
if [ "$(doas_rules "$1")" = "$(grep -v -E '^[[:space:]]*(#|$)' "$DOAS_FILE")" ]; then
|
|
rm -f "$tmp"; return 1 # same rules written by hand earlier
|
|
fi
|
|
warn "$DOAS_FILE exists, is not managed by these scripts and differs from the shipped rules: left untouched"
|
|
rm -f "$tmp"; return 2
|
|
fi
|
|
fi
|
|
doas_check_file "$tmp" || { rm -f "$tmp"; die "doas rejects the new rules ($DOAS_FILE): nothing was changed"; }
|
|
chown root:root "$tmp"; chmod 600 "$tmp"; mv "$tmp" "$DOAS_FILE"
|
|
return 0
|
|
fi
|
|
# block mode
|
|
new=$(doas_block_text "$1")
|
|
if [ -f "$DOAS_FILE" ] && [ "$(doas_current_block)" = "$new" ]; then return 1; fi
|
|
{
|
|
if [ -f "$DOAS_FILE" ]; then
|
|
awk -v b="$DOAS_BEGIN" -v e="$DOAS_END" '$0 == b { skip = 1; next } $0 == e { skip = 0; next } !skip { print }' "$DOAS_FILE"
|
|
fi
|
|
printf '%s\n' "$new"
|
|
} > "$tmp"
|
|
doas_check_file "$tmp" || { rm -f "$tmp"; die "doas rejects the resulting configuration: nothing was changed in $DOAS_FILE"; }
|
|
if [ -f "$DOAS_FILE" ]; then
|
|
chmod --reference="$DOAS_FILE" "$tmp" 2>/dev/null || chmod 600 "$tmp"
|
|
else
|
|
chmod 600 "$tmp"
|
|
fi
|
|
chown root:root "$tmp"
|
|
mv "$tmp" "$DOAS_FILE"
|
|
return 0
|
|
}
|
|
|
|
# doas_selftest: the service user can call the helper through doas and the helper accepts its configuration
|
|
doas_selftest() {
|
|
local out
|
|
out=$(su -s /bin/sh "$SERVICE_USER" -c "doas -n $HELPER_DEST service status" 2>&1 || true)
|
|
case "$out" in
|
|
*'"status": "rejected"'*) warn "the helper refuses to run: $out"; return 1 ;;
|
|
*'"status"'*) return 0 ;;
|
|
esac
|
|
warn "the user $SERVICE_USER cannot call the helper through doas (output: $(printf '%s' "$out" | tail -n 1)). Check $DOAS_FILE"
|
|
return 1
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Code, virtualenvs, UI
|
|
# ---------------------------------------------------------------------------------------------
|
|
# copy_component SRC_COMPONENT_DIR DEST_DIR: code only (no venv, easy-rsa, client-config, databases, logs, caches)
|
|
copy_component() {
|
|
[ -d "$1" ] || die "source directory not found: $1"
|
|
mkdir -p "$2"
|
|
( cd "$1" && tar -cf - --exclude=venv --exclude=easy-rsa --exclude=client-config \
|
|
--exclude=__pycache__ --exclude='*.pyc' --exclude='*.db' --exclude='*.db-*' --exclude='*.log' \
|
|
--exclude=.pytest_cache --exclude='Dockerfile*' --exclude=entrypoint.sh . ) | ( cd "$2" && tar -xf - --no-same-owner )
|
|
}
|
|
|
|
# sync_deploy_files SRC_DIR PREFIX: the scripts and templates that update.sh/install.sh need, next to the code
|
|
# (skipped when the source is the install directory itself)
|
|
sync_deploy_files() {
|
|
local s p d
|
|
s=$(abs_dir "$1"); p=$(abs_dir "$2")
|
|
[ "$s" != "$p" ] || return 0
|
|
for d in deploy/native DOCS/General/systemd DOCS/General/openrc DOCS/General/privilege-separation; do
|
|
[ -d "$1/$d" ] || continue
|
|
mkdir -p "$2/$d"
|
|
( cd "$1/$d" && tar -cf - --exclude='*.tar.gz' --exclude=__pycache__ . ) | ( cd "$2/$d" && tar -xf - --no-same-owner )
|
|
done
|
|
chown -R root:root "$2/deploy" "$2/DOCS" 2>/dev/null || true
|
|
}
|
|
|
|
# make_venv DIR [EXTRA_PACKAGES...]: virtualenv in DIR/venv with the requirements of DIR
|
|
make_venv() {
|
|
local dir
|
|
dir="$1"; shift
|
|
[ -f "$dir/requirements.txt" ] || die "no requirements.txt in $dir"
|
|
if [ ! -x "$dir/venv/bin/python" ]; then
|
|
python3 -m venv "$dir/venv" || die "cannot create a virtualenv in $dir/venv (is python3-venv installed?)"
|
|
fi
|
|
log "installing Python dependencies into $dir/venv (needs access to the Python package index)"
|
|
"$dir/venv/bin/python" -m pip install --disable-pip-version-check --no-cache-dir -q -r "$dir/requirements.txt" "$@" \
|
|
|| die "pip install failed in $dir"
|
|
}
|
|
|
|
# smoke_component DIR KIND: the virtualenv can import what the component needs
|
|
smoke_component() {
|
|
case "$2" in
|
|
core) ( cd "$1" && venv/bin/python -c 'import flask, flask_cors, jwt, pyotp, qrcode, bcrypt, gunicorn, migrations' ) ;;
|
|
profiler) ( cd "$1" && venv/bin/python -c 'import fastapi, uvicorn, sqlalchemy, psutil, jinja2, jwt, migrations' ) ;;
|
|
esac
|
|
}
|
|
|
|
# fix_venv_paths DIR OLD NEW: a virtualenv built under another path (staging) gets its scripts pointed at the final path
|
|
fix_venv_paths() {
|
|
local f
|
|
[ "$2" != "$3" ] || return 0
|
|
grep -rIl -- "$2" "$1/venv/bin" "$1/venv/pyvenv.cfg" 2>/dev/null | while read -r f; do
|
|
sed -i "s#$2#$3#g" "$f"
|
|
done
|
|
}
|
|
|
|
# compile_code DIR: byte-code caches written as root (the service user cannot write them in the root-owned tree)
|
|
compile_code() {
|
|
( cd "$1" && venv/bin/python -m compileall -q -x '/venv/' . >/dev/null 2>&1 ) || true
|
|
}
|
|
|
|
# check_gnu_tar: the scripts use GNU tar options (Alpine: the 'tar' package, not busybox tar)
|
|
check_gnu_tar() {
|
|
tar --version 2>/dev/null | grep -q 'GNU tar' || die "GNU tar is required (Alpine: apk add tar); the busybox tar is not enough"
|
|
}
|
|
|
|
# lock_down_code DIR...: root-owned, not writable by others. Data inside the code dirs (easy-rsa, client-config,
|
|
# *.db, *.log such as profiler.log) is skipped: its owner is never changed.
|
|
lock_down_code() {
|
|
local d
|
|
for d in "$@"; do
|
|
find "$d" \( -path "$d/easy-rsa" -o -path "$d/client-config" -o -name '*.db' -o -name '*.db-*' -o -name '*.log' \) -prune -o \
|
|
-exec chown -h root:root {} + -exec chmod go-w,a+rX {} +
|
|
done
|
|
}
|
|
|
|
# ensure_runtime_files PREFIX: what the services must be able to write below the (root-owned) code dirs
|
|
ensure_runtime_files() {
|
|
local p
|
|
p="$1/APP_PROFILER"
|
|
[ -d "$p/easy-rsa" ] || { mkdir -p "$p/easy-rsa"; chown "$SERVICE_USER:$SERVICE_USER" "$p/easy-rsa"; chmod 750 "$p/easy-rsa"; }
|
|
[ -d "$p/client-config" ] || { mkdir -p "$p/client-config"; chown "$SERVICE_USER:$SERVICE_USER" "$p/client-config"; chmod 750 "$p/client-config"; }
|
|
if [ ! -e "$p/profiler.log" ]; then
|
|
: > "$p/profiler.log"
|
|
chown "$SERVICE_USER:$SERVICE_USER" "$p/profiler.log"
|
|
chmod 640 "$p/profiler.log"
|
|
fi
|
|
}
|
|
|
|
# ---- UI: sources, committed build, staging ----
|
|
# The UI is part of the repository: deploy/ui-dist holds a build of APP_UI together with SOURCE_HASH, the hash
|
|
# of the sources it was built from (ui_source_hash). The installed web root records the hash it came from in
|
|
# $UI_HASH_FILE, so update.sh can tell when the UI changed. See DOCS/Deployment/Scenario2_System_Services.md
|
|
UI_HASH_FILE=.ui-source-hash
|
|
|
|
# sha256_run [FILE...]: sha256sum, or shasum -a 256 (macOS); "hash name" lines, stdin when no file is given
|
|
sha256_run() {
|
|
if command -v sha256sum >/dev/null 2>&1; then sha256sum "$@"
|
|
elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$@"
|
|
else die "neither sha256sum nor shasum found"
|
|
fi
|
|
}
|
|
|
|
# valid_hash VALUE: 64 hexadecimal digits
|
|
valid_hash() {
|
|
case "$1" in
|
|
""|*[!0-9a-f]*) return 1 ;;
|
|
esac
|
|
[ "${#1}" -eq 64 ]
|
|
}
|
|
|
|
# ui_source_hash [APP_UI_DIR]: SHA-256 over the UI sources (default: this repository's APP_UI), deterministic and
|
|
# the same on busybox and GNU: relative paths of src/, public/ and the build configuration files in C-locale
|
|
# order, the sha256 of every file with its path, then the sha256 of that list. node_modules and dist are never
|
|
# part of it. Prints the hash; fails when there are no sources.
|
|
ui_source_hash() {
|
|
local d
|
|
d="${1:-$ROOT_DIR/APP_UI}"
|
|
[ -f "$d/package.json" ] && [ -d "$d/src" ] || return 1
|
|
command -v sha256sum >/dev/null 2>&1 || command -v shasum >/dev/null 2>&1 || return 1
|
|
(
|
|
cd "$d" || exit 1
|
|
{
|
|
for f in index.html package.json package-lock.json vite.config.js jsconfig.json; do
|
|
if [ -f "$f" ]; then echo "$f"; fi
|
|
done
|
|
find src public -type f ! -name .DS_Store 2>/dev/null
|
|
} | LC_ALL=C sort | while IFS= read -r f; do sha256_run "$f"; done | sha256_run | awk '{ print $1 }'
|
|
)
|
|
}
|
|
|
|
# ui_hash_of SOURCE: the SOURCE_HASH stored in a UI directory or tarball, "unknown" when there is none
|
|
ui_hash_of() {
|
|
local h
|
|
h=""
|
|
if [ -d "$1" ]; then
|
|
h=$(tr -d ' \t\r\n' 2>/dev/null < "$1/SOURCE_HASH" || true)
|
|
elif [ -f "$1" ]; then
|
|
h=$(tar -xzOf "$1" ./SOURCE_HASH 2>/dev/null || tar -xzOf "$1" SOURCE_HASH 2>/dev/null || true)
|
|
h=$(printf '%s' "$h" | tr -d ' \t\r\n')
|
|
fi
|
|
if valid_hash "$h"; then printf '%s' "$h"; else printf 'unknown'; fi
|
|
}
|
|
|
|
# ui_repo_state REPO_DIR: sets UI_STATE (fresh | stale | missing), UI_BUILT_HASH (SOURCE_HASH of the committed
|
|
# build) and UI_CUR_HASH (hash of the APP_UI sources now). A build without a valid SOURCE_HASH counts as stale
|
|
# (it cannot be shown to match). Returns 1 when the hash of the sources cannot be computed.
|
|
ui_repo_state() {
|
|
local dir
|
|
dir="$1/deploy/ui-dist"
|
|
UI_STATE=missing; UI_BUILT_HASH=""; UI_CUR_HASH=""
|
|
[ -f "$dir/index.html" ] || return 0
|
|
UI_CUR_HASH=$(ui_source_hash "$1/APP_UI") || { UI_CUR_HASH=""; return 1; }
|
|
UI_BUILT_HASH=$(ui_hash_of "$dir")
|
|
if [ "$UI_BUILT_HASH" = "$UI_CUR_HASH" ]; then UI_STATE=fresh; else UI_STATE=stale; fi
|
|
}
|
|
|
|
# ui_stale_lines: the explanation printed when the committed build is older than the sources (one line each)
|
|
ui_stale_lines() {
|
|
echo "the committed UI build deploy/ui-dist is OLDER than the APP_UI sources (build: ${UI_BUILT_HASH:-none}, sources: $UI_CUR_HASH)."
|
|
echo "It does NOT contain the latest UI changes."
|
|
echo "Maintainers: run deploy/native/build-ui.sh --publish and commit deploy/ui-dist."
|
|
echo "On this host: add --build-ui to build the current UI now (Docker, or Node 20.19+ / --node-download; see build-ui.sh --help)."
|
|
}
|
|
|
|
# ui_build_check / ui_build_run REPO OUT: prerequisites of the build, the build into the tarball OUT (build-ui.sh
|
|
# picks Docker when it works, otherwise the local Node; UI_NODE_DL=1 adds --node-download)
|
|
ui_build_check() {
|
|
[ -f "$SCRIPT_DIR/build-ui.sh" ] || die "$SCRIPT_DIR/build-ui.sh not found"
|
|
if [ "${UI_NODE_DL:-0}" = 1 ]; then sh "$SCRIPT_DIR/build-ui.sh" --check --node-download; else sh "$SCRIPT_DIR/build-ui.sh" --check; fi \
|
|
|| die "the UI cannot be built on this host (see above)"
|
|
}
|
|
ui_build_run() {
|
|
log "building the UI from $1/APP_UI (--build-ui); this takes a few minutes"
|
|
if [ "${UI_NODE_DL:-0}" = 1 ]; then sh "$SCRIPT_DIR/build-ui.sh" --repo "$1" -o "$2" --node-download; else sh "$SCRIPT_DIR/build-ui.sh" --repo "$1" -o "$2"; fi \
|
|
|| die "the UI build failed (see above)"
|
|
}
|
|
|
|
# check_ui_source FILE|DIR: a readable .tar.gz with index.html at its top level, or a directory with index.html
|
|
check_ui_source() {
|
|
if [ -d "$1" ]; then
|
|
[ -f "$1/index.html" ] && [ -r "$1/index.html" ] || die "UI directory has no readable index.html: $1"
|
|
elif [ -f "$1" ]; then
|
|
tar -tzf "$1" >/dev/null 2>&1 || die "UI archive is not a readable .tar.gz: $1"
|
|
tar -tzf "$1" | grep -q -E '^(\./)?index\.html$' || die "UI archive has no index.html at its top level: $1"
|
|
else
|
|
die "UI source not found: $1 (a .tar.gz from deploy/native/build-ui.sh, or a directory with index.html)"
|
|
fi
|
|
}
|
|
|
|
# ui_resolve REPO_DIR TMP_DIR: picks the UI source. Input: UI_DIST (explicit FILE|DIR), UI_BUILD=1 (--build-ui),
|
|
# UI_FALLBACK (optional tarball of last resort). Order: explicit, build now, committed deploy/ui-dist, fallback.
|
|
# Output: UI_SRC (path, empty when nothing was found), UI_KIND (explicit|build|repo|fallback), UI_ORIGIN (text).
|
|
# A stale committed build is used with a loud warning; --build-ui always builds fresh from the sources.
|
|
ui_resolve() {
|
|
local repo tmp line
|
|
repo="$1"; tmp="$2"
|
|
UI_SRC=""; UI_KIND=""; UI_ORIGIN=""
|
|
if [ -n "${UI_DIST:-}" ]; then
|
|
check_ui_source "$UI_DIST"
|
|
UI_SRC="$UI_DIST"; UI_KIND=explicit; UI_ORIGIN="$UI_DIST (--ui-dist)"
|
|
return 0
|
|
fi
|
|
if [ "${UI_BUILD:-0}" = 1 ]; then
|
|
ui_build_check
|
|
ui_build_run "$repo" "$tmp/ui-build.tar.gz"
|
|
check_ui_source "$tmp/ui-build.tar.gz"
|
|
UI_SRC="$tmp/ui-build.tar.gz"; UI_KIND=build; UI_ORIGIN="built now from APP_UI (--build-ui)"
|
|
return 0
|
|
fi
|
|
if ui_repo_state "$repo"; then
|
|
case "$UI_STATE" in
|
|
fresh)
|
|
UI_SRC="$repo/deploy/ui-dist"; UI_KIND=repo; UI_ORIGIN="deploy/ui-dist of the repository (up to date with APP_UI)" ;;
|
|
stale)
|
|
ui_stale_lines | while IFS= read -r line; do warn "$line"; done
|
|
UI_SRC="$repo/deploy/ui-dist"; UI_KIND=repo; UI_ORIGIN="deploy/ui-dist of the repository (STALE: older than APP_UI)" ;;
|
|
*)
|
|
log "no committed UI build in $repo/deploy/ui-dist" ;;
|
|
esac
|
|
else
|
|
warn "cannot compute the hash of $repo/APP_UI (sources missing or no sha256sum/shasum): the committed UI build cannot be checked for staleness"
|
|
if [ -f "$repo/deploy/ui-dist/index.html" ]; then
|
|
UI_SRC="$repo/deploy/ui-dist"; UI_KIND=repo; UI_ORIGIN="deploy/ui-dist of the repository (not verified)"
|
|
fi
|
|
fi
|
|
if [ -z "$UI_SRC" ] && [ -n "${UI_FALLBACK:-}" ] && [ -f "$UI_FALLBACK" ]; then
|
|
check_ui_source "$UI_FALLBACK"
|
|
UI_SRC="$UI_FALLBACK"; UI_KIND=fallback; UI_ORIGIN="$UI_FALLBACK (archive found next to the scripts)"
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
# stage_ui FILE|DIR DIR: a validated copy in a fresh DIR (never the live web root). Records the hash of the
|
|
# sources the build came from in DIR/$UI_HASH_FILE (SOURCE_HASH of the build, "unknown" for foreign archives).
|
|
stage_ui() {
|
|
local h
|
|
check_ui_source "$1"
|
|
h=$(ui_hash_of "$1")
|
|
rm -rf "$2"
|
|
mkdir -p "$2"
|
|
if [ -d "$1" ]; then
|
|
( cd "$1" && tar -cf - . ) | ( cd "$2" && tar -xf - --no-same-owner ) || die "cannot copy the UI directory $1"
|
|
else
|
|
tar -xzf "$1" -C "$2" --no-same-owner || die "cannot extract the UI archive"
|
|
fi
|
|
[ -f "$2/index.html" ] && [ -r "$2/index.html" ] || die "index.html missing in the staged UI files"
|
|
rm -f "$2/SOURCE_HASH"
|
|
printf '%s\n' "$h" > "$2/$UI_HASH_FILE"
|
|
chown -R root:root "$2"
|
|
chmod -R go-w "$2"
|
|
chmod -R a+rX "$2"
|
|
}
|
|
|
|
# swap_dir NEW LIVE OLD: LIVE becomes NEW, the previous LIVE (if any) is kept as OLD
|
|
swap_dir() {
|
|
[ -d "$1" ] || die "missing directory to swap in: $1"
|
|
rm -rf "$3"
|
|
if [ -e "$2" ]; then mv "$2" "$3"; fi
|
|
mv "$1" "$2"
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# nginx
|
|
# ---------------------------------------------------------------------------------------------
|
|
# check_nginx_include: the distribution nginx.conf includes our directory
|
|
check_nginx_include() {
|
|
if [ -f /etc/nginx/nginx.conf ] && ! grep -q -E "include[[:space:]]+/etc/nginx/$NGINX_INCLUDE_RE" /etc/nginx/nginx.conf; then
|
|
die "/etc/nginx/nginx.conf does not include $NGINX_CONF_DIR/*.conf: add 'include $NGINX_CONF_DIR/*.conf;' to its http block"
|
|
fi
|
|
}
|
|
|
|
nginx_apply() { # test the configuration, reload or start nginx
|
|
local out
|
|
out=$(nginx -t 2>&1) || { indent "$out" >&2; return 1; }
|
|
if svc_active nginx; then svc_reload nginx >/dev/null 2>&1 || svc_restart nginx >/dev/null 2>&1 || return 1
|
|
else svc_start nginx >/dev/null 2>&1 || return 1
|
|
fi
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Health
|
|
# ---------------------------------------------------------------------------------------------
|
|
http_code() { curl -sk -o /dev/null -w '%{http_code}' --max-time 5 "$@" 2>/dev/null || true; }
|
|
|
|
# wait_http LABEL URL REGEX: waits until the HTTP status matches REGEX (e.g. '200' or '401|403')
|
|
wait_http() {
|
|
local label url re w code
|
|
label="$1"; url="$2"; re="$3"
|
|
w=0
|
|
while :; do
|
|
code=$(http_code "$url")
|
|
if printf '%s' "$code" | grep -q -E "^($re)$"; then
|
|
log " $label: HTTP $code ($url)"
|
|
return 0
|
|
fi
|
|
if [ "$w" -ge "$HEALTH_TIMEOUT" ]; then
|
|
log " $label: no good answer after ${HEALTH_TIMEOUT}s (last HTTP status '$code', wanted $re): $url" >&2
|
|
return 1
|
|
fi
|
|
sleep 2
|
|
w=$((w + 2))
|
|
done
|
|
}
|
|
|
|
# health_check HTTPS_PORT: the three services, both APIs directly, and the UI/APIs through nginx
|
|
health_check() {
|
|
local s bad
|
|
bad=0
|
|
log "health checks (up to ${HEALTH_TIMEOUT}s each)"
|
|
for s in $SERVICES; do
|
|
if svc_active "$s"; then log " $s: $(svc_state_word "$s")"; else log " $s: NOT running" >&2; show_service_log "$s"; bad=1; fi
|
|
done
|
|
[ "$bad" = 0 ] || return 1
|
|
wait_http "monitoring API" "http://127.0.0.1:5001/" '[1-5][0-9][0-9]' || bad=1
|
|
wait_http "profiler API" "http://127.0.0.1:8000/" '200' || bad=1
|
|
if [ -n "${1:-}" ]; then
|
|
wait_http "UI (nginx)" "https://127.0.0.1:$1/" '200' || bad=1
|
|
wait_http "monitoring API through nginx" "https://127.0.0.1:$1/api/v1/user/me" '401|403' || bad=1
|
|
wait_http "profiler API through nginx" "https://127.0.0.1:$1/profiles-api/config" '401|403' || bad=1
|
|
fi
|
|
[ "$bad" = 0 ] || return 1
|
|
log "all health checks passed"
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Backup (same format for backup.sh, update.sh phase 1 and the safety copy of restore)
|
|
#
|
|
# <BACKUP_ROOT>/<UTC timestamp>/ (mode 700)
|
|
# db/monitor.db, db/profiler.db consistent copies (SQLite backup API)
|
|
# etc-ovpmon.tar.gz /etc/ovpmon: env, helper.conf, tls, deploy.state
|
|
# etc-openvpn.tar.gz /etc/openvpn: server.conf, crl.pem, scripts, ipp.txt
|
|
# easy-rsa.tar.gz, client-config.tar.gz PKI and client profiles (APP_PROFILER/...)
|
|
# code.tar.gz APP_CORE and APP_PROFILER including the virtualenvs
|
|
# system.tar.gz helper, init scripts/units, nginx site, sysctl, doas file (file mode)
|
|
# ui.tar.gz the web root
|
|
# doas-block.txt our block of /etc/doas.conf (block mode)
|
|
# MANIFEST, SHA256SUMS, COMPLETE only a backup with COMPLETE is ever used
|
|
# ---------------------------------------------------------------------------------------------
|
|
# backup_db SRC DST: consistent copy through the SQLite backup API, integrity-checked, owner and mode kept
|
|
backup_db() {
|
|
python3 - "$1" "$2" <<'PY' || return 1
|
|
import os, sqlite3, sys, urllib.parse
|
|
src, dst = sys.argv[1], sys.argv[2]
|
|
if os.path.exists(dst):
|
|
os.remove(dst)
|
|
s = sqlite3.connect("file:" + urllib.parse.quote(src) + "?mode=ro", uri=True, timeout=60)
|
|
d = sqlite3.connect(dst)
|
|
s.backup(d)
|
|
res = d.execute("PRAGMA integrity_check").fetchone()[0]
|
|
d.close()
|
|
s.close()
|
|
if res != "ok":
|
|
print("integrity_check of the copy failed: " + str(res), file=sys.stderr)
|
|
sys.exit(1)
|
|
st = os.stat(src)
|
|
os.chown(dst, st.st_uid, st.st_gid)
|
|
os.chmod(dst, st.st_mode & 0o777)
|
|
PY
|
|
}
|
|
|
|
# check_db_copy FILE: integrity check of a copy
|
|
check_db_copy() {
|
|
python3 - "$1" <<'PY'
|
|
import sqlite3, sys
|
|
c = sqlite3.connect("file:" + sys.argv[1].replace("%", "%25").replace("?", "%3f").replace("#", "%23") + "?mode=ro", uri=True)
|
|
r = c.execute("PRAGMA integrity_check").fetchone()[0]
|
|
sys.exit(0 if r == "ok" else 1)
|
|
PY
|
|
}
|
|
|
|
# mk_tar OUT TAR-ARGS...: written under a temporary name and renamed when complete
|
|
mk_tar() {
|
|
local out
|
|
out="$1"; shift
|
|
if tar -czpf "$out.tmp" "$@"; then
|
|
mv "$out.tmp" "$out"
|
|
else
|
|
rm -f "$out.tmp"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# system_files: paths (relative to /) of the files that update.sh may replace
|
|
system_files() {
|
|
local s f
|
|
doas_target
|
|
for s in $SERVICES; do f=$(unit_file "$s"); [ -e "$f" ] && echo "${f#/}"; done
|
|
[ -e "$HELPER_DEST" ] && echo "${HELPER_DEST#/}"
|
|
[ -e "$NGINX_SITE" ] && echo "${NGINX_SITE#/}"
|
|
[ -e "$SYSCTL_FILE" ] && echo "${SYSCTL_FILE#/}"
|
|
if [ "$DOAS_MODE" = file ] && [ -e "$DOAS_FILE" ]; then echo "${DOAS_FILE#/}"; fi
|
|
return 0
|
|
}
|
|
|
|
# make_backup DIR KIND PREFIX [SKIP_CODE]: full backup, verified, marked COMPLETE.
|
|
# SKIP_CODE=1 keeps an existing code.tar.gz (the final snapshot after the stop).
|
|
make_backup() {
|
|
local dir kind prefix skipcode list
|
|
dir="$1"; kind="$2"; prefix="$3"; skipcode="${4:-0}"
|
|
mkdir -p "$dir/db"
|
|
chmod 700 "$dir"
|
|
rm -f "$dir/COMPLETE"
|
|
resolve_db_paths "$prefix"
|
|
doas_target
|
|
|
|
log " databases (consistent copies)"
|
|
if [ -f "$DB_MONITOR" ]; then
|
|
backup_db "$DB_MONITOR" "$dir/db/monitor.db" || { log "backup of $DB_MONITOR failed" >&2; return 1; }
|
|
else
|
|
warn "monitoring database not found: $DB_MONITOR (not backed up)"; rm -f "$dir/db/monitor.db"
|
|
fi
|
|
if [ -f "$DB_PROFILER" ]; then
|
|
backup_db "$DB_PROFILER" "$dir/db/profiler.db" || { log "backup of $DB_PROFILER failed" >&2; return 1; }
|
|
else
|
|
warn "profiler database not found: $DB_PROFILER (not backed up)"; rm -f "$dir/db/profiler.db"
|
|
fi
|
|
|
|
log " configuration (/etc/ovpmon, /etc/openvpn)"
|
|
[ -d "$ETC_DIR" ] && { mk_tar "$dir/etc-ovpmon.tar.gz" -C / "${ETC_DIR#/}" || return 1; }
|
|
[ -d /etc/openvpn ] && { mk_tar "$dir/etc-openvpn.tar.gz" -C / etc/openvpn || return 1; }
|
|
|
|
log " PKI (easy-rsa) and client profiles"
|
|
if [ -d "$prefix/APP_PROFILER/easy-rsa" ]; then mk_tar "$dir/easy-rsa.tar.gz" -C "$prefix/APP_PROFILER" easy-rsa || return 1; fi
|
|
if [ -d "$prefix/APP_PROFILER/client-config" ]; then mk_tar "$dir/client-config.tar.gz" -C "$prefix/APP_PROFILER" client-config || return 1; fi
|
|
|
|
if [ "$skipcode" != 1 ] || [ ! -s "$dir/code.tar.gz" ]; then
|
|
log " code and virtualenvs"
|
|
mk_tar "$dir/code.tar.gz" -C "$prefix" --exclude=APP_PROFILER/easy-rsa --exclude=APP_PROFILER/client-config \
|
|
APP_CORE APP_PROFILER || return 1
|
|
fi
|
|
|
|
log " helper, service definitions, nginx site, UI, doas rules"
|
|
list=$(system_files)
|
|
# shellcheck disable=SC2086
|
|
if [ -n "$list" ]; then mk_tar "$dir/system.tar.gz" -C / $list || return 1; fi
|
|
[ -d "$WEB_ROOT" ] && { mk_tar "$dir/ui.tar.gz" -C "$(dirname "$WEB_ROOT")" "$(basename "$WEB_ROOT")" || return 1; }
|
|
if [ "$DOAS_MODE" = block ]; then
|
|
doas_current_block > "$dir/doas-block.txt"
|
|
[ -s "$dir/doas-block.txt" ] || rm -f "$dir/doas-block.txt"
|
|
fi
|
|
|
|
{
|
|
echo "created_utc=$(now_iso)"
|
|
echo "kind=$kind"
|
|
echo "os=$OS_ID $OS_VERSION_ID"
|
|
echo "init=$INIT"
|
|
echo "prefix=$prefix"
|
|
echo "https_port=$(state_get OVPMON_HTTPS_PORT)"
|
|
echo "version=$(cat "$prefix/VERSION" 2>/dev/null || echo unknown)"
|
|
echo "db_monitor=$DB_MONITOR"
|
|
echo "db_profiler=$DB_PROFILER"
|
|
echo "doas_mode=$DOAS_MODE"
|
|
echo "doas_file=$DOAS_FILE"
|
|
} > "$dir/MANIFEST"
|
|
|
|
( cd "$dir" && rm -f SHA256SUMS && find . -type f \( -name '*.tar.gz' -o -path './db/*' -o -name doas-block.txt -o -name MANIFEST \) | sed 's#^\./##' | sort | xargs sha256sum > SHA256SUMS )
|
|
chmod -R go-rwx "$dir"
|
|
log " verifying the backup"
|
|
verify_backup "$dir" || return 1
|
|
now_iso > "$dir/COMPLETE"
|
|
chmod 600 "$dir/COMPLETE"
|
|
}
|
|
|
|
# verify_backup DIR: checksums, readable archives, database copies pass the integrity check
|
|
verify_backup() {
|
|
local dir f
|
|
dir="$1"
|
|
[ -s "$dir/SHA256SUMS" ] || { log "backup $dir has no SHA256SUMS" >&2; return 1; }
|
|
[ -s "$dir/MANIFEST" ] || { log "backup $dir has no MANIFEST" >&2; return 1; }
|
|
( cd "$dir" && sha256sum -c SHA256SUMS >/dev/null 2>&1 ) || { log "checksum mismatch in backup $dir" >&2; return 1; }
|
|
[ -s "$dir/etc-ovpmon.tar.gz" ] || { log "backup $dir has no etc-ovpmon.tar.gz" >&2; return 1; }
|
|
[ -s "$dir/code.tar.gz" ] || { log "backup $dir has no code.tar.gz" >&2; return 1; }
|
|
for f in "$dir"/*.tar.gz; do
|
|
[ -e "$f" ] || continue
|
|
grep -q " $(basename "$f")\$" "$dir/SHA256SUMS" || { log "archive $f is not listed in SHA256SUMS" >&2; return 1; }
|
|
tar -tzf "$f" >/dev/null 2>&1 || { log "corrupt archive: $f" >&2; return 1; }
|
|
done
|
|
for f in "$dir"/db/*.db; do
|
|
[ -e "$f" ] || continue
|
|
grep -q " db/$(basename "$f")\$" "$dir/SHA256SUMS" || { log "database copy $f is not listed in SHA256SUMS" >&2; return 1; }
|
|
check_db_copy "$f" || { log "database copy fails the integrity check: $f" >&2; return 1; }
|
|
done
|
|
return 0
|
|
}
|
|
|
|
list_backups() { # timestamp directories, oldest first
|
|
[ -d "$BACKUP_ROOT" ] || return 0
|
|
ls -1 "$BACKUP_ROOT" 2>/dev/null | grep -E '^[0-9]{8}T[0-9]{6}Z$' | sort || true
|
|
}
|
|
|
|
latest_backup() { # newest COMPLETE backup (empty if none)
|
|
local b
|
|
for b in $(list_backups | sort -r); do
|
|
if [ -f "$BACKUP_ROOT/$b/COMPLETE" ]; then echo "$BACKUP_ROOT/$b"; return 0; fi
|
|
done
|
|
return 0
|
|
}
|
|
|
|
prune_backups() { # keep the newest KEEP timestamp directories (default 5); -pre-restore copies are kept
|
|
local keep
|
|
keep="${1:-$KEEP_BACKUPS}"
|
|
list_backups | sort -r | awk -v k="$keep" 'NR > k' | while read -r b; do
|
|
log "removing old backup $BACKUP_ROOT/$b"
|
|
rm -rf "${BACKUP_ROOT:?}/$b"
|
|
done
|
|
}
|
|
|
|
manifest_get() { sed -n "s/^$2=//p" "$1/MANIFEST" 2>/dev/null | tail -n 1; }
|
|
|
|
# safe_remove_old DIR: removes a replaced code directory (old or rolled-back version). A directory that still
|
|
# holds the PKI or client profiles is NOT removed: only the code and the virtualenv in it are, the data stays.
|
|
safe_remove_old() {
|
|
local e
|
|
case "$1" in
|
|
*.old-*|*.rollback-*|*.replaced-*) ;;
|
|
*) warn "refusing to remove $1 (unexpected name)"; return 0 ;;
|
|
esac
|
|
[ -e "$1" ] || return 0
|
|
if [ -e "$1/easy-rsa" ] || [ -e "$1/client-config" ]; then
|
|
for e in "$1"/* "$1"/.[!.]*; do
|
|
[ -e "$e" ] || continue
|
|
case "$(basename "$e")" in easy-rsa|client-config) continue ;; esac
|
|
rm -rf "$e"
|
|
done
|
|
warn "$1 still holds easy-rsa or client-config: only its code was removed, the data was left in place (check it, then remove the directory yourself)"
|
|
return 0
|
|
fi
|
|
rm -rf "$1"
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------------------------
|
|
# Restore. The application services must be stopped. MODE:
|
|
# auto after a failed update in the same run: PKI and client profiles are the live ones (never touched by the
|
|
# update); they are restored from the backup only when missing
|
|
# full from a backup chosen by the operator: code, databases, configuration, PKI and client profiles all
|
|
# come from the backup (the caller made a safety backup first)
|
|
# ---------------------------------------------------------------------------------------------
|
|
# restore_code DIR PREFIX MODE STAMP
|
|
restore_code() {
|
|
local dir prefix mode ts c cur src
|
|
dir="$1"; prefix="$2"; mode="$3"; ts="$4"
|
|
log " restoring the code dirs"
|
|
for c in APP_CORE APP_PROFILER; do
|
|
if [ -e "$prefix/$c" ]; then mv "$prefix/$c" "$prefix/$c.rollback-$ts" || return 1; fi
|
|
done
|
|
tar -xzpf "$dir/code.tar.gz" -C "$prefix" || { log "cannot extract code.tar.gz" >&2; return 1; }
|
|
for c in APP_CORE APP_PROFILER; do
|
|
[ -d "$prefix/$c" ] || { log "$prefix/$c missing after the restore" >&2; return 1; }
|
|
done
|
|
# live PKI and client profiles that were moved away with the failed version (auto mode)
|
|
if [ "$mode" = auto ]; then
|
|
for src in "$prefix/APP_PROFILER.rollback-$ts" "$prefix/APP_PROFILER.old-$ts"; do
|
|
for cur in easy-rsa client-config; do
|
|
if [ -e "$src/$cur" ] && [ ! -e "$prefix/APP_PROFILER/$cur" ]; then
|
|
mv "$src/$cur" "$prefix/APP_PROFILER/$cur" || return 1
|
|
fi
|
|
done
|
|
done
|
|
fi
|
|
}
|
|
|
|
# restore_data_dir DIR PREFIX MODE ARCHIVE NAME
|
|
restore_data_dir() {
|
|
local dir prefix mode arch name
|
|
dir="$1"; prefix="$2"; mode="$3"; arch="$4"; name="$5"
|
|
[ -s "$dir/$arch" ] || return 0
|
|
if [ "$mode" = full ] || [ ! -e "$prefix/APP_PROFILER/$name" ]; then
|
|
if [ -e "$prefix/APP_PROFILER/$name" ]; then
|
|
rm -rf "$prefix/APP_PROFILER/$name" # full restore: the safety backup holds the current content
|
|
fi
|
|
tar -xzpf "$dir/$arch" -C "$prefix/APP_PROFILER" || return 1
|
|
log " $name restored from the backup"
|
|
else
|
|
log " $name: the live copy is kept"
|
|
fi
|
|
}
|
|
|
|
# restore_db FILE TARGET STAMP: atomic, the replaced file is kept as TARGET.failed-STAMP
|
|
restore_db() {
|
|
local tmp
|
|
[ -s "$1" ] || return 0
|
|
case "$2" in /*) ;; *) log "refusing a relative database path: $2" >&2; return 1 ;; esac
|
|
mkdir -p "$(dirname "$2")"
|
|
tmp="$2.restore.$$"
|
|
cp -p "$1" "$tmp" || return 1
|
|
[ ! -e "$2" ] || mv "$2" "$2.failed-$3"
|
|
rm -f "$2-journal" "$2-wal" "$2-shm"
|
|
mv "$tmp" "$2"
|
|
log " database restored: $2 (replaced file kept as $2.failed-$3)"
|
|
}
|
|
|
|
# restore_all DIR PREFIX MODE: see above. Returns non-zero on the first failure.
|
|
restore_all() {
|
|
local dir prefix mode ts dbm dbp tmp
|
|
dir="$1"; prefix="$2"; mode="$3"
|
|
ts=$(stamp)
|
|
[ -n "${RESTORE_STAMP:-}" ] && ts="$RESTORE_STAMP"
|
|
verify_backup "$dir" || { log "backup $dir failed verification: nothing was changed" >&2; return 1; }
|
|
dbm=$(manifest_get "$dir" db_monitor)
|
|
dbp=$(manifest_get "$dir" db_profiler)
|
|
|
|
restore_code "$dir" "$prefix" "$mode" "$ts" || return 1
|
|
restore_data_dir "$dir" "$prefix" "$mode" easy-rsa.tar.gz easy-rsa || return 1
|
|
restore_data_dir "$dir" "$prefix" "$mode" client-config.tar.gz client-config || return 1
|
|
|
|
[ -z "$dbm" ] || restore_db "$dir/db/monitor.db" "$dbm" "$ts" || return 1
|
|
[ -z "$dbp" ] || restore_db "$dir/db/profiler.db" "$dbp" "$ts" || return 1
|
|
|
|
log " configuration (/etc/ovpmon, /etc/openvpn)"
|
|
tar -xzpf "$dir/etc-ovpmon.tar.gz" -C / || return 1
|
|
if [ -s "$dir/etc-openvpn.tar.gz" ]; then tar -xzpf "$dir/etc-openvpn.tar.gz" -C / || return 1; fi
|
|
|
|
log " helper, service definitions, nginx site, doas rules"
|
|
if [ -s "$dir/system.tar.gz" ]; then tar -xzpf "$dir/system.tar.gz" -C / || return 1; fi
|
|
if [ -s "$dir/doas-block.txt" ]; then restore_doas_block "$dir/doas-block.txt" || return 1; fi
|
|
|
|
if [ -s "$dir/ui.tar.gz" ]; then
|
|
log " UI files"
|
|
tmp="$(dirname "$WEB_ROOT")/.ovpmon-ui-restore.$$"
|
|
rm -rf "$tmp"; mkdir -p "$tmp"
|
|
tar -xzpf "$dir/ui.tar.gz" -C "$tmp" || return 1
|
|
if [ -d "$tmp/$(basename "$WEB_ROOT")" ]; then
|
|
[ ! -e "$WEB_ROOT" ] || mv "$WEB_ROOT" "$WEB_ROOT.rollback-$ts"
|
|
mv "$tmp/$(basename "$WEB_ROOT")" "$WEB_ROOT"
|
|
fi
|
|
rm -rf "$tmp"
|
|
fi
|
|
|
|
ensure_runtime_files "$prefix"
|
|
svc_defs_reload || true
|
|
if command -v nginx >/dev/null 2>&1 && [ -f "$NGINX_SITE" ]; then nginx_apply >/dev/null 2>&1 || warn "nginx did not accept the restored configuration"; fi
|
|
|
|
# replaced (new or failed) code is not needed any more; data-holding dirs are never removed
|
|
for c in APP_CORE APP_PROFILER; do
|
|
safe_remove_old "$prefix/$c.rollback-$ts"
|
|
safe_remove_old "$prefix/$c.old-$ts"
|
|
done
|
|
return 0
|
|
}
|
|
|
|
# restore_doas_block FILE: replace our block in /etc/doas.conf with the saved one
|
|
restore_doas_block() {
|
|
local tmp
|
|
doas_target
|
|
[ "$DOAS_MODE" = block ] || return 0
|
|
tmp="$DOAS_FILE.new.$$"
|
|
{
|
|
[ -f "$DOAS_FILE" ] && awk -v b="$DOAS_BEGIN" -v e="$DOAS_END" '$0 == b { skip = 1; next } $0 == e { skip = 0; next } !skip { print }' "$DOAS_FILE"
|
|
cat "$1"
|
|
} > "$tmp"
|
|
if [ -f "$DOAS_FILE" ]; then chmod --reference="$DOAS_FILE" "$tmp" 2>/dev/null || chmod 600 "$tmp"; else chmod 600 "$tmp"; fi
|
|
chown root:root "$tmp"
|
|
mv "$tmp" "$DOAS_FILE"
|
|
}
|