首页 > 代码库 > 第三周:HAproxy配置文件操作
第三周:HAproxy配置文件操作
作业
HAproxy配置文件操作:
1. 根据用户输入输出对应的backend下的server信息
2. 可添加backend 和sever信息
3. 可修改backend 和sever信息
4. 可删除backend 和sever信息
5. 操作配置文件前进行备份
6 添加server信息时,如果ip已经存在则修改;如果backend不存在则创建;若信息与已有信息重复则不操作
#Author xq import os import shutil import time def f_new(f_src): print(" 开始执行新建函数".center(60,‘*‘)) f_input = open(‘../db/HAproxyNew‘, ‘w‘) with open(f_src,‘r‘,encoding=‘utf-8‘) as f_output: for line in f_output: if line.lstrip().startswith(‘backend‘) == True: continue elif line.lstrip().startswith(‘server‘) == True: continue else: f_input.write(line) f_input.close() return None def f_dic(filename): backend_dic={} server_dic={} server_list=[] with open(filename,‘r+‘,encoding=‘utf-8‘) as f_src: for line in f_src: line=line.strip() if line.startswith(‘backend‘) == True: backend_name = line.split()[1] # backend_list.append(backend_name) server_list = [] elif line.startswith(‘server‘) == True: server_dic[‘server‘] = line.split()[1] server_dic[‘weight‘] = line.split()[3] server_dic[‘maxconn‘] = line.split()[5] server_list.append(server_dic.copy()) backend_dic[backend_name] = server_list return backend_dic def f_backend_list(backend_dic): backend_list=[] for k in backend_dic.keys(): backend_name=k backend_list.append(backend_name) return backend_list def backend_list_show(backend_list,backend_dic): print("backend列表如下".center(60,‘*‘)) backend_show_dict = {} for k,v in enumerate(backend_list): print(k,v) backend_show_dict[k] = v return backend_show_dict def server_show(backend_show_dict,backend_dic,choice): print("查询结果如下".center(60,‘*‘)) show_name=backend_show_dict[int(choice)] #print(backend_dic.values()) print("backend",show_name) print(‘\tserver‘,backend_dic[show_name][0][‘server‘],end=‘ ‘) print(‘\tweight‘,backend_dic[show_name][0][‘weight‘],end=‘ ‘) print(‘\tmaxconn‘,backend_dic[show_name][0][‘maxconn‘],end=‘ ‘) print("\n",end=‘ ‘) def backend_insert(backend_dic): print("增加结果如下".center(60,‘*‘)) while True: i_choice=input("继续增加backend节点:y; 退出:q;") if i_choice == ‘y‘: backend_new={} server_list=[] backend_name_new=input("请输入增加的backend名称:") backend_new[‘server‘]=input("请输入%s下新增server值:"%backend_name_new) backend_new[‘weight‘]=input("请输入%s下新增weight值:"%backend_name_new) backend_new[‘maxconn‘]=input("请输入%s下新增maxconn值:"%backend_name_new) if backend_name_new in backend_dic.keys(): print("节点已存在") #if backend_new[‘server‘] in backend_dic.values(): #print(backend_dic[backend_name_new][0][k]) if backend_new[‘server‘] == backend_dic[backend_name_new][0][‘server‘]: print(‘IP已存在,只修改weight、maxconn的值‘) backend_dic[backend_name_new][0][‘weight‘]=backend_new[‘weight‘] backend_dic[backend_name_new][0][‘maxconn‘]=backend_new[‘maxconn‘] else: print("新增的server信息如下:\n%s"%backend_new) server_list.append(backend_new.copy()) backend_dic[backend_name_new]=server_list elif i_choice == ‘q‘: break else : print("输入错误,请重新输入") continue print("新增操作后,字典内容",backend_dic) return def backend_del(backend_show_dict,backend_dic,choice): print("删除结果如下".center(60,‘*‘)) show_name=backend_show_dict[int(choice)] while True: del_choice=input("删除此backend名称:y; 删除此backend下的server信息:s; 退出:q;") if del_choice == ‘y‘: del backend_dic[show_name] break elif del_choice == ‘s‘: for k in backend_dic[show_name][0].keys(): print(k, backend_dic[show_name][0][k]) option=input("进行选择,删除:y; 跳过:p;退出:q;") if option == ‘y‘: print(backend_dic[show_name][0]) backend_dic[show_name][0][k]=‘‘ continue elif option == ‘p‘: continue elif option == ‘q‘: break else: print("输入错误,请重新输入") continue break elif del_choice == ‘q‘: break else: print("输入错误,请重新输入") continue print("\n删除后,字典内容:",backend_dic) return backend_dic def backend_update(backend_show_dict,backend_dic,choice): print("输入修改内容".center(60,‘*‘)) show_name=backend_show_dict[int(choice)] old_name=backend_show_dict[int(choice)] print("backend",show_name) while True: g=input("修改此backend名称,修改:y;跳过:p;") if g == ‘y‘: backend_name_new=input("请输入新的backend名称:") print("修改backend名称前,字典内容",backend_dic) backend_dic[backend_name_new]=backend_dic.pop(old_name)# pop()如果为空则默认为-1最后一项。dict2.pop(‘name‘)删除并返回键为“name”的条目 show_name=backend_name_new print("修改backend名称后,字典内容",backend_dic) break elif g == ‘p‘: break else: print("输入的选项格式错误,请重新输入") continue next=input("继续修改server的值,继续:y;返回:任意键;") if next == ‘y‘: for k in backend_dic[show_name][0].keys(): print(k, backend_dic[show_name][0][k]) option=input("进行选择,修改:y; 跳过:p;") if option == ‘y‘: backend_dic[show_name][0][k]=input("请输入新的值%s:"%k) continue elif option == ‘p‘: continue else: print("输入的选项格式错误,请重新输入") continue else: print("此backend:%s修改完成"%show_name) print("更新后的字典:",backend_dic) return def f_Writeback(backend_dic,f_target) : print(" 开始执行回写函数".center(60,‘*‘)) print(‘打印配置文件backend节点内容‘.center(50,‘-‘)) with open(f_target,‘a+‘,encoding=‘utf-8‘) as f_input: for key in backend_dic.keys(): print("\nbackend",key,file=f_input) print(‘\tserver‘,backend_dic[key][0][‘server‘],end=‘ ‘,file=f_input) print(‘\tweight‘,backend_dic[key][0][‘weight‘],end=‘ ‘,file=f_input) print(‘\tmaxconn‘,backend_dic[key][0][‘maxconn‘],end=‘ ‘,file=f_input) print("\n",end=‘ ‘) return None #主程序开始 print("主程序开始,请输入您要对配置文件进行的操作".center(60,‘*‘)) b=f_dic(‘../db/HAproxy‘) while True: choice = input("i:查询;o:操作(增加、删除、修改);q:退出;请输入您的选择:") a=f_backend_list(b) if choice == ‘i‘: print("查询操作界面".center(100,‘#‘)) c=backend_list_show(a,b) choice=input("展示此backend的server信息,请选择序号:") server_show(c,b,choice) continue elif choice == ‘q‘: f_new(‘../db/HAproxy‘) f_Writeback(b,‘../db/HAproxyNew‘) if (os.path.exists(‘../db/HAproxy‘)): print("文件存在,进行删除") os.remove(‘../db/HAproxy‘) os.rename(‘../db/HAproxyNew‘,‘../db/HAproxy‘) exit() elif choice == ‘o‘: shutil.copyfile(‘../db/HAproxy‘,‘../db/‘+‘HAproxy‘+time.strftime(‘%Y-%m-%d‘,time.localtime(time.time()))) #输入选择 print("请选择操作内容") while True: choice_operation = input("a:新增;u:更新;d:删除;请选择您要进行的操作:") if choice_operation == ‘a‘: print("新增操作界面".center(100,‘#‘)) print("请输入新增内容") backend_insert(b) break elif choice_operation == ‘u‘: print("更新操作界面".center(100,‘#‘)) c=backend_list_show(a,b) choice=input("请选择您要修改的backend节点:") backend_update(c,b,choice) break elif choice_operation == ‘d‘: print("删除操作界面".center(100,‘#‘)) c=backend_list_show(a,b)#调用展示列表函数 choice=input("请选择您要进行删除操作的backend序号:") backend_del(c,b,choice) break else: print("输入的选项格式错误,请重新输入") continue continue else: print("输入的选项格式错误,请重新输入") continue
第三周:HAproxy配置文件操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。