diff --git a/scripts/new-app.sh b/scripts/new-app.sh index 64ee3af..f96d9c6 100755 --- a/scripts/new-app.sh +++ b/scripts/new-app.sh @@ -130,32 +130,52 @@ if [ "$ZONE_CHECK" != "200" ]; then echo " Created DNS zone for $APP" fi -echo "==> Checking A records for $APP..." +echo "==> Setting A records for $APP..." RECORDS=$(curl -s "$VULTR_API/domains/$APP/records" -H "$VULTR_AUTH") -# Check/create root A record -ROOT_EXISTS=$(echo "$RECORDS" | jq -r ".records[] | select(.type == \"A\" and .name == \"\" and .data == \"$SERVER_IP\") | .id") -if [ -z "$ROOT_EXISTS" ]; then - echo " Creating A record: $APP -> $SERVER_IP" - curl -sf -X POST "$VULTR_API/domains/$APP/records" \ - -H "$VULTR_AUTH" \ - -H "Content-Type: application/json" \ - -d "{\"name\": \"\", \"type\": \"A\", \"data\": \"$SERVER_IP\", \"ttl\": 3600}" > /dev/null -else - echo " A record for $APP already exists." -fi +# Delete existing root A records +echo "$RECORDS" | jq -r '.records[] | select(.type == "A" and .name == "") | .id' | while read -r id; do + echo " Deleting existing A record for $APP (id: $id)" + curl -s -X DELETE "$VULTR_API/domains/$APP/records/$id" -H "$VULTR_AUTH" +done -# Check/create www A record -WWW_EXISTS=$(echo "$RECORDS" | jq -r ".records[] | select(.type == \"A\" and .name == \"www\" and .data == \"$SERVER_IP\") | .id") -if [ -z "$WWW_EXISTS" ]; then - echo " Creating A record: www.$APP -> $SERVER_IP" - curl -sf -X POST "$VULTR_API/domains/$APP/records" \ - -H "$VULTR_AUTH" \ - -H "Content-Type: application/json" \ - -d "{\"name\": \"www\", \"type\": \"A\", \"data\": \"$SERVER_IP\", \"ttl\": 3600}" > /dev/null -else - echo " A record for www.$APP already exists." +# Delete existing www A records +echo "$RECORDS" | jq -r '.records[] | select(.type == "A" and .name == "www") | .id' | while read -r id; do + echo " Deleting existing A record for www.$APP (id: $id)" + curl -s -X DELETE "$VULTR_API/domains/$APP/records/$id" -H "$VULTR_AUTH" +done + +# Create root A record +echo " Creating A record: $APP -> $SERVER_IP" +RESP=$(mktemp) +HTTP_CODE=$(curl -s -o "$RESP" -w "%{http_code}" \ + -X POST "$VULTR_API/domains/$APP/records" \ + -H "$VULTR_AUTH" \ + -H "Content-Type: application/json" \ + -d "{\"name\": \"\", \"type\": \"A\", \"data\": \"$SERVER_IP\", \"ttl\": 300}") +if [ "$HTTP_CODE" != "200" ] && [ "$HTTP_CODE" != "201" ] && [ "$HTTP_CODE" != "204" ]; then + echo "Error: Failed to create A record for $APP (HTTP $HTTP_CODE)" + cat "$RESP" + rm -f "$RESP" + exit 1 fi +rm -f "$RESP" + +# Create www A record +echo " Creating A record: www.$APP -> $SERVER_IP" +RESP=$(mktemp) +HTTP_CODE=$(curl -s -o "$RESP" -w "%{http_code}" \ + -X POST "$VULTR_API/domains/$APP/records" \ + -H "$VULTR_AUTH" \ + -H "Content-Type: application/json" \ + -d "{\"name\": \"www\", \"type\": \"A\", \"data\": \"$SERVER_IP\", \"ttl\": 300}") +if [ "$HTTP_CODE" != "200" ] && [ "$HTTP_CODE" != "201" ] && [ "$HTTP_CODE" != "204" ]; then + echo "Error: Failed to create A record for www.$APP (HTTP $HTTP_CODE)" + cat "$RESP" + rm -f "$RESP" + exit 1 +fi +rm -f "$RESP" echo "==> Waiting for DNS to propagate..." ELAPSED=0