This repository has been archived by the owner on Feb 25, 2025. It is now read-only.
forked from colinmollenhour/haproxy-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-entrypoint.sh
executable file
·106 lines (91 loc) · 2.59 KB
/
docker-entrypoint.sh
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
#!/bin/bash
# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
set -- haproxy "$@"
fi
if haproxy -v | grep -qF 'version 1.7.'; then
KILLSIGNAL=HUP
if [ "$1" = 'haproxy' ]; then
# if the user wants "haproxy", let's use "haproxy-systemd-wrapper" instead so we can have proper reloadability implemented by upstream
shift # "haproxy"
rm -f /run/haproxy.pid
set -- "$(which haproxy-systemd-wrapper)" -p /run/haproxy.pid "$@"
fi
else
KILLSIGNAL=USR2
if [ "$1" = 'haproxy' ]; then
shift # "haproxy"
# if the user wants "haproxy", let's add a couple useful flags
# -W -- "master-worker mode" (similar to the old "haproxy-systemd-wrapper"; allows for reload via "SIGUSR2")
# -db -- disables background mode
set -- haproxy -W -db "$@"
fi
fi
PREFIX=docker-entrypoint.sh
TEMPLATE=${TEMPLATE:-/etc/haproxy.cfg.tpl}
UPDATE_FREQUENCY=${UPDATE_FREQUENCY:-30}
# enable job control, start processes
set -m
# Set 'TRACE=y' environment variable to see detailed output for debugging
[ "$TRACE" = "y" ] && set -x
if [ -z "$SERVICE_HOSTNAME" ]; then
echo $PREFIX: The SERVICE_HOSTNAME environment variable is not defined.
exit 1
fi
# Run init script if it exists
if [[ -f /docker-entrypoint-init.sh ]]; then
source /docker-entrypoint-init.sh
fi
# Render config template once before starting HAProxy
/render_cfg.sh $SERVICE_HOSTNAME $TEMPLATE
if [ $? -eq 1 ]; then
sleep 5
/render_cfg.sh $SERVICE_HOSTNAME $TEMPLATE
if [ $? -eq 1 ]; then
echo $PREFIX: Could not render ${TEMPLATE%.tpl}. Refusing to start.
exit 1
fi
fi
# Run rsyslogd if enabled
RSYSLOG_PID=0
if [ "$RSYSLOG" != "n" ]; then
rm -f /var/run/rsyslog.pid
rsyslogd -n -f /etc/rsyslogd.conf &
RSYSLOGD_PID=$!
fi
# Run HAProxy (haproxy-systemd-wrapper) and wait for exit
"$@" &
WRAPPER_PID=$!
# Trap Shutdown
function shutdown () {
echo $PREFIX: Shutting down...
kill -TERM $WRAPPER_PID
}
trap shutdown TERM INT
# Trap Reload (HUP or USR2)
function reload () {
if haproxy -c -f ${TEMPLATE%.tpl} >/dev/null; then
echo $PREFIX: Reloading config...
kill -$KILLSIGNAL $WRAPPER_PID
else
echo $PREFIX: Config test failed, will not reload haproxy.
fi
}
trap reload HUP USR2
# Run loop to update config template
while sleep $UPDATE_FREQUENCY; do
/render_cfg.sh $SERVICE_HOSTNAME $TEMPLATE
# Exit code 0 means template was updated, 1 means error and 2 means not updated
RENDER_RESULT=$?
if [ $RENDER_RESULT -eq 0 ]; then
reload
elif [ $RENDER_RESULT -eq 1 ]; then
echo $PREFIX: Error updating config template!
fi
done &
RENDER_PID=$!
wait $WRAPPER_PID
RC=$?
kill $RENDER_PID
[ "$RSYSLOG_PID" -ne 0 ] && kill $RSYSLOG_PID
exit $RC