forked from gplv2/haproxy-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_haproxy_check.py
More file actions
executable file
·144 lines (121 loc) · 4.9 KB
/
create_haproxy_check.py
File metadata and controls
executable file
·144 lines (121 loc) · 4.9 KB
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
#! /usr/bin/env python3
import config
import os, errno
import stat
import string
import sys
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):
project = props["<%= @bn.project %>"]
new_conf = "%s/%s/haproxy-%s.cnf" % (BASEDIR, project, project)
print("Creating %s" % new_conf, file=sys.stderr)
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):
print('')
#print("Add the following lines to pg_hba.conf:", file=sys.stderr)
print("# special loadbalancer account in trust")
print("host template1 %s %s/32 trust" % (props["<%= @bn.checkuser %>"], props["<%= @bn.vipip %>"]))
print("host template1 %s %s/32 trust" % (props["<%= @bn.checkuser %>"], props["<%= @bn.masterdsn %>"].split(':')[0]))
print("host template1 %s %s/32 trust" % (props["<%= @bn.checkuser %>"], props["<%= @bn.standbydsn %>"].split(':')[0]))
print('')
def add_hba_repmgr(props):
print('')
#print("Add the following lines to pg_hba.conf:", file=sys.stderr)
print("# repmgr account")
print("local replication repmgr trust")
print("host replication repmgr 127.0.0.1/32 trust")
print("host replication repmgr %s/32 trust" % props["<%= @bn.masterdsn %>"].split(':')[0])
print("host replication repmgr %s/32 trust" % props["<%= @bn.standbydsn %>"].split(':')[0])
print("local repmgr repmgr trust")
print("host repmgr repmgr 127.0.0.1/32 trust")
print("host repmgr repmgr %s/32 trust" % props["<%= @bn.masterdsn %>"].split(':')[0])
print("host repmgr repmgr %s/32 trust" % props["<%= @bn.standbydsn %>"].split(':')[0])
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;
#print("D %s" % d)
#print("H %s" % hex(d).split('x')[-1])
# 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.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
}
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)
add_hba_checkuser(props)
add_hba_repmgr(props)
print("Done!", file=sys.stderr)
if __name__ == '__main__':
main()