修改ha.conf配置文件
应用场景说明:
现有一配置文件如下所示,要求查看、增加、删除所给的backend项。
实现思路:
查询:逐行读取ha.conf,如果找到所给的backend项,则将其下所有项打包成列表打印出来
增加:如果record项在查询结果的列表中,则退出。否则在列表中增加一项,再逐行写入ha_new.conf
删除:如果record项在查询结果的列表中,则将此项从结果列表中删除,再逐行写入ha_new.conf
ha.conf
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 www.oldboy.org if www backend hao.oldboy.org server 100.1.7.9 weight 20 maxconn 3000 backend test.oldboy.org server 100.1.7.9 weight 20 maxconn 3000 backend buy.oldboy.org server 100.1.7.90 weight 20 maxconn 3000
流程图

实现代码:
#!/usr/bin/env python3 # _*_ coding:utf-8 _*_ import json import sys def ha_check(backend): check_list = [] chk_flag = False with open('ha.conf') as f: for line in f: line.strip() if line.startswith('backend'): if line.split()[1] == backend: chk_flag = True continue if chk_flag: if line.startswith('backend'): break if chk_flag and len(line.strip()) > 0: check_list.append(line.strip()) if check_list: return(check_list) else: print('输入的查看项不存在') return(False) def ha_add(dict_backend): dict_backend = json.loads(dict_backend) ha_msg = '''server %s weight %s maxconn %s'''%( dict_backend['record']['server'], dict_backend['record']['weight'], dict_backend['record']['maxconn'] ) ha_chk_list = ha_check(dict_backend['backend'].strip()) if ha_chk_list: if ha_msg in ha_chk_list: sys.exit('增加项已存在') else: ha_chk_list.append(ha_msg) with open('ha.conf') as old,open('ha_new.conf','w') as new: write_flag = True for line in old: if line.strip().startswith('backend'): if line.split()[1].strip() == dict_backend['backend'].strip(): new.write('backend '+ dict_backend['backend'].strip() + '\n') for x in ha_chk_list: new.write(' '*8 + x + '\n') write_flag = False else: write_flag = True if write_flag: new.write(line) else: with open('ha.conf') as old,open('ha_new.conf','w') as new: new.write(old.read()) new.write('backend ' + dict_backend['backend'] + '\n') new.write(' '*8 + ha_msg) def ha_delete(dict_backend): dict_backend = json.loads(dict_backend) ha_msg = '''server %s weight %s maxconn %s'''%( dict_backend['record']['server'], dict_backend['record']['weight'], dict_backend['record']['maxconn'] ) ha_chk_list = ha_check(dict_backend['backend'].strip()) if ha_chk_list: if ha_msg in ha_chk_list: ha_chk_list.remove(ha_msg) else: print('删除项%s不存在%s'%(dict_backend['backend'].strip(),ha_msg)) with open('ha.conf') as old,open('ha_new.conf','w') as new: write_flag = True for line in old: if line.strip().startswith('backend'): if line.split()[1].strip() == dict_backend['backend'].strip(): new.write('backend '+ dict_backend['backend'].strip() + '\n') for x in ha_chk_list: new.write(' '*8 + x + '\n') write_flag = False else: write_flag = True if write_flag: new.write(line) else: print('输入项%s不存在'%dict_backend['backend']) with open('ha.conf') as old,open('ha_new.conf','w') as new: new.write(old.read()) new.write('backend ' + dict_backend['backend'] + '\n') new.write(' '*8 + ha_msg) def menu(): print(''' ----------- 1 查看 2 增加 3 删除 ----------- ''') def main(): menu() action_flag = True action = input('请选择序号:') if action == '1': print('输入示例:www.test.org') check_action = input('请输入查看字段:') check_action.strip() chk_list = ha_check(check_action) if chk_list: for s,c in enumerate(chk_list): print(s,c) else: sys.exit('您查看的配置不存在') if action == '2': print('输入示例:{"backend": "www.oldboy.org","record":{"server": "100.1.7.90","weight": 20,"maxconn": 30}}') add_action = input('请输入 >') ha_add(add_action) if action == '3': print('输入示例:{"backend": "www.oldboy.org","record":{"server": "100.1.7.90","weight": 20,"maxconn": 30}}') add_action = input('请输入 >') ha_delete(add_action) if __name__ == '__main__': main()
浙公网安备 33010602011771号