-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhaproxy.py
380 lines (307 loc) · 10 KB
/
haproxy.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
"""
Copyright (C) 2013 - Aybuke Ozdemir <[email protected]>
This file is part of python-haproxy-tools
python-haproxy-tools is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
python-haproxy-tools is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
"""
# -*- coding: utf-8 -*-
from utils import *
class HAProxyConfig():
def __init__(self, config_path):
self.config_path = config_path
self.config = self.__readConfig()
# Set globals section.
gs = self.__getSection('global')
if gs:
self.globalh = Global(gs)
else:
self.globalh = None
# Set defaults section.
ds = self.__getSection('defaults')
if ds:
self.defaults = Defaults(ds)
else:
self.defaults = None
#Set Listen.
self.listens = []
for name in self.__getSectionNames('listen'):
l = Listen(self.__getSectionWithName('listen', name))
self.listens.append(l)
# Set Frontends.
self.frontends = []
for name in self.__getSectionNames('frontend'):
f = Frontend(self.__getSectionWithName('frontend', name))
self.frontends.append(f)
#Set Backend.
self.backends = []
for name in self.__getSectionNames('backend'):
b = Backend(self.__getSectionWithName('backend', name))
self.backends.append(b)
def getConfig(self):
out = "# This file created by python-haproxy-tools\n"
if self.globalh:
out += self.globalh.getConfig()
if self.defaults:
out += self.defaults.getConfig()
for l in self.listens:
out += l.getConfig()
for f in self.frontends:
out += f.getConfig()
for b in self.backends:
out += b.getConfig()
return out
def getGlobal(self):
return self.globalh
def getDefaults(self):
return self.defaults
def getFrontend(self, name):
name = name.strip()
for fe in self.frontends:
if fe.description.name == name:
return fe
def getBackend(self, name):
name = name.strip()
for be in self.backends:
if be.description.name == name:
return be
def getListen(self, name):
name = name.strip()
for l in self.listens:
if l.description.name == name:
return l
def getFnames(self):
fa = []
for f in self.frontends:
fa.append(f.name)
return fa
def getLnames(self):
la = []
for l in self.listens:
la.append(l.name)
return la
def getBnames(self):
ba = []
for b in self.backends:
ba.append(b.name)
return ba
def addListen(self, name, ip=None, port=None, mode=None):
ca = []
ca.append('listen %s' %name)
coption = ""
if ip:
cip = ip
else:
cip = "*"
if port:
cport = port
else:
cport = "80"
coption = "bind %s:%s" %(cip,cport)
ca.append(coption)
if mode:
ca.append('mode %s' %mode)
listen = Listen(ca)
self.listens.append(listen)
def addFrontend(self, name, ip=None, port=None, mode=None):
ca = []
ca.append('frontend %s' %name)
coption = ""
if ip:
cip = ip
else:
cip = ip
if port:
cport = port
else:
cport = "80"
coption = "bind %s:%s" %(cip, cport)
ca.append(coption)
if mode:
ca.append('mode:%s' %mode)
frontend = Frontend(ca)
self.frontends.append(frontend)
def addBackend(self, name, balance):
ca = []
ca.append('backend %s' %name)
coption = ""
coption = "balance %s" %balance
ca.append(coption)
backend = Backend(ca)
self.backends.append(backend)
def delBackend(self, name):
for be in self.backends:
if be.description.name == name:
self.backends.remove(be)
def delFrontend(self, name):
for fe in self.frontends:
if fe.description.name == name:
self.frontends.remove(fe)
def delListen(self, name):
for l in self.listens:
if l.description.name == name:
self.listens.remove(l)
def getFrontends(self):
return self.frontends
def getBackends(self):
return self.backends
def getListens(self):
return self.listens
def __getSectionNames(self, title):
l_names = []
for row in self.config:
row = row.strip()
if row.startswith(title):
name = row.split()[1]
l_names.append(name )
return l_names
def __getSectionWithName(self, title, name):
config_array = []
title = title.strip()
start_flag = False
for line in self.config:
line = line.strip()
if line == '':
continue
line_array = line.split()
ltitle = line_array[0].strip()
if len(line_array)> 1:
lname = line_array[1].strip()
if ltitle == title.strip():
if name == lname:
config_array.append(line)
start_flag = True
continue
if ltitle in SECTIONS:
start_flag = False
if start_flag:
config_array.append(line)
return config_array
def __getSection(self, title):
config_array = []
title = title.strip()
start_flag = False
for line in self.config:
line = line.strip()
if line == '':
continue
ltitle = line.split()[0].strip()
if ltitle == title:
config_array.append(line)
start_flag = True
continue
if ltitle in SECTIONS:
start_flag = False
if start_flag:
config_array.append(line)
if config_array:
return config_array
else:
return None
def __readConfig(self):
config_file = open(self.config_path)
config = config_file.readlines()
config_file.close()
return config
class Option():
def __init__(self, param_name, params):
self.name = param_name
self.params = params
def getRow(self):
return self.__repr__()
def getParamName(self):
return self.name
def getParams(self):
return self.params
def getConfig(self):
return "%s %s" %(self.name, " ".join(self.params))
class Section():
def __init__(self, config_array):
self.config_array = config_array
self.options = []
self.description = None
is_first_row = True
for row in self.config_array:
if is_first_row:
is_first_row = False
items = row.split()
title = items[0]
if len(items) > 1:
name = items[1]
else:
name = None
if len(items) > 2:
params = items[2:]
else:
params = None
self.description = Description(title, name)
if params:
for p in params:
o = Option('bind', tuple([p]))
self.options.append(o)
continue
param_name = self.getParamName(row).strip()
params = self.getParams(row)
option = Option(param_name, params)
self.options.append(option)
def getParamName(self, row):
return row.split()[0]
def getParams(self, row):
return tuple(row.split()[1:])
def getConfig(self):
opts = self.options
des = self.description
config_output = ""
config_output += des.getConfig() + '\n'
for opt in opts:
config_output += ' ' + opt.getConfig() + '\n'
config_output += '\n'
return config_output
def addOption(self, option):
self.options.append(option)
return True
def delOption(self, option):
for opt in self.options:
if opt == option.name:
self.options.remove(opt)
return True
def setOption(self, option):
for opt in self.options:
if opt.name == option.name:
opt.params = option.params
return self.options
class Description():
def __init__(self, title, name=None):
self.title = title
self.name = name
def getConfig(self):
out = []
out.append(self.title)
if self.name:
out.append(self.name)
return " ".join(out)
class Global(Section):
def __init__(self, config_array):
Section.__init__(self, config_array)
class Defaults(Section):
def __init__(self, config_array):
Section.__init__(self, config_array)
class Listen(Section):
def __init__(self, config_array):
Section.__init__(self, config_array)
self.name = self.description.name
class Frontend(Section):
def __init__(self, config_array):
Section.__init__(self, config_array)
self.name = self.description.name
class Backend(Section):
def __init__(self, config_array):
Section.__init__(self, config_array)
self.name = self.description.name