只要管理配置haproxy的配置文件:
conf_tools_func.py
1 #!/usr/bin/env python
1
1 import json
1 import copy
1
1 #读取配置文件函数
1 def read_config():
1 with open('server.conf', 'r') as f:
1 cont = f.readlines()
1 return cont
1
1 #写入配置文件函数
1 def change_config(cont):
1 if cont: #如果传入的cont内容为空,则返回False。同时防止清空配置文件
1 with open('server.conf', 'w') as f:
1 f.writelines(cont)
1 return True
1 else:
1 return False
1
1
1 #节点backend删除,显示配置的函数:
1 # 用一个函数是因为判断节点是否存在是一样的不用重复写。
1 # But删除和显示用户输入的backend格式不一样,处理真实backend也就不一样
1 # So,如果严格按照流程图的流程来的话这里又加了一个判断,以获取真实backend。
1 def backend_show_del(cont,backend,chose_num):
1 if chose_num=='s':
1 rel_backend = 'backend %s%s' %(backend, "\n")
1 elif chose_num=='d':
1 nbackend = json.loads(backend)['backend']
1 rel_backend = 'backend %s%s' %(nbackend, "\n")
1
1 if rel_backend in cont: #判断节点是否存在
1 if chose_num=='s':
1
1 print(cont[cont.index(rel_backend)])
1 next_num = cont.index(rel_backend) + 1 #找到backend的索引加1 就是backend下配置的索引
1 #循环遍历找到的backend 下的配置包括空行‘\n’
1 while next_num <= (len(cont)-1) and ('server' in cont[next_num] or cont[next_num] == '\n') and \
1 ('backend' not in cont[next_num]):
1 print(cont[next_num])
1 next_num += 1
1
1 elif chose_num=='d':
1
1 rel_del_index=[] #初始一个获取配置索引的列表
1 next_num = cont.index(rel_backend)
1 rel_del_index.append(next_num) #把该backend节点的索引先加到索引列表
1 next_num +=1
1 #循环取到要删除的配置节点下的配置(包括空行)的索引,加到索引列表。
1 while next_num <= (len(cont)-1) and ('server' in cont[next_num] or cont[next_num] == '\n') and \
1 ('backend' not in cont[next_num]):
1 rel_del_index.append(next_num)
1 next_num += 1
1
1 #print(rel_del_index) #调试
1 #循环删除读取到配置文件内容的cont中有索引列表中元素的元素
1 del_index=rel_del_index[0]
1 count=1
1 while count <= len(rel_del_index):
1 del cont[del_index]
1 count +=1
1
1 return cont
1 else:
1 print("\n——————————Not find the backend!!! Plases redo!")
1
1
1
1 #该函数负责取得配置节点backend下所有配置(包括空行)的索引的函数,返回一个列表
1 def server_check(cont,server):
1
1 rel_server_config = json.loads(server) #获取输入配置字典
1 backend = rel_server_config['backend'] #取得输入配置的backend
1 rel_backend = 'backend %s%s' %(backend, '\n') #rel_backend为在配置列表cont中元素的格式
1
1 if rel_backend in cont: #如果输入的backend在cont中:
1 next_num = cont.index(rel_backend) + 1 #取到配置节点backend下一个元素的索引
1 rel_del_index = [] #初始一个存放backend下所有配置的元素索引的列表
1 #循环取到配置节点backend下的配置(包括空行)的索引,加到索引列表。
1 while next_num <= (len(cont) - 1) and ('server' in cont[next_num] or cont[next_num] == '\n') and \
1 ('backend' not in cont[next_num] ):
1 rel_del_index.append(next_num)
1 next_num += 1
1
1 return rel_del_index
1 else:
1 print("\n——————————Not find the backend!!! Plases redo!")
1
1
1
1 #删除server配置函数:
1 # 整体思路:
1 # 1,调用server_check()获取到该backend下所有server配置的索引。
1 # 2,遍历server配置索引在cont中的元素,如果找到要删除的server配置,则删除并退出遍历循环。
1 def server_del(server):
1 cont = read_config()
1 rel_server_config = json.loads(server)
1 rel_server = rel_server_config['server']
1
1 rel_del_index=server_check(cont,server)
1 if rel_del_index:
1 fn=False #这里用标志位防止一遍循环没找到报出'Not find the server',影响用户体验。
1 for i in rel_del_index:
1 if rel_server in cont[i]: #因为在cont中配置元素前面有空格,考虑通用性这里用in,
1 del cont[i]
1 fn=True
1 break
1 else:
1 pass
1
1 if not fn:
1 cont.clear() #清空cont是防止后面报错和影响下一步操作。
1 print("\n——————————The server is not exsit,nothing to do!")
1 else:
1 cont.clear()
1 return cont
1
1
1
1 #添加配置函数:
1 # 整体思路:
1 # 1.如果添加的backend不存在,直接追加到cont中写入即可。
1 # 2,backend已存在,把backend下原来的所有配置删除后添加需要添加的server配置,然后把原有的配置再添加上去。
1 def server_add(cont,server):
1 rel_server=json.loads(server)
1 rel_add_backend='backend %s%s' % (rel_server['backend'],'\n')
1 add_backend='%sbackend %s%s' % ('\n',rel_server['backend'],'\n')
1 add_server=' %s%s' % (rel_server['server'],'\n') #添加就可以按标准来了,前面一个tab后面是server配置
1
1 # 添加操作的函数,提供给下面backend不存在的情况和backend存在server不存在的情况直接调用。
1 def add(cont):
1 ccont=copy.copy(cont) #这里用用copy
1 for i in range(add_index,len_num):
1 ocont.append(cont[i])
1 del ccont[add_index] #按节点下一个元素的索引删除,下面有几行就删除多少次,即把节点下所有行删除了。
1 # print(ccont) #调试,以为这里一定要用深拷贝,后来试试深浅拷贝是一样的。
1 # print(cont)
1 ccont.append(add_server)
1 cont = ccont + ocont
1 return cont
1
1 if rel_add_backend in cont:
1 ocont=[] #存放要添加backend下原来的配置
1 len_num=len(cont)
1 add_index=cont.index(rel_add_backend)+ 1
1 rel_del_index = server_check(cont,server)
1
1 if rel_del_index:
1 fn=True
1 for i in rel_del_index:
1 if rel_server['server'] in cont[i]:
1 cont.clear()
1 fn=False
1 print("\n——————————The server is exsit,nothing to do!")
1 break
1 if fn:
1 cont=add(cont)
1 else:
1 cont=add(cont)
1 else:
1 cont.append(add_backend)
1 cont.append(add_server)
1
1
1 return cont
main.py
1 #!/usr/bin/env python
1
1 import json
1 from conf_tools_func import *
1
1 #以供复制^_^ {"backend":"www.oldboy.org","server":"server 100.1.7.91 100.1.7.9 weight 20 maxconn 3000"}
1
1 chose_num='b'
1 while chose_num != 'q':
1 print('''
1 欢迎使用GDS运维配置工具:
1 "s":查看主机配置
1 "a":添加主机配置
1 "d":删除主机配置
1 "q":退出
1 ''')
1
1 chose_num=input("请输入要进行的操作:").strip()
1 if chose_num=='s':
1 cont=read_config()
1 show_cont=input("请输入查询的backend配置节点:").strip()
1 backend_show_del(cont,show_cont,'s')
1
1 elif chose_num=='d':
1 cont=read_config()
1 del_cont=input('请输入删除的配置内容:')
1 rel_del_cont = json.loads(del_cont)
1 type_cont=type(rel_del_cont)
1 backend = rel_del_cont['backend']
1 rel_backend = 'backend %s%s' %(backend,'\n')
1 if 'server' in del_cont and 'dict' in str(type_cont):
1 cont = server_del(del_cont) #调用删除server配置函数
1 if cont:
1 del_index=server_check(cont,del_cont)
1
1 # 取得backend下配置的索引列表。如果列表为空或列表中索引的元素为空行,则删除该backend
1 if not del_index or cont[del_index[0]]=='\n':
1 cont.remove(rel_backend)
1 print("\n %s:is no server!!————So,will be delete!" % (backend))
1
1 change_config(cont) #调用写入配置文件函数
1
1 if change_config(cont):
1 print("\n %s:——————Delete Success!" % (del_cont))
1 else:
1 print("\n %s:——————Delete Faild!" % (del_cont))
1
1 # 如果删除的是backend节点,直接调用backend_show_del()
1 elif 'backend' in del_cont and 'dict' in str(type_cont):
1 cont=backend_show_del(cont,del_cont,'d')
1 change_config(cont)
1 if change_config(cont):
1 print("\n %s:——————Delete Success!" %(del_cont))
1 else:
1 print("\n %s:——————Delete Faild!" % (del_cont))
1 else:
1 print('\n——————You input Error!')
1
1 elif chose_num=='a':
1 cont = read_config()
1 add_cont=input('请输入添加的配置内容:')
1 if 'server' in add_cont and 'backend' in add_cont:
1 cont=server_add(cont,add_cont)
1
1 if change_config(cont):
1 print("\n %s:——————Add Success!" % (add_cont))
1 else:
1 print("\n %s:——————Add Faild!" % (add_cont))
1 else:
1 print("\n——————You input Error!")
1 else:
1 if chose_num !='q':
1 print("\n——————You input Error!")
转载于:https://blog.51cto.com/luyaliang/1864212