Skip to content

Instantly share code, notes, and snippets.

@aleskiontherun
Created August 28, 2019 14:49
Show Gist options
  • Save aleskiontherun/a8be18177188b96c5d3e0eba4f5ff32b to your computer and use it in GitHub Desktop.
Save aleskiontherun/a8be18177188b96c5d3e0eba4f5ff32b to your computer and use it in GitHub Desktop.
Register AWS EC2 instance in HAProxy.
#!/bin/bash
set -e
region=eu-central-1
haproxy_url=http://haproxy.internal:5555
haproxy_username=admin
haproxy_password=password
cluster_size=3
ACTION="$1"
BACKEND_NAME="$2"
BACKEND_PORT="$3"
VER_RETRIES=${cluster_size}
RETRIES=${cluster_size}
RANDOM=$$
SLEEP_MIN=1
SLEEP_MAX=${cluster_size}
PRIVATE_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/[a-z]$//')
INSTANCE_NAME=$(aws ec2 describe-tags --region="$REGION" --filters "Name=resource-id,Values=$INSTANCE_ID" Name=key,Values=Name --query Tags[].Value --output text)
haproxyConfVersion() {
VER_RES=$(curl -s -w '\n%{http_code}' -X GET --user "${haproxy_username}:${haproxy_password}" "${haproxy_url}/v1/services/haproxy/configuration/servers?backend=$BACKEND_NAME")
VER_RES_CODE=$(echo "$VER_RES" | tail -1)
if [ "$VER_RES_CODE" != "200" ]; then
if [ $VER_RETRIES -gt 0 ]; then
VER_RETRIES=$((VER_RETRIES - 1))
echo >&2 'WARNING: Failed to fetch HAProxy configuration version. Retrying.'
sleep $((RANDOM % SLEEP_MAX + SLEEP_MIN))
haproxyConfVersion
return $?
fi
echo >&2 'ERROR: Failed to fetch HAProxy configuration version.'
return 2
fi
echo "$VER_RES" | head -1 | jq -r '._version'
return 0
}
haproxyServerExists() {
RESPONSE_CODE=$(
curl -s -o /dev/null -w %{http_code} -X GET \
--user "${haproxy_username}:${haproxy_password}" \
"${haproxy_url}/v1/services/haproxy/configuration/servers/$INSTANCE_NAME?backend=$BACKEND_NAME"
)
if [ "$RESPONSE_CODE" = "200" ]; then
echo 'true'
else
echo 'false'
fi
}
haproxyRegister() {
HAPROXY_BE_VERSION=$(haproxyConfVersion)
if [ "$HAPROXY_BE_VERSION" = "null" ] || [ "$HAPROXY_BE_VERSION" = "" ]; then
echo >&2 'ERROR: HAProxy configuration version is invalid.'
exit 1
fi
RESPONSE_CODE=$(
curl -s -o /dev/null -w %{http_code} -X POST \
--user "${haproxy_username}:${haproxy_password}" \
-H "Content-Type: application/json" \
-d "{\"address\":\"$PRIVATE_IP\",\"check\":\"enabled\",\"cookie\":\"A\",\"name\":\"$INSTANCE_NAME\",\"port\":$BACKEND_PORT}" \
"${haproxy_url}/v1/services/haproxy/configuration/servers?force_reload=true&version=$HAPROXY_BE_VERSION&backend=$BACKEND_NAME"
)
if [ "$RESPONSE_CODE" != "201" ] || [ "$(haproxyServerExists)" = "false" ]; then
if [ $RETRIES -gt 0 ]; then
RETRIES=$((RETRIES - 1))
echo >&2 'WARNING: Failed to register the instance in HAProxy. Retrying.'
sleep $((RANDOM % SLEEP_MAX + SLEEP_MIN))
haproxyRegister
return $?
fi
echo >&2 'ERROR: Failed to register the instance in HAProxy.'
return 2
fi
return 0
}
haproxyUnregister() {
HAPROXY_BE_VERSION=$(haproxyConfVersion)
if [ "$HAPROXY_BE_VERSION" = "null" ] || [ "$HAPROXY_BE_VERSION" = "" ]; then
echo >&2 'ERROR: HAProxy configuration version is invalid.'
exit 1
fi
RESPONSE_CODE=$(
curl -s -o /dev/null -w %{http_code} -X DELETE \
--user "${haproxy_username}:${haproxy_password}" \
"${haproxy_url}/v1/services/haproxy/configuration/servers/$INSTANCE_NAME?force_reload=true&version=$HAPROXY_BE_VERSION&backend=$BACKEND_NAME"
)
if [ "$RESPONSE_CODE" != "204" ] || [ "$(haproxyServerExists)" = "true" ]; then
if [ $RETRIES -gt 0 ]; then
RETRIES=$((RETRIES - 1))
echo >&2 'WARNING: Failed to unregister the instance in HAProxy. Retrying.'
sleep $((RANDOM % SLEEP_MAX + SLEEP_MIN))
haproxyUnregister
return $?
fi
echo >&2 'ERROR: Failed to unregister the instance in HAProxy.'
return 2
fi
return 0
}
if [ "$ACTION" = "unregister" ]; then
haproxyUnregister && echo "Instance $INSTANCE_NAME unregistered from $BACKEND_NAME." || (
echo "ERROR: Failed to unregister instance $INSTANCE_NAME from $BACKEND_NAME."
exit 1
)
else
haproxyRegister && echo "Instance $INSTANCE_NAME registered in $BACKEND_NAME." || (
echo "ERROR: Failed to register instance $INSTANCE_NAME in $BACKEND_NAME."
exit 1
)
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment