From 46c6e7dfeedafb499e4eca3159f276e73cda7335 Mon Sep 17 00:00:00 2001 From: dvirlabs Date: Sun, 22 Jun 2025 23:53:12 +0300 Subject: [PATCH] Debug --- automation/cloudflared-sync.sh | 108 ++++++++++++++------------------- 1 file changed, 46 insertions(+), 62 deletions(-) diff --git a/automation/cloudflared-sync.sh b/automation/cloudflared-sync.sh index c0ab8dc..53d32e4 100644 --- a/automation/cloudflared-sync.sh +++ b/automation/cloudflared-sync.sh @@ -1,6 +1,9 @@ #!/bin/bash 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..." @@ -9,103 +12,76 @@ mkdir -p generated-values rm -rf .tmp-repos mkdir -p .tmp-repos +# === REPO CONFIG === SANDBOX_REPO_URL="https://git.dvirlabs.com/dvirlabs/sandbox.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="generated-values/cloudflared-values.yaml" +GENERATED_FILE="$(pwd)/generated-values/cloudflared-values.yaml" ORIGINAL_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml" MERGED_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml" -echo "###### Debug ls -l ######" -ls -l -echo "###### Debug ls -l generated-values ######" -ls -l generated-values -echo "###### Debug pwd ######" -pwd -echo "###### Debug pwd $GENERATED_FILE ######" -pwd $GENERATED_FILE - -echo "๐Ÿ“ฆ Cloning repos..." +# === STEP 1: Clone Repos === +echo "๐Ÿ“ฆ Cloning sandbox-apps..." git clone --depth=1 "$SANDBOX_REPO_URL" "$SANDBOX_CLONE" + +echo "๐Ÿ“ฆ Cloning infra..." git clone --depth=1 "$INFRA_REPO_URL" "$INFRA_CLONE" -echo "โš™๏ธ Generating ingress entries..." +ls -l .tmp-repos/ + +# === STEP 2: Extract Generated CNAMEs from sandbox === +echo "โš™๏ธ Generating sandbox ingress list..." cat < "$GENERATED_FILE" ingress: [] EOF find "$SANDBOX_CLONE/manifests" -name cname.yaml | while read -r cname_file; do - app_dir=$(dirname "$cname_file") - app_name=$(basename "$app_dir") - namespace=$(basename "$(dirname "$app_dir")") + app_name=$(basename "$(dirname "$cname_file")") enabled=$(yq '.enabled' "$cname_file") if [[ "$enabled" == "true" ]]; then hostname=$(yq '.hostname' "$cname_file") - service="http://${app_name}.${namespace}.svc.cluster.local:80" + service="http://${app_name}.sandbox.svc.cluster.local:80" - echo "โœ… $hostname โ†’ $service" - yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\", \"namespace\": \"$namespace\"}]" -i "$GENERATED_FILE" + echo "โœ… Found $hostname โ†’ $service" + + yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$GENERATED_FILE" fi done -echo "๐Ÿ“„ Ingress generated:" +echo "๐Ÿ“„ Generated Ingress:" cat "$GENERATED_FILE" -# === Merge new ingress into cloudflare.ingress === -echo "๐Ÿ” Merging new entries..." +# === STEP 3: Merge with existing cloudflared values === +echo "๐Ÿ” Merging new entries into: $ORIGINAL_FILE" TEMP_FILE=$(mktemp) cp "$ORIGINAL_FILE" "$TEMP_FILE" -existing_json=$(yq e -o=json '.cloudflare.ingress' "$TEMP_FILE" 2>/dev/null || echo "[]") -echo "$existing_json" > /tmp/existing.json - +# 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') - namespace=$(echo "$new_entry" | jq -r '.namespace') - exists=$(jq --arg hostname "$hostname" '.[] | select(.hostname == $hostname)' /tmp/existing.json) + exists=$(yq e ".cloudflare.ingress[] | select(.hostname == \"$hostname\")" "$TEMP_FILE") if [ -z "$exists" ]; then - echo "โž• Adding $hostname" - yq e ".cloudflare.ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\", \"namespace\": \"$namespace\"}]" -i "$TEMP_FILE" + echo "โž• Adding $hostname โ†’ $service" + yq eval ".cloudflare.ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$TEMP_FILE" else echo "โš ๏ธ $hostname already exists, skipping" fi done -# === Format ingress list === -echo "๐Ÿงผ Regrouping by namespace..." +# === STEP 4: Overwrite only ingress list and preserve all other fields === +echo "๐Ÿ“ Writing final merged values.yaml" +cp "$TEMP_FILE" "$MERGED_FILE" -FINAL_FILE=$(mktemp) -yq e 'del(.cloudflare.ingress)' "$TEMP_FILE" > "$FINAL_FILE" -echo " ingress:" >> "$FINAL_FILE" - -yq e '.cloudflare.ingress' "$TEMP_FILE" | yq -o=json | jq -s 'group_by(.namespace)[]' | while read -r group; do - namespace=$(echo "$group" | jq -r '.[0].namespace') - echo " # ############ $namespace ############" >> "$FINAL_FILE" - echo "$group" | jq -c '.[]' | while read -r item; do - hostname=$(echo "$item" | jq -r '.hostname') - service=$(echo "$item" | jq -r '.service') - echo " - hostname: $hostname" >> "$FINAL_FILE" - echo " service: $service" >> "$FINAL_FILE" - done -done - -# Merge back under cloudflare.ingress -FINAL_MERGED=$(mktemp) -yq e 'del(.cloudflare.ingress)' "$TEMP_FILE" > "$FINAL_MERGED" -cat "$FINAL_FILE" >> "$FINAL_MERGED" - -cp "$FINAL_MERGED" "$MERGED_FILE" - -echo "โœ… Final values.yaml:" +echo "โœ… Final merged values.yaml:" cat "$MERGED_FILE" -# === Git commit/push === +# === STEP 5: Optional push to Git === cd "$INFRA_CLONE" git config user.name "woodpecker-bot" git config user.email "ci@dvirlabs.com" @@ -113,27 +89,35 @@ 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 & group ingress" + git commit -m "chore(cloudflared): auto-merge CNAME entries from sandbox" git push origin HEAD + echo "โœ… Changes pushed successfully." else - echo "โ„น๏ธ No changes to commit." + echo "โ„น๏ธ No changes to commit." fi -# === Cloudflare CNAME === -echo "๐ŸŒ Creating CNAME records..." +# === STEP 5: Create CNAME records in Cloudflare === +ls -l +pwd +ls -l "$GENERATED_FILE" +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}" -yq eval '.ingress' "$GENERATED_FILE" | yq -o=json | jq -c '.[]' | while read -r record; do +# Check and create each CNAME +yq eval '.ingress' "$GENERATED_FILE" | yq eval -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') if [ "$exists" -eq 0 ]; then - echo "โž• Creating $name.dvirlabs.com โ†’ $TARGET" + 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" \ @@ -145,6 +129,6 @@ yq eval '.ingress' "$GENERATED_FILE" | yq -o=json | jq -c '.[]' | while read -r \"proxied\": true }" > /dev/null else - echo "โš ๏ธ $name.dvirlabs.com already exists" + echo "โš ๏ธ CNAME for $name.dvirlabs.com already exists, skipping" fi -done +done \ No newline at end of file