Some checks failed
CI / test-and-build (push) Failing after 25s
Co-authored-by: Cursor <cursoragent@cursor.com>
254 lines
7.7 KiB
Bash
254 lines
7.7 KiB
Bash
#!/usr/bin/env bash
|
|
# Unique production deploy mechanism for Tloto.
|
|
# Used by: manual SSH, and (optionally) Gitea Actions via restricted sudo/SSH.
|
|
# Does NOT roll back PostgreSQL automatically.
|
|
set -euo pipefail
|
|
|
|
APP_DIR="${TLOTO_APP_DIR:-/home/deploy/games_app}"
|
|
STATE_DIR="${TLOTO_STATE_DIR:-/var/lib/tloto}"
|
|
RELEASES_DIR="${STATE_DIR}/releases"
|
|
BACKUPS_DIR="${STATE_DIR}/backups"
|
|
LOCK_FILE="${STATE_DIR}/deploy.lock"
|
|
CURRENT_FILE="${STATE_DIR}/current.json"
|
|
MODE="${TLOTO_DEPLOY_MODE:-http}" # http | https
|
|
HEALTH_URL="${TLOTO_HEALTH_URL:-http://127.0.0.1:9080/health}"
|
|
ALLOW_DESTRUCTIVE=0
|
|
REF=""
|
|
ROLLBACK_ONLY=0
|
|
SKIP_MIGRATE=0
|
|
|
|
usage() {
|
|
cat <<'EOF'
|
|
Usage: tloto-deploy --ref <git-sha|tag|branch> [options]
|
|
tloto-deploy --rollback
|
|
|
|
Options:
|
|
--ref <ref> Git ref to deploy (required unless --rollback)
|
|
--mode http|https Compose profile path (default: http until DNS cutover)
|
|
--health-url <url> Health check URL
|
|
--allow-destructive-migrate Allow DROP/TRUNCATE-like schema changes without abort
|
|
--skip-migrate Skip npm run db:migrate
|
|
--rollback Redeploy previous successful release (app only)
|
|
-h, --help Show help
|
|
EOF
|
|
}
|
|
|
|
log() { printf '[tloto-deploy] %s\n' "$*"; }
|
|
die() { printf '[tloto-deploy] ERROR: %s\n' "$*" >&2; exit 1; }
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--ref) REF="${2:-}"; shift 2 ;;
|
|
--mode) MODE="${2:-}"; shift 2 ;;
|
|
--health-url) HEALTH_URL="${2:-}"; shift 2 ;;
|
|
--allow-destructive-migrate) ALLOW_DESTRUCTIVE=1; shift ;;
|
|
--skip-migrate) SKIP_MIGRATE=1; shift ;;
|
|
--rollback) ROLLBACK_ONLY=1; shift ;;
|
|
-h|--help) usage; exit 0 ;;
|
|
*) die "unknown arg: $1" ;;
|
|
esac
|
|
done
|
|
|
|
mkdir -p "$RELEASES_DIR" "$BACKUPS_DIR"
|
|
cd "$APP_DIR"
|
|
|
|
if [[ ! -f .env ]]; then
|
|
die "missing $APP_DIR/.env"
|
|
fi
|
|
|
|
exec 9>"$LOCK_FILE"
|
|
if ! flock -n 9; then
|
|
die "another deploy is in progress (lock $LOCK_FILE)"
|
|
fi
|
|
|
|
ts() { date -u +%Y-%m-%dT%H:%M:%SZ; }
|
|
release_id() { date -u +%Y%m%dT%H%M%SZ; }
|
|
|
|
read_prev_sha() {
|
|
if [[ -f "$CURRENT_FILE" ]]; then
|
|
python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("sha",""))' "$CURRENT_FILE" 2>/dev/null || true
|
|
fi
|
|
}
|
|
|
|
write_release() {
|
|
local id="$1" sha="$2" prev="$3" status="$4" detail="${5:-}"
|
|
local path="$RELEASES_DIR/${id}.json"
|
|
python3 - <<PY
|
|
import json
|
|
from pathlib import Path
|
|
doc = {
|
|
"id": "$id",
|
|
"sha": "$sha",
|
|
"prev_sha": "$prev",
|
|
"status": "$status",
|
|
"mode": "$MODE",
|
|
"started_at": "${STARTED_AT}",
|
|
"finished_at": "$(ts)",
|
|
"detail": """${detail}""",
|
|
"health_url": "$HEALTH_URL",
|
|
}
|
|
Path("$path").write_text(json.dumps(doc, indent=2) + "\n")
|
|
if "$status" == "success":
|
|
Path("$CURRENT_FILE").write_text(json.dumps(doc, indent=2) + "\n")
|
|
print(path)
|
|
PY
|
|
}
|
|
|
|
REGISTRY="${TLOTO_REGISTRY:-127.0.0.1:5000}"
|
|
|
|
compose_up() {
|
|
local sha_tag="${TLOTO_IMAGE_TAG:-local}"
|
|
export TLOTO_IMAGE_TAG="$sha_tag"
|
|
export TLOTO_REGISTRY="$REGISTRY"
|
|
# Prefer pre-built registry images when present (CI); else build via existing npm scripts.
|
|
if [[ "$sha_tag" != "local" ]] \
|
|
&& sudo docker image inspect "${REGISTRY}/tloto-backend:${sha_tag}" >/dev/null 2>&1 \
|
|
&& sudo docker image inspect "${REGISTRY}/tloto-web:${sha_tag}" >/dev/null 2>&1; then
|
|
log "using registry images tag=$sha_tag"
|
|
if [[ "$MODE" == "https" ]]; then
|
|
sudo -E docker compose --profile https-dns --profile clamav up -d
|
|
sudo docker restart vps_gateway_nginx >/dev/null 2>&1 || true
|
|
else
|
|
sudo -E docker compose --profile http --profile clamav up -d
|
|
sudo docker restart vps_gateway_nginx >/dev/null 2>&1 || true
|
|
fi
|
|
else
|
|
log "building via npm phase-b:up* (tag=$sha_tag)"
|
|
if [[ "$MODE" == "https" ]]; then
|
|
sudo -E npm run phase-b:up:https
|
|
else
|
|
sudo -E npm run phase-b:up
|
|
fi
|
|
# Record tags for future rollback
|
|
sudo docker tag "games_app-backend:latest" "${REGISTRY}/tloto-backend:${sha_tag}" 2>/dev/null || true
|
|
sudo docker tag "games_app-prediqo-web:latest" "${REGISTRY}/tloto-web:${sha_tag}" 2>/dev/null || true
|
|
sudo docker push "${REGISTRY}/tloto-backend:${sha_tag}" 2>/dev/null || true
|
|
sudo docker push "${REGISTRY}/tloto-web:${sha_tag}" 2>/dev/null || true
|
|
fi
|
|
}
|
|
|
|
health_check() {
|
|
local i code
|
|
for i in $(seq 1 40); do
|
|
code="$(curl -s -o /tmp/tloto-health.body -w '%{http_code}' -m 5 "$HEALTH_URL" || echo 000)"
|
|
if [[ "$code" == "200" ]]; then
|
|
log "health OK ($code)"
|
|
return 0
|
|
fi
|
|
log "health wait $i code=$code"
|
|
sleep 3
|
|
done
|
|
return 1
|
|
}
|
|
|
|
schema_looks_destructive() {
|
|
# Heuristic on schema.sql diff vs previous working tree / release marker
|
|
local schema="$APP_DIR/backend/src/db/schema.sql"
|
|
local marker="$STATE_DIR/last-schema.sha256"
|
|
[[ -f "$schema" ]] || return 1
|
|
local now
|
|
now="$(sha256sum "$schema" | awk '{print $1}')"
|
|
if [[ -f "$marker" ]]; then
|
|
local old
|
|
old="$(cat "$marker")"
|
|
if [[ "$old" == "$now" ]]; then
|
|
return 1
|
|
fi
|
|
fi
|
|
if grep -Eiq 'DROP[[:space:]]+(TABLE|COLUMN|SCHEMA)|TRUNCATE[[:space:]]' "$schema"; then
|
|
return 0
|
|
fi
|
|
return 1
|
|
}
|
|
|
|
backup_db() {
|
|
local out="$BACKUPS_DIR/games_platform_db_$(date -u +%Y%m%dT%H%M%SZ).dump"
|
|
log "pg_dump → $out"
|
|
# Use peer/local via sudo -u postgres
|
|
sudo -u postgres pg_dump -Fc -d games_platform_db -f "$out"
|
|
echo "$out"
|
|
}
|
|
|
|
checkout_ref() {
|
|
local ref="$1"
|
|
if [[ ! -d .git ]]; then
|
|
die "$APP_DIR is not a git checkout — clone from Gitea first"
|
|
fi
|
|
git fetch --all --tags --prune
|
|
git checkout --force "$ref"
|
|
git reset --hard "$ref"
|
|
git rev-parse HEAD
|
|
}
|
|
|
|
export_image_tags() {
|
|
local sha="$1"
|
|
# Optional override for compose image tags (compose-image-tags support)
|
|
export TLOTO_IMAGE_TAG="$sha"
|
|
export COMPOSE_DOCKER_CLI_BUILD=1
|
|
}
|
|
|
|
deploy_sha() {
|
|
local sha="$1"
|
|
local prev id
|
|
prev="$(read_prev_sha)"
|
|
id="$(release_id)"
|
|
STARTED_AT="$(ts)"
|
|
log "deploy start id=$id sha=$sha prev=${prev:-none} mode=$MODE"
|
|
|
|
export_image_tags "$sha"
|
|
|
|
if [[ "$SKIP_MIGRATE" -eq 0 ]]; then
|
|
if schema_looks_destructive && [[ "$ALLOW_DESTRUCTIVE" -eq 0 ]]; then
|
|
write_release "$id" "$sha" "$prev" "aborted" "destructive schema heuristic; pass --allow-destructive-migrate"
|
|
die "schema.sql looks destructive — aborting (no migrate/up). Use --allow-destructive-migrate after review."
|
|
fi
|
|
local marker="$STATE_DIR/last-schema.sha256"
|
|
local now
|
|
now="$(sha256sum backend/src/db/schema.sql | awk '{print $1}')"
|
|
if [[ ! -f "$marker" ]] || [[ "$(cat "$marker")" != "$now" ]]; then
|
|
backup_db >/dev/null
|
|
fi
|
|
log "npm run db:migrate"
|
|
sudo npm run db:migrate
|
|
echo "$now" > "$marker"
|
|
else
|
|
log "skip migrate"
|
|
fi
|
|
|
|
log "compose up ($MODE)"
|
|
compose_up
|
|
|
|
if ! health_check; then
|
|
write_release "$id" "$sha" "$prev" "failed" "health check failed"
|
|
if [[ -n "${prev:-}" ]]; then
|
|
log "rolling back app to $prev"
|
|
checkout_ref "$prev" >/dev/null
|
|
export_image_tags "$prev"
|
|
compose_up || true
|
|
health_check || log "rollback health still failing — manual intervention required"
|
|
write_release "$(release_id)" "$prev" "$sha" "rollback" "auto after failed $sha"
|
|
fi
|
|
die "deploy failed health check"
|
|
fi
|
|
|
|
write_release "$id" "$sha" "$prev" "success" "ok"
|
|
log "deploy success sha=$sha"
|
|
}
|
|
|
|
STARTED_AT="$(ts)"
|
|
|
|
if [[ "$ROLLBACK_ONLY" -eq 1 ]]; then
|
|
prev="$(read_prev_sha)"
|
|
[[ -n "$prev" ]] || die "no current release to infer previous from; set --ref explicitly"
|
|
# Prefer prev_sha inside current.json
|
|
target="$(python3 -c 'import json;print(json.load(open("'"$CURRENT_FILE"'")).get("prev_sha",""))' 2>/dev/null || true)"
|
|
[[ -n "$target" ]] || die "no prev_sha recorded"
|
|
sha="$(checkout_ref "$target")"
|
|
deploy_sha "$sha"
|
|
exit 0
|
|
fi
|
|
|
|
[[ -n "$REF" ]] || die "--ref required"
|
|
sha="$(checkout_ref "$REF")"
|
|
deploy_sha "$sha"
|