-
Notifications
You must be signed in to change notification settings - Fork 3
/
update-haproxy.sh
executable file
·46 lines (33 loc) · 1.35 KB
/
update-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
#!/bin/bash
ETCD_PEER="172.17.42.1:4001"
HAPROXY_CONFIG="/etc/haproxy/haproxy.cfg"
function update {
HOST_NAME=$(echo $ETCD_WATCH_KEY | cut -d/ -f4)
HOST_IP=$(echo $ETCD_WATCH_VALUE | tr -d '"{} ' | cut -d, -f1 | cut -d: -f2)
HOST_PORT=$(echo $ETCD_WATCH_VALUE | tr -d '"{} ' | cut -d, -f2 | cut -d: -f2)
HAPROXY_BACKEND_SERVER_MAX_CONNECTIONS=$(etcdctl --peers ${ETCD_PEER} get /config/HAPROXY_BACKEND_SERVER_MAX_CONNECTIONS | grep -v "Error: 100: Key not found" || echo "32")
if grep -q "server ${HOST_NAME}" $HAPROXY_CONFIG; then
echo "Updating host ${HOST_NAME}"
sed -i -e "s/ server ${HOST_NAME}.*$/ server ${HOST_NAME} ${HOST_IP}:${HOST_PORT} maxconn ${HAPROXY_BACKEND_SERVER_MAX_CONNECTIONS} check/g" $HAPROXY_CONFIG
else
echo "Adding host ${HOST_NAME}"
echo " server ${HOST_NAME} ${HOST_IP}:${HOST_PORT} maxconn ${HAPROXY_BACKEND_SERVER_MAX_CONNECTIONS} check" >> $HAPROXY_CONFIG
fi
}
function remove {
HOST_NAME=$(echo $ETCD_WATCH_KEY | cut -d/ -f4)
echo "Removing host ${HOST_NAME}"
sed -i'' "/server ${HOST_NAME} /d" $HAPROXY_CONFIG
}
case $ETCD_WATCH_ACTION in
compareAndSwap | update | set)
update
;;
delete | expire)
remove
;;
*)
echo "Something went wrong..."
exit 1
esac
exec /usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)