HAproxy配置文件操作

要求

1. 根据用户输入输出对应的backend下的server信息
2. 可添加backend 和sever信息
3. 可修改backend 和sever信息
4. 可删除backend 和sever信息
5. 操作配置文件前进行备份
6 添加server信息时,如果ip已经存在则修改;如果backend不存在则创建;若信息与已有信息重复则不操作

HAproxy配置文件操作

 def find(backend):
'''
查看backend下sever信息
:param backend:
:return:
'''
ls = []
with open('HA.txt', 'r') as f:
T = True
for line in f:
# 找到第一个backend,做记录 T = False
if line.strip() == 'backend %s' % (backend):
T = False
continue
# 找到第二个backend,做记录 T = True
if T == False and line.strip().startswith('backend'):
T = True
break
# 把关于False的加入到列表中
if T == False and line.strip():
ls.append(line.strip())
return ls def check_bk():
'''
列出backend的信息
:return: 选中的backend
'''
ls = []
T = True
with open('HA.txt', 'r') as f1:
for line in f1:
if line.strip().startswith('backend') == True:
to = line.split(' ')
ls.append(to[1])
for i, v in enumerate(ls,1):
print (i, v)
while T:
t1 = input('请输入查看的server的 backend信息')
if t1.isdecimal() and int(t1) in range(0,len(ls)+1):
a = ls[int(t1)-1]
break
else:
print('输入有误,重新输入')
return a def backup():
'''
备份
:return:
'''
with open('HA.txt', 'r') as f1, open('HA_old.txt', 'w') as f2:
for line in f1:
f2.write(line) def add(bk, sr):
'''
添加记录
:param bk:
:param sr:
:return:
'''
r_list = find(bk)
# 没有关于bk的记录,直接添加内容
T = True
a = 'backend %s' % bk
backup()
if not r_list:
with open('HA.txt', 'a') as f1:
f1.write('\n%s\n'% a)
f1.write(" %s\n" % sr)
# 有关于bk的记录
else:
# 没有sever记录
backup()
if sr not in r_list:
with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
for line in f1:
if line.strip() == 'backend %s' % (bk):
T = False
f2.write(line)
continue
if T == False and line.strip().startswith('backend'):
T = True
if T == True:
f2.write(line)
else:
if sr not in r_list:
r_list.append(sr)
t = '\n '.join(r_list)
f2.write(' '* 8 + t + '\n\n')
# 有sever记录,直接复制
else:
print('记录已存在,请重新选择')
caidan() def update_bk ():
'''
需要修改或删除backend
:return:
'''
backup()
T = True
bk = check_bk()
ls =[]
with open('HA.txt', 'r') as f1:
for line in f1:
if line.strip().startswith('backend') == True:
to = line.split(' ')
ls.append(to[1].strip())
c = input('''
请选择需要选项:
1 修改backend
2 删除backend
''')
while T:
#修改backend信息
if c == '':
bk1 = input('请输入修改后的backend信息')
t3 = input('信息确认请按 y, 重新输入请按其他键')
if bk1 in ls:
print('信息已存在,请重新选择')
break
else:
if t3.lower() == 'y':
with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
for line in f1:
#需要修改的backend
if line.strip() == 'backend %s' % (bk.strip()):
f2.write('backend %s \n' % (bk1))
else:
f2.write(line)
print('修改成功')
break
else:
continue
#删除backend信息
elif c == '':
t = find(bk.strip())
while T:
with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
for line in f1:
if line.strip() == 'backend %s' % (bk.strip()):
continue
if line.strip() in t:
continue
else:
f2.write(line)
print('删除成功')
break
break def check_sr():
T1 = True
while T1:
cont = 0
a1 = input('sever:')
ti = a1.split('.')
for ii in ti:
if ii.isdecimal() and int(ii) in range(0, 255) and len(ti) == 4:
cont = cont + 1
continue
else:
print('输入有误,请输入由四组0-255组成的IP地址')
break
if cont == 4:
break
return a1 def update_sr():
'''
修改或删除sever
:return:
'''
t = check_bk()
backup()
T = True
T1 = True
ls = find(t.strip())
cc = 0
with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
for line in f1:
if line.strip() == 'backend %s' %(t.strip()):
T = False
f2.write(line)
continue
if T == False and line.strip().startswith('backend'):
T = True
if T == True:
if line.strip() != cc and line.strip() not in ls:
f2.write(line) else:
for i, v in enumerate(ls, 1):
print ('%s: %s' % (i, v))
while T1:
v1 = input('请输入要修改或删除的编号')
if v1.isdecimal() and int(v1) in range(len(ls)+1):
break
else:
print('输入有误,请重新输入')
continue
x = input('请选择 1 = 修改 其他键 = 删除')
if x == '':
while T1:
a1 = check_sr()
a2 = input('weight:')
a3 = input('maxconn:')
if a2.isdecimal() and a3.isdecimal():
t1 = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)
print('backend: %s \n%s' % (t, t1))
else:
print('所输入weight 和 maxconn 必须为数字,请重新输入')
continue
li_sr = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)
# server
if li_sr in ls:
tt = input('server已存在,请按 y = 重新输入,或按 other key = 返回')
if tt == 'y':
continue
else:
break
else:
a4 = input('请确认信息正确,y = 确认, other key = 重新输入') if a4 == 'y':
f2.write(' '*8 + li_sr +'\n')
else:
continue
print('修改成功')
break
cc = ls[int(v1)-1]
del ls[int(v1)-1]
for tt in ls:
f2.write(' '*8 + tt +'\n')
T = True
else:
print('删除成功')
cc = ls[int(v1)-1]
del ls[int(v1)-1]
for tt in ls:
f2.write(' '*8 + tt +'\n')
T = True def caidan ():
T = True
while T:
ls = input('''请输入所需编号:
1 查看backend下sever信息
2 添加记录
3 修改或删除backend
4 修改或删除sever
5 退出
''')
if ls == '':
i = check_bk()
l1 = find(i.strip())
for a in l1:
print (a)
elif ls == '':
while T:
t = input('请输入backend信息')
print('请输入sever信息')
cont = 0
while T:
a1 = input('sever:')
ti = a1.split('.')
for ii in ti:
if ii.isdecimal() and int(ii) in range(0, 255) and len(ti) == 4:
cont = cont + 1
continue
else:
print('输入有误,请输入由四组0-255组成的IP地址')
break
if cont == 4:
break
while T:
a2 = input('weight:')
a3 = input('maxconn:')
if a2.isdecimal() and a3.isdecimal():
t1 = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)
print('backend: %s \n%s' % (t, t1))
break
else:
print('所输入weight 和 maxconn 必须为数字,请重新输入')
continue
t3 = input('信息确认请按 y, 重新输入请按其他键')
if t3.lower() == 'y':
add (t, t1)
print('添加成功')
break
else:
continue
elif ls == '':
update_bk()
elif ls == '':
update_sr()
elif ls == '':
exit()
else:
print('输入有误,请重新输入') caidan()
def find(backend):
'''
查看backend下sever信息
:param backend:
:return:
'''
ls = []
with open('HA.txt', 'r') as f:
T = True
for line in f:
# 找到第一个backend,做记录 T = False
if line.strip() == 'backend %s' % (backend):
T = False
continue
# 找到第二个backend,做记录 T = True
if T == False and line.strip().startswith('backend'):
T = True
break
# 把关于False的加入到列表中
if T == False and line.strip():
ls.append(line.strip())
return ls def check_bk():
'''
列出backend的信息
:return: 选中的backend
'''
ls = []
T = True
with open('HA.txt', 'r') as f1:
for line in f1:
if line.strip().startswith('backend') == True:
to = line.split(' ')
ls.append(to[])
for i, v in enumerate(ls,):
print (i, v)
while T:
t1 = input('请输入查看的server的 backend信息')
if t1.isdecimal() and int(t1) in range(,len(ls)+):
a = ls[int(t1)-]
break
else:
print('输入有误,重新输入')
return a def backup():
'''
备份
:return:
'''
with open('HA.txt', 'r') as f1, open('HA_old.txt', 'w') as f2:
for line in f1:
f2.write(line) def add(bk, sr):
'''
添加记录
:param bk:
:param sr:
:return:
'''
r_list = find(bk)
# 没有关于bk的记录,直接添加内容
T = True
a = 'backend %s' % bk
backup()
if not r_list:
with open('HA.txt', 'a') as f1:
f1.write('\n%s\n'% a)
f1.write(" %s\n" % sr)
# 有关于bk的记录
else:
# 没有sever记录
backup()
if sr not in r_list:
with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
for line in f1:
if line.strip() == 'backend %s' % (bk):
T = False
f2.write(line)
continue
if T == False and line.strip().startswith('backend'):
T = True
if T == True:
f2.write(line)
else:
if sr not in r_list:
r_list.append(sr)
t = '\n '.join(r_list)
f2.write(' '* 8 + t + '\n\n')
# 有sever记录,直接复制
else:
print('记录已存在,请重新选择')
caidan() def update_bk ():
'''
需要修改或删除backend
:return:
'''
backup()
T = True
bk = check_bk()
ls =[]
with open('HA.txt', 'r') as f1:
for line in f1:
if line.strip().startswith('backend') == True:
to = line.split(' ')
ls.append(to[].strip())
c = input('''
请选择需要选项:
1 修改backend
2 删除backend
''')
while T:
#修改backend信息
if c == '1':
bk1 = input('请输入修改后的backend信息')
t3 = input('信息确认请按 y, 重新输入请按其他键')
if bk1 in ls:
print('信息已存在,请重新选择')
break
else:
if t3.lower() == 'y':
with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
for line in f1:
#需要修改的backend
if line.strip() == 'backend %s' % (bk.strip()):
f2.write('backend %s \n' % (bk1))
else:
f2.write(line)
print('修改成功')
break
else:
continue
#删除backend信息
elif c == '2':
t = find(bk.strip())
while T:
with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
for line in f1:
if line.strip() == 'backend %s' % (bk.strip()):
continue
if line.strip() in t:
continue
else:
f2.write(line)
print('删除成功')
break
break def check_sr():
T1 = True
while T1:
cont = 0
a1 = input('sever:')
ti = a1.split('.')
for ii in ti:
if ii.isdecimal() and int(ii) in range(, ) and len(ti) == :
cont = cont + 1
continue
else:
print('输入有误,请输入由四组0-255组成的IP地址')
break
if cont == :
break
return a1 def update_sr():
'''
修改或删除sever
:return:
'''
t = check_bk()
backup()
T = True
T1 = True
ls = find(t.strip())
cc = 0
with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
for line in f1:
if line.strip() == 'backend %s' %(t.strip()):
T = False
f2.write(line)
continue
if T == False and line.strip().startswith('backend'):
T = True
if T == True:
if line.strip() != cc and line.strip() not in ls:
f2.write(line) else:
for i, v in enumerate(ls, ):
print ('%s: %s' % (i, v))
while T1:
v1 = input('请输入要修改或删除的编号')
if v1.isdecimal() and int(v1) in range(len(ls)+):
break
else:
print('输入有误,请重新输入')
continue
x = input('请选择 1 = 修改 其他键 = 删除')
if x == '1':
while T1:
a1 = check_sr()
a2 = input('weight:')
a3 = input('maxconn:')
if a2.isdecimal() and a3.isdecimal():
t1 = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)
print('backend: %s \n%s' % (t, t1))
else:
print('所输入weight 和 maxconn 必须为数字,请重新输入')
continue
li_sr = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)
# server
if li_sr in ls:
tt = input('server已存在,请按 y = 重新输入,或按 other key = 返回')
if tt == 'y':
continue
else:
break
else:
a4 = input('请确认信息正确,y = 确认, other key = 重新输入') if a4 == 'y':
f2.write(' '*8 + li_sr +'\n')
else:
continue
print('修改成功')
break
cc = ls[int(v1)-]
del ls[int(v1)-]
for tt in ls:
f2.write(' '*8 + tt +'\n')
T = True
else:
print('删除成功')
cc = ls[int(v1)-]
del ls[int(v1)-]
for tt in ls:
f2.write(' '*8 + tt +'\n')
T = True def caidan ():
T = True
while T:
ls = input('''请输入所需编号:
1 查看backend下sever信息
2 添加记录
3 修改或删除backend
4 修改或删除sever
5 退出
''')
if ls == '1':
i = check_bk()
l1 = find(i.strip())
for a in l1:
print (a)
elif ls == '2':
while T:
t = input('请输入backend信息')
print('请输入sever信息')
cont = 0
while T:
a1 = input('sever:')
ti = a1.split('.')
for ii in ti:
if ii.isdecimal() and int(ii) in range(, ) and len(ti) == :
cont = cont + 1
continue
else:
print('输入有误,请输入由四组0-255组成的IP地址')
break
if cont == :
break
while T:
a2 = input('weight:')
a3 = input('maxconn:')
if a2.isdecimal() and a3.isdecimal():
t1 = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)
print('backend: %s \n%s' % (t, t1))
break
else:
print('所输入weight 和 maxconn 必须为数字,请重新输入')
continue
t3 = input('信息确认请按 y, 重新输入请按其他键')
if t3.lower() == 'y':
add (t, t1)
print('添加成功')
break
else:
continue
elif ls == '3':
update_bk()
elif ls == '4':
update_sr()
elif ls == '5':
exit()
else:
print('输入有误,请重新输入') caidan()
上一篇:Golang 调用 aws-sdk 操作 S3对象存储


下一篇:configparser配置文件操作