Skip to content

Commit

Permalink
Merge pull request #66 from floushee/main
Browse files Browse the repository at this point in the history
Added support for Slack / MS Teams webhooks
  • Loading branch information
willfarrell authored Oct 5, 2021
2 parents 452613d + 7f8ed3a commit 3fb6901
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ ENV AUTOHEAL_CONTAINER_LABEL=autoheal \
AUTOHEAL_INTERVAL=5 \
AUTOHEAL_DEFAULT_STOP_TIMEOUT=10 \
DOCKER_SOCK=/var/run/docker.sock \
CURL_TIMEOUT=30
CURL_TIMEOUT=30 \
WEBHOOK_URL=""

HEALTHCHECK --interval=5s CMD pgrep -f autoheal || exit 1

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ AUTOHEAL_START_PERIOD=0 # wait 0 seconds before first health check
AUTOHEAL_DEFAULT_STOP_TIMEOUT=10 # Docker waits max 10 seconds (the Docker default) for a container to stop before killing during restarts (container overridable via label, see below)
DOCKER_SOCK=/var/run/docker.sock # Unix socket for curl requests to Docker API
CURL_TIMEOUT=30 # --max-time seconds for curl requests to Docker API
WEBHOOK_URL="" # post message to the webhook if a container was restarted (or restart failed)
```

### Optional Container Labels
Expand Down
24 changes: 24 additions & 0 deletions docker-entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set -o pipefail
DOCKER_SOCK=${DOCKER_SOCK:-/var/run/docker.sock}
UNIX_SOCK=""
CURL_TIMEOUT=${CURL_TIMEOUT:-30}
WEBHOOK_URL=${WEBHOOK_URL:-""}

# only use unix domain socket if no TCP endpoint is defined
case "${DOCKER_SOCK}" in
Expand Down Expand Up @@ -56,6 +57,26 @@ restart_container() {
docker_curl -f -X POST "${HTTP_ENDPOINT}/containers/${container_id}/restart?t=${timeout}"
}

notify_webhook() {
local text="$@"

if [ -n "$WEBHOOK_URL" ]
then
# execute webhook requests as background process to prevent healer from blocking
curl -X POST -H "Content-type: application/json" -d "$(generate_webhook_payload $text)" $WEBHOOK_URL
fi
}

# https://towardsdatascience.com/proper-ways-to-pass-environment-variables-in-json-for-curl-post-f797d2698bf3
generate_webhook_payload() {
local text="$@"
cat <<EOF
{
"text":"$text"
}
EOF
}

# SIGTERM-handler
term_handler() {
exit 143 # 128 + 15 -- SIGTERM
Expand Down Expand Up @@ -94,6 +115,9 @@ if [ "$1" = "autoheal" ] && [ -e "$DOCKER_SOCK" ];then
if ! restart_container "$CONTAINER_ID" "$TIMEOUT"
then
echo "$DATE Restarting container $CONTAINER_SHORT_ID failed" >&2
notify_webhook "Container ${CONTAINER_NAME:1} (${CONTAINER_SHORT_ID}) found to be unhealthy. Failed to restart the container!" &
else
notify_webhook "Container ${CONTAINER_NAME:1} (${CONTAINER_SHORT_ID}) found to be unhealthy. Successfully restarted the container!" &
fi
fi
done
Expand Down

0 comments on commit 3fb6901

Please sign in to comment.