程序1: 實現簡單的shell sed替換功能shell
程序2:修改haproxy配置文件 app
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 + '\n') 30 else: 31 write_file.write("%s%s\n" % (' ' * 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+'\n') 53 else: 54 write_file.write('%s%s\n' %(' '*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('\033[33;1m無此條記錄\033[0m') 92 return 93 else: 94 #處理record_list 95 record_list.insert(0,backend_data) 96 record_list.remove(current_record) 97 file_handle('haproxy.conf',backend_data,record_list,type='change') 98 def change(data): 99 backend=data[0]['backend'] 100 record_list=fetch(backend) 101 102 old_record="server %s %s weight %s maxconn %s" %(data[0]['record']['server'],\ 103 data[0]['record']['server'],\ 104 data[0]['record']['weight'],\ 105 data[0]['record']['maxconn']) 106 107 new_record = "server %s %s weight %s maxconn %s" % (data[1]['record']['server'], \ 108 data[1]['record']['server'], \ 109 data[1]['record']['weight'], \ 110 data[1]['record']['maxconn']) 111 backend_data="backend %s" %backend 112 113 if not record_list or old_record not in record_list: 114 print('\033[33;1m無此內容\033[0m') 115 return 116 else: 117 record_list.insert(0,backend_data) 118 index=record_list.index(old_record) 119 record_list[index]=new_record 120 file_handle('haproxy.conf',backend_data,record_list,type='change') 121 122 123 if __name__ == '__main__': 124 msg=''' 125 1:查詢 126 2:添加 127 3:刪除 128 4:修改 129 5:退出 130 ''' 131 menu_dic={ 132 '1':fetch, 133 '2':add, 134 '3':remove, 135 '4':change, 136 '5':exit, 137 } 138 while True: 139 print(msg) 140 choice=input("操做>>: ").strip() 141 if len(choice) == 0 or choice not in menu_dic:continue 142 if choice == '5':break 143 144 data=input("數據>>: ").strip() 145 146 #menu_dic[choice](data)==fetch(data) 147 if choice != '1': 148 data=eval(data) 149 menu_dic[choice](data) #add(data) 150 151 152 153 154 # [{'backend':'www.oldboy20.org','record':{'server':'2.2.2.3','weight':20,'maxconn':3000}},{'backend':'www.oldboy10.org','record':{'server':'10.10.0.10','weight':9999,'maxconn':33333333333}}]