功能:1. 根据用户输入输出对应的backend下的server信息
2. 可添加backend 和sever信息
3. 可修改backend 和sever信息
4. 可删除backend 和sever信息
5. 操作配置文件前进行备份
6. 添加server信息时,如果ip已经存在则修改;如果backend不存在则创建;若信息与已有信息重复则不操作
环境:Windows 10 专业版
PyCharm 2016.3.2
python 3.5
配置文件:
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 backend www.oldboy.org server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
流程图:
代码:
#!/usr/bin/env python # _*_ coding:utf-8 _*_ # __author__ = "lx" # 1. 根据用户输入输出对应的backend下的server信息 # 2. 可添加backend 和sever信息 # 3. 可修改backend 和sever信息 # 4. 可删除backend 和sever信息 # 5. 操作配置文件前进行备份 # 6 添加server信息时,如果ip已经存在则修改;如果backend不存在则创建;若信息与已有信息重复则不操作 import json import copy #HAproxy为原文件;HAproxy.txt为传递文件;HAproxy.BAK为备份文件 #备份文件函数 def Backup(): with open("HAproxy",'r') as r ,open("HAproxy.BAK",'w') as w: for line in r: w.write(line) #查询信息函数 def query(backend): flag = False #用于定位server信息的区域 query_list = [] #用于传递查询出的信息 with open("HAproxy",'r') as f: for line in f: if line.strip() == "backend %s" %(backend): flag = True continue if line.strip().startswith('backend'): flag = False if flag and line.strip(): query_list.append(line.strip()) return query_list #添加信息函数 def add(data_dict): flag = False whether_w = True #辅助判断原文件与修改文件是否重复 backend_flag = data_dict.get("backend") #从字典中取出backend数据 record_list = data_dict.get("record") #从字典中取出server数据 insert_backend = "backend %s"%(backend_flag) #用于将backend信息写入文件 insert_record = " server %s weight %s maxconn %s"%(record_list['server'],record_list['weight'],record_list['maxconn']) #用于将server信息写入文件 query_list = query(backend_flag) #将原server信息写入列表用于保护原有信息 with open("HAproxy",'r') as r ,open("HAproxy.txt",'w') as w: for line in r: if line.strip() == "backend %s" %(backend_flag): w.write(line) flag = True whether_w = False continue if flag and line.strip().startswith('backend'): flag = False if flag: for line_old in query_list: w.write(' '*8+line_old) w.write(" "+insert_record+" ") else: w.write(line) if whether_w: w.write(" "+insert_backend+" ") w.write(insert_record+" ") with open("HAproxy.txt",'r') as r ,open("HAproxy",'w') as w: for line in r: w.write(line) print("添加成功!!") #修改信息函数 def amend(data_dict): data_copy_dict = copy.deepcopy(data_dict) #复制修改前的信息用于在文件中定位修改信息 select = input("请需修改的信息的编号: 1、backend 2、server 3、weight 4、maxconn ").strip() if select == '1': data_dict['backend']=input("请输入修改后的数据:").strip() if select == '2': data_dict["record"]['server']=input("请输入修改后的数据:").strip() if select == '3': data_dict["record"]['weight']=input("请输入修改后的数据:").strip() if select == '4': data_dict["record"]['maxconn']=input("请输入修改后的数据:").strip() flag = False #定位server信息的区域 whether_w = True #判断要修改的信息是否存在 has_w = True #避免在循环中的重复写入文件操作 backend_flag = data_copy_dict.get("backend") #从字典中读出backend信息便于定位 record_list = data_copy_dict.get("record") #从字典中读出server信息用于保护原有信息 record_new_list = data_dict.get("record") #将要修改的信息写入列表,便于将要修改的信息写入文件 insert_record = " server %s weight %s maxconn %s" % (record_list['server'], record_list['weight'], record_list['maxconn']) insert_new_record = " server %s weight %s maxconn %s" % (record_new_list['server'], record_new_list['weight'], record_new_list['maxconn']) query_list = query(backend_flag) #将原server信息写入列表用于保护原有信息 with open("HAproxy", 'r') as r, open("HAproxy.txt", 'w') as w: for line in r: if line.strip() == "backend %s" % (backend_flag): if select == '1': w.write("backend %s " % (data_dict['backend'])) else: w.write(line) flag = True whether_w = False continue if flag and line.strip().startswith('backend'): flag = False if flag: for line_old in query_list: if select != '1' and line_old == insert_record.strip() and has_w: w.write(insert_new_record+" ") has_w = False else: w.write(' ' * 8 + line_old+" ") else: w.write(line) if whether_w: print("修改的信息不存在..") with open("HAproxy.txt", 'r') as r, open("HAproxy", 'w') as w: for line in r: w.write(line) print("修改成功!!") #删除信息函数 def delete(data_dict): flag = False backend_flag = data_dict.get("backend") #从字典中读出backend信息便于定位 record_list = data_dict.get("record") #将要删除的server信息写入列表,便于定位 insert_record = " server %s weight %s maxconn %s" % (record_list['server'], record_list['weight'], record_list['maxconn']) query_list = query(backend_flag) #将原有server信息写入列表进行保护 select = input("请输入要删除的信息编号: 1、backend 2、server ").strip() with open("HAproxy", 'r') as r,open("HAproxy.txt", 'w') as w: for line in r: if line.strip() == "backend %s" %(backend_flag): if select != '1': w.write(line) flag = True whether_w = False continue if flag and line.strip().startswith('backend'): flag = False if flag: if select == '1': continue else: for line_old in query_list: if line_old == insert_record.strip(): continue else: w.write(' ' * 8 + line_old) else: w.write(line) with open("HAproxy.txt", 'r') as r, open("HAproxy", 'w') as w: for line in r: w.write(line) print("删除成功!!") #主函数 if __name__ == "__main__": Backup() while True: data_str = None data_dict = {} print(" _______选择界面_______ ") print("1、 查询信息") print("2、 添加信息") print("3、 修改信息") print("4、 删除信息") print("0、 退出程序 ") select = input("请选择操作:").strip() if select == '1': backend = input("请输入backend:").strip() query_list = query(backend) print("backend:%s下的信息为:" % (backend)) if query_list: for i in query_list: print(i) else: print("您查询的backend不存在!!") elif select == '0': break else: print("请按照以下格式输入信息:") print(' {"backend":"www.oldboy.org","record":{"server":"100.1.7.9","weight":"20","maxconn":"3000"}}') if select == '2': data_str = input("请输入需添加信息:") data_dict = json.loads(data_str) add(data_dict) elif select == '3': data_str = input("请输入需修改信息:") data_dict = json.loads(data_str) amend(data_dict) elif select == '4': data_str = input("请输入需删除信息:") data_dict = json.loads(data_str) delete(data_dict) else: print("输入错误!!请重新选择") print("退出程序!!")