Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
caddy-lb-policy-switch: Add docs and adjust verbose mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kreatoo committed Jun 11, 2024
1 parent a356fd3 commit c38a405
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
37 changes: 37 additions & 0 deletions .bin/caddy-lb-policy-switch.conf
Original file line number Diff line number Diff line change
@@ -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
14 changes: 10 additions & 4 deletions caddy/caddy-lb-policy-switch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ function debug() {
fi
}

function verbose() {
if [[ "$VERBOSE" -eq 1 ]]; then
echo "$1"
fi
}

function hostname_to_url() {
local hostname="$1"

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit c38a405

Please sign in to comment.