#运行测试见末尾
#!/usr/bin/python
# -*- coding utf8 -*-
import os
def search(arg):
'''查询backend后端的配置信息'''
with open('haproxy.cfg',encoding='utf8',mode='r') as hacfg:
bo = bool(False)
back_info = ''
for line in hacfg:
if line.strip() == "backend %s" %(arg):
#print(line)
bo = not bo
print(bo,line)
continue
if bo == True and (line.startswith('backend') or line.startswith('front') or line.startswith('listen')):
break
elif bo == True:
back_info = back_info + str(line)
#print(back_info)
else:
continue
print(back_info)
#pass
def ins(arg):
'''插入配置'''
back_name = arg
server_ip = input('Please input the backend IP address: ').strip()
weight = input('Please input the weight num.: ').strip()
max_con = input('Please input the max connect num.: ').strip()
back_dic = {
'bakend':back_name,
'record':{
'server':server_ip,
'weight':weight,
'maxcon':max_con,
}
}
print(back_dic)
target = '\n' + 'backend ' + back_dic['bakend'] + '\n' + '\t'
ip_a = back_dic['record']['server']
weig = back_dic['record']['weight']
max_co = back_dic['record']['maxcon']
ser_info ='server ' + ip_a + ' ' + ip_a + ' weight ' + weig + ' maxconn ' + max_co
target = target + ser_info
print(target)
with open('haproxy.cfg', encoding='utf8', mode='a') as hacfg:
hacfg.write(target)
def dele(arg):
with open('haproxy.cfg',encoding='utf8',mode='r') as hacfg , open('haproxy.cfg.swap',mode='w') as hacfg.swap:
bo = bool(False)
back_info = ''
for line in hacfg:
if line.strip() == "backend %s" %(arg):
#print(line)
bo = not bo
print(bo,line)
continue
if bo == True and (line.startswith('backend') or line.startswith('listen') or line.startswith('frontend')):
bo = False
if bo == False:
hacfg.swap.write(line)
os.remove('haproxy.cfg')
os.rename('haproxy.cfg.swap','haproxy.cfg')
#---------------------------------------------------------------------
def search_front(arg):
with open('haproxy.cfg', encoding='utf8', mode='r') as hacfg:
bo = bool(False)
front_info = ''
for line in hacfg:
if line.strip() == "frontend %s" %(arg):
print(line)
bo = not bo
print(bo, line)
continue
if bo == True and (line.startswith('backend') or line.startswith('front') or line.startswith('listen')):
break
elif bo == True:
front_info = front_info + str(line)
# print(back_info)
else:
continue
print(front_info)
def search_listen(arg):
with open('haproxy.cfg', encoding='utf8', mode='r') as hacfg:
bo = bool(False)
listen_info = ''
for line in hacfg:
if line.strip() == "listen %s" % (arg):
# print(line)
bo = not bo
print(bo, line)
continue
if bo == True and (line.startswith('backend') or line.startswith('front') or line.startswith('listen')):
break
elif bo == True:
listen_info = listen_info + str(line)
else:
continue
print(listen_info)
def search_all(arg):
print('your input message is: ',arg)
cfg = str(arg)
cfg_ini = cfg.replace('backend','',1).replace('frontend','',1).replace('listen','',1)
print('What you want to do: ',cfg_ini)
if cfg.startswith('back'):
search(cfg_ini)
elif cfg.startswith('frontend'):
search_front(cfg_ini)
elif cfg.startswith('listen'):
search_listen(cfg_ini)
#pass
def add_all(arg):
cfg_info = str(arg)
cfg_ini = cfg_info.replace('backend', '', 1).replace('frontend', '', 1).replace('listen', '', 1).strip()
targt=''
if cfg_info.startswith('listen'):
listen_name = cfg_ini
bind = input('Please input the bind IP address and port(for example 184.168.22.196:80): ').strip()
ip_a = input('Please input the IP address and port(for example 10.2.3.5:80) : ').strip()
weig = input('Please input the weight num.: ').strip()
max_co = input('Please input the max connect num.: ').strip()
target = '\n' + 'listen ' + listen_name + '\n' + '\t'
bind_info = 'bind' + ' ' + bind + '\n' + '\t'
ser_info = 'server ' + ip_a + ' ' + ip_a + ' weight ' + weig + ' maxconn ' + max_co
target = target + bind_info + ser_info
elif cfg_info.startswith('frontend'):
listen_name = cfg_ini
bind = input('Please input the bind IP address and port(for example 10.2.3.5:80): ').strip()
acl = 'acl ' + 'acl_' + cfg_ini + ' hdr(host) -i ' + bind
use_backend = 'host_' + cfg_ini + ' if ' + 'acl_' + cfg_ini
target = '\n' + 'frontend ' + listen_name + '\n\t'
target = target + 'bind' + '\t' + bind + '\n\t' + acl +'\n\t' +' use_backend ' + use_backend +'\n'
print('your add config is: ',target)
with open('haproxy.cfg', encoding='utf8', mode='a') as hacfg:
hacfg.write(target)
def del_all(arg):
cfg_info = str(arg)
cfg_ini = cfg_info.replace('backend', '', 1).replace('frontend', '', 1).replace('listen', '', 1).strip()
with open('haproxy.cfg',encoding='utf8',mode='r') as hacfg , open('haproxy.cfg.swap',mode='w') as hacfg.swap:
bo = bool(False)
back_info = ''
for line in hacfg:
if line.strip() == "listen %s" %(cfg_ini) or line.strip() == "frontend %s"%(cfg_ini):
bo = not bo
print(bo,line)
continue
if bo == True and line.startswith('listen'):
bo = not bo
if bo == True and line.startswith('frontend'):
bo = not bo
if bo == False:
hacfg.swap.write(line)
os.remove('haproxy.cfg')
os.rename('haproxy.cfg.swap','haproxy.cfg')
if __name__ == '__main__':
user_guide = '''
1:search
2:add configuration
3: delete backend
4:exit program
5:extended function(search frontend & listen,for example: 'listen baidu.com' 'frontend sohu.com')
6:extended function(add frontend & listen,for example: 'listen baidu.com' 'frontend sohu.com')
7:extended function(delete frontend & listen,for example: 'listen baidu.com' 'frontend sohu.com')
... ...
'''
menu={
1:search,
2:ins,
3:dele,
5:search_all,
6:add_all,
7:del_all,
}
while True:
print(user_guide)
try:
cho = int(input('Please input num.: ').strip())
except(ValueError,NameError):
print('input error,Please choice 1 to 5')
# except ValueError:
# print('input error')
# except NameError:
# print('input error')
#else:
# print('Please choice 1 to 5')
# finally:
# print('Please choice 1 to 5',)
cho = int(cho)
if cho == 0 :
continue
elif cho == 4 :
break
elif cho in menu:
data = str(input('Please input your domainname to operate: ').strip())
menu[cho](data)
else:
print('Please choice 1 to 5')
运行程序
#搜索部分测试内容
输入1 输入oldboy.com 查询backend的oldboy信息
输入5 输入frontendog.com 查询frontend的og信息
输入5 输入listenzq.com 查询listen区域的配置为zq.com的信息
#插入部分测试内容
输入2 输入oldboy1.com 插入默认区域(默认区域backend)
输入6 输入frontendoldboy2.com 插入frontend区域
输入6 输入listenoldboy3.com 插入listen区域
#删除部分测试内容
输入2 输入oldboy1.com 删除默认区域(默认区域backend)
输入6 输入frontendoldboy2.com 删除frontend区域
输入6 输入listenoldboy3.com 删除listen区域