← back to Coming Soon Template

add_zones.sh

54 lines

#!/usr/bin/env bash
# Adds all 21 abrams/butler domains as Cloudflare zones (free plan).
# Each zone starts in "pending" state and activates the moment GoDaddy's
# nameservers flip to the ones CF assigned to that zone.
#
# Run this AFTER (or any time before) you change nameservers at GoDaddy.
# Safe to run more than once — already-added zones are reported as no-ops.
set -uo pipefail
cd "$(dirname "$0")"

source .env
# Prefer the DNS-scoped token; fall back to the Pages token (it often has Zone perms too).
# If neither has Zone:Edit, the API will return a clear 403 below.
TOKEN="${CLOUDFLARE_API_TOKEN:-${CLOUDFLARE_PAGES_TOKEN:?neither CLOUDFLARE_API_TOKEN nor CLOUDFLARE_PAGES_TOKEN is set}}"
ACCT="${CLOUDFLARE_ACCOUNT_ID:?CLOUDFLARE_ACCOUNT_ID not set}"

DOMAINS=(
  abramsindex.com abramsos.com abramsintel.com abramsspace.com abramsterminal.com
  abramsvc.com abramsindustries.com abramsprotection.com abramscivic.com abramslive.com
  abramsatlas.com abramsdirectory.com abramsguide.com abramslocal.com abramsmaps.com
  abramsmarkets.com abramsintelligence.com
  818butler.com beverlyhillsbutler.com bhbutler.com boulevardbutler.com
)

ok=0; skip=0; fail=0
echo "Adding zones to Cloudflare (account $ACCT)..."
for d in "${DOMAINS[@]}"; do
  resp=$(curl -s -X POST \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json" \
    --data "{\"name\":\"$d\",\"account\":{\"id\":\"$ACCT\"},\"type\":\"full\"}" \
    "https://api.cloudflare.com/client/v4/zones")

  status=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin).get('success','?'))" 2>/dev/null)
  if [ "$status" = "True" ]; then
    # Extract the 2 CF nameservers to enter at GoDaddy
    ns=$(echo "$resp" | python3 -c "import sys,json; print(' '.join(json.load(sys.stdin)['result'].get('name_servers',[])))" 2>/dev/null)
    printf "  + %-30s -> add NS at GoDaddy: %s\n" "$d" "$ns"
    ok=$((ok+1))
  elif echo "$resp" | grep -qE "already exists|already in"; then
    printf "  ~ %-30s already added\n" "$d"
    skip=$((skip+1))
  else
    err=$(echo "$resp" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('errors',[{}])[0].get('message','?'))" 2>/dev/null)
    printf "  x %-30s -> %s\n" "$d" "$err"
    fail=$((fail+1))
  fi
done

echo
echo "summary: $ok added, $skip already added, $fail failed"
echo
echo "NEXT STEP: at GoDaddy, for each domain above, set its nameservers to the 2 listed."