From a85dc0f4b9148d945fba6b8ef751ed2139e887ab Mon Sep 17 00:00:00 2001 From: Tyler Hoang Date: Wed, 10 Jun 2026 00:28:09 -0700 Subject: Simplify deploy script and docs --- scripts/deploy.sh | 105 +++++++++++++++--------------------------------------- 1 file changed, 28 insertions(+), 77 deletions(-) (limited to 'scripts/deploy.sh') diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 5eae840..5a2ac5c 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -1,35 +1,29 @@ #!/usr/bin/env bash # -# Production deploy script for Prism v2. +# Build/setup script for Prism v2. # -# Runs on the production server, AFTER the working tree has been updated -# (typically by a git post-receive hook that does `git checkout -f` into -# /var/www/prism-v2). This script does NOT pull — it only builds + restarts. +# Prepares backend and frontend dependencies and builds the frontend so the +# stack is ready to run. This script does NOT install systemd or nginx, restart +# services, or manage git hooks. # # Usage: -# sudo ./scripts/deploy.sh # build + restart + smoke-check -# sudo ./scripts/deploy.sh --install # also install systemd units + nginx site -# sudo ./scripts/deploy.sh --help +# ./scripts/deploy.sh +# ./scripts/deploy.sh --help set -euo pipefail -APP_DIR="${APP_DIR:-/var/www/prism-v2}" -APP_USER="${APP_USER:-www-data}" -APP_GROUP="${APP_GROUP:-www-data}" -DOMAIN="${DOMAIN:-prism.tylerhoang.xyz}" -BACKEND_SVC="prismv2-backend.service" -FRONTEND_SVC="prismv2-frontend.service" - -DO_INSTALL=0 +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +APP_DIR="${APP_DIR:-$(cd "$SCRIPT_DIR/.." && pwd)}" +PYTHON_BIN="${PYTHON_BIN:-python3}" +BACKEND_VENV="$APP_DIR/backend/.venv" usage() { - sed -n '2,14p' "$0" | sed 's/^# \{0,1\}//' + sed -n '2,12p' "$0" | sed 's/^# \{0,1\}//' exit 0 } for arg in "$@"; do case "$arg" in - --install) DO_INSTALL=1 ;; -h|--help) usage ;; *) echo "Unknown arg: $arg" >&2; exit 2 ;; esac @@ -37,74 +31,31 @@ done log() { printf '\n=== %s ===\n' "$*"; } -if [[ $EUID -ne 0 ]]; then - echo "deploy.sh must be run as root (use sudo)" >&2 - exit 1 -fi - if [[ ! -d "$APP_DIR/backend" || ! -d "$APP_DIR/frontend" ]]; then - echo "Expected working tree at $APP_DIR (backend/ + frontend/ not found)" >&2 + echo "Expected repo root at $APP_DIR (backend/ + frontend/ not found)" >&2 exit 1 fi -cd "$APP_DIR" - -log "Ensuring ownership of $APP_DIR" -chown -R "$APP_USER:$APP_GROUP" "$APP_DIR" -install -d -o "$APP_USER" -g "$APP_GROUP" "$APP_DIR/frontend/.npm" -install -d -o "$APP_USER" -g "$APP_GROUP" "$APP_DIR/backend/data" - -run_as_app() { - sudo -u "$APP_USER" \ - env HOME="$APP_DIR/frontend" \ - NPM_CONFIG_CACHE="$APP_DIR/frontend/.npm" \ - "$@" -} - log "Backend: venv + dependencies" -if [[ ! -x "$APP_DIR/backend/.venv/bin/pip" ]]; then - sudo -u "$APP_USER" python3 -m venv "$APP_DIR/backend/.venv" +if [[ ! -x "$BACKEND_VENV/bin/pip" ]]; then + "$PYTHON_BIN" -m venv "$BACKEND_VENV" fi -sudo -u "$APP_USER" "$APP_DIR/backend/.venv/bin/pip" install --quiet --upgrade pip -sudo -u "$APP_USER" "$APP_DIR/backend/.venv/bin/pip" install --quiet -r "$APP_DIR/backend/requirements.txt" +"$BACKEND_VENV/bin/pip" install --quiet --upgrade pip +"$BACKEND_VENV/bin/pip" install --quiet -r "$APP_DIR/backend/requirements.txt" log "Frontend: npm ci + build" -run_as_app npm --prefix "$APP_DIR/frontend" ci -run_as_app npm --prefix "$APP_DIR/frontend" run build +npm --prefix "$APP_DIR/frontend" ci +npm --prefix "$APP_DIR/frontend" run build -if [[ $DO_INSTALL -eq 1 ]]; then - log "Installing systemd units" - cp "$APP_DIR/systemd/$BACKEND_SVC" "/etc/systemd/system/$BACKEND_SVC" - cp "$APP_DIR/systemd/$FRONTEND_SVC" "/etc/systemd/system/$FRONTEND_SVC" - systemctl daemon-reload - systemctl enable "$BACKEND_SVC" "$FRONTEND_SVC" +log "Build complete" +cat </dev/null; then - echo " backend /health OK" -else - echo " backend /health FAILED" >&2 - journalctl -u "$BACKEND_SVC" -n 50 --no-pager >&2 || true - exit 1 -fi -if curl -fsS -o /dev/null -w '%{http_code}\n' http://127.0.0.1:3001/ | grep -qE '^(200|301|302|307|308)$'; then - echo " frontend / OK" -else - echo " frontend / FAILED" >&2 - journalctl -u "$FRONTEND_SVC" -n 50 --no-pager >&2 || true - exit 1 -fi +Development: + $BACKEND_VENV/bin/uvicorn app.main:app --reload --host 127.0.0.1 --port 8001 --app-dir "$APP_DIR/backend" + NEXT_PUBLIC_API_BASE_URL=http://127.0.0.1:8001 npm --prefix "$APP_DIR/frontend" run dev -- --hostname 127.0.0.1 --port 3001 -log "Deploy complete" -systemctl status "$BACKEND_SVC" "$FRONTEND_SVC" --no-pager --lines=0 || true +Production-style local run: + $BACKEND_VENV/bin/uvicorn app.main:app --host 127.0.0.1 --port 8001 --app-dir "$APP_DIR/backend" + NEXT_PUBLIC_API_BASE_URL=http://127.0.0.1:8001 npm --prefix "$APP_DIR/frontend" run start -- --hostname 127.0.0.1 --port 3001 +EOF -- cgit v1.3-2-g0d8e