forked from dekobon/bouncy-nginx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-lb-pool.sh
executable file
·37 lines (24 loc) · 1.06 KB
/
update-lb-pool.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
#!/usr/bin/env bash
die () {
echo >&2 "$@"
exit 1
}
[ "$#" -eq 3 ] || die "3 arguments required, $# provided - <prefix of container to add to pool> <port of container adding to pool> <name or id of load balancing container>"
NAME_PREFIX="$1"
PORT="$2"
LB_CONTAINER="$3"
IDS="$(docker ps --no-trunc=true -f name=$NAME_PREFIX | tail -n +2 | cut -f1 -d ' ')"
POOL=""
echo -e $IDS
while read -r id; do
ip="$(docker inspect -f '{{ .NetworkSettings.IPAddress }}' $id)"
name="$(docker inspect -f '{{ .Name }}' $id | tr -d '/')"
POOL="server $ip:$PORT;\n$POOL"
done <<< "$IDS"
echo "Wiping existing pool configuration"
docker exec -ti $LB_CONTAINER bash -c "sed -i '/### start pool ###/,/### end pool ###/{//!d}' /etc/nginx/sites-available/default"
echo "Updating with the latest configuration"
echo -e $POOL
docker exec -ti $LB_CONTAINER bash -c "sed -i '/### end pool ###/i $POOL' /etc/nginx/sites-available/default"
docker exec -ti $LB_CONTAINER bash -c 'bounce_nginx'
echo "Loadbalancer IP: $(docker inspect -f '{{ .NetworkSettings.IPAddress }}' $LB_CONTAINER)"