haproxy配置文件修改

# -*- coding: utf-8 -*-
import sys
import shutil
reload(sys)
sys.setdefaultencoding('utf8')
default_encoding = "utf-8"


def the_backend_list(list_back):
# type: (object) -> object
back_name = []
T = True
for i in open("ha.conf"):
if T == True and i.startswith('backend %s' % list_back):
T = False
continue
if T == False and i.startswith('backend '):
T = True
break
if T == False and i.split():
back_name.append(i.split())
return back_name


def the_server_name(list_back):
# type: (object) -> object
server_name = []
T = False
for i in open("ha.conf"):
if i.startswith('backend %s' % list_back):
T = True
continue
if T == True and i.startswith('backend '):
break
if T:
server_name.append(i.split()[1])
return server_name


# def backend_list()


shutil.copy('ha.conf','ha.conf.bak')
change_list = raw_input("请输出入需要进行的操作:")

if change_list == '1':
list_back = raw_input("输入backend:")
the_backend_list(list_back)

elif change_list == '2':
tt=False
count=0
add_back = raw_input("添加的backend:")
add_server = raw_input("添加的server:")
add_host = raw_input("添加的host:")
add_weight = raw_input("添加的weight:")
add_maxconn = raw_input("添加的maxconn:")
add_list = str('server %s %s weight %s maxconn %s' % (add_server, add_host, add_weight, add_maxconn))
if add_server in the_server_name(add_back):
tt = True
T = False
has_write = False
backend_list = []

for i in the_backend_list(add_back):
if add_server != i[1]:
backend_list.append(i)

backend_list.append(add_list.split())
print backend_list
file_new = file('ha.new', 'w')
for read_line in open('ha.conf'):
if read_line.strip() == 'backend %s' % add_back:
file_new.write(read_line)
T = True
continue
if T and read_line.startswith('backend'):
T = False
if T:
for i in backend_list:
if not has_write:
add_backend = " ".join(i)
file_new.write('%s%s\n' % (' ' * 8, add_backend))
has_write = True
else:
file_new.write(read_line)
else:
file_add = file("ha.new", 'a')
file_add.write('backend %s\n' % add_back)
file_add.write('%s%s\n' % (' ' * 8, add_list))

file_new.close()
shutil.copy('ha.new', 'ha.conf')




elif change_list == '3':

print "===================="

for read_line in open('ha.conf'):
if read_line.startswith('backend ') == True:
print read_line

tt=False
count=0
while tt==False:
input_backend = raw_input("请输入需要修改的back名:")
for read_line in open('ha.conf'):
if read_line.startswith('backend %s' % input_backend) == True:
tt=True

if tt == False and count<2:
print "请输入正确的backend"
count+=1
continue
elif count >2:
print "错误超过3次"
exit()

tt=False
count=0
while tt==False:
add_server = raw_input("添加的server:")
if add_server in the_server_name(input_backend):
tt=True
add_host = raw_input("添加的host:")
add_weight = raw_input("添加的weight:")
add_maxconn = raw_input("添加的maxconn:")
add_list = str('server %s %s weight %s maxconn %s' % (add_server, add_host, add_weight, add_maxconn))
T = False
has_write = False
backend_list = []

for i in the_backend_list(input_backend):
if add_server != i[1]:
backend_list.append(i)

backend_list.append(add_list.split())

file_new = file('ha.new', 'w')
for read_line in open('ha.conf'):
if read_line.strip() == 'backend %s' % input_backend:
file_new.write(read_line)
T = True
continue
if T and read_line.startswith('backend'):
T = False
if T:
for i in backend_list:
if not has_write:
add_backend = " ".join(i)
file_new.write('%s%s\n' % (' ' * 8, add_backend))
has_write = True
else:
file_new.write(read_line)
else:
if tt == False and count < 2:
print "server不存在"
count += 1
continue
elif count >2:
print "错误超过3次"
exit()
file_new.close()
shutil.copy('ha.new', 'ha.conf')
elif change_list == "4":
tt=False
count=0
while tt==False:
del_backend = raw_input("请输入需要删除的back名:")
for read_line in open('ha.conf'):
if read_line.startswith('backend %s' % del_backend) == True:
tt=True

if tt == False and count<2:
print "请输入正确的backend"
count+=1
continue
elif count >2:
print "错误超过3次"
exit()
tt = False
count = 0

while tt==False:
del_server = raw_input("请输入需要删除的server:")
if del_server not in the_server_name(del_backend):
print "请输入正确的server"
count+=1
continue
else:
tt=True

backend_list = []
for i in the_backend_list(del_backend):
if del_server != i[1]:
backend_list.append(i)


T=False
has_write = False
file_new = file('ha.new', 'w')
for read_line in open('ha.conf'):
if read_line.strip() == 'backend %s' % del_backend:
file_new.write(read_line)
T = True
continue
if T and read_line.startswith('backend'):
T = False
if T:
for i in backend_list:
if not has_write:
add_backend = " ".join(i)
file_new.write('%s%s\n' % (' ' * 8, add_backend))
has_write = True
else:
file_new.write(read_line)

file_new.close()
shutil.copy('ha.new', 'ha.conf')



转载于:https://www.cnblogs.com/airring/p/6598333.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值