流程图:
代码
1 #!/sur/bin/env python 2 # -*- coding: utf-8 -*- 3 #{"backend": "www.oldboy.org","record":{"server": "100.1.7.90","weight": 20,"maxconn": 30}} 4 import json 5 import sys 6 7 def add_ha(backend_de): 8 backend_dic = json.loads(backend_de) 9 backend_add = 'server %s weight %s maxconn %s'%(backend_dic['record']['server'],backend_dic['record']['weight'],+backend_dic['record']['maxconn'])10 with open("haproxy.conf") as read_f,open("ceshi","w") as write_f :11 for lines in read_f.readlines():12 write_f.write(lines)13 write_f.write('backend'' '+backend_dic['backend']+'\n')14 write_f.write(' '*8+backend_add) #15 16 def delet_ha(backend_de):17 backend_dic = json.loads(backend_de) ##将字符串转换为相印的数据类型18 backned_check=list(bakcned_dic['backend']) #www.oldboy.org19 backend_del = ' server %s weight %s maxconn %s\n'%(backend_dic['record']['server'],backend_dic['record']['weight'],+backend_dic['record']['maxconn']) ##数据类容20 with open("haproxy.conf") as read_f,open("ceshi","w") as write_f :21 for lines in read_f.readline():22 backend_list=[]23 backend_list.append(lines.strip())24 backend_list.append(lines)25 if lines.strip().split()[2] == backned_check:26 continue27 elif backend_del == lines :28 backend_list.remove(lines)29 write_f.write(backend_list)30 31 def change_ha(backend_de):32 backend_dic = json.loads(backend_de) ##将字符串转换为相印的数据类型33 backned_check=list(bakcned_dic['backend']) #www.oldboy.org34 backend_del = ' server %s weight %s maxconn %s\n'%(backend_dic['record']['server'],backend_dic['record']['weight'],+backend_dic['record']['maxconn']) ##数据类容35 with open("haproxy.conf") as read_f,open("ceshi","w") as write_f :36 for lines in read_f.readline():37 backend_list=[]38 backend_list.append(lines.strip())39 backend_list.append(lines)40 if lines.strip().split()[2] == backned_check:41 write_f.write(backend_del)42 continue43 elif backend_del == lines :44 print("类容已存在,请确定后再修改")45 sys.exit()46 write_f.write(backend_list)47 48 while True:49 number_ha = int(input("1、添加\n2、删除\n3、修改\n请输入以上阿拉伯数字:"))50 if number_ha == 1:51 backend_de=input("请输入backend:") #如输入:["bakcend","www.oldboy.org"]52 add_ha(add_file)53 elif number_ha == 2:54 backend_de=input("请输入要删除的bakcned:") #如 www.oldboy.org55 delet_ha(delet_file)56 elif number_ha == 3:57 backend_de=input("请输入要删除的bakcned:")58 change_ha()59 else:60 print("你输入的功能有误,请重新输入")61 continue
ha配置文件
1 frontend oldboy.org2 bind 0.0.0.0:803 4 backend www.oldboy.org5 server 100.1.7.9 1020.1.7.9 weight 20 maxconn 30006 7 backend buy.oldboy.org8 server 100.1.7.90 100.1.7.90 weight 20 maxconn 30000