-
Notifications
You must be signed in to change notification settings - Fork 16
/
create_haproxy_check.py
executable file
·237 lines (203 loc) · 8.96 KB
/
create_haproxy_check.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#! /usr/bin/env python3
import config
import os, errno
import sys
from io import StringIO
#from pprint import pprint
BASEDIR = "configs"
APPLICATION_PATH = "."
def utf8len(s):
return len(s.encode('utf-8'))
def help_exit(exit_status):
if exit_status != 0:
print("Error: Wrong arguments in call", file=sys.stderr)
help_msg = """Usage:
%s <templatename> <project>
Options:
templatename name of a template in the template dir
project project name
""" % sys.argv[0]
print(help_msg, file=sys.stderr)
os.system('ls -altr template/*')
sys.exit(exit_status)
def replace(source_name, props, output_name):
output = open(output_name, 'w')
source = open(source_name, 'r')
for line in source:
newline = line
for prop in props:
newline = newline.replace(prop, props[prop])
output.write(newline)
output.close()
source.close()
def new_haproxy_conf(props, slavelist):
project = props["<%= @bn.project %>"]
new_conf = "%s/%s/haproxy-%s.cnf" % (BASEDIR, project, project)
print("Creating %s" % new_conf, file=sys.stderr)
if(slavelist):
print("Config.py has multiple slave definitions, using -multi template instead : template/%s-multi.template" % sys.argv[1], file=sys.stderr)
if os.path.isfile("template/%s-multi.template" % sys.argv[1]):
print("Looking for partial template for slave entries: template/%s-partial.template" % sys.argv[1], file=sys.stderr)
if os.path.isfile("template/%s-partial.template" % sys.argv[1]):
#load partial template in variable (expect this to be a one liner)
with open("template/%s-partial.template" % sys.argv[1], 'r') as templatefile:
slavetemplate = templatefile.read().rstrip()
slaves = StringIO()
#pprint(locals())
for name,dsn in slavelist.items():
format_dictionary = {'name': name, 'dsn': dsn, 'checkport': props["<%= @bn.checkport %>"] }
print(" " + slavetemplate.format(**format_dictionary), file=slaves)
props["<%= @SLAVELIST %>"] = slaves.getvalue()
replace("template/%s-multi.template" % sys.argv[1], props, new_conf)
else:
print("Partial template does not exist : template/%s-partial.template" % sys.argv[1], file=sys.stderr)
sys.exit(0)
else:
print("Template does not exist : template/%s-multi.template" % sys.argv[1], file=sys.stderr)
sys.exit(0)
else:
if os.path.isfile("template/%s.template" % sys.argv[1]):
replace("template/%s.template" % sys.argv[1], props, new_conf)
else:
print("Template does not exist : template/%s.template" % sys.argv[1], file=sys.stderr)
sys.exit(0)
def add_hba_checkuser(props,extras):
print('')
#print("Add the following lines to pg_hba.conf:", file=sys.stderr)
print("### master/slaves are only a concept at start, any database node can later have a different role")
print("### this is just to give some clear pointers to the ones using this")
print("### special loadbalancer account in trust")
print("")
print("## vip / haproxy check user")
print("host template1 %s %s/32 trust" % (props["<%= @bn.checkuser %>"], props["<%= @bn.vipip %>"]))
print("# master")
print("host template1 %s %s/32 trust" % (props["<%= @bn.checkuser %>"], props["<%= @bn.masterdsn %>"].split(':')[0]))
print("# slaves")
if(extras['checkuser'] == "#"):
print("host template1 %s %s/32 trust" % (props["<%= @bn.checkuser %>"], props["<%= @bn.standbydsn %>"].split(':')[0]))
if(extras):
print(extras['checkuser'])
def add_hba_repmgr(props,extras):
#print("Add the following lines to pg_hba.conf:", file=sys.stderr)
print("### replication user")
print("local replication repmgr trust")
print("host replication repmgr 127.0.0.1/32 trust")
print("# master")
print("host replication repmgr %s/32 trust" % props["<%= @bn.masterdsn %>"].split(':')[0])
print("# slaves")
if(extras['repmgr'] == "#"):
print("host replication repmgr %s/32 trust" % props["<%= @bn.standbydsn %>"].split(':')[0])
if(extras):
print(extras['repl'])
print('')
print("### repmgr user")
print("local repmgr repmgr trust")
print("host repmgr repmgr 127.0.0.1/32 trust")
print("# master")
print("host repmgr repmgr %s/32 trust" % props["<%= @bn.masterdsn %>"].split(':')[0])
print("# slaves")
if(extras['repl'] == "#"):
print("host repmgr repmgr %s/32 trust" % props["<%= @bn.standbydsn %>"].split(':')[0])
if(extras):
print(extras['repmgr'])
print('')
def main():
args = len(sys.argv)
if args == 2:
if sys.argv[1] == "help":
help_exit(0)
else:
help_exit(1)
if args != 3:
help_exit(1)
mastername = config.HA_MASTER_NAME
masterdsn = config.HA_MASTER_DSN
standbyname = config.HA_STANDBY_NAME
standbydsn = config.HA_STANDBY_DSN
checkport = config.HA_CHECK_PORT
checkuser = config.HA_CHECK_USER
listenport = config.HA_LISTEN_PORT
statsuser = config.HA_STATS_USER
statspassword = config.HA_STATS_PASSWORD
vipip = config.HA_VIP_IP
d = utf8len(checkuser) + 33 + 1;
multiple_slaves = False
extras = { "repl": "#", "repmgr": "#" , "checkuser": "#" }
#print("D %s" % d)
#print("H %s" % hex(d).split('x')[-1])
# clean up when receiving a cidr block from config (\ escaped or not)
if vipip.find('/'):
vipip = vipip.split('/')[0].strip('\\')
# the props
props = {
"<%= @bn.template %>": sys.argv[1],
"<%= @bn.project %>": sys.argv[2],
"<%= @bn.mastername %>": mastername,
"<%= @bn.standbyname %>": standbyname,
"<%= @bn.masterdsn %>": masterdsn,
"<%= @bn.masterip %>": masterdsn.split(':')[0],
"<%= @bn.standbydsn %>": standbydsn,
"<%= @bn.checkuserhex %>": checkuser.encode("utf-8").hex() + "00",
"<%= @bn.checkport %>": checkport,
"<%= @bn.stats_user %>": statsuser,
"<%= @bn.stats_password %>": statspassword,
"<%= @bn.checkuser %>": checkuser,
"<%= @bn.listenport %>": listenport,
"<%= @bn.checkuserlen %>": str(utf8len(checkuser)+1),
"<%= @bn.totalsize %>": str(d),
"<%= @bn.vipip %>": vipip,
"<%= @bn.totalbytes %>": str(hex(d).split('x')[-1]),
"<%= @bn.path %>": APPLICATION_PATH
}
if standbyname.find(";")!=-1:
print("Found multiple slave names.", file=sys.stderr)
names = standbyname.split(";")
multiple_slaves = True
for name in names:
print(name, file=sys.stderr)
else:
print("No multiple standy servers found.", file=sys.stderr)
if standbydsn.find(";")!=-1:
print("Found multiple slave dsn.", file=sys.stderr)
dsns = standbydsn.split(";")
multiple_slaves = True
for ip in dsns:
print(ip, file=sys.stderr)
else:
print("No multiple standy ips found.", file=sys.stderr)
slavelist = False
if multiple_slaves == True:
if len(dsns) != len(names):
print("dsn and names do not have the same number of entries", file=sys.stderr)
sys.exit(0)
slavelist = dict(zip(names,dsns))
hba_repl = StringIO()
hba_repmgr = StringIO()
hba_extra_checkuser = StringIO()
for dsn in dsns:
print("host replication repmgr %s/32 trust" % dsn.split(':')[0], file=hba_repl)
print("host repmgr repmgr %s/32 trust" % dsn.split(':')[0], file=hba_repmgr)
print("host template1 %s %s/32 trust" % (props["<%= @bn.checkuser %>"], dsn.split(':')[0]), file=hba_extra_checkuser)
#hba_repl.seek(0)
#hba_repmgr.seek(0)
#hba_extra_checkuser.seek(0)
extras['repl'] = hba_repl.getvalue()
extras['repmgr'] = hba_repmgr.getvalue()
extras['checkuser'] = hba_extra_checkuser.getvalue()
project = props["<%= @bn.project %>"]
directory = ('%s/%s' % (BASEDIR, project))
if not os.path.isfile("template/%s.template" % sys.argv[1]):
print("Template does not exist : %s" % sys.argv[1], file=sys.stderr)
sys.exit(0)
try:
print("Creating haproxy project %s" % (project), file=sys.stderr)
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise
new_haproxy_conf(props, slavelist)
add_hba_checkuser(props, extras)
add_hba_repmgr(props, extras)
print("Done!", file=sys.stderr)
if __name__ == '__main__':
main()