-
Notifications
You must be signed in to change notification settings - Fork 8
/
docker-entrypoint.sh
executable file
·71 lines (58 loc) · 1.88 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
#!/bin/sh -eu
cat_file_with_header () {
local filename="${1}"
echo "==> ${filename} <=="
cat "${filename}" || true
echo
}
# cleanup when the container is stopped or ucarp exits
cleanup () {
kill -USR2 $(pidof ucarp)
sleep 1
$KARP_DOWNSCRIPT $KARP_INTERFACE $KARP_VIRTUAL_IP $KARP_SUBNET
}
trap "cleanup" SIGINT
trap "cleanup" SIGTERM
# set the debug env var to anything for debug logging
if [ ! -z "${KARP_DEBUG}" ] ; then
set -x
cat_file_with_header /etc/conf.d/ucarp
cat_file_with_header /etc/init.d/ucarp
cat_file_with_header "${KARP_DOWNSCRIPT}"
cat_file_with_header "${KARP_UPSCRIPT}"
/usr/sbin/ucarp --help
fi
# if no explicit host interface is set, we determine it from the default route
if [ -z "$KARP_INTERFACE" ] ; then
KARP_INTERFACE="$(ip r | awk '/^default / { for(i=1; i<=NF; i++) if ($i == "dev") print $(i+1) }')"
if [ -z "$KARP_INTERFACE" ]; then
echo "Could not infer host interface"
exit 1
fi
echo "Auto host interface: ${KARP_INTERFACE}"
fi
# if no explicit host ip is set, we determine it from the interface
if [ -z "${KARP_HOST_IP}" ] ; then
KARP_HOST_IP=$(ip addr show ${KARP_INTERFACE} | grep inet | grep -v secondary | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}" | head -n 1)
if [ -z "$KARP_HOST_IP" ]; then
echo "Could not infer host ip"
exit 1
fi
echo "Auto host IP: ${KARP_HOST_IP}"
fi
# start up the service and put it into background
/usr/sbin/ucarp \
--interface=${KARP_INTERFACE} \
--srcip=${KARP_HOST_IP} \
--vhid=${KARP_SERVER_ID} \
--pass=${KARP_PASSWORD} \
--addr=${KARP_VIRTUAL_IP} \
--upscript=${KARP_UPSCRIPT} \
--downscript=${KARP_DOWNSCRIPT} \
--xparam=${KARP_SUBNET} \
${KARP_EXTRA_FLAGS} &
# wait for the last process sent to background to finish (ucarp)
wait $!
# cleanup even if the process exits by itself
# otherwise, the traps handle container stops initiated by the user
cleanup