-
Notifications
You must be signed in to change notification settings - Fork 0
/
haconf
executable file
·155 lines (132 loc) · 4.31 KB
/
haconf
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
#!/usr/bin/env python
# Import modules here
from __future__ import print_function
import os
import sys
# Colored output on terminal for fun ;)
BLUE = '\033[94m'
GREEN = '\033[92m'
ORANGE = '\033[93m'
RED = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
# Path to haproxy conf files
base_dir = './conf.d/'
#### Adds a domain entry to haproxy config ####
# Create a file with the name of domain in conf.d/domains
# Put the IP(s) of the server in this file
# Rebuild haproxy config
# Reload haproxy
# domain string - Name of domain to add
# port string - Port to connect to for reverse proxy
# ips array(string) - List of IPs that belong to the domain
def addDomain(domain, port, ips):
domain_conf_path = base_dir + 'domains/' + domain
with open(domain_conf_path, 'w+') as domain_conf:
for ip in ips:
domain_conf.write(ip + ':' + port + '\n')
print('Domain conf created at ' + BLUE + domain_conf_path + ENDC)
rebuildConf()
#### List all the domains in haproxy config ####
# Read the files in conf.d/domains
# Output the contents of each file
def listDomains():
domains = os.listdir(base_dir + 'domains')
for domain in domains:
with open(base_dir + 'domains/' + domain, 'r') as domain_conf:
ips = domain_conf.read()
print(BLUE + domain + ENDC)
print(ips)
#### Deletes a domain entry from haproxy config ####
# Delete the corresponding domain file in conf.d/domains
# Rebuild haproxy config
# Reload haproxy
# domain string - Name of domain to delete
def destroyDomain(domain):
os.remove(base_dir + 'domains/' + domain)
print(GREEN + 'Removed ' + domain + ' from haproxy' + ENDC)
rebuildConf()
def addAclRule():
pass
def listAclRules():
pass
def destroyAclRule():
pass
#### Throws an error message and exits ####
def throwError(msg = 'Usage: ' + GREEN + 'haconf'+ ORANGE +' [domain|acl] [add|del|list] [domain_name|rule] [port]' + ENDC +' [IPs]'):
print(msg)
sys.exit(1)
#### Regenerate haproxy conf ####
# Generate a new conf at haproxy.cfg.new
# Move current conf to haproxy.cfg.bak
# Move new conf to haproxy.cfg
# Reload systemd service
def rebuildConf():
print('Rebuilding haproxy config')
domains = os.listdir(base_dir + 'domains')
with open(base_dir + 'haproxy.cfg.new', 'w+') as new_conf:
with open(base_dir + 'header.cfg', 'r') as header_file:
header = header_file.read()
new_conf.write(header)
new_conf.write('\n')
### ACL RULES pending
for domain in domains:
new_conf.write(' acl host_' + domain + ' hdr(host) ' + domain + '\n')
for domain in domains:
new_conf.write(' use_backend ' + domain + ' if host_' + domain + '\n')
new_conf.write('default_backend local\n\n')
new_conf.write('backend local\n')
new_conf.write(' balance roundrobin\n')
new_conf.write(' server local_1 127.0.0.1:80 check\n\n')
for domain in domains:
with open(base_dir + 'domains/' + domain, 'r') as domain_conf:
ips = domain_conf.read()
ips = ips.split('\n')
new_conf.write('backend ' + domain + '\n')
new_conf.write(' balance roundrobin\n')
counter = 0
for ip in ips:
if len(ip) > 0:
new_conf.write(' server ' + domain + '_' + str(counter) + ' ' + ip)
counter += 1
if ':443' in ip:
new_conf.write(' ssl verify none\n')
else:
new_conf.write(' check\n')
new_conf.write('\n')
print('Built new config file')
os.rename(base_dir + 'haproxy.cfg', base_dir + 'haproxy.cfg.bak')
os.rename(base_dir + 'haproxy.cfg.new', base_dir + 'haproxy.cfg')
os.system('systemctl reload-or-restart haproxy')
print(GREEN + 'Loaded new settings' + ENDC)
def inputParser(args):
if len(args) < 3:
throwError()
cmd = args[1]
option = args[2]
if cmd == 'domain':
if option == 'add':
addDomain(args[3], args[4], args[5:])
elif option == 'del':
destroyDomain(args[3])
elif option == 'list':
listDomains()
else:
msg = 'Usage:'+ GREEN +' haconf' + ENDC + ' domain add '+ BLUE + 'example.iiit.ac.in '
msg += ORANGE + '<port> ' + BLUE + '10.4.255.1 10.4.265.1\n' + ENDC
msg += ' '+ GREEN +' haconf' + ENDC + ' domain del '+ BLUE + 'example.iiit.ac.in\n' + ENDC
msg += ' '+ GREEN +' haconf' + ENDC + ' domain list'
throwError(msg)
elif cmd == 'acl':
if option == 'add':
pass
elif option == 'del':
pass
elif option == 'list':
pass
else:
pass
else:
throwError()
if __name__ == '__main__':
inputParser(sys.argv)