Files
proxmox/scripts/deployment/ensure-sankofa-phoenix-api-db-migrate-up-7800.sh
2026-04-13 21:41:14 -07:00

92 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Run sankofa-api DB migrations (pnpm db:migrate:up) on CT 7800 against DB_* from /opt/sankofa-api/.env.
# Uses Python to load .env so JWT_SECRET and other values with shell metacharacters do not break `source`.
#
# Prerequisites: DB_HOST/DB_USER/DB_PASSWORD/DB_NAME correct; Postgres reachable (e.g. VMID 7803).
#
# Usage:
# bash scripts/deployment/ensure-sankofa-phoenix-api-db-migrate-up-7800.sh --dry-run --vmid 7800
# PROXMOX_OPS_APPLY=1 PROXMOX_OPS_ALLOWED_VMIDS=7800 bash scripts/deployment/ensure-sankofa-phoenix-api-db-migrate-up-7800.sh --apply --vmid 7800
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
# shellcheck source=/dev/null
source "${PROJECT_ROOT}/scripts/lib/load-project-env.sh"
# shellcheck source=/dev/null
source "${PROJECT_ROOT}/scripts/lib/proxmox-production-guard.sh"
API_ROOT="${SANKOFA_PHOENIX_API_ROOT:-/opt/sankofa-api}"
APPLY=false
DRY_RUN=false
VMID="${SANKOFA_PHOENIX_VMID:-7800}"
SSH_OPTS="-o BatchMode=yes -o ConnectTimeout=15 -o StrictHostKeyChecking=accept-new"
while [[ $# -gt 0 ]]; do
case "$1" in
--apply) APPLY=true ;;
--dry-run) DRY_RUN=true ;;
--vmid) VMID="${2:?}"; shift ;;
*) echo "Unknown arg: $1" >&2; exit 2 ;;
esac
shift
done
PROXMOX_HOST="${PROXMOX_HOST:-$(get_host_for_vmid "$VMID")}"
echo "=== ensure-sankofa-phoenix-api-db-migrate-up-7800 ==="
echo "PVE: root@${PROXMOX_HOST} VMID=${VMID} dir=${API_ROOT}"
echo ""
if $DRY_RUN || ! $APPLY; then
# shellcheck disable=SC2029
ssh $SSH_OPTS "root@${PROXMOX_HOST}" "pct exec ${VMID} -- bash -lc \"
[[ -d '${API_ROOT}' ]] || { echo 'missing ${API_ROOT}'; exit 0; }
test -f '${API_ROOT}/package.json' && grep -E 'db:migrate' '${API_ROOT}/package.json' | head -3
\""
echo "For apply: --apply and PROXMOX_OPS_APPLY=1 PROXMOX_OPS_ALLOWED_VMIDS=${VMID}"
exit 0
fi
if ! pguard_require_apply_flag true; then
echo "Refused: set PROXMOX_OPS_APPLY=1" >&2
exit 3
fi
if ! pguard_vmid_allowed "$VMID"; then
exit 3
fi
WORKDIR="$(mktemp -d)"
trap 'rm -rf "$WORKDIR"' EXIT
REMOTE_SH="${WORKDIR}/remote.sh"
{
printf 'export API_ROOT=%q\n' "$API_ROOT"
cat <<'EOS'
set -euo pipefail
cd "$API_ROOT"
python3 <<'PY'
import os, subprocess
from pathlib import Path
env = dict(os.environ)
p = Path(".env")
if not p.is_file():
raise SystemExit("ERROR: missing .env")
for line in p.read_text().splitlines():
line = line.strip()
if not line or line.startswith("#"):
continue
if "=" not in line:
continue
k, v = line.split("=", 1)
env[k] = v
r = subprocess.run(["pnpm", "run", "db:migrate:up"], cwd=str(Path.cwd()), env=env)
raise SystemExit(r.returncode)
PY
echo "OK: db:migrate:up finished"
EOS
} >"$REMOTE_SH"
ssh $SSH_OPTS "root@${PROXMOX_HOST}" "pct exec ${VMID} -- bash -s" <"$REMOTE_SH"
echo ""
echo "Optional: systemctl restart sankofa-api (if schema changed compatibility)."