-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_haproxy_config.sh
executable file
·109 lines (96 loc) · 2.45 KB
/
generate_haproxy_config.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
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
#!/bin/bash
# Author: Josef Johansson <[email protected]>
# This file is a really quick-and-dirty way of generating haproxy
# configuration from a list of backends
function dn {
from='[:upper:]'
to='[:lower:]'
if [ ${#@} -eq 2 ] && [ "$2" == 'up' ]
then
from='[:lower:]'
to='[:upper:]'
fi
echo $1 | cut -d '=' -f 1 | /usr/bin/tr "$from" "$to"
}
function ipv4 {
echo $1 | cut -d '=' -f 2
}
function ipv6 {
echo $1 | cut -d '=' -f 3
}
function generate {
echo "global
maxconn 4096
external-check
defaults
balance roundrobin
log global
mode http
timeout connect 5000
timeout client 50000
timeout server 50000
option external-check
external-check command $PWD/check-timestamp.sh
listen stats
bind :1936
mode http
stats enable
stats hide-version
stats uri /
stats realm Haproxy\ Statistics
stats auth stats:ceph-mirrors-rocks
frontend port_80
bind *:80
"
src="src"
grep -q USE_X_FORWARDED_FOR .env
ret=$?
if [ $ret -eq 0 ]
then
src="hdr(x-forwarded-for)"
fi
echo "
http-request set-header backend %[${src},map_ip($PWD/geoip.lst)]
acl letsencrypt-acl path_beg /.well-known/acme-challenge/
"
for country in ${countries[@]}
do
echo " acl $(dn $country up) req.fhdr(backend) -m str $(dn $country up)"
done
echo " use_backend letsencrypt-backend if letsencrypt-acl"
for country in ${countries[@]}
do
echo " use_backend $(dn $country up) if $(dn $country up)"
done
echo " default_backend default
##
# Backends
##
backend letsencrypt-backend
server letsencrypt 127.0.0.1:54321
backend default
server download 127.0.1.1:80 redir http://download.ceph.com weight 1
"
for country in ${countries[@]}
do
echo "backend $(dn $country up)"
echo " balance static-rr"
echo " balance hdr(backend)"
echo " server download 173.236.253.173:80 redir http://download.ceph.com weight 1"
echo " server download6 2607:f298:6050:51f3:f816:3eff:fe71:9135:80 redir http://download.ceph.com weight 1"
for _country in ${countries[@]}
do
weight=2
inter='120s'
if [ $(dn $_country) == $(dn $country) ]
then
weight=256
inter='10s'
fi
echo " server $(dn $_country) $(ipv4 $_country):80 redir http://$(dn $_country).ceph.com check weight ${weight} inter ${inter}"
echo " server $(dn $_country)6 $(ipv6 $_country):80 redir http://$(dn $_country).ceph.com check weight ${weight} inter ${inter}"
done
done
}
declare -a countries=($(./generate_backend_sources.sh))
generate > haproxy.cfg