From 4dabfc4e2802268f351a1a0fb3d9d4a49b790a02 Mon Sep 17 00:00:00 2001 From: dvirlabs Date: Sun, 22 Jun 2025 18:09:20 +0300 Subject: [PATCH] Test the script --- automation/cloudflared-sync.sh | 89 +++++++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 18 deletions(-) diff --git a/automation/cloudflared-sync.sh b/automation/cloudflared-sync.sh index 9ab171b..3dcf23f 100644 --- a/automation/cloudflared-sync.sh +++ b/automation/cloudflared-sync.sh @@ -1,43 +1,96 @@ -# CONFIG -ORIGINAL_FILE="../infra/cloudflared/values.yaml" -MERGED_FILE="../infra/cloudflared/values.yaml" +#!/bin/bash +set -e + +# 📦 Install required tools (run via Woodpecker .woodpecker.yml) +# Ensure apk installs: yq, jq, git, bash, curl + +echo "🔍 Scanning for apps with cname.yaml..." + +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.dvirlabs.com/dvirlabs/infra.git" +SANDBOX_CLONE=".tmp-repos/sandbox" +INFRA_CLONE=".tmp-repos/infra" GENERATED_FILE="generated-values/cloudflared-values.yaml" +ORIGINAL_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml" +MERGED_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml" -apt install -y yq jq +# === 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" + +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_name=$(basename "$(dirname "$cname_file")") + enabled=$(yq '.enabled' "$cname_file") + + if [[ "$enabled" == "true" ]]; then + hostname=$(yq '.hostname' "$cname_file") + service="http://${app_name}.sandbox.svc.cluster.local:80" + + echo "✅ Found $hostname → $service" + + # Append new entry to generated ingress list + yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$GENERATED_FILE" + fi +done + +echo "📄 Generated Ingress:" +cat "$GENERATED_FILE" + +# === STEP 3: Merge with existing cloudflared values === echo "🔁 Merging new entries into: $ORIGINAL_FILE" -# Extract original ingress list -ORIGINAL_INGRESS=$(yq eval '.cloudflare.ingress' "$ORIGINAL_FILE") TEMP_FILE=$(mktemp) -# Start a fresh ingress list -echo "$ORIGINAL_INGRESS" | yq eval '.' - > "$TEMP_FILE" +# Copy original ingress list +yq eval '.cloudflare.ingress' "$ORIGINAL_FILE" > "$TEMP_FILE" -# Loop over new entries -yq eval '.ingress[]' "$GENERATED_FILE" | yq eval -o=json '.' - | jq -c '.[]' | while read -r new_entry; do +# Append new unique entries (skip existing hostnames) +yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' - | jq -c '.[]' | while read -r new_entry; do hostname=$(echo "$new_entry" | jq -r '.hostname') service=$(echo "$new_entry" | jq -r '.service') - # Check if hostname already exists exists=$(yq e ".cloudflare.ingress[] | select(.hostname == \"$hostname\")" "$ORIGINAL_FILE") if [ -z "$exists" ]; then echo "➕ Adding $hostname → $service" - # Append new entry yq eval ". += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$TEMP_FILE" else echo "⚠️ $hostname already exists, skipping" fi done -# Rebuild final values.yaml with updated ingress +# === STEP 4: Write merged file === yq eval ' .cloudflare.ingress = load("'"$TEMP_FILE"'") | - .cloudflare.tunnelName = strenv(TUNNEL_NAME) | - .cloudflare.enableWarp = false | - .cloudflare.secretName = "cloudflared-creds" | - .cloudflared = load("'"$ORIGINAL_FILE"'") | .cloudflared + .cloudflare.tunnelName = .cloudflare.tunnelName | + .cloudflare.enableWarp = .cloudflare.enableWarp | + .cloudflare.secretName = .cloudflare.secretName | + .cloudflared = .cloudflared ' "$ORIGINAL_FILE" > "$MERGED_FILE" -echo "✅ Patched values.yaml saved to: $MERGED_FILE" \ No newline at end of file +echo "✅ Final merged values.yaml:" +cat "$MERGED_FILE" + +# === STEP 5: Optional push to Git === +# cd "$INFRA_CLONE" +# git config user.name "woodpecker-bot" +# git config user.email "ci@dvirlabs.com" +# git add manifests/cloudflared/values.yaml +# git commit -m "chore(cloudflared): auto-add ingress from sandbox" +# git push