fix dns record creation in new-app.sh
This commit is contained in:
+40
-20
@@ -130,32 +130,52 @@ if [ "$ZONE_CHECK" != "200" ]; then
|
|||||||
echo " Created DNS zone for $APP"
|
echo " Created DNS zone for $APP"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "==> Checking A records for $APP..."
|
echo "==> Setting A records for $APP..."
|
||||||
RECORDS=$(curl -s "$VULTR_API/domains/$APP/records" -H "$VULTR_AUTH")
|
RECORDS=$(curl -s "$VULTR_API/domains/$APP/records" -H "$VULTR_AUTH")
|
||||||
|
|
||||||
# Check/create root A record
|
# Delete existing root A records
|
||||||
ROOT_EXISTS=$(echo "$RECORDS" | jq -r ".records[] | select(.type == \"A\" and .name == \"\" and .data == \"$SERVER_IP\") | .id")
|
echo "$RECORDS" | jq -r '.records[] | select(.type == "A" and .name == "") | .id' | while read -r id; do
|
||||||
if [ -z "$ROOT_EXISTS" ]; then
|
echo " Deleting existing A record for $APP (id: $id)"
|
||||||
echo " Creating A record: $APP -> $SERVER_IP"
|
curl -s -X DELETE "$VULTR_API/domains/$APP/records/$id" -H "$VULTR_AUTH"
|
||||||
curl -sf -X POST "$VULTR_API/domains/$APP/records" \
|
done
|
||||||
-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
|
|
||||||
|
|
||||||
# Check/create www A record
|
# Delete existing www A records
|
||||||
WWW_EXISTS=$(echo "$RECORDS" | jq -r ".records[] | select(.type == \"A\" and .name == \"www\" and .data == \"$SERVER_IP\") | .id")
|
echo "$RECORDS" | jq -r '.records[] | select(.type == "A" and .name == "www") | .id' | while read -r id; do
|
||||||
if [ -z "$WWW_EXISTS" ]; then
|
echo " Deleting existing A record for www.$APP (id: $id)"
|
||||||
echo " Creating A record: www.$APP -> $SERVER_IP"
|
curl -s -X DELETE "$VULTR_API/domains/$APP/records/$id" -H "$VULTR_AUTH"
|
||||||
curl -sf -X POST "$VULTR_API/domains/$APP/records" \
|
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 "$VULTR_AUTH" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d "{\"name\": \"www\", \"type\": \"A\", \"data\": \"$SERVER_IP\", \"ttl\": 3600}" > /dev/null
|
-d "{\"name\": \"\", \"type\": \"A\", \"data\": \"$SERVER_IP\", \"ttl\": 300}")
|
||||||
else
|
if [ "$HTTP_CODE" != "200" ] && [ "$HTTP_CODE" != "201" ] && [ "$HTTP_CODE" != "204" ]; then
|
||||||
echo " A record for www.$APP already exists."
|
echo "Error: Failed to create A record for $APP (HTTP $HTTP_CODE)"
|
||||||
|
cat "$RESP"
|
||||||
|
rm -f "$RESP"
|
||||||
|
exit 1
|
||||||
fi
|
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..."
|
echo "==> Waiting for DNS to propagate..."
|
||||||
ELAPSED=0
|
ELAPSED=0
|
||||||
|
|||||||
Reference in New Issue
Block a user