From c38a405cc87813427c70efbf2b5ed0f5ccdc09ff Mon Sep 17 00:00:00 2001 From: "Kreato (Work)" Date: Tue, 11 Jun 2024 15:10:50 +0300 Subject: [PATCH] caddy-lb-policy-switch: Add docs and adjust verbose mode --- .bin/caddy-lb-policy-switch.conf | 37 ++++++++++++++++++++++++++++++++ caddy/caddy-lb-policy-switch.sh | 14 ++++++++---- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/.bin/caddy-lb-policy-switch.conf b/.bin/caddy-lb-policy-switch.conf index d9a21e5..aa67d54 100644 --- a/.bin/caddy-lb-policy-switch.conf +++ b/.bin/caddy-lb-policy-switch.conf @@ -1,16 +1,53 @@ +# +# Caddy LB-Policy Switcher configuration +# + + +# Caddy API URLs +# Format: "username:password@host:port" +# Example: "testusername:testpassword@localhost:2019" CADDY_API_URLS=( "testusername:testpassword@localhost:2019" "testusername:testpassword@localhost:2019" ) +# Caddy server/vHost names +# The script will switch the LB policy for these servers +# Format: "vhost_name" +# Example: ":80" or "test.com" CADDY_SERVERS=( "test.com" "test2.com" ) +# Caddy whoami LB URLs +# +# The script will adjust the priority of the CADDY_API_URLS according to the first that respond +# to these urls. +# +# The URL must use https://github.com/traefik/whoami and a hostname that matches the example below +# Example output: +# Hostname test-test2-test3 will result in https://api.test3.test2.test.biz.tr +# +# Format: "url" +# Example: "whoami.test.com" CADDY_LB_URLS=( "whoami.test.com" "whoami.test2.com" ) +# Threshold to exit after a certain number of no changes. NO_CHANGES_EXIT_THRESHOLD=1 + +# Override config even if it is the same +SERVER_OVERRIDE_CONFIG=1 + +# Loop order +# SERVERS: Prioritize servers +# API_URLS: Prioritize API URLs +# Default: "API_URLS" +LOOP_ORDER="API_URLS" + +# Disables dynamic API URLs +# If set to 1, the script will not use the CADDY_LB_URLS to adjust the priority of the CADDY_API_URLS +NO_DYNAMIC_API_URLS=0 diff --git a/caddy/caddy-lb-policy-switch.sh b/caddy/caddy-lb-policy-switch.sh index 5fd8473..0b21af2 100644 --- a/caddy/caddy-lb-policy-switch.sh +++ b/caddy/caddy-lb-policy-switch.sh @@ -12,6 +12,12 @@ function debug() { fi } +function verbose() { + if [[ "$VERBOSE" -eq 1 ]]; then + echo "$1" + fi +} + function hostname_to_url() { local hostname="$1" @@ -183,13 +189,13 @@ function adjust_api_urls() { URL="${URL_UP#*@}" USERNAME_PASSWORD="${URL_UP%%@*}" - - debug "LB $i" + + verbose "LB $i" url_new="$(hostname_to_url "$(curl -s "$i" | grep "Hostname:" | awk '{print $2}')")" if [[ "$url_new" == "$URL" ]]; then debug "$url_new is the same as URL, adding to CADDY_API_URLS_NEW" CADDY_API_URLS_NEW+=("$URL_UP") # Make sure the ones that respond first are added first - debug "CADDY_API_URLS_NEW: ${CADDY_API_URLS_NEW[*]}" + verbose "CADDY_API_URLS_NEW: ${CADDY_API_URLS_NEW[*]}" fi done @@ -261,7 +267,7 @@ for conf in /etc/glb/*.conf; do fi - for i in CADDY_API_URLS CADDY_API_URLS_NEW CADDY_SERVERS ALARM_BOT_USER_EMAILS ALARM_WEBHOOK_URLS ALARM_BOT_EMAIL ALARM_BOT_API_KEY ALARM_BOT_API_URL ALARM_WEBHOOK_URL SEND_ALARM SEND_DM_ALARM NO_CHANGES_EXIT_THRESHOLD CADDY_LB_URLS NO_DYNAMIC_API_URLS SERVER_OVERRIDE_CONFIG LOOP_ORDER; do + for i in CADDY_API_URLS CADDY_API_URLS_NEW CADDY_SERVERS ALARM_BOT_USER_EMAILS ALARM_WEBHOOK_URLS ALARM_BOT_EMAIL ALARM_BOT_API_KEY ALARM_BOT_API_URL ALARM_WEBHOOK_URL SEND_ALARM SEND_DM_ALARM NO_CHANGES_EXIT_THRESHOLD CADDY_LB_URLS NO_DYNAMIC_API_URLS SERVER_OVERRIDE_CONFIG LOOP_ORDER VERBOSE DEBUG; do unset $i done