Files

306 lines
15 KiB
Bash
Raw Permalink Normal View History

#!/bin/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 [--docker | --local [--node-download]] [-o FILE] [--publish]
# [--image IMAGE] [--repo DIR] [--check]
#
# --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
#
# 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/System_Services/README.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && 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,/^set -eu$/{/^set -eu$/!p}' "$0" | sed 's/^# \{0,1\}//'; }
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 ;;
--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
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"
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
# ---------------------------------------------------------------------------------------------
# Builder selection and prerequisites
# ---------------------------------------------------------------------------------------------
docker_ready() { command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; }
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