haproxy配置文件简单管理

时间:2023-03-09 14:51:57
haproxy配置文件简单管理

版本:python3
功能:对haproxy配置文件进行简单的查询、添加以及删除功能
操作流程:
1.根据提示选择相应的选项
2.进入所选项后,根据提示写入相应的参数
3.查询功能会返回查询结果,添加、删除以及回退功能无回显

流程图:

haproxy配置文件简单管理

 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 backend buy.oldboy.org
server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000 backend nb.oldboy.org
server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000 backend ttt.oldboy.org
server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000 backend www.oldboy.com
server 192.168.1.1 192.168.1.1 weight 30 maxcoon 3000

haproxy配置示例文件

 import os
import os.path
import time
def query(back_name):#定义查询函数
flage=False #[通过用户给定的backend,找到backend所在行,在寻找下一个以backend开头的行,将两个backend中间的内容添加进列表中]
query_list=[]
with open('ha','r') as ha: #打开配置文件
for line in ha: #获取配置文件的内容
if line.strip()=='backend %s'%(back_name): #获取要查询的backend所在行的内容
flage=True#善用标志位
continue
if line.strip().startswith('backend'):#判断是否行开头为backend
flage=False
if flage and line.strip(): #判断flage是否为真
query_list.append(line.strip())#将符合的内容添加单独进列表中
return query_list def add_backend(back_name,ip,weight,maxcoon):#定义添加函数
current_title='backend %s'%back_name #获得用户输入backend所在行内容
current_record='server %s %s weight %s maxcoon %s'%(ip,ip,weight,maxcoon) #获得用户所要添加的server
fetch_list=query(back_name) #获取指定的backend下的记录
if fetch_list: #如果添加的backend存在
if current_record in fetch_list: #如果添加的子项存在backend中,则不进行操作
pass # '''【主要思路:通过判断,将需要添加的backend下的内容和原来已经存在的内容存入一个列表,将需要添加的
# backend之前的内容写入新文件,再将backend和存入列表中需要添加的sever写入,最后添加其余内容】'''
else:
fetch_list.append(current_record)#如果不存在backend中,添加进指定的backend中
flage=False #定义标示位,用于判断前后backend之间需要写入的内容
has_write=False##定义标示位,用于判断需要添加的内容是否已存在
with open('ha','r') as ha,open('ha.new','w') as new_ha: #打开ha配置文件,创建一个新的文件
for line in ha: #迭代配置文件
if line.strip() == current_title: #判断文件中是否有用户输入的backend
flage=True
new_ha.write(line)#把backend写入到新配置文件内
continue
if flage and line.strip().startswith('backend'):
flage=False
if flage :
if not has_write: #通过标示位判断
for line in fetch_list:#获得需要添加的backend下的所以server
temp="%s%s\n" %(" "*8,line)
new_ha.write(temp) #写入新的文件中
has_write = True
else:
new_ha.write(line)#将其余内容写入到新的文件中
else:#如果用户输入的backend不存在配置文件中
with open('ha','r') as ha,open('ha.new','w') as new_ha:
for line in ha:
new_ha.write(line)#将配置文件的所以内容写入新的文件中
new_ha.write('\n'+current_title)#将用户输入的backend写入新的文件中
tem=''*8+current_record+'\n'
new_ha.write('\n'+tem)#将用户输入的sever写入文件中
os.rename('ha','ha_bak')
os.rename('ha.new','ha') def quit_time():#查询完成后退出
for i in [3,2,1]:
print('\033[32;1m查询完毕,正在返回主菜单.....\033[1m',i)
time.sleep(1) def del_backend(back_name,ip,weight,maxcoon): #与添加类似,通过将需要的server存入一个列表中,最后将其余内容和列表中的内容写入新的文件中
current_title='backend %s'%back_name #通过文件重命名的方式实现功能
current_record='server %s %s weight %s maxcoon %s'%(ip,ip,weight,maxcoon)
fetch_list=query(back_name) #获取指定的backend下的记录
if fetch_list: #如果添加的backend存在
if current_record in fetch_list: #如果添加的子项存在backend中
fetch_list.remove(current_record)
else:
pass
flage=False
has_write=False
with open('ha','r') as ha,open('ha.new','w') as new_ha:
for line in ha:
if line.strip() == current_title:
flage=True
new_ha.write(line)#把backend写入到新配置文件内
continue
if flage and line.strip().startswith('backend'):
flage=False if flage :
if not has_write:
for line in fetch_list:
temp="%s%s\n" %(" "*8,line)
new_ha.write(temp)
has_write = True
else:
new_ha.write(line)
else:
with open('ha','r') as ha,open('ha.new','w') as new_ha:
for line in ha:
new_ha.write(line)
new_ha.write('\n'+current_title)
tem=''*8+current_record+'\n'
new_ha.write('\n'+tem)
if os.path.isfile('ha_bak'): #文件重命名需要判断文件是否存在,当第一次时,备份文件不存在会报错
os.remove('ha_bak')
os.rename('ha','ha_bak')
os.rename('ha.new','ha') def fallback(): #回退代码,将备份文件和当前配置文件重命名的方式实现功能
os.rename('ha','ha_new')
os.rename('ha_bak','ha') while True:
print('\033[34;1m####################################### \033[1m') #打印选项
user_enter=input('''\033[32;1m 1.查询后端backend
2.添加后端backend
3.删除后端backend
4.回滚至前一版本
5.退出\033[1m
请输入你要选择的操作:''' )
if user_enter =='':
back_name=input('\033[32;1m请输入需要查询的backend名称,example:www.oldboy.org:\033[1m')
result=query(back_name) #调用查询函数
if result:
print('\033[33;1m查询结果如下:\033[1m')
for i in result:
print(i)
quit_time()
continue
else:
print('\033[34;1m查询错误,backend不存在,结果为空!\033[1m')
elif user_enter=='':
back_name=input('\033[32;1m请输入需要查询的backend名称,example:www.oldboy.org:\033[1m')
ip=input('\033[33;1m请输入要添加的serverIP地址,example192.168.1.1:\033[1m')
weight=input('\033[34;1m请输入该server的weight:\033[1m')
maxcoon=input('\033[35;1m请输入该server的maxcoon:\033[1m')
add_backend(back_name,ip,weight,maxcoon)
print('添加完成')
quit_time()
elif user_enter=='':
back_name=input('\033[32;1m请输入需要删除的backend名称,example:www.oldboy.org:\033[1m')
ip=input('\033[33;1m请输入要添加的serverIP地址,example192.168.1.1:\033[1m')
weight=input('\033[34;1m请输入该server的weight:\033[1m')
maxcoon=input('\033[35;1m请输入该server的maxcoon:\033[1m')
del_backend(back_name,ip,weight,maxcoon)
print('删除完成')
quit_time()
elif user_enter=='':
try:
fallback()
except FileNotFoundError: #回退时,备份文件不存在报错
print('\033[31;1m回退失败,当前为最新版本,无法回退!\033[1m')
except FileExistsError:#用户重复回退
print('已经是最新版本')
quit_time()
elif user_enter=='':
quit_time()
break
else:#非法输入时
print('\033[31;1m您输入的选项错误,请重新选择!\033[1m')

程序代码