70 lines
3.4 KiB
Bash
70 lines
3.4 KiB
Bash
#!/bin/sh
|
|||
|
|
# Build the web UI (APP_UI, Vue/Vite) in a Node 20 container into a tarball for install.sh / update.sh.
|
||
|
|
#
|
||
|
|
# deploy/native/build-ui.sh [-o FILE] [--image IMAGE]
|
||
|
|
#
|
||
|
|
# -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)
|
||
|
|
#
|
||
|
|
# 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:
|
||
|
|
# 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)
|
||
|
|
|
||
|
|
log() { printf '%s %s\n' "$(date -u '+%Y-%m-%d %H:%M:%S')" "$*"; }
|
||
|
|
die() { log "ERROR: $*" >&2; exit 1; }
|
||
|
|
usage() { sed -n '2,14p' "$0" | sed 's/^# \{0,1\}//'; }
|
||
|
|
|
||
|
|
OUT="$SCRIPT_DIR/ui-dist.tar.gz"
|
||
|
|
IMAGE="${OVPMON_NODE_IMAGE:-node:20-alpine}"
|
||
|
|
|
||
|
|
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" ;;
|
||
|
|
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"
|
||
|
|
|
||
|
|
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
|
||
|
|
|
||
|
|
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)"
|
||
|
|
|
||
|
|
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"
|