#!/usr/bin/env bash # # Deploy the SkateSpots AI proxy to the CVM host (same box as the CVM server). # # It is the same class of service as deploy-cvm.sh: a Bun process under systemd, # reachable only via Caddy (TLS) on the public side. Config/host come from # ~/.config/skatespots/cvm.env (never committed). # # npm run deploy:ai # or: bash scripts/deploy-ai-proxy.sh # bash scripts/deploy-ai-proxy.sh --logs set -euo pipefail REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" CONF="${HOME}/.config/skatespots/cvm.env" if [[ -f "$CONF" ]]; then # shellcheck disable=SC1090 source "$CONF" fi : "${CVM_HOST:?Set CVM_HOST (e.g. in ~/.config/skatespots/cvm.env) — the host is never committed}" CVM_SSH_KEY="${CVM_SSH_KEY:-$HOME/.ssh/skatespots-new}" AI_DIR="${AI_DIR:-/home/ubuntu/skatespots-ai}" AI_SERVICE="${AI_SERVICE:-skatespots-ai}" SSH=(ssh -i "$CVM_SSH_KEY" -o BatchMode=yes -o ConnectTimeout=12 "$CVM_HOST") if [[ "${1:-}" == "--logs" ]]; then "${SSH[@]}" "journalctl -u $AI_SERVICE -n 40 --no-pager" exit 0 fi echo "→ Ensuring $AI_DIR exists" "${SSH[@]}" "mkdir -p $AI_DIR" echo "→ Uploading ai-proxy/{server.ts,package.json,skatespots-ai.service}" scp -i "$CVM_SSH_KEY" -o BatchMode=yes \ "$REPO_ROOT/ai-proxy/server.ts" \ "$REPO_ROOT/ai-proxy/package.json" \ "$REPO_ROOT/ai-proxy/skatespots-ai.service" \ "$CVM_HOST:$AI_DIR/" echo "→ bun install" "${SSH[@]}" "cd $AI_DIR && ~/.bun/bin/bun install --silent 2>&1 | tail -3 || true" echo "→ Installing the unit and (re)starting $AI_SERVICE" "${SSH[@]}" "sudo cp $AI_DIR/skatespots-ai.service /etc/systemd/system/ \ && sudo systemctl daemon-reload \ && sudo systemctl enable --now $AI_SERVICE >/dev/null 2>&1 || true; \ sudo systemctl restart $AI_SERVICE \ && sleep 2 && echo \"active: \$(systemctl is-active $AI_SERVICE)\"" echo "→ Local health check (through the service, on the host)" "${SSH[@]}" "curl -s -o /dev/null -w 'status endpoint: %{http_code}\n' -X POST http://127.0.0.1:8787/api/ai/status || true" echo "→ Recent log:" "${SSH[@]}" "journalctl -u $AI_SERVICE -n 15 --no-pager | tail -6" echo "✅ AI proxy deployed. Public TLS is served by Caddy — see ai-proxy/DEPLOY.md."