需要掌握的知识:
1、函数
2、文件处理
3、tag的用法
4、程序的解耦
需求:
1:查询
2:添加
3:删除
4:修改
5:退出
haproxy.conf 配置文件内容:
1 global 2 log 127.0.0.1 local2 3 daemon 4 maxconn 256 5 log 127.0.0.1 local2 info 6 defaults 7 log global 8 mode http 9 timeout connect 5000ms 10 timeout client 50000ms 11 timeout server 50000ms 12 option dontlognull 13 14 listen stats :8888 15 stats enable 16 stats uri /admin 17 stats auth admin:1234 18 19 frontend oldboy.org 20 bind 0.0.0.0:80 21 option httplog 22 option httpclose 23 option forwardfor 24 log global 25 acl www hdr_reg(host) -i www.oldboy.org 26 use_backend www.oldboy.org if www 27 28 backend www.oldboy1.org 29 server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333 30 server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000 31 server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000 32 backend www.oldboy2.org 33 server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000 34 backend www.oldboy20.org 35 server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
一、优化后的代码:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 6 import os 7 def file_handle(filename,backend_data,record_list=None,type='fetch'): #type:fetch append change 8 new_file=filename+'_new' 9 bak_file=filename+'_bak' 10 if type == 'fetch': 11 r_list = [] 12 with open(filename, 'r') as f: 13 tag = False 14 for line in f: 15 if line.strip() == backend_data: 16 tag = True 17 continue 18 if tag and line.startswith('backend'): 19 break 20 if tag and line: 21 r_list.append(line.strip()) 22 for line in r_list: 23 print(line) 24 return r_list 25 elif type == 'append': 26 with open(filename, 'r') as read_file, 27 open(new_file, 'w') as write_file: 28 for r_line in read_file: 29 write_file.write(r_line) 30 31 for new_line in record_list: 32 if new_line.startswith('backend'): 33 write_file.write(new_line + ' ') 34 else: 35 write_file.write("%s%s " % (' ' * 8, new_line)) 36 os.rename(filename, bak_file) 37 os.rename(new_file, filename) 38 os.remove(bak_file) 39 elif type == 'change': 40 with open(filename, 'r') as read_file, 41 open(new_file, 'w') as write_file: 42 tag=False 43 has_write=False 44 for r_line in read_file: 45 if r_line.strip() == backend_data: 46 tag=True 47 continue 48 if tag and r_line.startswith('backend'): 49 tag=False 50 if not tag: 51 write_file.write(r_line) 52 else: 53 if not has_write: 54 for new_line in record_list: 55 if new_line.startswith('backend'): 56 write_file.write(new_line+' ') 57 else: 58 write_file.write('%s%s ' %(' '*8,new_line)) 59 has_write=True 60 os.rename(filename, bak_file) 61 os.rename(new_file, filename) 62 os.remove(bak_file) 63 64 65 def fetch(data): 66 backend_data="backend %s" %data 67 return file_handle('haproxy.conf',backend_data,type='fetch') 68 69 def add(data): 70 backend=data['backend'] 71 record_list=fetch(backend) 72 current_record="server %s %s weight %s maxconn %s" %(data['record']['server'], 73 data['record']['server'], 74 data['record']['weight'], 75 data['record']['maxconn']) 76 backend_data="backend %s" %backend 77 78 if not record_list: 79 record_list.append(backend_data) 80 record_list.append(current_record) 81 file_handle('haproxy.conf',backend_data,record_list,type='append') 82 else: 83 record_list.insert(0,backend_data) 84 if current_record not in record_list: 85 record_list.append(current_record) 86 file_handle('haproxy.conf',backend_data,record_list,type='change') 87 88 def remove(data): 89 backend=data['backend'] 90 record_list=fetch(backend) 91 current_record="server %s %s weight %s maxconn %s" %(data['record']['server'], 92 data['record']['server'], 93 data['record']['weight'], 94 data['record']['maxconn']) 95 backend_data = "backend %s" % backend 96 if not record_list or current_record not in record_list: 97 print('