-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlb-reload
141 lines (127 loc) · 5.45 KB
/
lb-reload
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#! /bin/sh
SPLIT_TOKEN=${SPLIT_TOKEN:-__split__}
entries=$(cat /etc/lb/entries | grep . | sort | uniq)
# Compile backend data into entries.yml
domains=
frontends=
current_backend=
current_domain=
current_ip=
echo "backends:" > /etc/lb/entries.yml
for entry in $entries; do
stack=$(echo $entry | awk -F "$SPLIT_TOKEN" '{ print $1 }')
service=$(echo $entry | awk -F "$SPLIT_TOKEN" '{ print $2 }')
port=$(echo $entry | awk -F "$SPLIT_TOKEN" '{ print $3 }')
if [[ "${stack}_${service}_${port}" != "$current_backend" ]]; then
echo " -" >> /etc/lb/entries.yml
echo " id: ${stack}_${service}_${port}" >> /etc/lb/entries.yml
echo " stack: $stack" >> /etc/lb/entries.yml
echo " service: $service" >> /etc/lb/entries.yml
echo " port: $port" >> /etc/lb/entries.yml
current_backend="${stack}_${service}_${port}"
current_domain=
current_ip=
fi
key=$(echo $entry | awk -F "$SPLIT_TOKEN" '{ print $4 }')
value=$(echo $entry | awk -F "$SPLIT_TOKEN" '{ print $5 }')
if [[ "$key" == "frontend" ]]; then
frontends="$frontends $value"
port=$(echo $value | awk -F '/' '{ print $1 }')
mode=$(echo $value | awk -F '/' '{ print $2 }')
case $port in
''|*[!0-9]*) is_number="false" ;;
*) is_number="true" ;;
esac
echo " frontend: $value" >> /etc/lb/entries.yml
if [[ "$is_number" == 'true' && "$mode" != "" ]]; then
echo " mode: $mode" >> /etc/lb/entries.yml
fi
elif [[ "$key" == "domain" || "$key" == "domains" ]]; then
if [[ "$current_domain" == "" ]]; then
echo " domains:" >> /etc/lb/entries.yml
fi
current_domain=$value
for domain in $(echo $value | tr ',' ' '); do
proto="$(echo $domain | grep :// | sed -e's,^\(.*://\).*,\1,g')"
url="$(echo ${domain##$proto})"
proto="$(echo ${proto%://})"
auth="$(echo $url | grep @ | cut -d@ -f1)"
user="$(echo "$auth" | awk -F ':' '{ print $1 }')"
pass="$(echo "$auth" | awk -F ':' '{ print $2 }')"
host="$(echo ${url##$auth@} | cut -d/ -f1)"
port="$(echo $host | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
path="$(echo $url | grep / | cut -d/ -f2-)"
echo " -" >> /etc/lb/entries.yml
echo " id: $(echo $domain | sed -E 's/[[:punct:]]/_/g')" >> /etc/lb/entries.yml
if [[ "$proto" != "" ]]; then echo " scheme: $proto" >> /etc/lb/entries.yml; fi
if [[ "$user" != "" ]]; then echo " user: $user" >> /etc/lb/entries.yml; fi
if [[ "$pass" != "" ]]; then echo " pass: $pass" >> /etc/lb/entries.yml; fi
if [[ "$host" != "" ]]; then echo " host: $host" >> /etc/lb/entries.yml; fi
if [[ "$path" != "" ]]; then echo " path: /$path" >> /etc/lb/entries.yml; fi
domains="${current_backend} $(echo $domain | sed -E 's/[[:punct:]]/_/g') $(echo $host/$path | wc -c | tr -d ' ')
$domains"
done
elif [[ "$key" == "container" ]]; then
ip=$value
state=$(echo $entry | awk -F "$SPLIT_TOKEN" '{ print $6 }')
health=$(echo $entry | awk -F "$SPLIT_TOKEN" '{ print $7 }')
if [[ "$health" == "healthy" && "$state" == "running" ]]; then
health="true"
else
health="false"
fi
if [[ "$current_ip" == "" ]]; then
echo " containers:" >> /etc/lb/entries.yml
fi
current_ip=$ip
echo " -" >> /etc/lb/entries.yml
echo " ip: $ip" >> /etc/lb/entries.yml
echo " healthy: $health" >> /etc/lb/entries.yml
fi
done
if [[ "$domains" == "" ]]; then
echo "_sorted: []" >> /etc/lb/entries.yml
else
domains=$(echo "$domains" | sort -k3 -n -r)
echo "_sorted:" >> /etc/lb/entries.yml
IFS=$'\n'; for domain in $domains; do
b=$(echo "$domain" | awk '{ print $1 }')
d=$(echo "$domain" | awk '{ print $2 }')
echo " - " >> /etc/lb/entries.yml
echo " backend: '$b'" >> /etc/lb/entries.yml
echo " domain: '$d'" >> /etc/lb/entries.yml
done
fi
frontends=$(echo "$frontends" | tr ' ' '\n' | sort | uniq)
echo "frontends:" >> /etc/lb/entries.yml
for frontend in $frontends; do
port=$(echo $frontend | awk -F '/' '{ print $1 }')
mode=$(echo $frontend | awk -F '/' '{ print $2 }')
case $port in
''|*[!0-9]*) is_number="false" ;;
*) is_number="true" ;;
esac
if [[ "$is_number" == 'true' ]]; then
echo " $frontend:" >> /etc/lb/entries.yml
echo " name: $(echo $frontend | sed -E 's/[[:punct:]]/_/g')" >> /etc/lb/entries.yml
echo " port: $port" >> /etc/lb/entries.yml
echo " mode: ${mode:-http}" >> /etc/lb/entries.yml
fi
done
# Download service metadata to metadata.yml and parse root domains
curl -s -H 'Accept: application/json' http://rancher-metadata.rancher.internal/latest/self/service/metadata \
| jq '.domains = (.domains | map(capture("(?<scheme>.*://)?(?<host>.*)")
| { scheme: .scheme[0:-3], host }
| { scheme, host, id: (.scheme + "_" + .host | gsub("[.]"; "_")) }))' \
| json2yml > /etc/lb/metadata.yml
# Merge entries and metadata to config.yml
node -e "m = require('merge-yaml'); console.log(JSON.stringify(m(['/etc/lb/entries.yml','/etc/lb/metadata.yml'])));" | json2yml > /etc/lb/config.yml
# Generate haproxy config file
gotpl /etc/lb/haproxy.cfg.tpl </etc/lb/config.yml > /etc/lb/haproxy.cfg
# Validate haproxy config and reload
if haproxy -c -f /etc/lb/haproxy.cfg; then
cp /etc/lb/haproxy.cfg /etc/haproxy/haproxy.cfg
/usr/local/sbin/haproxy -D -p /var/run/haproxy.pid -f /etc/haproxy/haproxy.cfg -sf $(cat /var/run/haproxy.pid)
else
echo "invalid haproxy config"
fi