-
Notifications
You must be signed in to change notification settings - Fork 0
/
haproxy_objects.py
1255 lines (946 loc) · 35 KB
/
haproxy_objects.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding=utf-8
import os
class ConfigIsInvalid(Exception):
pass
class Config(object):
def __init__(self):
self.globals = GlobalConfig()
self.defaults = DefaultConfig()
self.frontends = {}
self.backends = {}
self.listens = {}
super(Config, self).__init__()
@classmethod
def _read_to_next_part(cls, lines):
_lines = []
i = 0
while i < len(lines):
line = lines[i].partition('#')[0].strip()
if line.startswith('global') or line.startswith('listen') or \
line.startswith('defaults') or line.startswith('frontend') or \
line.startswith('backend'):
break
if not line.startswith('#'):
_lines.append(line)
i += 1
return _lines, i
@classmethod
def from_string(cls, filename):
if not os.path.exists(filename):
raise ConfigIsInvalid('%s is not exist' % filename)
c = cls()
with open(filename, 'rb') as handler:
lines = handler.readlines()
while lines:
line = lines[0].partition('#')[0].strip()
lines = lines[1:]
if line and not line.startswith('#'):
parts = line.split()
part_name = parts[0]
if part_name in ['global', 'defaults', 'listen', 'frontend', 'backend']:
part_lines, _next_i = Config._read_to_next_part(lines)
if part_name == 'global':
c.globals = GlobalConfig()
c.globals.from_string(part_lines)
elif part_name == 'defaults':
c.defaults = DefaultConfig()
c.defaults.from_string(part_lines)
elif part_name == 'listen':
if len(parts) == 3:
part_lines.insert(0, 'bind %s' % parts[2])
l = ListenConfig()
l.name = parts[1]
l.from_string(part_lines)
c.listens[parts[1]] = l
elif part_name == 'frontend':
if len(parts) == 3:
part_lines.insert(0, 'bind %s' % parts[2])
l = FrontendConfig()
l.name = parts[1]
l.from_string(part_lines)
c.frontends[parts[1]] = l
elif part_name == 'backend':
l = BackendConfig()
l.name = parts[1]
l.from_string(part_lines)
c.backends[parts[1]] = l
lines = lines[_next_i:]
continue
return c
def to_string(self):
lines = ['# created by haproxy-tool', '']
lines.append('global')
for line in self.globals.to_string().split('\n'):
lines.append('\t' + line)
lines.append('\n')
lines.append('defaults')
for line in self.defaults.to_string().split('\n'):
lines.append('\t' + line)
lines.append('\n')
for frontend_name in self.frontends:
lines.append('frontend %s' % frontend_name)
for line in self.frontends[frontend_name].to_string().split('\n'):
lines.append('\t' + line)
lines.append('\n')
for backend_name in self.backends:
lines.append('backend %s' % backend_name)
for line in self.backends[backend_name].to_string().split('\n'):
lines.append('\t' + line)
lines.append('\n')
for listen_name in self.listens:
lines.append('listen %s' % listen_name)
for line in self.listens[listen_name].to_string().split('\n'):
lines.append('\t' + line)
lines.append('\n')
return '\n'.join(lines)
def __dict__(self):
out = {
'global': dict(self.globals),
'defaults': dict(self.defaults),
'frontend': {},
'backend': {},
'listen': {}
}
for key in self.frontends:
out['frontend'][key] = dict(self.frontends[key])
for key in self.backends:
out['backend'][key] = dict(self.backends[key])
for key in self.listens:
out['listen'][key] = dict(self.listens[key])
return out
class GlobalConfig(object):
def __init__(self):
# log <address> <facility> [<level> [<minlevel>]]
self.log = {}
self.max_connections = None
self.user = 'haproxy'
self.group = 'haproxy'
self.daemon = True
self.chroot = None
self.stats = {
'socket': '/tmp/haproxy'
}
self.number_processes = 5
self.pid_file = '/var/run/haproxy.pid'
self._raw = None
super(GlobalConfig, self).__init__()
def __dict__(self):
return {
'log': self.log,
'max_connections': self.max_connections,
'use': self.user,
'group': self.group,
'daemon': self.daemon,
'chroot': self.chroot,
'stats': self.stats,
'number_processes': self.number_processes,
'pid_file': self.pid_file
}
def set_value(self, key, line):
parts = line.split()
if key == 'log':
address = parts[0]
facility = parts[1]
try:
level = parts[2]
except:
level = None
try:
min_level = parts[3]
except:
min_level = None
self.set_log(address, facility, level, min_level)
elif key == 'maxconn':
self.set_max_connections(parts[0])
elif key == 'pidfile':
self.set_pid_file(parts[0])
elif key == 'daemon':
self.set_daemon(True)
elif key == 'user':
self.set_user(parts[0])
elif key == 'group':
self.set_group(parts[0])
elif key == 'chroot':
try:
chroot = parts[0]
except:
chroot = ''
self.set_chroot(chroot)
elif key == 'nbproc':
self.set_number_processes(parts[0])
def from_string(self, lines):
self._raw = lines
for line in lines:
line = line.partition('#')[0].strip()
if line and not line.startswith('#'):
parts = line.split()
key = parts[0]
self.set_value(key, ' '.join(parts[1:]))
def to_string(self):
lines = []
for address in self.log:
for facility in self.log[address]:
_log = 'log %s %s' % (address, facility)
if self.log[address][facility]:
_log += ' %s' % self.log[address][facility].get('level', '')
_log += ' %s' % self.log[address][facility].get('min-level', '')
lines.append(_log.strip())
lines.append('user %s' % self.user)
lines.append('group %s' % self.group)
lines.append('pidfile %s' % self.pid_file)
if self.max_connections:
lines.append('maxconn %s' % self.max_connections)
if self.daemon:
lines.append('daemon')
if self.chroot:
lines.append('chroot')
lines.append('nbproc %s' % self.number_processes)
for t in self.stats:
lines.append('stats %s %s' % (t, self.stats[t]))
return '\n'.join(lines)
def set_log(self, address, facility, level=None, min_level=None):
"""
{
'127.0.0.1': {
'local0': {
'level': 'notice'
}
}
}
"""
if address not in self.log:
self.log[address] = {}
if facility not in self.log[address]:
self.log[address][facility] = {}
if level:
self.log[address][facility]['level'] = level
if min_level:
self.log[address][facility]['min-level'] = min_level
def set_stats_socket(self, socket_path):
self.stats['socket'] = socket_path
def set_stats_timeout(self, timeout='10s'):
"""
timeout - milliseconds, The default timeout on the stats socket is set to 10 seconds
the time unit is us, ms, s, m, h, d
"""
self.stats['timeout'] = timeout
def set_stats_max_connections(self, connections=10):
"""
connections - By default, the stats socket is limited to 10 concurrent connections.
"""
self.stats['maxconn'] = connections
def set_pid_file(self, filename):
dir_name = os.path.dirname(filename)
if os.path.exists(dir_name):
self.pid_file = filename
else:
raise ConfigIsInvalid('Global pid file config is not exists')
def set_number_processes(self, value):
try:
self.number_processes = int(value)
except:
raise ConfigIsInvalid('Global nbproc config is invalid')
def set_max_connections(self, value):
try:
self.max_connections = int(value)
except:
raise ConfigIsInvalid('Global maxconn config is invalid')
def set_user(self, user):
self.user = user
def set_group(self, group):
self.group = group
def set_daemon(self, daemon=True):
try:
self.daemon = bool(daemon)
except:
raise ConfigIsInvalid('Global daemon config is invalid')
def set_chroot(self, chroot='/var/lib/haproxy'):
"""
Changes current directory to <jail dir> and performs a chroot() there before
dropping privileges. This increases the security level in case an unknown
vulnerability would be exploited, since it would make it very hard for the
attacker to exploit the system. This only works when the process is started
with superuser privileges. It is important to ensure that <jail_dir> is both
empty and unwritable to anyone.
"""
self.chroot = chroot
class DefaultConfig(object):
def __init__(self):
self.log = {}
self.option = {}
self.mode = 'http'
self.option = {}
self.retries = None
self.max_connections = None
self.client_timeout = None
self.server_timeout = None
self.connect_timeout = None
self._raw = None
super(DefaultConfig, self).__init__()
def __dict__(self):
return {
'log': self.log,
'option': self.option,
'mode': self.mode,
'retries': self.retries,
'max_connections': self.max_connections,
}
def set_log(self, address, facility, level=None, min_level=None):
"""
{
'127.0.0.1': {
'local0': {
'level': 'notice'
}
}
}
"""
if address not in self.log:
self.log[address] = {}
if facility not in self.log[address]:
self.log[address][facility] = {}
if level:
self.log[address][facility]['level'] = level
if min_level:
self.log[address][facility]['min-level'] = min_level
def set_option(self, key, parts):
if isinstance(parts, list):
value = ' '.join(parts)
else:
value = parts
self.option[key] = value
def set_max_connections(self, value):
try:
self.max_connections = int(value)
except:
raise ConfigIsInvalid('Default maxconn config is invalid')
def set_retries(self, value):
try:
self.retries = int(value)
except:
raise ConfigIsInvalid('Default retries config is invalid')
def set_connect_timeout(self, value):
try:
self.connect_timeout = int(value)
except:
raise ConfigIsInvalid('Default contimeout config is invalid')
def set_client_timeout(self, value):
try:
self.client_timeout = int(value)
except:
raise ConfigIsInvalid('Default clitimeout config is invalid')
def set_server_timeout(self, value):
try:
self.server_timeout = int(value)
except:
raise ConfigIsInvalid('Default srvtimeout config is invalid')
def set_mode(self, value):
self.mode = value
def set_value(self, key, line):
parts = line.split()
if key == 'log':
if len(parts) == 1:
self.set_log(parts[0], parts[0])
else:
address = parts[0]
facility = parts[1]
try:
level = parts[2]
except:
level = None
try:
min_level = parts[3]
except:
min_level = None
self.set_log(address, facility, level, min_level)
elif key == 'mode':
self.set_mode(parts[0])
elif key == 'maxconn':
self.set_max_connections(parts[0])
elif key == 'retries':
self.set_retries(parts[0])
elif key == 'option':
self.set_option(parts[0], parts[1:])
elif key == 'contimeout':
self.set_connect_timeout(parts[0])
elif key == 'clitimeout':
self.set_client_timeout(parts[0])
elif key == 'srvtimeout':
self.set_server_timeout(parts[0])
def from_string(self, lines):
self._raw = lines
for line in lines:
line = line.partition('#')[0].strip()
if line and not line.startswith('#'):
parts = line.split()
key = parts[0]
self.set_value(key, ' '.join(parts[1:]))
def to_string(self):
lines = []
for address in self.log:
if address == 'global':
lines.append('log global')
else:
for facility in self.log[address]:
line = 'log %s %s' % (address, facility)
if self.log[address][facility].get('level'):
line += ' %s' % self.log[address][facility]['level']
if self.log[address][facility].get('min-level'):
line += ' %s' % self.log[address][facility]['min-level']
lines.append(line)
if self.mode:
lines.append('mode %s' % self.mode)
if self.connect_timeout:
lines.append('timeout connect %s' % self.connect_timeout)
if self.client_timeout:
lines.append('timeout client %s' % self.client_timeout)
if self.server_timeout:
lines.append('timeout server %s' % self.server_timeout)
for key in self.option:
if self.option[key]:
lines.append('option %s %s' % (key, self.option[key]))
else:
lines.append('option %s' % key)
if self.retries:
lines.append('retries %s' % self.retries)
if self.max_connections:
lines.append('maxconn %s' % self.max_connections)
return '\n'.join(lines)
class ServerConfig(object):
def __init__(self):
self.name = None
self.ip = None
self.port = 80
self.weight = 1
self.cookie = None
self.check_inter = 2000
self.check_fall = 3
self.max_connections = None
self.min_connections = None
self.backup = False
self.keywords = ['cookie', 'check', 'maxconn', 'minconn', 'backup']
self._raw = None
super(ServerConfig, self).__init__()
def __dict__(self):
return {
'name': self.name,
'ip': self.ip,
'port': self.port,
'weight': self.weight,
'cookie': self.cookie,
'check_inter': self.check_inter,
'check_fall': self.check_fall,
'max_connections': self.max_connections,
'min_connections': self.min_connections,
'backup': self.backup,
}
def set_cookie(self, value):
self.cookie = value
def set_min_connections(self, value):
try:
self.min_connections = int(value)
except:
raise ConfigIsInvalid('Server minconn config is invalid')
def set_max_connections(self, value):
try:
self.max_connections = int(value)
except:
raise ConfigIsInvalid('Server maxconn config is invalid')
def set_check_inter(self, inter=None):
try:
self.check_inter = int(inter)
except:
raise ConfigIsInvalid('Server check inter config is invalid')
def set_check_fall(self, fall=None):
try:
self.check_fall = int(fall)
except:
raise ConfigIsInvalid('Server check fall config is invalid')
def set_backup(self, backup=True):
self.backup = backup
def set_value(self, key, parts):
if key == 'cookie':
if parts and parts[0] not in self.keywords:
self.set_cookie(parts[0])
parts = parts[1:]
elif key == 'backup':
self.set_backup(True)
elif key == 'minconn':
self.set_min_connections(parts[0])
parts = parts[1:]
elif key == 'maxconn':
self.set_max_connections(parts[0])
parts = parts[1:]
elif key == 'check':
while parts:
if parts[0] == 'inter':
self.set_check_inter(parts[1])
parts = parts[2:]
elif parts[0] == 'fall':
self.set_check_fall(parts[1])
parts = parts[2:]
else:
break
def from_string(self, value):
self._raw = value
_config = value.split()
self.name = _config[0]
ip_and_port = _config[1]
if ':' not in ip_and_port:
ip_and_port = '%s:80' % ip_and_port
self.ip, _t, self.port = ip_and_port.partition(':')
self.port = int(self.port)
_config = _config[2:]
while _config:
key = _config[0]
_config = _config[1:]
self.set_value(key, _config)
def to_string(self):
output = 'server %s %s:%s weight %s' % (self.name, self.ip, self.port, self.weight)
if self.cookie:
output += ' cookie %s' % self.cookie
if self.check_inter or self.check_fall:
output += ' check'
if self.check_inter:
output += ' inter %s' % self.check_inter
if self.check_fall:
output += ' fall %s' % self.check_fall
if self.max_connections:
output += ' maxconn %s' % self.max_connections
if self.min_connections:
output += ' minconn %s' % self.min_connections
if self.backup:
output += ' backup'
return output
class ListenConfig(object):
def __init__(self):
self.name = None
self.ip = '*'
self.port = None
self.balance = 'roundrobin'
self.mode = 'http'
self.option = {
'httpchk': '/ GET HTTP/1.0'
}
self.max_connections = None
self.retries = None
self.server = {}
self.cookie_name = None
self.cookie_insert = False
self.cookie_rewrite = False
self.cookie_nocache = False
self.cookie_indirect = False
self.cookie_maxidle = None
self.cookie_maxlife = None
self.client_timeout = None
self.server_timeout = None
self.connect_timeout = None
self._raw = None
super(ListenConfig, self).__init__()
def __dict__(self):
out = {
'name': self.name,
'ip': self.ip,
'port': self.port,
'balance': self.balance,
'mode': self.mode,
'option': self.option,
'max_connections': self.max_connections,
'retries': self.retries,
'server': {},
'cookie_name': self.cookie_name,
'cookie_insert': self.cookie_insert,
'cookie_rewrite': self.cookie_rewrite,
'cookie_nocache': self.cookie_nocache,
'cookie_indirect': self.cookie_indirect,
'cookie_maxidle': self.cookie_maxidle,
'cookie_maxlife': self.cookie_maxlife,
'client_timeout': self.client_timeout,
'server_timeout': self.server_timeout,
'connect_timeout': self.connect_timeout
}
for key in self.server:
out['server'][key] = dict(self.server[key])
return out
def set_cookie(self, parts):
self.cookie_name = parts[0]
if 'insert' in parts:
self.cookie_insert = True
if 'rewrite' in parts:
self.cookie_rewrite = True
if 'nocache' in parts:
self.cookie_nocache = True
if 'indirect' in parts:
self.cookie_indirect = True
if 'maxidle' in parts:
index = parts.index('maxidle')
self.cookie_maxidle = parts[index+1]
if 'maxlife' in parts:
index = parts.index('maxlife')
self.cookie_maxlife = parts[index+1]
def set_balance(self, value):
self.balance = value
def set_bind(self, value):
if ':' not in value:
self.ip = value
self.port = 80
else:
self.ip, _t, self.port = value.partition(':')
def set_option(self, key, parts):
if isinstance(parts, list):
value = ' '.join(parts)
else:
value = parts
self.option[key] = value
def set_max_connections(self, value):
try:
self.max_connections = int(value)
except:
raise ConfigIsInvalid('Default maxconn config is invalid')
def set_retries(self, value):
try:
self.retries = int(value)
except:
raise ConfigIsInvalid('Default retries config is invalid')
def set_connect_timeout(self, value):
try:
self.connect_timeout = int(value)
except:
raise ConfigIsInvalid('Default contimeout config is invalid')
def set_client_timeout(self, value):
try:
self.client_timeout = int(value)
except:
raise ConfigIsInvalid('Default clitimeout config is invalid')
def set_server_timeout(self, value):
try:
self.server_timeout = int(value)
except:
raise ConfigIsInvalid('Default srvtimeout config is invalid')
def set_server(self, value):
server = ServerConfig()
server.from_string(value)
if server.name:
self.server[server.name] = server
def set_value(self, key, line):
parts = line.split()
if key == 'bind':
self.set_bind(parts[0])
elif key == 'cookie':
self.set_cookie(parts)
elif key == 'balance':
self.set_balance(parts[0])
elif key == 'maxconn':
self.set_max_connections(parts[0])
elif key == 'retries':
self.set_retries(parts[0])
elif key == 'option':
self.set_option(parts[0], parts[1:])
elif key == 'retries':
self.set_retries(parts[0])
elif key == 'contimeout':
self.set_connect_timeout(parts[0])
elif key == 'clitimeout':
self.set_client_timeout(parts[0])
elif key == 'srvtimeout':
self.set_server_timeout(parts[0])
elif key == 'server':
self.set_server(line)
def from_string(self, lines):
self._raw = lines
for line in lines:
line = line.partition('#')[0].strip()
if line and not line.startswith('#'):
parts = line.split()
key = parts[0]
self.set_value(key, ' '.join(parts[1:]))
def to_string(self):
lines = []
lines.append('bind %s:%s' % (self.ip, self.port))
lines.append('balance %s' % self.balance)
lines.append('mode %s' % self.mode)
if self.connect_timeout:
lines.append('timeout connect %s' % self.connect_timeout)
if self.client_timeout:
lines.append('timeout client %s' % self.client_timeout)
if self.server_timeout:
lines.append('timeout server %s' % self.server_timeout)
if self.cookie_name:
cookie_define = 'cookie %s' % self.cookie_name
if self.cookie_insert:
cookie_define += ' insert'
if self.cookie_rewrite:
cookie_define += ' rewrite'
if self.cookie_nocache:
cookie_define += ' nocache'
if self.cookie_indirect:
cookie_define += ' indirect'
if self.cookie_maxidle:
cookie_define += ' maxidle %s' % self.cookie_maxidle
if self.cookie_maxlife:
cookie_define += ' maxlife %s' % self.cookie_maxlife
lines.append(cookie_define)
if self.max_connections:
lines.append('maxconn %s' % self.max_connections)
for key in self.option:
line = 'option %s' % key
if self.option[key]:
line += ' %s' % self.option[key]
lines.append(line)
for server_name in self.server:
lines.append(self.server[server_name].to_string())
return '\n'.join(lines)
class FrontendConfig(object):
def __init__(self):
self.name = None
self.ip = '*'
self.port = None
self.acl = {}
self.option = {}
self.use_backend = {}
self.default_backend = None
self._raw = None
self.client_timeout = 60000
self.max_connections = None
super(FrontendConfig, self).__init__()
def __dict__(self):
return {
'name': self.name,
'ip': self.ip,
'port': self.port,
'acl': self.acl,
'option': self.option,
'use_backend': self.use_backend,
'default_backend': self.default_backend,
'client_timeout': self.client_timeout,
'max_connections': self.max_connections
}
def to_string(self):
lines = []
lines.append('bind %s:%s' % (self.ip, self.port))
if self.client_timeout:
lines.append('timeout client %s' % self.client_timeout)
if self.max_connections:
lines.append('maxconn %s' % self.max_connections)
for acl_name in self.acl:
lines.append('acl %s %s %s' % (acl_name, self.acl[acl_name]['method'], self.acl[acl_name]['value']))
for key in self.option:
line = 'option %s' % key
if self.option[key]:
line += ' %s' % self.option[key]
lines.append(line)
for backend_name in self.use_backend:
for conditions in self.use_backend[backend_name]:
lines.append('use_backend %s if %s' % (backend_name, ' '.join(conditions)))
lines.append('default_backend %s' % self.default_backend)
return '\n'.join(lines)
def from_string(self, lines):
self._raw = lines
for line in lines:
line = line.partition('#')[0].strip()
if line and not line.startswith('#'):
parts = line.split()
key = parts[0]
self.set_value(key, ' '.join(parts[1:]))
def set_value(self, key, line):
parts = line.split()
if key == 'bind':
self.set_bind(parts[0])
elif key == 'option':
self.set_option(parts[0], parts[1:])
elif key == 'clitimeout':
self.set_client_timeout(parts[0])
elif key == 'use_backend':
self.set_use_backend(parts)
elif key == 'acl':
self.set_acl(parts)