程序1: 实现简单的shell sed替换功能
程序2:修改haproxy配置文件

1 1、查 2 输入:www.oldboy.org 3 获取当前backend下的所有记录 4 5 2、新建 6 输入: 7 arg = { 8 'bakend': 'www.oldboy.org', 9 'record':{ 10 'server': '100.1.7.9', 11 'weight': 20, 12 'maxconn': 30 13 } 14 } 15 16 3、删除 17 输入: 18 arg = { 19 'bakend': 'www.oldboy.org', 20 'record':{ 21 'server': '100.1.7.9', 22 'weight': 20, 23 'maxconn': 30 24 } 25 } 26 27 需求

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.oldboy.org 29 server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000 30 31 原配置文件
1 #_*_coding:utf-8_*_ 2 import os 3 def file_handle(filename,backend_data,record_list=None,type='fetch'): #type:fetch append change 4 new_file=filename+'_new' 5 bak_file=filename+'_bak' 6 if type == 'fetch': 7 r_list = [] 8 with open(filename, 'r') as f: 9 tag = False 10 for line in f: 11 if line.strip() == backend_data: 12 tag = True 13 continue 14 if tag and line.startswith('backend'): 15 break 16 if tag and line: 17 r_list.append(line.strip()) 18 for line in r_list: 19 print(line) 20 return r_list 21 elif type == 'append': 22 with open(filename, 'r') as read_file, 23 open(new_file, 'w') as write_file: 24 for r_line in read_file: 25 write_file.write(r_line) 26 27 for new_line in record_list: 28 if new_line.startswith('backend'): 29 write_file.write(new_line + ' ') 30 else: 31 write_file.write("%s%s " % (' ' * 8, new_line)) 32 os.rename(filename, bak_file) 33 os.rename(new_file, filename) 34 os.remove(bak_file) 35 elif type == 'change': 36 with open(filename, 'r') as read_file, 37 open(new_file, 'w') as write_file: 38 tag=False 39 has_write=False 40 for r_line in read_file: 41 if r_line.strip() == backend_data: 42 tag=True 43 continue 44 if tag and r_line.startswith('backend'): 45 tag=False 46 if not tag: 47 write_file.write(r_line) 48 else: 49 if not has_write: 50 for new_line in record_list: 51 if new_line.startswith('backend'): 52 write_file.write(new_line+' ') 53 else: 54 write_file.write('%s%s ' %(' '*8,new_line)) 55 has_write=True 56 os.rename(filename, bak_file) 57 os.rename(new_file, filename) 58 os.remove(bak_file) 59 60 61 def fetch(data): 62 backend_data="backend %s" %data 63 return file_handle('haproxy.conf',backend_data,type='fetch') 64 def add(data): 65 backend=data['backend'] 66 record_list=fetch(backend) 67 current_record="server %s %s weight %s maxconn %s" %(data['record']['server'], 68 data['record']['server'], 69 data['record']['weight'], 70 data['record']['maxconn']) 71 backend_data="backend %s" %backend 72 73 if not record_list: 74 record_list.append(backend_data) 75 record_list.append(current_record) 76 file_handle('haproxy.conf',backend_data,record_list,type='append') 77 else: 78 record_list.insert(0,backend_data) 79 if current_record not in record_list: 80 record_list.append(current_record) 81 file_handle('haproxy.conf',backend_data,record_list,type='change') 82 def remove(data): 83 backend=data['backend'] 84 record_list=fetch(backend) 85 current_record="server %s %s weight %s maxconn %s" %(data['record']['server'], 86 data['record']['server'], 87 data['record']['weight'], 88 data['record']['maxconn']) 89 backend_data = "backend %s" % backend 90 if not record_list or current_record not in record_list: 91 print('