#!/bin/sh # Permanent deploy on the ces-control host: /opt/llm-survey/releases/, `current` symlink, # systemd unit `llm-survey`. Port is never 3000 or 18131 (taken). set -eu SHA="${1:?usage: deploy.sh }" APP=llm-survey ROOT=/opt/$APP PORT="${APP_PORT:-18231}" case "$PORT" in 3000|18131) echo "refusing reserved port $PORT" >&2; exit 1;; esac command -v node >/dev/null || { echo "node is not installed on this host" >&2; exit 1; } node -e 'process.exit(+process.versions.node.split(".")[0] >= 18 ? 0 : 1)' || { echo "need node >= 18" >&2; exit 1; } # port must be free, or held by our own service if ss -ltnH "sport = :$PORT" | grep -q . && ! systemctl is-active --quiet "$APP"; then echo "port $PORT is in use by another process" >&2; exit 1 fi REL="$ROOT/releases/$SHA" mkdir -p "$ROOT/releases" rm -rf "$REL"; mkdir -p "$REL" tar --exclude=.git --exclude=node_modules --exclude=.next -cf - . | tar -xf - -C "$REL" cd "$REL" test -s data/papers.db || { echo "data/papers.db missing or empty" >&2; exit 1; } npm ci --no-audit --no-fund npm run build node -e 'const D=require("better-sqlite3");const d=new D("data/papers.db",{readonly:true});console.log("papers:",d.prepare("select count(*) c from papers").get().c)' ln -sfn "$REL" "$ROOT/current.new" && mv -T "$ROOT/current.new" "$ROOT/current" cat > /etc/systemd/system/$APP.service </dev/null 2>&1 systemctl restart "$APP" for i in 1 2 3 4 5 6 7 8 9 10; do curl -fsS -o /dev/null "http://127.0.0.1:$PORT/" && { echo "healthy on :$PORT ($SHA)"; ok=1; break; }; sleep 2 done [ "${ok:-0}" = 1 ] || { journalctl -u "$APP" -n 40 --no-pager; exit 1; } ls -1dt "$ROOT"/releases/* | tail -n +4 | xargs -r rm -rf