← back to Games Agentabrams
deploy/deploy.sh
58 lines
#!/bin/bash
# games.agentabrams.com — full first deploy (DNS + rsync + nginx + SSL).
# Steve-gated: run only with explicit approval.
set -euo pipefail
KAMATERA=root@45.61.58.125
SITE=games.agentabrams.com
HERE="$(cd "$(dirname "$0")/.." && pwd)"
# 0. Sync guard: rebuild games/<id>/ from their source projects BEFORE rsync so
# the live site can never ship a stale copy (sync.sh is otherwise manual —
# edit a source, forget to run it, and the old build would deploy). Fail-closed
# via set -e; ships whatever the current sources say.
"$HERE/sync.sh"
# 1. DNS: A record games -> Kamatera (DNS-only, matches fleet pattern)
export $(grep -E "^CLOUDFLARE_API_TOKEN=" ~/Projects/secrets-manager/.env)
ZONE=$(curl -s -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
"https://api.cloudflare.com/client/v4/zones?name=agentabrams.com" \
| python3 -c "import sys,json; print(json.load(sys.stdin)['result'][0]['id'])")
EXISTS=$(curl -s -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
"https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records?name=$SITE" \
| python3 -c "import sys,json; print(len(json.load(sys.stdin)['result']))")
if [ "$EXISTS" = "0" ]; then
curl -s -X POST -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" \
"https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records" \
-d "{\"type\":\"A\",\"name\":\"games\",\"content\":\"45.61.58.125\",\"ttl\":300,\"proxied\":false}" \
| python3 -c "import sys,json; r=json.load(sys.stdin); assert r['success'], r['errors']; print('DNS record created')"
else
echo "DNS record already exists"
fi
# 2. Content
# --delete: mirror the local tree so retired games are purged from the server
# too (the site dir is fully generated from this repo). --exclude patterns also
# protect .git/tests/deploy on the destination from deletion. Scoped to the one
# site dir; --delete only ever removes files under /var/www/$SITE/.
rsync -az --delete --exclude .git --exclude tests --exclude deploy "$HERE/" $KAMATERA:/var/www/$SITE/
echo "content rsynced (mirrored)"
# 3. nginx (80 first so certbot HTTP-01 can pass)
scp -q "$HERE/deploy/$SITE.nginx" $KAMATERA:/etc/nginx/sites-available/$SITE
ssh $KAMATERA "ln -sf /etc/nginx/sites-available/$SITE /etc/nginx/sites-enabled/$SITE && nginx -t && systemctl reload nginx"
echo "nginx staged"
# 4. Wait for DNS, then SSL
for i in $(seq 1 30); do
[ -n "$(dig +short $SITE @1.1.1.1)" ] && break
sleep 10
done
ssh $KAMATERA "certbot --nginx -d $SITE --non-interactive --agree-tos -m steve@designerwallcoverings.com && systemctl reload nginx"
# 5. Smoke test
sleep 2
code=$(curl -s -o /dev/null -w '%{http_code}' https://$SITE/)
echo "https://$SITE -> HTTP $code"
curl -s https://$SITE/games.json | head -c 200; echo
[ "$code" = "200" ] && echo "DEPLOY OK" || { echo "DEPLOY FAILED"; exit 1; }