对文件进行修改,添加,删除
文件:
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.oldboy1.org
server 101.1000.7.9 101.1000.7.9 weight 20 maxconn 30
server 2.2.2.7 2.2.2.7 weight 30 maxconn 4000
server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
server 2.2.2.5 2.2.2.5 weight 20 maxconn 3000
backend www.oldboy2.org
server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
backend www.oldboy20.org
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
import os
def file_handle(filename,backend_data,record_list=None,type='fetch'): #提前定义参数,文件,筛选的套件,方法
new_file=filename+'_new'
bak_file=filename+'_bak'
if type == 'fetch': #如果遇见了查询功能
r_list=[]
with open(filename,'r') as f: #读取整个文件
tag = False #作为一个警报的参数
for line in f: #循环这个文件
if line.strip() == backend_data: #如果遇见你要筛选的哪一行,拉响警报,跳过下一行
tag=True
continue
if tag and line.startswith('backend'):#如果警报还在响,并且又遇见一次开头是'backend',说明已打印完毕,应该结束
break
if tag and line: #如果警报拉响,并且循环的东西又内容,就追加空列表里面
r_list.append(line.strip())
for line in r_list: #打印整个列表
print(line)
return r_list #并且返还值,就是一个列表,以后好用
if type == 'change':
with open('haproxy.conf','r') as read_file,\
open(new_file,'w') as write_file: #打开文件,并同时打开一个新文件
tag = False #设置警报参数
has_write=False
for r_line in read_file: #循环整个原文件
if r_line.strip() == backend_data: #当发现www.oldboy1.org时候,拉响警报,并且跳出这个循环
tag=True
continue
if tag and r_line.startswith('backend'): #当警报在的时候,并且又遇见一次'backend',结束循环
tag=False
if not tag: #如果是Flase,将原文件写入
write_file.write(r_line)
else:
if not has_write: #如果循环
for new_line in record_list: #将得到的列表循环
if new_line.startswith('backend'): #如果遇到backend www.oldboy1.org,将文件写入
write_file.write(new_line+'\n')
else:
write_file.write('%s%s\n'%(' '*8,new_line)) #将正行吸入文件,并保持以前的格式
has_write=True #关闭警报
os.rename(filename, bak_file) #将文件改名
os.rename(new_file, filename)
os.remove(bak_file)
def fetch(data):
backend_data='backend %s'%data #将自己输入的值传入到函数里面,并且拼接,
return file_handle('haproxy.conf',backend_data,type='fetch')
def add():
pass
def remove():
pass
def change(data): #将data你自己定义的数据结构传到函数里面
backend=data[0]['backend'] #因为自己定义的输入的列表,0代表列表第一个元素,['backend']代表字典的元素也就是www.oldboy1.org
record_list=fetch(backend) #返回一个查询的列表
old_server_recoed = 'server %s %s weight %s maxconn %s' % (data[0]['record']['server'],
data[0]['record']['server'],
data[0]['record']['weight'],
data[0]['record']['maxconn']
) #这是老的数据
new_server_recoed = 'server %s %s weight %s maxconn %s' % (data[1]['record']['server'],
data[1]['record']['server'],
data[1]['record']['weight'],
data[1]['record']['maxconn']
) #这是新的数据
backend_data='backend %s'%backend #将所要筛选的数据backend www.oldboy1.org进行拼接
if not record_list or old_server_recoed not in record_list: #列表不存在,或者老数据不再你查询到的列表里面,说明数据不存在
print('\033[33;1m无此内容\033[0m')
else:
record_list.insert(0,backend_data) #将backend www.oldboy1.org放入列表里面
index=record_list.index(old_server_recoed) #找到老数据的索引
record_list[index]=new_server_recoed #替换成你自己准备写入的数据
file_handle('haproxy.conf',backend_data,record_list,type='change') #调用函数
def exit():
pass
def qita():
pass
if __name__=='__main__':
msg='''
1:查询
2:添加
3:删除
4:修改
5:退出
6:其他操作
'''
menu_dic={
'1':fetch,
'2':add,
'3':remove,
'4':change,
'5':exit,
'6':qita
}
while True:
print(msg)
choice=input('操作>>>: ').strip()
if len(choice) == 0 or choice not in menu_dic:continue # choice not in menu_dic,可直接判断元素在不在字典里面
if choice=='5':break
data=input('数据>>: ').strip()
if choice != '1':
data=eval(data)
menu_dic[choice](data)
#自己定义的函数结构,前面的字典是你准备修改的内容,后面的字典是你修改以后的内容,这个结构是固定的
#[{'backend':'www.oldboy1.org','record':{'server':'2.2.2.4','weight':30,'maxconn':4000}},{'backend':'www.oldboy1.org','record':{'server':'10.10.0.10','weight':9999,'maxconn':33333333333}}]
#_*_coding:utf-8_*_ import os def file_handle(filename,backend_data,record_list=None,type='fetch'): #type:fetch append change new_file=filename+'_new' bak_file=filename+'_bak' if type == 'fetch': r_list = [] with open(filename, 'r') as f: tag = False for line in f: if line.strip() == backend_data: tag = True continue if tag and line.startswith('backend'): break if tag and line: r_list.append(line.strip()) for line in r_list: print(line) return r_list elif type == 'append': with open(filename, 'r') as read_file, \ open(new_file, 'w') as write_file: for r_line in read_file: write_file.write(r_line) for new_line in record_list: if new_line.startswith('backend'): write_file.write(new_line + '\n') else: write_file.write("%s%s\n" % (' ' * 8, new_line)) os.rename(filename, bak_file) os.rename(new_file, filename) os.remove(bak_file) elif type == 'change': with open(filename, 'r') as read_file, \ open(new_file, 'w') as write_file: tag=False has_write=False for r_line in read_file: if r_line.strip() == backend_data: tag=True continue if tag and r_line.startswith('backend'): tag=False if not tag: write_file.write(r_line) else: if not has_write: for new_line in record_list: if new_line.startswith('backend'): write_file.write(new_line+'\n') else: write_file.write('%s%s\n' %(' '*8,new_line)) has_write=True os.rename(filename, bak_file) os.rename(new_file, filename) os.remove(bak_file) def fetch(data): backend_data="backend %s" %data return file_handle('haproxy.conf',backend_data,type='fetch') def add(data): backend=data['backend'] record_list=fetch(backend) current_record="server %s %s weight %s maxconn %s" %(data['record']['server'],\ data['record']['server'],\ data['record']['weight'],\ data['record']['maxconn']) backend_data="backend %s" %backend if not record_list: record_list.append(backend_data) record_list.append(current_record) file_handle('haproxy.conf',backend_data,record_list,type='append') else: record_list.insert(0,backend_data) if current_record not in record_list: record_list.append(current_record) file_handle('haproxy.conf',backend_data,record_list,type='change') def remove(data): backend=data['backend'] record_list=fetch(backend) current_record="server %s %s weight %s maxconn %s" %(data['record']['server'],\ data['record']['server'],\ data['record']['weight'],\ data['record']['maxconn']) backend_data = "backend %s" % backend if not record_list or current_record not in record_list: print('\033[33;1m无此条记录\033[0m') return else: #处理record_list record_list.insert(0,backend_data) record_list.remove(current_record) file_handle('haproxy.conf',backend_data,record_list,type='change') def change(data): backend=data[0]['backend'] record_list=fetch(backend) old_record="server %s %s weight %s maxconn %s" %(data[0]['record']['server'],\ data[0]['record']['server'],\ data[0]['record']['weight'],\ data[0]['record']['maxconn']) new_record = "server %s %s weight %s maxconn %s" % (data[1]['record']['server'], \ data[1]['record']['server'], \ data[1]['record']['weight'], \ data[1]['record']['maxconn']) backend_data="backend %s" %backend if not record_list or old_record not in record_list: print('\033[33;1m无此内容\033[0m') return else: record_list.insert(0,backend_data) index=record_list.index(old_record) record_list[index]=new_record file_handle('haproxy.conf',backend_data,record_list,type='change') def qita(data): pass if __name__ == '__main__': msg=''' 1:查询 2:添加 3:删除 4:修改 5:退出 6:其他操作 ''' menu_dic={ '1':fetch, '2':add, '3':remove, '4':change, '5':exit, '6':qita, } while True: print(msg) choice=input("操作>>: ").strip() if len(choice) == 0 or choice not in menu_dic:continue if choice == '5':break data=input("数据>>: ").strip() #menu_dic[choice](data)==fetch(data) if choice != '1': data=eval(data) menu_dic[choice](data) #add(data) #[{'backend':'www.oldboy1.org','record':{'server':'2.2.2.5','weight':20,'maxconn':3000}},{'backend':'www.oldboy1.org','record':{'server':'10.10.0.10','weight':9999,'maxconn':33333333333}}]

浙公网安备 33010602011771号