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: Remove some alarms to make it faster, add 2 a…
Browse files Browse the repository at this point in the history
…larms at the end that show which servers failed and which switched upstream
  • Loading branch information
kreatoo committed Jun 25, 2024
1 parent 5876a20 commit 5b4ad8b
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions caddy/caddy-lb-policy-switch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if [[ "$1" == "--version" ]] || [[ "$1" == "-v" ]]; then
exit 0
fi

trap 'rm -f /tmp/caddy-lb-policy-switch.json && exit' INT
trap 'rm -f /tmp/caddy-lb-policy-lock && exit' INT

function debug() {
if [[ "$DEBUG" -eq 1 ]]; then
Expand Down Expand Up @@ -219,7 +219,7 @@ function change_upstreams() {
else
echo "Sending request to change lb_policy to $1"
if curl -u "$USERNAME_PASSWORD" -X PATCH -H "Content-Type: application/json" -d "$REQ_TO_SEND" "$REQ_URL" 2> /tmp/caddy-lb-policy-switch-error.log; then
alarm "[Caddy lb-policy Switch] [$IDENTIFIER] [$URL_TO_FIND] [:check:] Switched lb_policy to $1"
echo "[$IDENTIFIER] [$URL_TO_FIND] Switched lb_policy to $1"
else
alarm "[Caddy lb-policy Switch] [$IDENTIFIER] [$URL_TO_FIND] [:red_circle:] Failed to switch lb_policy to $1\nError log: \`\`\`\n$(cat /tmp/caddy-lb-policy-switch-error.log)\n\`\`\`"
fi
Expand Down Expand Up @@ -317,21 +317,37 @@ function main() {
fi

if [[ "$LOOP_ORDER" == "SERVERS" ]]; then
bad_urls=()
slept_for=0
start="$(date +%s)"
for URL_TO_FIND in "${CADDY_SERVERS[@]}"; do
for URL_UP in "${CADDY_API_URLS_NEW[@]}"; do
URL="${URL_UP#*@}"
USERNAME_PASSWORD="${URL_UP%%@*}"
echo '---------------------------------'
echo "Checking '$URL_TO_FIND' on '$URL'"
start="$(date +%s)"
identify_request "$1" "$2"
end="$(date +%s)"
identify_request "$1" "$2" || bad_urls+=("$URL")
echo '---------------------------------'
read -r IDENTIFIER < <(awk -F '[@;]' '{print $2}' <<< "$URL")
verbose_alarm "$URL_TO_FIND switched to upstream $1 for $IDENTIFIER in $((end-start)) seconds, slept for ${LB_POLICY_CHANGE_SLEEP:-1} seconds"

done
sleep "${LB_POLICY_CHANGE_SLEEP:-1}"
slept_for=$((slept_for+LB_POLICY_CHANGE_SLEEP))
done
end="$(date +%s)"

CADDY_SERVERS_WO_BAD=("${CADDY_SERVERS[@]}")

for i in "${bad_urls[@]}"; do
CADDY_SERVERS_WO_BAD=("${CADDY_SERVERS_WO_BAD[@]/$i}")
done

debug "CADDY_SERVERS_WO_BAD: ${CADDY_SERVERS_WO_BAD[*]}"

CADDY_SERVERS_WO_BAD_HUMANREADABLE="${CADDY_SERVERS_WO_BAD[*]// /, }"
bad_urls_humanreadable="${bad_urls[*]// /, }"

alarm "$CADDY_SERVERS_WO_BAD_HUMANREADABLE switched to upstream $1 in $((end-start)) seconds, slept for $slept_for seconds"
alarm "$bad_urls_humanreadable failed to switch upstreams to $1"
else
for URL_UP in "${CADDY_API_URLS_NEW[@]}"; do
URL="${URL_UP#*@}"
Expand All @@ -345,13 +361,12 @@ function main() {
sleep "${LB_POLICY_CHANGE_SLEEP:-1}"
echo '---------------------------------'
done
read -r IDENTIFIER < <(awk -F '[@;]' '{print $2}' <<< "$URL")
caddy_servers_humanreadable=$(IFS=, ; echo "${CADDY_SERVERS[*]}")
verbose_alarm "$caddy_servers_humanreadable switched to upstream $1 for $IDENTIFIER in $((end-start)) seconds, slept for ${LB_POLICY_CHANGE_SLEEP:-1} seconds"
done
fi
}

rm -f /tmp/caddy-lb-policy-switch.json

if [[ "$USE_ENV" -eq 1 ]]; then
main "$1" "$2"
else
Expand All @@ -372,3 +387,4 @@ fi
end="$(date +%s)"
runtime=$((end-start))
echo "Script runtime: $runtime seconds"
rm -f /tmp/caddy-lb-policy-lock

0 comments on commit 5b4ad8b

Please sign in to comment.