This repository has been archived by the owner on Aug 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathhaproxy-reload-ec2.sh
80 lines (57 loc) · 2.25 KB
/
haproxy-reload-ec2.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
#!/bin/dash
# usage: haproxy-reload.sh [/path/to/haproxy.cfg [/path/to/haproxy.cfg.in]]
# script to be run periodically that regenerates /etc/haproxy/haproxy.cfg from our backend bitcoind servers.
HAPROXY_CFG="/etc/haproxy/haproxy.cfg"
HAPROXY_CFG_IN="/etc/haproxy/haproxy.cfg.in"
if [ "$#" -ge 1 ]; then
HAPROXY_CFG=$1
shift 1
fi
if [ "$#" -ge 1 ]; then
HAPROXY_CFG_IN=$1
shift 1
fi
EC2_TMPFILE=/tmp/bitcoind-vms.json
trap "rm -f \"$EC2_TMPFILE\"" 0 1 2 3 15
# print out the "server BACKEND_NAME BACKEND_IP" lines of an haproxy config file,
# by querying Amazon EC2 for the VMs and using the public IP address for the BACKEND_IP
# and the instance name for the BACKEND_NAME.
# $1 Temporary file to store the EC2 json
print_backends() {
local ec2_tmpfile
ec2_tmpfile=$1
# find all bitcoind HAProxy backends that are running
aws ec2 describe-instances --filters "Name=tag-key,Values=haproxy" "Name=tag-value,Values=1" "Name=instance-state-name,Values=running" > $ec2_tmpfile
# get their public IP addresses and instance names...
cat $ec2_tmpfile | jq '.Reservations[].Instances[].PublicIpAddress' | \
while read public_ip; do
if [ "$public_ip" = "null" ]; then
continue
fi
# strip quotes
public_ip=${public_ip##\"}
public_ip=${public_ip%%\"}
# find associated instance name
jq_instance=$(printf '.Reservations[].Instances[] | select(.PublicIpAddress=="%s").Tags[] | select(.Key == "Name")["Value"]' $public_ip)
instance_name=$(cat $EC2_TMPFILE | jq "$jq_instance")
if [ -z "$instance_name" -o "$instance_name" = "null" ]; then
echo >&2 "WARN: failed to look up instance name of $public_ip"
continue
fi
# strip quotes
instance_name=${instance_name##\"}
instance_name=${instance_name%%\"}
echo -n " server $instance_name $public_ip\\\n"
done
rm -f "$ec2_tmpfile"
}
# what's the original contents?
sha256_before=$(sha256sum "$HAPROXY_CFG" || echo "")
cat "$HAPROXY_CFG_IN" | \
sed -e "s~@HAPROXY_BACKENDS@~$(print_backends $EC2_TMPFILE)~g;" > "$HAPROXY_CFG"
sha256_after=$(sha256sum "$HAPROXY_CFG")
if [ "$sha256_before" != "$sha256_after" ]; then
# signal haproxy to reload
/etc/init.d/haproxy reload
fi
exit 0