← back to Stock Vshape Viewer

redeploy.sh

54 lines

#!/usr/bin/env bash
# redeploy.sh — gated one-command redeploy of charts.agentabrams.com to Kamatera.
# PRE-DEPLOY GATE: spins up a throwaway local server, runs the panel spec against it,
# and ABORTS the deploy if the spec is red. Only green builds reach production.
#   Usage:  ./redeploy.sh            (gate + deploy)
#           ./redeploy.sh --no-gate  (skip the spec, deploy anyway — discouraged)
set -euo pipefail
cd "$(dirname "$0")"
source ./.deploy.conf
REMOTE="${REMOTE_HOST:-root@45.61.58.125}"
U="${VIEWER_USER:-admin}"; P="${VIEWER_PASS:-DW2024!}"
PUB="${PUBLIC_URL:-https://charts.agentabrams.com}"

# ---------- PRE-DEPLOY GATE ----------
if [ "${1:-}" != "--no-gate" ]; then
  echo "==> PRE-DEPLOY GATE: panel spec against a throwaway local server"
  node server.js >/tmp/redeploy-server.log 2>&1 &   # PORT unset -> OS-assigned free port
  SRV=$!
  trap 'kill $SRV 2>/dev/null || true' EXIT
  sleep 2
  TESTURL=$(grep -oE 'http://localhost:[0-9]+' /tmp/redeploy-server.log | head -1)
  if [ -z "$TESTURL" ]; then echo "❌ local server failed to start"; cat /tmp/redeploy-server.log; exit 1; fi
  echo "    testing $TESTURL"
  if ! node test/panel.spec.mjs "$TESTURL"; then
    echo "❌ PRE-DEPLOY GATE FAILED — panel spec is red. NOT deploying."; exit 1
  fi
  kill $SRV 2>/dev/null || true; trap - EXIT
  echo "✅ gate passed — proceeding to deploy"
else
  echo "⚠️  --no-gate: skipping the pre-deploy spec"
fi

# ---------- DEPLOY ----------
echo "==> rsync to $REMOTE:$DEPLOY_PATH"
rsync -az --delete \
  --exclude '.git' --exclude 'node_modules' --exclude 'test' --exclude 'backtest' \
  --exclude '5x' --exclude '.playwright-mcp' --exclude '*.png' --exclude '.deploy.conf' \
  ./ "$REMOTE:$DEPLOY_PATH/"

echo "==> pm2 reload $PROJECT_NAME (zero-downtime, preserves PORT=$PORT)"
ssh "$REMOTE" "cd $DEPLOY_PATH && (pm2 reload $PROJECT_NAME 2>/dev/null || PORT=$PORT pm2 start server.js --name $PROJECT_NAME); pm2 save >/dev/null 2>&1"

# ---------- POST-DEPLOY SMOKE ----------
echo "==> prod smoke test ($PUB)"
sleep 1
c() { curl -s -o /dev/null -w "%{http_code}" "$@"; }
G=$(c "$PUB/"); A=$(c -u "$U:$P" "$PUB/"); API=$(c -u "$U:$P" "$PUB/api/history?ticker=NVDA")
echo "    gate:$G (401)  authed:$A (200)  api:$API (200)"
if [ "$G" = "401" ] && [ "$A" = "200" ] && [ "$API" = "200" ]; then
  echo "✅ redeploy complete → $PUB"
else
  echo "⚠️  redeploy finished but smoke test unexpected — check $PUB"; exit 2
fi