不废话,上代码
readme:
# Auther:ccorz Mail:ccniubi@163.com Blog:http://www.cnblogs.com/ccorz/ # GitHub:https://github.com/ccorzorz ha_proxy配置文件修改程序 1.查询时输入域名即可查询,显示域名相关的backend配置 2.删除时输入域名,即可删除包括域名以及配置服务器信息的相关内容;如相关域名信息,会提示用户,不改动文件 3.修改时需要输入列表信息,列表中需要包括backend的域名信息,以及包含weight、server、maxconn的record列表 3.1 修改需输入内容举例:{"backend": "test.oldboy.org","record":{"server": "100.1.7.9","weight": 20,"maxconn": 30}} 3.2 程序会自动判断输入的格式是否正确,如果不正确,会提醒用户重新输入,直至正确格式 3.3 程序会自动判断域名信息是否存在,如存在,增加服务器信息;如不存在,追加至配置文件结尾 3.4 程序会自动判断用户想增加的服务器信息是否与已存在的配置重叠,如存在,提示用户,不修改文件;如不存在,在域名相关信息后追加 4.程序会自动备份修改之前的文件,后缀为修改时间 5.本程序有日志记录,以便管理员查询修改记录
流程图:
ha_proxy配置文件:
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 server 100.1.7.9 100.1.7.9 weight 20 maxconn 30 backend buy.oldboy.org server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000
程序代码:
#!/usr/bin/env python # -*-coding=utf-8-*- # Auther:ccorz Mail:ccniubi@163.com Blog:http://www.cnblogs.com/ccorz/ # GitHub:https://github.com/ccorzorz import json,time,os time_now=time.strftime('%Y-%m-%d %H:%M:%S') time_flag=time.strftime('%Y-%m-%d-%H-%M-%S') #定义通过域名查询ha配置文件函数 def search_backend(search_info): """ :param search_info: 用户输入的需要查询的域名 :return: result_list :结果列表,供增删改使用 """ b_flag=1 #设置初始标识符 result_list=[] #设置查询结果为空列表 with open('ha','r') as ha_read: for line in ha_read.readlines(): #将内容通过readlines转化为列表 line=line.strip() #每一行去掉两头的空格和回车 if line=='backend %s'%search_info: #发现内容匹配 b_flag=0 #更改标识符值 elif line.startswith('backend'): #发现以'backend'开头的内容 b_flag=1 #更改标识符为初始值 elif b_flag==0 and len(line)!=0: #将标识符为0的非空内容加入查询结果列表 result_list.append(line) return result_list def add_backend(): """ :return: None """ while True: add_contect_str=input('Input the server_info:') #提示用户输入配置文件信息 res=enter_query(add_contect_str) #将确认格式函数的返回值赋予res变量 if res==False: #如果变量为Flase,提醒用户输入的字符串格式不正确 print('String format error,try again!') if res == True: #如果变量值为True,即用户输入格式正确,执行以下 # print(result_list) add_contect=json.loads(add_contect_str) #通过json模块,将字符串转化为列表 search_info=add_contect['backend'] #变量赋值 weight=add_contect['record']['weight'] server=add_contect['record']['server'] maxconn=add_contect['record']['maxconn'] server_info='server %s %s weight %s maxconn %s'%(server,server,weight,maxconn) result_list=search_backend(search_info) #将字符串中解析得到的列表赋值于result_list print(result_list) if len(result_list)==0: #如果无对应的域名,新增域名以及对应的记录 with open('ha','r') as ha_read,open('ha_new','w') as ha_wirte: con_list=ha_read.readlines() #通过readlines将ha文件中的内容转化为列表 con_list.append(' ') #列表中加一空行 con_list.append('backend %s '%search_info) #列表中新增域名信息 con_list.append('%s%s'%(8*' ',server_info)) #列表中追加域名对应服务器信息 ha_wirte.writelines(con_list) #将列表通过writelines全部写入ha_write文件 #回显,提示用户无域名信息,将新增信息到配置文件最后 print('No domain target,will add to the end of the "ha",has been add the end of file..') time.sleep(1) print('