← back to Aurora Drift
deploy/deploy.sh
34 lines
#!/bin/bash
set -euo pipefail
KAMATERA=root@45.61.58.125
SITE=aurora.agentabrams.com
HERE="$(cd "$(dirname "$0")/.." && pwd)"
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":"aurora","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
rsync -az --delete --exclude .git --exclude deploy --exclude README.md --exclude verification "$HERE/" "$KAMATERA:/var/www/$SITE/"
CERT="/etc/letsencrypt/live/$SITE/fullchain.pem"
if ssh "$KAMATERA" "test -f $CERT"; then
scp -q "$HERE/deploy/$SITE.ssl.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"
else
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"
for _ in $(seq 1 30); do [ -n "$(dig +short "$SITE" @1.1.1.1)" ] && break; sleep 10; done
ssh "$KAMATERA" "certbot certonly --webroot -w /var/www/$SITE -d $SITE --non-interactive --agree-tos -m steve@designerwallcoverings.com"
scp -q "$HERE/deploy/$SITE.ssl.nginx" "$KAMATERA:/etc/nginx/sites-available/$SITE"
ssh "$KAMATERA" "nginx -t && systemctl reload nginx"
fi
sleep 2
code=$(curl -s -o /dev/null -w '%{http_code}' "https://$SITE/")
echo "https://$SITE -> HTTP $code"
[ "$code" = '200' ] && echo 'DEPLOY OK' || exit 1