#!/bin/bash echo "🌐 Creating CNAME records in Cloudflare..." : "${CLOUDFLARE_API_TOKEN:?CLOUDFLARE_API_TOKEN not set}" : "${CLOUDFLARE_ZONE_ID:?CLOUDFLARE_ZONE_ID not set}" yq eval '.ingress' "$GENERATED_FILE" | yq -o=json '.' | jq -c '.[]' | while read -r record; do name=$(echo "$record" | jq -r '.hostname' | sed 's/\.dvirlabs\.com//') exists=$(curl -s -X GET "$CLOUDFLARE_API/zones/$CLOUDFLARE_ZONE_ID/dns_records?type=CNAME&name=$name.dvirlabs.com" \ -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" | jq '.result | length') if [ "$exists" -eq 0 ]; then echo "➕ Creating CNAME: $name.dvirlabs.com → $TARGET" curl -s -X POST "$CLOUDFLARE_API/zones/$CLOUDFLARE_ZONE_ID/dns_records" \ -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ -H "Content-Type: application/json" \ --data "{ \"type\": \"CNAME\", \"name\": \"$name\", \"content\": \"$TARGET\", \"ttl\": 1, \"proxied\": true }" > /dev/null else echo "⚠️ CNAME for $name.dvirlabs.com already exists, skipping" fi done