#!/usr/bin/env bash # Install or update the Hermeven bridge application tree and the static phone page. # # Run as root from a checkout (or extracted archive) of the release you want to deploy: # # sudo deploy/install.sh --bridge-url https://bridge.example.com # # What it does, in order: # 1. Creates the `hermeven` system user, /opt/hermeven, /etc/hermeven and the 0700 state # directory /var/lib/hermeven, if they do not exist. # 2. Copies this checkout to /opt/hermeven/app.new and gives ONLY that directory to `hermeven`. # 3. Runs `npm ci` there as `hermeven`, so no package install script ever runs as root. # 4. Builds the phone page with the bridge address preset (also as `hermeven`). # 5. Gives the finished tree back to root and removes group/other write, so the running # service cannot modify its own code. # 6. Installs the built page into /opt/hermeven/www (root-owned, world-readable) for Caddy. # 7. Swaps the new tree into /opt/hermeven/app (the previous one is kept as app.old) and # installs the systemd unit. It does not start or restart the service. # # What it never touches: /etc/hermeven/* (secrets), /var/lib/hermeven/* (state) and # /opt/hermeven/models (the verified speech model). No recursive ownership change reaches them. set -euo pipefail NODE_BIN=${NODE_BIN:-/opt/node/bin} PREFIX=/opt/hermeven BRIDGE_URL="" SOURCE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" while [ $# -gt 0 ]; do case "$1" in --bridge-url) BRIDGE_URL="${2:-}"; shift 2 ;; --source) SOURCE="$(cd "${2:?}" && pwd)"; shift 2 ;; -h|--help) sed -n '2,24p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit 0 ;; *) echo "Unknown option: $1" >&2; exit 2 ;; esac done fail() { echo "install.sh: $*" >&2; exit 1; } [ "$(id -u)" = 0 ] || fail "must be run as root" [ -x "$NODE_BIN/node" ] || fail "Node.js not found at $NODE_BIN/node (see docs/deploy-droplet.md, step 2)" command -v make >/dev/null 2>&1 && command -v g++ >/dev/null 2>&1 || fail "a C/C++ toolchain is required (apt-get install -y build-essential python3)" [ -f "$SOURCE/package-lock.json" ] && [ -d "$SOURCE/services/bridge" ] || fail "$SOURCE is not a Hermeven checkout" case "$BRIDGE_URL" in https://*) ;; "") fail "--bridge-url https:// is required (it is built into the phone page)" ;; *) fail "--bridge-url must start with https://" ;; esac export PATH="$NODE_BIN:$PATH" echo "==> 1/7 user and directories" id hermeven >/dev/null 2>&1 || useradd --system --home "$PREFIX" --shell /usr/sbin/nologin hermeven mkdir -p "$PREFIX" /etc/hermeven install -d -m 0700 -o hermeven -g hermeven /var/lib/hermeven echo "==> 2/7 copy the release to $PREFIX/app.new" rm -rf "$PREFIX/app.new" mkdir "$PREFIX/app.new" if git -C "$SOURCE" rev-parse --git-dir >/dev/null 2>&1; then # Exactly the committed files, and a record of which commit this is. git -C "$SOURCE" archive --format=tar HEAD | tar -x -C "$PREFIX/app.new" git -C "$SOURCE" rev-parse HEAD > "$PREFIX/app.new/DEPLOYED_SHA" else tar -C "$SOURCE" --exclude=./node_modules --exclude='./*/*/node_modules' --exclude=./apps/even-client/dist -cf - . | tar -x -C "$PREFIX/app.new" fi # Ownership is handed over for this one directory only. chown -R hermeven:hermeven "$PREFIX/app.new" echo "==> 3/7 npm ci as the unprivileged user" BUILD_HOME="$(mktemp -d)" chown hermeven:hermeven "$BUILD_HOME" as_hermeven() { runuser -u hermeven -- env PATH="$PATH" HOME="$BUILD_HOME" npm_config_cache="$BUILD_HOME/npm-cache" "$@"; } ( cd "$PREFIX/app.new" && as_hermeven npm ci --no-audit --no-fund ) echo "==> 4/7 build the phone page for $BRIDGE_URL" ( cd "$PREFIX/app.new" && as_hermeven env VITE_HERMEVEN_BRIDGE_URL="$BRIDGE_URL" npm run --silent client:build ) [ -f "$PREFIX/app.new/apps/even-client/dist/index.html" ] || fail "the client build produced no index.html" rm -rf "$BUILD_HOME" echo "==> 5/7 hand the tree back to root" chown -R root:root "$PREFIX/app.new" chmod -R go-w "$PREFIX/app.new" echo "==> 6/7 install the phone page into $PREFIX/www" rm -rf "$PREFIX/www.new" mkdir "$PREFIX/www.new" cp -R "$PREFIX/app.new/apps/even-client/dist/." "$PREFIX/www.new/" chown -R root:root "$PREFIX/www.new" find "$PREFIX/www.new" -type d -exec chmod 0755 {} + find "$PREFIX/www.new" -type f -exec chmod 0644 {} + rm -rf "$PREFIX/www.old" [ -d "$PREFIX/www" ] && mv "$PREFIX/www" "$PREFIX/www.old" mv "$PREFIX/www.new" "$PREFIX/www" echo "==> 7/7 activate the new tree and install the unit" rm -rf "$PREFIX/app.old" [ -d "$PREFIX/app" ] && mv "$PREFIX/app" "$PREFIX/app.old" mv "$PREFIX/app.new" "$PREFIX/app" install -m 0644 -o root -g root "$PREFIX/app/deploy/hermeven-bridge.service" /etc/systemd/system/hermeven-bridge.service command -v systemctl >/dev/null 2>&1 && systemctl daemon-reload || true echo echo "Installed $(cat "$PREFIX/app/DEPLOYED_SHA" 2>/dev/null || echo '(no commit recorded)') to $PREFIX/app" echo "Phone page installed to $PREFIX/www" echo "Not started. Next: configuration, speech model, then 'systemctl enable --now hermeven-bridge'" echo "(or 'systemctl restart hermeven-bridge' for an update). See docs/deploy-droplet.md."