This repository has been archived by the owner on Apr 22, 2021. It is now read-only.
forked from willfarrell/docker-autoheal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-entrypoint
executable file
·140 lines (109 loc) · 3.77 KB
/
docker-entrypoint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env sh
set -e
set -o pipefail
DOCKER_SOCK=${DOCKER_SOCK:-/var/run/docker.sock}
CURL_TIMEOUT=${CURL_TIMEOUT:-30}
MAIL_ENABLED=${MAIL_ENABLED:-false}
MAILHUB=${MAILHUB}
ROOT=${ROOT}
SUBJECT=${SUBJECT:-[AUTOHEAL]}
TO=${TO}
CC=${CC}
REWRITE_DOMAIN=${REWRITE_DOMAIN}
FROMLINEOVERRIDE=${FROMLINEOVERRIDE:-yes}
HOSTNAME=${HOSTNAME}
TLS_CA_FILE=${TLS_CA_FILE:-/etc/pki/tls/certs/ca-bundle.crt}
USETLS=${USETLS:-yes}
USESTARTTLS=${USESTARTTLS:-yes}
AUTHUSER=${AUTHUSER}
AUTHPASS=${AUTHPASS}
# SIGTERM-handler
term_handler() {
exit 143; # 128 + 15 -- SIGTERM
}
docker_curl() {
curl --max-time "${CURL_TIMEOUT}" --no-buffer -s --unix-socket "${DOCKER_SOCK}" "$@" || return 1
return 0
}
send_mail () {
if [ "${MAIL_ENABLED}" = 'true' ]; then
sendmail -vit << MAIL_END
Subject: $SUBJECT
To: $TO
CC: $CC
Hostname: $HOSTNAME
$1
MAIL_END
fi
}
trap 'kill ${!}; term_handler' SIGTERM
if [ "$1" = 'healthcheck' ]; then
docker_curl --fail http://localhost/_ping
exit $?
fi
if [ "${MAIL_ENABLED}" = 'true' ]; then
cat > /etc/ssmtp/ssmtp.conf << EOF
# The user that gets all the mails (UID < 1000, usually the admin)
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=${ROOT}
# The mail server (where the mail is sent to),
# For google both port 465 or 587 should be acceptable
# See also https://support.google.com/mail/answer/78799
mailhub=${MAILHUB}
# The address where the mail appears to come from for user authentication.
rewriteDomain=${REWRITE_DOMAIN}
# The full hostname. Must be correctly formed, fully qualified domain name or GMail will reject connection.
hostname=${HOSTNAME}
# Use SSL/TLS before starting negotiation
TLS_CA_FILE=${TLS_CA_FILE}
UseTLS=${USETLS}
UseSTARTTLS=${USESTARTTLS}
# Username/Password
AuthUser=${AUTHUSER}
AuthPass=${AUTHPASS}
AuthMethod=LOGIN
# Email 'From header's can override the default domain?
# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=${FROMLINEOVERRIDE}
EOF
send_mail "Autoheal is running and checking your containers health!"
fi
if [ "$1" = 'autoheal' ] && [ -e ${DOCKER_SOCK} ]; then
# https://docs.docker.com/engine/api/v1.25/
# Set container selector
if [ "$AUTOHEAL_CONTAINER_LABEL" == "all" ]; then
labelFilter=""
else
labelFilter=",\"label\":\[\"${AUTOHEAL_CONTAINER_LABEL:=autoheal}=true\"\]"
fi
AUTOHEAL_START_PERIOD=${AUTOHEAL_START_PERIOD:=0}
echo "Monitoring containers for unhealthy status in ${AUTOHEAL_START_PERIOD} second(s)"
sleep ${AUTOHEAL_START_PERIOD}
while true; do
sleep ${AUTOHEAL_INTERVAL:=5}
apiUrl="http://localhost/containers/json?filters=\{\"health\":\[\"unhealthy\"\]${labelFilter}\}"
stopTimeout=".Labels[\"autoheal.stop.timeout\"] // ${AUTOHEAL_DEFAULT_STOP_TIMEOUT:-10}"
docker_curl "$apiUrl" | \
jq -r "foreach .[] as \$CONTAINER([];[]; \$CONTAINER | .Id, .Names[0], $stopTimeout )" | \
while read -r CONTAINER_ID && read -r CONTAINER_NAME && read -r TIMEOUT; do
CONTAINER_SHORT_ID=${CONTAINER_ID:0:12}
DATE=$(date +%d-%m-%Y" "%H:%M:%S)
if [ "null" = "$CONTAINER_NAME" ]; then
MSG="$DATE Container name of ($CONTAINER_SHORT_ID) is null, which implies container does not exist - don't restart"
echo "$MSG"
send_mail "$MSG"
else
MSG="$DATE Container ${CONTAINER_NAME} ($CONTAINER_SHORT_ID) found to be unhealthy - Restarting container now with ${TIMEOUT}s timeout"
echo "$MSG"
send_mail "$MSG"
docker_curl -f -XPOST "http://localhost/containers/${CONTAINER_ID}/restart?t=${TIMEOUT}" \
|| echo "$DATE Restarting container $CONTAINER_SHORT_ID failed"
fi
done
done
else
exec "$@"
fi