← back to Graphics Agentabrams

deploy/deploy.sh

64 lines

#!/bin/bash
# graphics.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=graphics.agentabrams.com
HERE="$(cd "$(dirname "$0")/.." && pwd)"

# 1. DNS: A record graphics -> 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\":\"graphics\",\"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
rsync -az --exclude .git --exclude tests --exclude deploy "$HERE/" $KAMATERA:/var/www/$SITE/
echo "content rsynced"

# 3. nginx. If the cert already exists, ship the full (SSL) config; otherwise ship an
#    HTTP-only bootstrap first — the committed .nginx references certs certbot hasn't
#    minted yet, so loading it pre-cert fails nginx -t (the first-deploy chicken-and-egg).
HAVE_CERT=$(ssh $KAMATERA "test -f /etc/letsencrypt/live/$SITE/fullchain.pem && echo yes || echo no")
if [ "$HAVE_CERT" = "yes" ]; then
  scp -q "$HERE/deploy/$SITE.nginx" $KAMATERA:/etc/nginx/sites-available/$SITE
else
  cat > /tmp/$SITE.bootstrap.nginx <<EOF
server {
    listen 45.61.58.125:80;
    server_name $SITE;
    root /var/www/$SITE;
    index index.html;
    location / { try_files \$uri \$uri/ \$uri.html =404; }
}
EOF
  scp -q /tmp/$SITE.bootstrap.nginx $KAMATERA:/etc/nginx/sites-available/$SITE
fi
ssh $KAMATERA "ln -sf /etc/nginx/sites-available/$SITE /etc/nginx/sites-enabled/$SITE && nginx -t && systemctl reload nginx"
echo "nginx staged (cert present: $HAVE_CERT)"

# 4. Wait for DNS, then SSL (certbot rewrites the http block to add 443 + redirect)
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 --redirect && 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/graphics.json | head -c 200; echo
[ "$code" = "200" ] && echo "DEPLOY OK" || { echo "DEPLOY FAILED"; exit 1; }