apps-gitops/automation/cloudflared-sync.sh
2025-06-21 22:31:18 +03:00

106 lines
3.2 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -e
# 📦 Install required tools
apk add --no-cache git bash curl yq jq
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_TOKEN}@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"
# === STEP 1: Clone Repos ===
echo "📦 Cloning sandbox..."
git clone --depth=1 "$SANDBOX_REPO_URL" "$SANDBOX_CLONE"
echo "📦 Cloning infra..."
git clone --depth=1 "$INFRA_REPO_URL" "$INFRA_CLONE"
# === STEP 2: Generate new ingress entries ===
echo "⚙️ Generating sandbox ingress list..."
cat <<EOF > "$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")
port=$(yq '.port // 80' "$cname_file") # default to 80 if not set
service="http://${app_name}.sandbox.svc.cluster.local:$port"
echo "✅ Found $hostname$service"
# Append to ingress list
yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$GENERATED_FILE"
fi
done
echo "📄 Generated Ingress:"
cat "$GENERATED_FILE"
# === STEP 3: Merge ingress only ===
echo "🔁 Merging ingress into: $ORIGINAL_FILE"
TEMP_FILE=$(mktemp)
yq eval '.cloudflare.ingress' "$ORIGINAL_FILE" > "$TEMP_FILE"
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')
exists=$(yq e ".cloudflare.ingress[] | select(.hostname == \"$hostname\")" "$ORIGINAL_FILE")
if [ -z "$exists" ]; then
echo " Adding $hostname$service"
yq eval ". += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$TEMP_FILE"
else
echo "⚠️ $hostname already exists, skipping"
fi
done
echo "📝 Writing merged file to $MERGED_FILE"
# Merge ingress back into full file, preserving all existing fields
yq eval-all '
select(fileIndex == 0) as $orig |
select(fileIndex == 1) as $newIngress |
$orig * {
cloudflare: $orig.cloudflare * {
ingress: $newIngress
}
}
' "$ORIGINAL_FILE" "$TEMP_FILE" > "$MERGED_FILE"
echo "✅ Final merged values.yaml:"
cat "$MERGED_FILE"
# === STEP 4: Git Push ===
echo "📤 Pushing updated values.yaml to infra repo..."
cd "$INFRA_CLONE"
git config user.name "woodpecker-bot"
git config user.email "ci@dvirlabs.com"
git remote set-url origin "https://${GIT_TOKEN}@git.dvirlabs.com/dvirlabs/infra.git"
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 push origin HEAD
echo "✅ Changes pushed successfully."
else
echo " No changes to commit."
fi