思路:读一行、写一行
global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info defaults log global mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms option dontlognull listen stats :8888 stats enable stats uri /admin stats auth admin:1234 frontend oldboy.org bind 0.0.0.0:80 option httplog option httpclose option forwardfor log global acl www hdr_reg(host) -i www.oldboy.org use_backend www.oldboy.org if www backend www.oldboy.org server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000 backend buy.oldboy.org server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000
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
|
#!/usr/bin/env python # -*- coding:utf-8 -*- def fetch(backend): result = [] with open ( 'ha.conf' , 'r' ) as f: flag = False for line in f: if line.strip().startswith( 'backend' ) and line.strip() = = "backend " + backend: flag = True continue if flag and line.strip().startswith( 'backend' ): break if flag and line.strip(): result.append(line.strip()) return result def add(backend, record): result = fetch(backend) if not result: # 无backend,无record pass else : # 有backend if record in result: # 记录record pass else : result.append(record) with open ( 'ha.conf' , 'r' ) as old, open ( 'new.conf' , 'w' ) as new: continue_flag = False for line in old: if line.strip().startswith( 'backend' ) and line.strip() = = "backend " + backend: continue_flag = True new.write(line) for temp in result: new.write( " " * 8 + temp + "\n" ) continue if continue_flag and line.strip().startswith( 'backend' ): continue_flag = False if continue_flag: pass else : new.write(line) def add2(backend, record): with open ( 'ha.conf' , 'r' ) as old, open ( 'new.conf' , 'w' ) as new: in_backend = False has_backend = False has_record = False for line in old: if line.strip().startswith( 'backend' ) and line.strip() = = "backend " + backend: has_backend = True in_backend = True new.write(line) continue if in_backend and line.strip().startswith( 'backend' ): if not has_record: new.write( " " * 8 + record + '\n' ) new.write(line) in_backend = False continue if in_backend and line.strip() = = record: has_record = True new.write(line) continue if line.strip(): new.write(line) if not has_backend: # 写backend,写record new.write( 'backend ' + backend + '\n' ) new.write( ' ' * 8 + record + '\n' ) # ret = fetch("www.oldboy.org") # print(ret) # add('www.oldboy.org', "server 100.1.7.10 100.1.7.10 weight 20 maxconn 3000") # add2('www.oldboy.org', "server 100.1.7.11 100.1.7.11 weight 20 maxconn 3000") |
思路:读一行、写一行
global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info defaults log global mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms option dontlognull listen stats :8888 stats enable stats uri /admin stats auth admin:1234 frontend oldboy.org bind 0.0.0.0:80 option httplog option httpclose option forwardfor log global acl www hdr_reg(host) -i www.oldboy.org use_backend www.oldboy.org if www backend www.oldboy.org server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000 backend buy.oldboy.org server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000
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
|
#!/usr/bin/env python # -*- coding:utf-8 -*- def fetch(backend): result = [] with open ( 'ha.conf' , 'r' ) as f: flag = False for line in f: if line.strip().startswith( 'backend' ) and line.strip() = = "backend " + backend: flag = True continue if flag and line.strip().startswith( 'backend' ): break if flag and line.strip(): result.append(line.strip()) return result def add(backend, record): result = fetch(backend) if not result: # 无backend,无record pass else : # 有backend if record in result: # 记录record pass else : result.append(record) with open ( 'ha.conf' , 'r' ) as old, open ( 'new.conf' , 'w' ) as new: continue_flag = False for line in old: if line.strip().startswith( 'backend' ) and line.strip() = = "backend " + backend: continue_flag = True new.write(line) for temp in result: new.write( " " * 8 + temp + "\n" ) continue if continue_flag and line.strip().startswith( 'backend' ): continue_flag = False if continue_flag: pass else : new.write(line) def add2(backend, record): with open ( 'ha.conf' , 'r' ) as old, open ( 'new.conf' , 'w' ) as new: in_backend = False has_backend = False has_record = False for line in old: if line.strip().startswith( 'backend' ) and line.strip() = = "backend " + backend: has_backend = True in_backend = True new.write(line) continue if in_backend and line.strip().startswith( 'backend' ): if not has_record: new.write( " " * 8 + record + '\n' ) new.write(line) in_backend = False continue if in_backend and line.strip() = = record: has_record = True new.write(line) continue if line.strip(): new.write(line) if not has_backend: # 写backend,写record new.write( 'backend ' + backend + '\n' ) new.write( ' ' * 8 + record + '\n' ) # ret = fetch("www.oldboy.org") # print(ret) # add('www.oldboy.org', "server 100.1.7.10 100.1.7.10 weight 20 maxconn 3000") # add2('www.oldboy.org', "server 100.1.7.11 100.1.7.11 weight 20 maxconn 3000") |
来源:https://www.cnblogs.com/cp-miao/p/5617890.html