-
Notifications
You must be signed in to change notification settings - Fork 1
/
start-haproxy.sh
executable file
·68 lines (54 loc) · 1.88 KB
/
start-haproxy.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
#!/bin/sh
set -ea
[ "${VERBOSE}" = 'true' ] && set -x
CERTS=${CERTS:-/certs}
CERT_CHAIN_PATH=${CERT_CHAIN_PATH:-/certs/export/chain.pem}
function update_ca_certificates() {
# only set CA bundle if using private certificate chain
if [[ -e "${CERTS}/ca-bundle.pem" ]]; then
if [[ "$(readlink -f "${CERTS}/export/chain.pem")" =~ \/private\/ ]]; then
mkdir -p /usr/local/share/ca-certificates
cat < "${CERTS}/root-ca.pem" > /usr/local/share/ca-certificates/balenaRootCA.crt
cat < "${CERTS}/server-ca.pem" > /usr/local/share/ca-certificates/balenaServerCA.crt
# shellcheck disable=SC2034
CURL_CA_BUNDLE=${CURL_CA_BUNDLE:-${CERTS}/ca-bundle.pem}
else
rm -f /usr/local/share/ca-certificates/balena*CA.crt
unset CURL_CA_BUNDLE
fi
update-ca-certificates
fi
}
update_ca_certificates
if [ -n "${HAPROXY_CRT}" ] && [ -n "${HAPROXY_KEY}" ]; then
tmpcfg="$(mktemp)"
echo "Assembling certificate chain..."
mkdir -p "$(dirname "${CERT_CHAIN_PATH}")"
echo "${HAPROXY_CRT}" | base64 -d > "${tmpcfg}"
# certificates issued by private CA
if [ -n "${ROOT_CA}" ]; then
echo "${ROOT_CA}" | base64 -d >> "${tmpcfg}"
fi
echo "${HAPROXY_KEY}" | base64 -d >> "${tmpcfg}"
rm -f "${CERT_CHAIN_PATH}"
cat < "${tmpcfg}" > "${CERT_CHAIN_PATH}"
rm -f "${tmpcfg}"
fi
sudo -Eu haproxy haproxy -f /usr/local/etc/haproxy/haproxy.cfg -W &
HAPROXY_PID=$!
echo "haproxy started with pid "$HAPROXY_PID
# Trap and forward USR1 ( graceful stop ) to haproxy
_usr1() {
echo "Caught SIGUSR1 signal!"
kill -USR1 "$HAPROXY_PID" 2>/dev/null
}
trap _usr1 USR1
# Trap and forward TERM ( hard stop ) to haproxy
_term() {
echo "Caught SIGTERM signal!"
kill -TERM "$HAPROXY_PID" 2>/dev/null
}
trap _term TERM
sudo -Eu haproxy /monitor_certs.sh $HAPROXY_PID &
# Wait for haproxy to process its exit signal
wait "$HAPROXY_PID"