程序需求如下:
1. 根据用户输入输出对应的backend下的server信息
2. 可添加backend 和sever信息
3. 可修改backend 和sever信息
4. 可删除backend 和sever信息
5. 操作配置文件前进行备份
6 添加server信息时,如果ip已经存在则修改;如果backend不存在则创建;若信息与已有信息重复则不操作
代码如下:
#!/usr/bin/env python3 # -*-conding:utf-8-** import json, shutil def fetch(backend): """ 根据用户输入站点查找对应配置项目 :param backend: 从主程序读取的用户输入的站点配置条目 :return: 返回配置的值 """ config_list = [] with open('haproxy','r',encoding='utf-8') as f: flag = False # 用于标志所读内容是否为需要的数据 for line in f: if line.strip().startswith('backend') and line.strip() == 'backend ' +backend: # 判断以backend开始,且后面内容和用户输入的域名相同,即所需要查询的配置条目 flag = True continue if line.strip().startswith('backend'): # 判断以backend开头,与用户输入域名不相同、即不是要查询的配置项目 flag = False if flag and line.strip(): config_list.append(line.strip()) return config_list def add(backend,record): """ 用于增加用户输入的配置项目 :param backend: 用户输入的要增加的backend和域名 :param record: 用户输入的服务器记录 :return: """ record_list = fetch(backend) if not record_list: # 用户输入的backend不存在 with open('haproxy','r') as old,open('new_haproxy','w') as new: for line in old: new.write(line) new.write(' backend ' + backend +' ') new.write(" " * 8 + record + ' ') else: # 用户输入的backend存在 if record in record_list: shutil.copy('haproxy','new_haproxy') # 复制配置文件到新文件中 print("配置列表添加退出,没有配置文件被改变!") else: # 用户输入的backend已经存在,record不存在 record_list.append(record) with open('haproxy','r') as old,open('new_haproxy','w') as new: flag = False for line in old: if line.strip().startswith("backend ") and line.strip() == "backend " + backend: # 如果该行以backend开始并且等于用户输入的backend域名 flag = True new.write(line) for new_line in record_list: new.write(" " * 8 + new_line + ' ') continue if flag and line.strip().startswith('backend'): flag = False new.write(line) continue if line.strip() and not flag: new.write(line) print("新增成功,新纪录为%s%s"%(backend,record)) shutil.copy('haproxy','haproxy_history') # 备份原文件 shutil.copy('new_haproxy','haproxy') # 把新修改好的文件改成正式文件 def del_item(backend): """ 用于删除用户输入的配置项目 :param backend: 用户输入的要删除的backend和域名 :param record: 用户输入的要删除的服务器记录 :return: """ record_list = fetch(backend) print(record_list) if not record_list: with open('haproxy','r') as old, open('new_haproxy','w') as new: for line in old: new.write(line) print("没有此项目,不需要删除") else: with open('haproxy', 'r') as old, open('new_haproxy', 'w') as new: flag = False for line in old: if line.strip().startswith("backend ") and line.strip() == "backend " + backend: flag = True continue if flag and line.strip().startswith('backend'): flag = False new.write(line) continue if line.strip() and not flag: new.write(line) print("%s已删除"%backend) shutil.copy('haproxy','haproxy_history') shutil.copy('new_haproxy','haproxy') if __name__ == '__main__': while True: print("欢迎来到ha配置工具".center(50,'-')) start_choice = input("1.获取ha记录 2.增加ha记录 3.删除ha记录 请输入你想要做什么:[q=quit]") print(''.center(50,'-')) if start_choice == 'q': exit() elif start_choice == '1': option1_choice = input('1.查询配置,请输入backend项目域名:') print_conf = fetch(option1_choice) print('你要查询的配置为:') print(print_conf) elif start_choice == '2': str_exa = '{"backend":"www.oldboy.org","record":{"server":"100.1.7.9","weight":"20","maxconn":3000}}' mod_config = input('2.选择要增加的配置项目及配置条目,格式%s: ' % str_exa) dic = json.loads(mod_config) bk = dic['backend'] rd = "server %s %s weight %s maxconn %s" % (dic['record']['server'], dic['record']['server'], dic['record']['weight'], dic['record']['maxconn']) add(bk,rd) elif start_choice == '3': str_del = '{"backend":"www.oldboy.org","record":{"server":"100.1.7.9","weight":"20","maxconn":3000}}' del_config = input('2.选择要删除的配置项目及配置条目,格式%s: ' % str_del) del_dic = json.loads(del_config) del_bk = del_dic['beckend'] del_item(del_bk) else: print("输入错误")