Native install: mandatory install directory, UI delivered from the repository
- 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>
This commit is contained in:
1 parent
4abd2044f0
commit
8371d889a4
31 files changed
+920
-114
No files matched your search
+282
-46
@@ -1,70 +1,306 @@
|
||||
#!/bin/sh
|
||||
# Build the web UI (APP_UI, Vue/Vite) in a Node 20 container into a tarball for install.sh / update.sh.
|
||||
# Build the web UI (APP_UI, Vue/Vite) into a tarball and/or into the repository (deploy/ui-dist).
|
||||
#
|
||||
# deploy/native/build-ui.sh [-o FILE] [--image IMAGE]
|
||||
# deploy/native/build-ui.sh [--docker | --local [--node-download]] [-o FILE] [--publish]
|
||||
# [--image IMAGE] [--repo DIR] [--check]
|
||||
#
|
||||
# -o FILE output tarball (default: deploy/native/ui-dist.tar.gz); its top level is the content of dist/
|
||||
# --image IMAGE Node image (default node:20-alpine, or OVPMON_NODE_IMAGE)
|
||||
# --docker build in a Node 20 container (needs Docker on the build machine)
|
||||
# --local build with the Node of this machine (>= 20.19 and npm), no container
|
||||
# --node-download with --local: download the official Node 20 (glibc, x86_64/aarch64) into a temporary
|
||||
# directory, verify its SHA-256, build with it and delete it (nothing is installed;
|
||||
# needs curl, tar and xz; not for Alpine/musl: there use apk add nodejs npm)
|
||||
# --publish refresh deploy/ui-dist/ and its SOURCE_HASH in the repository (maintainer step before
|
||||
# committing UI changes); without -o no tarball is written then
|
||||
# -o FILE output tarball (default without --publish: deploy/native/ui-dist.tar.gz); its top level is
|
||||
# the content of dist/ plus SOURCE_HASH
|
||||
# --image IMAGE Node image for --docker (default node:20-alpine, or OVPMON_NODE_IMAGE)
|
||||
# --repo DIR build another checkout of the repository (default: the one this script is in)
|
||||
# --check only check that the chosen builder is usable, build nothing
|
||||
#
|
||||
# Needs Docker on the machine that builds (not on the target host, which needs no Node at all).
|
||||
# Nothing is written into the source tree: APP_UI is mounted read-only and copied inside the
|
||||
# container; only the output file is created. Then, on the target host:
|
||||
# Without --docker/--local: Docker when the daemon works, otherwise the local Node.
|
||||
# Nothing is written into the source tree: the build runs on a copy of APP_UI (a container sees it read-only;
|
||||
# --local copies it into a temporary directory and runs npm ci there), only the outputs are created.
|
||||
# SOURCE_HASH is the hash of the UI sources (lib.sh ui_source_hash); install.sh and update.sh compare it with
|
||||
# the current sources. On the target host, use the build directly or pass the tarball:
|
||||
# deploy/native/install.sh --ui-dist deploy/native/ui-dist.tar.gz
|
||||
# deploy/native/update.sh --ui-dist deploy/native/ui-dist.tar.gz
|
||||
# See DOCS/Deployment/Scenario2_System_Services.md
|
||||
set -eu
|
||||
|
||||
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
|
||||
ROOT_DIR=$(cd "$SCRIPT_DIR/../.." && pwd)
|
||||
. "$SCRIPT_DIR/lib.sh"
|
||||
DEPLOY_LOG="" # a build tool is not a deployment: no entries in the deploy log
|
||||
|
||||
log() { printf '%s %s\n' "$(date -u '+%Y-%m-%d %H:%M:%S')" "$*"; }
|
||||
warn() { log "WARNING: $*" >&2; }
|
||||
die() { log "ERROR: $*" >&2; exit 1; }
|
||||
usage() { sed -n '2,14p' "$0" | sed 's/^# \{0,1\}//'; }
|
||||
usage() { sed -n '2,/^set -eu$/{/^set -eu$/!p}' "$0" | sed 's/^# \{0,1\}//'; }
|
||||
|
||||
OUT="$SCRIPT_DIR/ui-dist.tar.gz"
|
||||
OUT=""
|
||||
IMAGE="${OVPMON_NODE_IMAGE:-node:20-alpine}"
|
||||
MODE=""
|
||||
NODE_DL=0
|
||||
PUBLISH=0
|
||||
CHECK_ONLY=0
|
||||
REPO="$ROOT_DIR"
|
||||
NODE_MAJOR="${OVPMON_NODE_MAJOR:-20}"
|
||||
WORK=""
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
-o) [ $# -ge 2 ] || die "-o needs a value"; OUT="$2"; shift 2 ;;
|
||||
--image) [ $# -ge 2 ] || die "--image needs a value"; IMAGE="$2"; shift 2 ;;
|
||||
-h|--help) usage; exit 0 ;;
|
||||
*) usage >&2; die "unknown option: $1" ;;
|
||||
-o) [ $# -ge 2 ] || die "-o needs a value"; OUT="$2"; shift 2 ;;
|
||||
--image) [ $# -ge 2 ] || die "--image needs a value"; IMAGE="$2"; shift 2 ;;
|
||||
--repo) [ $# -ge 2 ] || die "--repo needs a value"; REPO="$2"; shift 2 ;;
|
||||
--docker) [ "$MODE" != local ] || die "--docker and --local exclude each other"; MODE=docker; shift ;;
|
||||
--local) [ "$MODE" != docker ] || die "--docker and --local exclude each other"; MODE=local; shift ;;
|
||||
--node-download) NODE_DL=1; shift ;;
|
||||
--publish) PUBLISH=1; shift ;;
|
||||
--check) CHECK_ONLY=1; shift ;;
|
||||
-h|--help) usage; exit 0 ;;
|
||||
*) usage >&2; die "unknown option: $1" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
command -v docker >/dev/null 2>&1 || die "docker not found in PATH. This script builds the UI in a Node 20 container; install Docker on the build machine (the target host does not need it), or build APP_UI yourself (npm install && npm run build) and pack the content of dist/ as a .tar.gz with index.html at its top level"
|
||||
docker info >/dev/null 2>&1 || die "the Docker daemon is not running or not accessible for this user (start it, use sudo or join the docker group)"
|
||||
[ -f "$ROOT_DIR/APP_UI/package.json" ] || die "APP_UI/package.json not found in $ROOT_DIR"
|
||||
REPO=$(cd "$REPO" 2>/dev/null && pwd) || die "--repo: directory not found"
|
||||
ROOT_DIR="$REPO"
|
||||
APP_UI="$REPO/APP_UI"
|
||||
[ -f "$APP_UI/package.json" ] || die "APP_UI/package.json not found in $REPO"
|
||||
|
||||
case "$OUT" in /*) ;; *) OUT="$PWD/$OUT" ;; esac
|
||||
OUT_DIR=$(dirname "$OUT")
|
||||
OUT_NAME=$(basename "$OUT")
|
||||
mkdir -p "$OUT_DIR"
|
||||
OUT_DIR=$(cd "$OUT_DIR" && pwd)
|
||||
TMP_NAME=".$OUT_NAME.tmp.$$"
|
||||
trap 'rm -f "$OUT_DIR/$TMP_NAME"' EXIT
|
||||
cleanup() {
|
||||
_rc=$?
|
||||
set +e
|
||||
[ -z "$WORK" ] || rm -rf "$WORK"
|
||||
[ -z "${OUT_TMP:-}" ] || rm -f "$OUT_TMP"
|
||||
exit "$_rc"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
trap 'exit 130' INT
|
||||
trap 'exit 143' TERM
|
||||
|
||||
log "building the UI with $IMAGE (APP_UI is mounted read-only, nothing is written into the source tree)"
|
||||
# npm ci when a lock file exists (reproducible), npm install otherwise (as the all-in-one Dockerfile)
|
||||
docker run --rm \
|
||||
-v "$ROOT_DIR/APP_UI":/src:ro \
|
||||
-v "$OUT_DIR":/out \
|
||||
-e TMP_NAME="$TMP_NAME" -e HOST_UID="$(id -u)" -e HOST_GID="$(id -g)" \
|
||||
"$IMAGE" sh -c '
|
||||
set -eu
|
||||
mkdir /build
|
||||
tar -C /src --exclude=node_modules --exclude=dist -cf - . | tar -C /build -xf -
|
||||
cd /build
|
||||
if [ -f package-lock.json ]; then npm ci --no-audit --no-fund; else npm install --no-audit --no-fund; fi
|
||||
npm run build
|
||||
[ -f dist/index.html ] || { echo "build produced no dist/index.html" >&2; exit 1; }
|
||||
tar -czf "/out/$TMP_NAME" -C dist .
|
||||
chown "$HOST_UID:$HOST_GID" "/out/$TMP_NAME"
|
||||
' || die "the UI build failed (see the output above)"
|
||||
# ---------------------------------------------------------------------------------------------
|
||||
# Builder selection and prerequisites
|
||||
# ---------------------------------------------------------------------------------------------
|
||||
docker_ready() { command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; }
|
||||
|
||||
tar -tzf "$OUT_DIR/$TMP_NAME" | grep -q -E '^(\./)?index\.html$' || die "the archive has no index.html"
|
||||
mv "$OUT_DIR/$TMP_NAME" "$OUT"
|
||||
trap - EXIT
|
||||
log "UI archive written: $OUT ($(du -h "$OUT" | awk '{print $1}'))"
|
||||
log "use it with: deploy/native/install.sh --ui-dist $OUT or deploy/native/update.sh --ui-dist $OUT"
|
||||
if [ "$NODE_DL" = 1 ]; then
|
||||
[ "$MODE" != docker ] || die "--node-download is for --local builds, not for --docker"
|
||||
MODE=local
|
||||
fi
|
||||
if [ -z "$MODE" ]; then
|
||||
if docker_ready; then MODE=docker; else MODE=local; fi
|
||||
fi
|
||||
|
||||
# node_ok NODE_BINARY: runs and is Node 20.19 or newer (the minimum of the Vite version of APP_UI)
|
||||
node_ok() {
|
||||
local v maj rest min
|
||||
v=$("$1" --version 2>/dev/null) || return 1
|
||||
v=${v#v}; maj=${v%%.*}; rest=${v#*.}; min=${rest%%.*}
|
||||
case "$maj$min" in ""|*[!0-9]*) return 1 ;; esac
|
||||
[ "$maj" -gt 20 ] || { [ "$maj" -eq 20 ] && [ "$min" -ge 19 ]; }
|
||||
}
|
||||
|
||||
is_musl() {
|
||||
[ -e /etc/alpine-release ] && return 0
|
||||
ls /lib/ld-musl-* >/dev/null 2>&1
|
||||
}
|
||||
|
||||
# node_help: how to get a usable Node on this OS (printed before the script stops)
|
||||
node_help() {
|
||||
local id like
|
||||
id=""; like=""
|
||||
if [ -r /etc/os-release ]; then
|
||||
id=$( . /etc/os-release; printf '%s' "${ID:-}" )
|
||||
like=$( . /etc/os-release; printf '%s' "${ID_LIKE:-}" )
|
||||
fi
|
||||
printf '%s\n' "How to get Node.js >= 20.19 with npm on this machine:" >&2
|
||||
case "$id $like" in
|
||||
*alpine*)
|
||||
printf '%s\n' " Alpine: apk add nodejs npm (then check: node --version, it must be 20.19 or newer)" >&2
|
||||
printf '%s\n' " --node-download does not work on Alpine (the official binaries are glibc, Alpine is musl)" >&2 ;;
|
||||
*debian*|*ubuntu*)
|
||||
printf '%s\n' " Debian/Ubuntu: the distribution's Node is usually too old (Debian 12 and Ubuntu 24.04: Node 18)." >&2
|
||||
printf '%s\n' " Either add --node-download (needs: apt-get install -y curl xz-utils; nothing is installed)," >&2
|
||||
printf '%s\n' " or install Node 20+ (with npm) from https://nodejs.org/ or the NodeSource repository." >&2 ;;
|
||||
*)
|
||||
printf '%s\n' " Install Node.js 20.19 or newer with npm (https://nodejs.org/), or use --docker on a machine with Docker." >&2 ;;
|
||||
esac
|
||||
printf '%s\n' " Without any of this: build on another machine with Docker and copy the tarball (-o FILE) here." >&2
|
||||
}
|
||||
|
||||
check_local() {
|
||||
if [ "$NODE_DL" = 1 ]; then
|
||||
[ "$(uname -s)" = Linux ] || die "--node-download supports Linux only"
|
||||
if is_musl; then
|
||||
node_help
|
||||
die "--node-download is not possible on Alpine/musl: the official Node binaries need glibc (apk add nodejs npm)"
|
||||
fi
|
||||
case "$(uname -m)" in
|
||||
x86_64|amd64) NODE_ARCH=x64 ;;
|
||||
aarch64|arm64) NODE_ARCH=arm64 ;;
|
||||
*) die "--node-download: unsupported CPU architecture $(uname -m) (x86_64 and aarch64 only)" ;;
|
||||
esac
|
||||
for _c in curl tar xz; do
|
||||
command -v "$_c" >/dev/null 2>&1 || die "--node-download needs $_c (Debian/Ubuntu: apt-get install -y curl xz-utils)"
|
||||
done
|
||||
return 0
|
||||
fi
|
||||
if ! command -v node >/dev/null 2>&1 || ! command -v npm >/dev/null 2>&1; then
|
||||
node_help
|
||||
die "node and npm are not installed (needed for --local)"
|
||||
fi
|
||||
if ! node_ok node; then
|
||||
node_help
|
||||
die "the installed Node is too old: $(node --version 2>&1) (needed: 20.19 or newer)"
|
||||
fi
|
||||
}
|
||||
|
||||
check_docker() {
|
||||
command -v docker >/dev/null 2>&1 || die "docker not found in PATH. Install Docker on the build machine (the target host does not need it), or use --local (Node 20.19+, or --node-download)"
|
||||
docker info >/dev/null 2>&1 || die "the Docker daemon is not running or not accessible for this user (start it, use sudo or join the docker group), or use --local"
|
||||
}
|
||||
|
||||
if [ "$MODE" = docker ]; then check_docker; else check_local; fi
|
||||
if [ "$CHECK_ONLY" = 1 ]; then
|
||||
if [ "$MODE" = docker ]; then log "builder ready: Docker ($IMAGE)"
|
||||
elif [ "$NODE_DL" = 1 ]; then log "builder ready: official Node $NODE_MAJOR will be downloaded into a temporary directory (linux-$NODE_ARCH)"
|
||||
else log "builder ready: local Node $(node --version) with npm $(npm --version)"
|
||||
fi
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ "$PUBLISH" = 1 ]; then
|
||||
[ -d "$REPO/deploy" ] || die "--publish needs the repository checkout ($REPO/deploy not found)"
|
||||
elif [ -z "$OUT" ]; then
|
||||
OUT="$SCRIPT_DIR/ui-dist.tar.gz"
|
||||
fi
|
||||
|
||||
# ---------------------------------------------------------------------------------------------
|
||||
# Build: both modes leave $WORK/ui.tar.gz (content of dist/ plus SOURCE_HASH)
|
||||
# ---------------------------------------------------------------------------------------------
|
||||
WORK=$(mktemp -d "${TMPDIR:-/tmp}/ovpmon-ui-build.XXXXXX") || die "cannot create a temporary directory"
|
||||
chmod 755 "$WORK"
|
||||
TAR="$WORK/ui.tar.gz"
|
||||
HASH=$(ui_source_hash "$APP_UI") || die "cannot compute the hash of the UI sources ($APP_UI/src missing, or no sha256sum/shasum)"
|
||||
log "UI sources: $APP_UI (hash $HASH)"
|
||||
|
||||
# fetch_node: official Node 20 (latest-v20.x) into $WORK/node; the archive is verified against SHASUMS256.txt
|
||||
fetch_node() {
|
||||
local base sums line want file got
|
||||
base="https://nodejs.org/dist/latest-v${NODE_MAJOR}.x"
|
||||
sums="$WORK/SHASUMS256.txt"
|
||||
log "downloading $base/SHASUMS256.txt"
|
||||
curl -fsSL --retry 3 --connect-timeout 20 -o "$sums" "$base/SHASUMS256.txt" \
|
||||
|| die "cannot download $base/SHASUMS256.txt (no network access to nodejs.org?)"
|
||||
line=$(grep -E "^[0-9a-f]{64} node-v${NODE_MAJOR}\.[0-9]+\.[0-9]+-linux-${NODE_ARCH}\.tar\.xz\$" "$sums" | head -n 1 || true)
|
||||
[ -n "$line" ] || die "no node-v${NODE_MAJOR}.*-linux-${NODE_ARCH}.tar.xz entry in $base/SHASUMS256.txt"
|
||||
want=$(printf '%s' "$line" | cut -c1-64)
|
||||
file=$(printf '%s' "$line" | cut -c67-)
|
||||
log "downloading $base/$file"
|
||||
curl -fsSL --retry 3 --connect-timeout 20 -o "$WORK/$file" "$base/$file" || die "cannot download $base/$file"
|
||||
got=$(sha256_run "$WORK/$file" | awk '{ print $1 }')
|
||||
[ "$got" = "$want" ] || die "SHA-256 of $file does not match SHASUMS256.txt (expected $want, got $got): the download is not used"
|
||||
log "SHA-256 verified: $file"
|
||||
mkdir -p "$WORK/node"
|
||||
xz -dc "$WORK/$file" | tar -xf - -C "$WORK/node" || die "cannot unpack $file"
|
||||
rm -f "$WORK/$file"
|
||||
NODE_BIN=$(ls -d "$WORK"/node/node-v*/bin 2>/dev/null | head -n 1 || true)
|
||||
[ -n "$NODE_BIN" ] && [ -x "$NODE_BIN/node" ] || die "the unpacked Node has no bin/node"
|
||||
node_ok "$NODE_BIN/node" || die "the downloaded Node is not usable here or older than 20.19: $("$NODE_BIN/node" --version 2>&1 || true)"
|
||||
log "using the downloaded Node $("$NODE_BIN/node" --version) (temporary, deleted after the build)"
|
||||
}
|
||||
|
||||
build_local() {
|
||||
local e name
|
||||
mkdir -p "$WORK/src"
|
||||
# copy of APP_UI without node_modules and dist: the source tree stays untouched
|
||||
for e in "$APP_UI"/* "$APP_UI"/.[!.]*; do
|
||||
[ -e "$e" ] || [ -L "$e" ] || continue
|
||||
name=$(basename "$e")
|
||||
case "$name" in node_modules|dist) continue ;; esac
|
||||
cp -R "$e" "$WORK/src/"
|
||||
done
|
||||
if [ "$NODE_DL" = 1 ]; then
|
||||
fetch_node
|
||||
PATH="$NODE_BIN:$PATH"
|
||||
export PATH
|
||||
export npm_config_cache="$WORK/npm-cache" # nothing is left on the host
|
||||
fi
|
||||
cd "$WORK/src"
|
||||
log "building the UI with the local Node $(node --version), npm $(npm --version)"
|
||||
if [ -f package-lock.json ]; then
|
||||
npm ci --no-audit --no-fund || die "npm ci failed (see the output above)"
|
||||
else
|
||||
npm install --no-audit --no-fund || die "npm install failed (see the output above)"
|
||||
fi
|
||||
npm run build || die "npm run build failed (see the output above)"
|
||||
[ -f dist/index.html ] || die "the build produced no dist/index.html"
|
||||
printf '%s\n' "$HASH" > dist/SOURCE_HASH
|
||||
tar -czf "$TAR" -C dist .
|
||||
cd "$WORK"
|
||||
}
|
||||
|
||||
build_docker() {
|
||||
log "building the UI with $IMAGE (APP_UI is mounted read-only, nothing is written into the source tree)"
|
||||
# npm ci when a lock file exists (reproducible), npm install otherwise (as the all-in-one Dockerfile)
|
||||
docker run --rm \
|
||||
-v "$APP_UI":/src:ro \
|
||||
-v "$WORK":/out \
|
||||
-e SOURCE_HASH="$HASH" -e HOST_UID="$(id -u)" -e HOST_GID="$(id -g)" \
|
||||
"$IMAGE" sh -c '
|
||||
set -eu
|
||||
mkdir /build
|
||||
tar -C /src --exclude=node_modules --exclude=dist -cf - . | tar -C /build -xf -
|
||||
cd /build
|
||||
if [ -f package-lock.json ]; then npm ci --no-audit --no-fund; else npm install --no-audit --no-fund; fi
|
||||
npm run build
|
||||
[ -f dist/index.html ] || { echo "build produced no dist/index.html" >&2; exit 1; }
|
||||
printf "%s\n" "$SOURCE_HASH" > dist/SOURCE_HASH
|
||||
tar -czf /out/ui.tar.gz -C dist .
|
||||
chown "$HOST_UID:$HOST_GID" /out/ui.tar.gz
|
||||
' || die "the UI build failed (see the output above)"
|
||||
}
|
||||
|
||||
if [ "$MODE" = docker ]; then build_docker; else build_local; fi
|
||||
|
||||
tar -tzf "$TAR" | grep -q -E '^(\./)?index\.html$' || die "the archive has no index.html"
|
||||
[ "$(ui_hash_of "$TAR")" = "$HASH" ] || die "the archive has no valid SOURCE_HASH"
|
||||
|
||||
# ---------------------------------------------------------------------------------------------
|
||||
# Outputs
|
||||
# ---------------------------------------------------------------------------------------------
|
||||
if [ -n "$OUT" ]; then
|
||||
case "$OUT" in /*) ;; *) OUT="$PWD/$OUT" ;; esac
|
||||
mkdir -p "$(dirname "$OUT")"
|
||||
OUT_TMP="$(dirname "$OUT")/.$(basename "$OUT").tmp.$$"
|
||||
cp "$TAR" "$OUT_TMP" && mv "$OUT_TMP" "$OUT" || die "cannot write $OUT"
|
||||
log "UI archive written: $OUT ($(du -h "$OUT" | awk '{print $1}'))"
|
||||
log "use it with: deploy/native/install.sh --ui-dist $OUT or deploy/native/update.sh --ui-dist $OUT"
|
||||
fi
|
||||
|
||||
if [ "$PUBLISH" = 1 ]; then
|
||||
DEST="$REPO/deploy/ui-dist"
|
||||
NEW="$REPO/deploy/.ui-dist.new.$$"
|
||||
OLD="$REPO/deploy/.ui-dist.old.$$"
|
||||
rm -rf "$NEW"
|
||||
mkdir -p "$NEW"
|
||||
tar -xzf "$TAR" -C "$NEW" --no-same-owner || { rm -rf "$NEW"; die "cannot unpack the build for publishing"; }
|
||||
chmod -R go-w,a+rX "$NEW"
|
||||
# the old directory is moved aside first, so a failure never leaves a half-written deploy/ui-dist
|
||||
if [ -e "$DEST" ]; then mv "$DEST" "$OLD" || { rm -rf "$NEW"; die "cannot replace $DEST"; }; fi
|
||||
if ! mv "$NEW" "$DEST"; then
|
||||
[ ! -e "$OLD" ] || mv "$OLD" "$DEST"
|
||||
rm -rf "$NEW"
|
||||
die "cannot move the new build to $DEST (the previous one was restored)"
|
||||
fi
|
||||
rm -rf "$OLD"
|
||||
ui_repo_state "$REPO" || true
|
||||
if [ "$UI_STATE" = fresh ]; then
|
||||
log "published: $DEST (SOURCE_HASH $HASH); the build matches the APP_UI sources"
|
||||
else
|
||||
warn "published, but the check against the sources says '$UI_STATE': were the sources changed during the build? Run deploy/native/check-ui.sh"
|
||||
fi
|
||||
if command -v git >/dev/null 2>&1 && git -C "$REPO" check-ignore -q deploy/ui-dist/index.html 2>/dev/null; then
|
||||
warn "deploy/ui-dist is ignored by git (.gitignore): it cannot be committed"
|
||||
fi
|
||||
log "now commit it together with the UI change: git add deploy/ui-dist"
|
||||
fi
|
||||
Executable
+39
@@ -0,0 +1,39 @@
|
||||
#!/bin/sh
|
||||
# Check that the committed UI build (deploy/ui-dist) matches the UI sources (APP_UI).
|
||||
#
|
||||
# deploy/native/check-ui.sh [REPO_DIR]
|
||||
#
|
||||
# Prints fresh, stale or missing. Exit code: 0 fresh, 1 stale (the build is older than the sources: run
|
||||
# deploy/native/build-ui.sh --publish and commit deploy/ui-dist), 2 missing or not checkable.
|
||||
# Run it before a commit or a release that touches APP_UI. It changes nothing.
|
||||
# See DOCS/Deployment/Scenario2_System_Services.md
|
||||
set -eu
|
||||
|
||||
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
|
||||
. "$SCRIPT_DIR/lib.sh"
|
||||
DEPLOY_LOG=""
|
||||
|
||||
case "${1:-}" in
|
||||
-h|--help) sed -n '2,/^set -eu$/{/^set -eu$/!p}' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
|
||||
esac
|
||||
REPO=$(cd "${1:-$ROOT_DIR}" 2>/dev/null && pwd) || { echo "error: directory not found: ${1:-}" >&2; exit 2; }
|
||||
|
||||
rc=0
|
||||
ui_repo_state "$REPO" || rc=1
|
||||
if [ "$rc" = 1 ]; then
|
||||
echo "error: cannot compute the hash of $REPO/APP_UI (sources missing, or no sha256sum/shasum)" >&2
|
||||
exit 2
|
||||
fi
|
||||
case "$UI_STATE" in
|
||||
fresh)
|
||||
echo "fresh: deploy/ui-dist matches the APP_UI sources (hash $UI_CUR_HASH)"
|
||||
exit 0 ;;
|
||||
stale)
|
||||
echo "stale: $(ui_stale_lines | head -n 1)"
|
||||
ui_stale_lines | sed -n '2,$p' | sed 's/^/ /'
|
||||
exit 1 ;;
|
||||
*)
|
||||
echo "missing: no committed UI build in $REPO/deploy/ui-dist (index.html)"
|
||||
echo " Maintainers: run deploy/native/build-ui.sh --publish and commit deploy/ui-dist."
|
||||
exit 2 ;;
|
||||
esac
|
||||
+110
-23
@@ -1,12 +1,22 @@
|
||||
#!/bin/sh
|
||||
# Install OpenVPN Monitor & Profiler as system services (no containers): Alpine (OpenRC), Debian, Ubuntu (systemd).
|
||||
#
|
||||
# deploy/native/install.sh [--prefix DIR] [--ui-dist FILE] [--https-port N] [--host-name NAME]
|
||||
# [--admin-user NAME] [--force]
|
||||
# deploy/native/install.sh [--prefix DIR] [--ui-dist FILE|DIR | --build-ui [--node-download] | --no-ui]
|
||||
# [--https-port N] [--host-name NAME] [--admin-user NAME] [--force]
|
||||
#
|
||||
# --prefix install directory (default /opt/OpenVPN-Monitoring-Simple): APP_CORE, APP_PROFILER, venvs
|
||||
# --ui-dist UI tarball made by deploy/native/build-ui.sh (the host needs no Node). Without it a
|
||||
# placeholder page is installed; add the UI later with update.sh --ui-dist
|
||||
# --prefix install directory (APP_CORE, APP_PROFILER, venvs). REQUIRED: without the option the script
|
||||
# asks for it (no default is assumed). It must be a new or empty directory, different from
|
||||
# this repository checkout (the checkout is only the source) and not a system directory
|
||||
# --ui-dist UI tarball made by deploy/native/build-ui.sh, or a directory with index.html (the host
|
||||
# needs no Node). Highest priority
|
||||
# --build-ui build the UI now from APP_UI (Docker when it works, otherwise Node >= 20.19 on this host);
|
||||
# for a host where the committed build is stale or missing
|
||||
# --node-download with --build-ui: fetch a temporary official Node 20 (checksum verified, glibc only,
|
||||
# needs curl and xz) instead of the system Node; nothing is installed
|
||||
# --no-ui install a placeholder page instead of the UI (on purpose)
|
||||
# Without these options the committed build deploy/ui-dist of the repository is installed
|
||||
# automatically (a warning is printed when it is older than APP_UI); when there is no UI
|
||||
# source at all the install stops
|
||||
# --https-port HTTPS port of the web UI (default 8088)
|
||||
# --host-name DNS name or IP of this host: certificate name and CORS origin (default: detected address)
|
||||
# --admin-user login of the initial administrator (default: admin); the password is generated
|
||||
@@ -22,11 +32,14 @@ SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
|
||||
. "$SCRIPT_DIR/lib.sh"
|
||||
|
||||
usage() {
|
||||
sed -n '2,18p' "$0" | sed 's/^# \{0,1\}//'
|
||||
sed -n '2,/^set -eu$/{/^set -eu$/!p}' "$0" | sed 's/^# \{0,1\}//'
|
||||
}
|
||||
|
||||
PREFIX="$DEFAULT_PREFIX"
|
||||
PREFIX=""
|
||||
UI_DIST=""
|
||||
UI_BUILD=0
|
||||
UI_NODE_DL=0
|
||||
NO_UI=0
|
||||
HTTPS_PORT=8088
|
||||
HOST_NAME=""
|
||||
ADMIN_USER="admin"
|
||||
@@ -37,6 +50,9 @@ 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 ;;
|
||||
--no-ui) NO_UI=1; shift ;;
|
||||
--https-port) [ $# -ge 2 ] || die "--https-port needs a value"; HTTPS_PORT="$2"; shift 2 ;;
|
||||
--host-name) [ $# -ge 2 ] || die "--host-name needs a value"; HOST_NAME="$2"; shift 2 ;;
|
||||
--admin-user) [ $# -ge 2 ] || die "--admin-user needs a value"; ADMIN_USER="$2"; shift 2 ;;
|
||||
@@ -46,7 +62,68 @@ while [ $# -gt 0 ]; do
|
||||
esac
|
||||
done
|
||||
|
||||
valid_prefix "$PREFIX" || die "invalid --prefix '$PREFIX' (absolute path of letters, digits, . _ - and /)"
|
||||
SRC="$ROOT_DIR"
|
||||
|
||||
# prefix_problem DIR: prints why DIR cannot be the install directory (nothing = acceptable).
|
||||
# The install directory gets mkdir/chmod 755 and root-owned code: it must be a dedicated place.
|
||||
prefix_problem() {
|
||||
local d n sabs
|
||||
d="$1"
|
||||
valid_prefix "$d" || { echo "not an absolute path of letters, digits, . _ - and /"; return; }
|
||||
# normalise: no trailing slash, no double slash, no . or .. segments
|
||||
case "$d" in *//*|*/./*|*/../*|*/.|*/..|*/) echo "write the path without //, ., .. or a trailing /"; return ;; esac
|
||||
case "$d" in
|
||||
/|/bin|/boot|/dev|/etc|/home|/lib|/lib64|/media|/mnt|/opt|/proc|/root|/run|/sbin|/srv|/sys|/tmp|/usr|/var|/usr/local|/usr/lib|/var/lib|/var/log|/var/www|/etc/openvpn|/etc/ovpmon)
|
||||
echo "a system directory (the installer changes its permissions); choose a dedicated directory, e.g. $DEFAULT_PREFIX"; return ;;
|
||||
esac
|
||||
sabs=$(abs_dir "$SRC")
|
||||
[ "$d" != "$sabs" ] || { echo "this is the repository checkout itself ($sabs): the checkout is only the source of the installation. Choose another directory, or move/clone the repository elsewhere"; return; }
|
||||
case "$d/" in "$sabs"/*) echo "inside the repository checkout ($sabs): choose a directory outside of it"; return ;; esac
|
||||
case "$sabs/" in "$d"/*) echo "the repository checkout ($sabs) is inside it: choose a directory that does not contain the checkout"; return ;; esac
|
||||
if [ -e "$d" ] && [ ! -d "$d" ]; then echo "exists and is not a directory"; return; fi
|
||||
if [ -d "$d" ] && [ ! -f "$INCOMPLETE_MARK" ]; then
|
||||
n=$(ls -A "$d" 2>/dev/null | wc -l)
|
||||
[ "$n" -eq 0 ] || { echo "already exists and is not empty (an installation, or other data). To upgrade an installation use update.sh; otherwise choose a new directory"; return; }
|
||||
fi
|
||||
}
|
||||
|
||||
# The install directory is always asked for: it decides where the code, the PKI and the client profiles
|
||||
# live, and it is not something to guess. Without a terminal it must be given with --prefix.
|
||||
ask_prefix() {
|
||||
local ans tries p
|
||||
[ -t 0 ] || die "--prefix DIR is required (no terminal to ask on). Example: --prefix $DEFAULT_PREFIX"
|
||||
echo "Install directory: where APP_CORE, APP_PROFILER and their virtualenvs are installed."
|
||||
echo "The PKI and the client profiles live below it. It must be a new or empty directory outside"
|
||||
echo "this repository checkout ($ROOT_DIR). Suggestion: $DEFAULT_PREFIX"
|
||||
tries=0
|
||||
while [ "$tries" -lt 3 ]; do
|
||||
printf 'Install directory (absolute path): '
|
||||
read -r ans || die "no input: --prefix DIR is required"
|
||||
if [ -z "$ans" ]; then
|
||||
echo " an install directory is required (type the path; there is no default)"
|
||||
else
|
||||
p=$(prefix_problem "$ans")
|
||||
if [ -z "$p" ]; then
|
||||
PREFIX="$ans"
|
||||
printf 'Install into %s ? [y/N] ' "$PREFIX"
|
||||
read -r ans || die "no input"
|
||||
case "$ans" in y|Y|yes|YES) return 0 ;; esac
|
||||
echo " not confirmed"
|
||||
else
|
||||
echo " not accepted: $p"
|
||||
fi
|
||||
fi
|
||||
tries=$((tries + 1))
|
||||
done
|
||||
die "no valid install directory given"
|
||||
}
|
||||
|
||||
if [ -z "$PREFIX" ]; then
|
||||
ask_prefix
|
||||
else
|
||||
_problem=$(prefix_problem "$PREFIX")
|
||||
[ -z "$_problem" ] || die "invalid --prefix '$PREFIX': $_problem"
|
||||
fi
|
||||
valid_port "$HTTPS_PORT" || die "invalid --https-port '$HTTPS_PORT'"
|
||||
case "$ADMIN_USER" in
|
||||
""|*[!A-Za-z0-9._-]*) die "invalid --admin-user (letters, digits, . _ - only)" ;;
|
||||
@@ -55,14 +132,19 @@ case "$HOST_NAME" in
|
||||
*[!A-Za-z0-9.-]*) die "invalid --host-name (letters, digits, . and - only)" ;;
|
||||
esac
|
||||
[ -z "$UI_DIST" ] || case "$UI_DIST" in /*) ;; *) UI_DIST="$PWD/$UI_DIST" ;; esac
|
||||
SRC="$ROOT_DIR"
|
||||
[ "$UI_NODE_DL" = 0 ] || [ "$UI_BUILD" = 1 ] || die "--node-download only works together with --build-ui"
|
||||
[ "$NO_UI" = 0 ] || { [ -z "$UI_DIST" ] && [ "$UI_BUILD" = 0 ]; } || die "--no-ui excludes --ui-dist and --build-ui"
|
||||
[ -z "$UI_DIST" ] || [ "$UI_BUILD" = 0 ] || die "use either --ui-dist or --build-ui, not both"
|
||||
|
||||
on_exit() {
|
||||
_rc=$?
|
||||
set +e
|
||||
trap '' INT TERM
|
||||
if [ "$_rc" -ne 0 ]; then
|
||||
log "install stopped in phase '$CURRENT_PHASE'. Nothing is lost: fix the cause and run the script again (it resumes; existing secrets, certificates and data are kept)" >&2
|
||||
case "$CURRENT_PHASE" in
|
||||
start|0*) log "install stopped in phase '$CURRENT_PHASE': nothing was changed on this system" >&2 ;;
|
||||
*) log "install stopped in phase '$CURRENT_PHASE'. Nothing is lost: fix the cause and run the script again with the same --prefix (it resumes; existing secrets, certificates and data are kept)" >&2 ;;
|
||||
esac
|
||||
fi
|
||||
[ -z "${TMPD:-}" ] || rm -rf "$TMPD"
|
||||
release_lock
|
||||
@@ -95,15 +177,9 @@ fi
|
||||
|
||||
[ -f "$SRC/APP_CORE/openvpn_api_v3.py" ] && [ -f "$SRC/APP_PROFILER/main.py" ] \
|
||||
|| die "run the script from a complete checkout of the repository (APP_CORE and APP_PROFILER not found in $SRC)"
|
||||
if [ -z "$UI_DIST" ] && [ -f "$SCRIPT_DIR/ui-dist.tar.gz" ]; then
|
||||
UI_DIST="$SCRIPT_DIR/ui-dist.tar.gz"
|
||||
log "using the UI archive $UI_DIST"
|
||||
fi
|
||||
if [ -n "$UI_DIST" ]; then
|
||||
check_ui_archive "$UI_DIST"
|
||||
else
|
||||
warn "no --ui-dist: a placeholder page is installed instead of the web UI (build it with deploy/native/build-ui.sh, then update.sh --ui-dist FILE)"
|
||||
fi
|
||||
# UI source order (ui_resolve, run below after the cheap checks): --ui-dist, --build-ui, the committed
|
||||
# deploy/ui-dist, an archive next to the scripts. Nothing found: stop, unless --no-ui
|
||||
UI_FALLBACK="$SCRIPT_DIR/ui-dist.tar.gz"
|
||||
|
||||
# ports: ours may already be bound when the script is resumed
|
||||
if svc_active ovpmon-api 2>/dev/null; then
|
||||
@@ -119,6 +195,16 @@ if port_in_use 1194 udp; then log "note: 1194/udp is in use; OpenVPN uses the po
|
||||
check_free "$PREFIX" "$MIN_FREE_PREFIX_MB" "the installation (code and virtualenvs)"
|
||||
check_free /var/lib 256 "data"
|
||||
|
||||
if [ "$NO_UI" = 1 ]; then
|
||||
warn "--no-ui: only a placeholder page is installed instead of the web UI (update.sh installs the UI from the repository later)"
|
||||
else
|
||||
ui_resolve "$SRC" "$TMPD"
|
||||
if [ -z "$UI_SRC" ]; then
|
||||
die "no UI source found: there is no --ui-dist, no committed build in $SRC/deploy/ui-dist and no archive $UI_FALLBACK. Choose one: --build-ui (builds from APP_UI with Docker or Node 20.19+, add --node-download for a temporary Node), --ui-dist FILE|DIR (built elsewhere with deploy/native/build-ui.sh), or --no-ui (placeholder page on purpose). A complete clone of the repository contains deploy/ui-dist"
|
||||
fi
|
||||
log "UI: $UI_ORIGIN"
|
||||
fi
|
||||
|
||||
mkdir -p "$ETC_DIR"
|
||||
chmod 755 "$ETC_DIR"
|
||||
: > "$INCOMPLETE_MARK"
|
||||
@@ -300,16 +386,16 @@ log "services enabled at boot: $SERVICES"
|
||||
|
||||
# UI files
|
||||
mkdir -p "$(dirname "$WEB_ROOT")"
|
||||
if [ -n "$UI_DIST" ]; then
|
||||
stage_ui "$UI_DIST" "$WEB_ROOT.new"
|
||||
if [ -n "$UI_SRC" ]; then
|
||||
stage_ui "$UI_SRC" "$WEB_ROOT.new"
|
||||
swap_dir "$WEB_ROOT.new" "$WEB_ROOT" "$WEB_ROOT.old"
|
||||
rm -rf "$WEB_ROOT.old"
|
||||
log "UI files installed to $WEB_ROOT from $UI_DIST"
|
||||
log "UI files installed to $WEB_ROOT: $UI_ORIGIN"
|
||||
elif [ ! -f "$WEB_ROOT/index.html" ]; then
|
||||
mkdir -p "$WEB_ROOT"
|
||||
cat > "$WEB_ROOT/index.html" <<'EOF'
|
||||
<!doctype html><meta charset="utf-8"><title>OpenVPN Monitor</title>
|
||||
<h1>OpenVPN Monitor</h1><p>The web UI is not installed yet. Build it with deploy/native/build-ui.sh and run deploy/native/update.sh --ui-dist FILE.</p>
|
||||
<h1>OpenVPN Monitor</h1><p>The web UI is not installed (install.sh --no-ui). Run deploy/native/update.sh: it installs the UI from the repository; or pass --ui-dist FILE|DIR or --build-ui.</p>
|
||||
EOF
|
||||
chmod -R a+rX "$WEB_ROOT"
|
||||
log "placeholder page written to $WEB_ROOT"
|
||||
@@ -417,6 +503,7 @@ FP=$(openssl x509 -noout -fingerprint -sha256 -in "$TLS_DIR/ovpmon.crt" 2>/dev/n
|
||||
printf '\n'
|
||||
log "=== INSTALLED: $OS_ID $OS_VERSION_ID, $INIT, prefix $PREFIX ==="
|
||||
printf ' Web UI: https://%s:%s/ (self-signed certificate; SHA-256 %s)\n' "${HOST_NAME:-${HOST_IP:-<server-address>}}" "$HTTPS_PORT" "${FP:-n/a}"
|
||||
printf ' UI files: %s (%s)\n' "$WEB_ROOT" "${UI_ORIGIN:-placeholder page (--no-ui)}"
|
||||
printf ' Config: %s (root:root 600), helper: %s, %s\n' "$ENV_FILE" "$HELPER_CONF" "$HELPER_DEST"
|
||||
printf ' Data: %s (databases), %s/APP_PROFILER/easy-rsa (PKI), %s/APP_PROFILER/client-config\n' "$DATA_DIR" "$PREFIX" "$PREFIX"
|
||||
printf ' Logs: %s, %s\n' "$APP_LOG_DIR" "$OVPN_LOG_DIR"
|
||||
|
||||
+155
-8
@@ -714,19 +714,166 @@ ensure_runtime_files() {
|
||||
fi
|
||||
}
|
||||
|
||||
# check_ui_archive FILE: a readable tarball with index.html
|
||||
check_ui_archive() {
|
||||
[ -f "$1" ] || die "UI archive not found: $1 (build it with deploy/native/build-ui.sh)"
|
||||
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"
|
||||
# ---- 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
|
||||
}
|
||||
|
||||
# stage_ui FILE DIR: extract into a fresh DIR (never into the live web root)
|
||||
# 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"
|
||||
tar -xzf "$1" -C "$2" --no-same-owner || die "cannot extract the UI archive"
|
||||
[ -f "$2/index.html" ] || die "index.html missing after extracting the UI archive"
|
||||
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"
|
||||
|
||||
+43
-12
@@ -1,17 +1,23 @@
|
||||
#!/bin/sh
|
||||
# Update OpenVPN Monitor & Profiler (system services) without touching the data.
|
||||
#
|
||||
# deploy/native/update.sh [--prefix DIR] [--ui-dist FILE] [--source DIR] [--no-pull] [--yes] [--rollback]
|
||||
# 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; without it the UI files are left as they are
|
||||
# --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 (<web root>/.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/<UTC time>/), 2 git pull, 3 PREPARE the new
|
||||
# code and virtualenvs next to the install dir (old code untouched), 4 STOP the application services (OpenVPN
|
||||
# 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.
|
||||
@@ -23,11 +29,14 @@ SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
|
||||
. "$SCRIPT_DIR/lib.sh"
|
||||
|
||||
usage() {
|
||||
sed -n '2,19p' "$0" | sed 's/^# \{0,1\}//'
|
||||
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
|
||||
@@ -206,6 +215,8 @@ main() {
|
||||
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 ;;
|
||||
@@ -215,6 +226,9 @@ main() {
|
||||
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
|
||||
@@ -271,10 +285,13 @@ main() {
|
||||
PULL_TOUCHES_LIVE=0
|
||||
fi
|
||||
if [ -n "$UI_DIST" ]; then
|
||||
check_ui_archive "$UI_DIST"
|
||||
log "UI archive: $UI_DIST"
|
||||
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 "no --ui-dist: the UI files stay as they are (if APP_UI changed, build and pass a new archive)"
|
||||
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
|
||||
@@ -340,7 +357,7 @@ main() {
|
||||
|
||||
# ----- Phase 3: prepare ----------------------------------------------------------------
|
||||
CURRENT_PHASE="3 prepare"
|
||||
phase 3 "PREPARE the new code and virtualenvs (the running system is not touched)"
|
||||
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"
|
||||
@@ -356,10 +373,23 @@ main() {
|
||||
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)"
|
||||
if [ -n "$UI_DIST" ]; then
|
||||
stage_ui "$UI_DIST" "$WEB_ROOT.new"
|
||||
UI_STAGED=1
|
||||
log "new UI files are ready in $WEB_ROOT.new"
|
||||
# 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 ----------------------------------------------------------
|
||||
@@ -477,6 +507,7 @@ main() {
|
||||
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'
|
||||
|
||||
Reference in new issue
Block a user