diff --git a/cloudflared-sync.sh b/cloudflared-sync.sh index c1254c5..c6afebe 100644 --- a/cloudflared-sync.sh +++ b/cloudflared-sync.sh @@ -2,8 +2,6 @@ set -e # đŸ“Ļ Ensure apk installs: yq, jq, git, bash, curl -# Required for pipeline: alpine image + apk add --no-cache git bash curl yq jq - apk add --no-cache git bash curl yq jq echo "🔍 Scanning for apps with cname.yaml..." @@ -13,41 +11,52 @@ rm -rf .tmp-repos mkdir -p .tmp-repos # === REPO CONFIG === -SANDBOX_REPO_URL="https://git.dvirlabs.com/dvirlabs/sandbox.git" +REPOS=( + "sandbox|https://git.dvirlabs.com/dvirlabs/sandbox.git" + "dev-tools|https://git.dvirlabs.com/dvirlabs/dev-tools.git" +) + INFRA_REPO_URL="https://${GIT_TOKEN}@git.dvirlabs.com/dvirlabs/infra.git" -SANDBOX_CLONE=".tmp-repos/sandbox" INFRA_CLONE=".tmp-repos/infra" GENERATED_FILE="$(pwd)/generated-values/cloudflared-values.yaml" ORIGINAL_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml" MERGED_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml" # === STEP 1: Clone Repos === -echo "đŸ“Ļ Cloning sandbox-apps..." -git clone --depth=1 "$SANDBOX_REPO_URL" "$SANDBOX_CLONE" +echo "đŸ“Ļ Cloning target app repos..." +for entry in "${REPOS[@]}"; do + SECTION_NAME="${entry%%|*}" + REPO_URL="${entry##*|}" + REPO_DIR=".tmp-repos/$SECTION_NAME" + git clone --depth=1 "$REPO_URL" "$REPO_DIR" +done -echo "đŸ“Ļ Cloning infra..." +echo "đŸ“Ļ Cloning infra repo..." git clone --depth=1 "$INFRA_REPO_URL" "$INFRA_CLONE" ls -l .tmp-repos/ -# === STEP 2: Extract Generated CNAMEs from sandbox === -echo "âš™ī¸ Generating sandbox ingress list..." -cat < "$GENERATED_FILE" -ingress: [] -EOF +# === STEP 2: Extract CNAMEs from all repos === +echo "âš™ī¸ Generating merged ingress list..." +echo "ingress: []" > "$GENERATED_FILE" -find "$SANDBOX_CLONE/manifests" -name cname.yaml | while read -r cname_file; do - app_name=$(basename "$(dirname "$cname_file")") - enabled=$(yq '.enabled' "$cname_file") +for entry in "${REPOS[@]}"; do + SECTION_NAME="${entry%%|*}" + REPO_DIR=".tmp-repos/$SECTION_NAME" - if [[ "$enabled" == "true" ]]; then - hostname=$(yq '.hostname' "$cname_file") - service="http://${app_name}.sandbox.svc.cluster.local:80" + find "$REPO_DIR/manifests" -name cname.yaml | while read -r cname_file; do + app_name=$(basename "$(dirname "$cname_file")") + enabled=$(yq '.enabled' "$cname_file") - echo "✅ Found $hostname → $service" + if [[ "$enabled" == "true" ]]; then + hostname=$(yq '.hostname' "$cname_file") + service="http://${app_name}.${SECTION_NAME}.svc.cluster.local:80" - yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$GENERATED_FILE" - fi + echo "✅ Found $hostname → $service in $SECTION_NAME" + + yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\", \"_section\": \"$SECTION_NAME\"}]" -i "$GENERATED_FILE" + fi + done done echo "📄 Generated Ingress:" @@ -59,7 +68,6 @@ echo "🔁 Merging new entries into: $ORIGINAL_FILE" TEMP_FILE=$(mktemp) cp "$ORIGINAL_FILE" "$TEMP_FILE" -# Loop through new entries and append only if hostname not exists yq eval '.ingress' "$GENERATED_FILE" | yq -o=json | jq -c '.[]' | while read -r new_entry; do hostname=$(echo "$new_entry" | jq -r '.hostname') service=$(echo "$new_entry" | jq -r '.service') @@ -89,14 +97,14 @@ git remote set-url origin "https://${GIT_TOKEN}@git.dvirlabs.com/dvirlabs/infra. if ! git diff --quiet manifests/cloudflared/values.yaml; then git add manifests/cloudflared/values.yaml - git commit -m "chore(cloudflared): auto-merge CNAME entries from sandbox" + git commit -m "chore(cloudflared): auto-merge CNAME entries from all repos" git push origin HEAD echo "✅ Changes pushed successfully." else echo "â„šī¸ No changes to commit." fi -# === STEP 5: Create CNAME records in Cloudflare === +# === STEP 6: Create CNAME records in Cloudflare === ls -l pwd ls -l "$GENERATED_FILE" @@ -104,15 +112,12 @@ echo "🌐 Creating CNAME records in Cloudflare..." CLOUDFLARE_API="https://api.cloudflare.com/client/v4" TARGET="b50bbf48-0a2f-47ce-b73e-336b6718318b.cfargotunnel.com" -# Load required secrets : "${CLOUDFLARE_API_TOKEN:?CLOUDFLARE_API_TOKEN not set}" : "${CLOUDFLARE_ZONE_ID:?CLOUDFLARE_ZONE_ID not set}" -# Check and create each CNAME -yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' - | jq -c '.[]' | while read -r record; do +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//') - # Check if already exists 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')