#!/usr/bin/env python
# _*_coding:utf-8_*_
'''
* Created on 2016/11/7 21:24.
* @author: Chinge_Yang.
'''
import shutil
import json
def list_function():
? ? print("Please choice the ID of a action.".center(50, "#"))
print("[1] 查詢 haproxy.cfg backend 信息.")
print("[2] 添加 haproxy.cfg backend 信息")
print("[3] 刪除 haproxy.cfg backend 信息")
print("End".center(50, "#"))
def fetch(backend):
? ? # 取出backend相關(guān)server信息
? ? result= []# 定義結(jié)果列表
? ? with open("haproxy.cfg", "r", encoding="utf-8")as f:? # 循環(huán)讀取文件
? ? ? ? flag= False? # 標(biāo)記為假
? ? ? ? for linein f:
? ? ? ? ? ? # 以backend開頭
? ? ? ? ? ? line= line.strip()
if line.startswith("backend")and line== "backend " + backend:
? ? ? ? ? ? ? ? flag= True? # 讀取到backend開頭的信息,標(biāo)記為真
? ? ? ? ? ? ? ? continue
? ? ? ? ? ? # 下一個(gè)backend開頭
? ? ? ? ? ? if flagand line.strip().startswith("backend"):
? ? ? ? ? ? ? ? flag = False
break
? ? ? ? ? ? # server信息添加到result列表
? ? ? ? ? ? if flagand line.strip():
? ? ? ? ? ? ? ? result.append(line.strip())
return result
def writer(backend, record_list): ##3條
? ? with open("haproxy.cfg", "r")as old, open("new.cfg", "w")as new:
? ? ? ? flag= False
? ? ? ? for linein old:
? ? ? ? ? ? if line.strip().startswith('backend')and line.strip()== "backend " + backend:
? ? ? ? ? ? ? ? flag= True
? ? ? ? ? ? ? ? new.write(line)
for new_linein record_list:
? ? ? ? ? ? ? ? ? ? new.write(" " * 4 + new_line+ "\n")
continue? # 跳到下一次循環(huán),避免下一個(gè)backend寫二次
? ? ? ? ? ? if flagand line.strip().startswith("backend"):? # 下一個(gè)backend
? ? ? ? ? ? ? ? flag= False
? ? ? ? ? ? ? ? new.write(line)
continue? # 退出此循環(huán),避免server信息二次寫入
? ? ? ? ? ? if line.strip()and not flag:
? ? ? ? ? ? ? ? new.write(line)
def add(backend, record):
? ? global change_flag
record_list= fetch(backend)# 查找是否存在記錄 這個(gè)是 fetch 返回值
? ? print(record_list)
if not record_list:? # backend不存在, record不存在
? ? ? ? with open('haproxy.cfg', 'r')as old, open('new.cfg', 'w')as new:
? ? ? ? ? ? for linein old:
? ? ? ? ? ? ? ? new.write(line)
# 添加新記錄
? ? ? ? ? ? new.write("\nbackend " + backend + "\n")
new.write(" " * 4 + record + "\n")
print("\033[32;1mAdd done\033[0m")
change_flag= True
? ? else:? # backend存在,record存在
? ? ? ? if record in record_list:
? ? ? ? ? ? print("\033[31;1mRecord already in it,Nothing to do!\033[0m")
change_flag= False
? ? ? ? else:? # backend存在,record不存在
? ? ? ? ? ? record_list.append(record)
writer(backend, record_list)
print(record_list)
print("\033[32;1mAdd done\033[0m")
change_flag= True
def delete(backend, record):
? ? global change_flag
record_list= fetch(backend)# 查找是否存在記錄
? ? if not record_list:? # backend不存在, record不存在
? ? ? ? print("Not match the backend,no need delete!".center(50, "#"))
change_flag= False
? ? else:? # backend存在,record存在
? ? ? ? if record in record_list:
? ? ? ? ? ? record_list.remove(record)# 移除元素
? ? ? ? ? ? writer(backend, record_list)# 寫入
? ? ? ? ? ? print("\033[31;1mDelete done\033[0m")
change_flag= True
? ? ? ? else:? # backend存在,record不存在
? ? ? ? ? ? print("Only match backend,no need delete!".center(50, "#"))
change_flag= False
? ? return change_flag
def output(servers):
? ? # 輸出指定backend的server信息
? ? print("Match the server info:".center(50, "#"))
for serverin servers:
? ? ? ? print("\033[32;1m%s\033[0m" % server)
print("#".center(50, "#"))
def input_json():
? ? # 判斷輸入,要求為json格式
? ? continue_flag= False
? ? while continue_flagis not True:
? ? ? ? backend_record= input("Input backend info(json):").strip()
try:
? ? ? ? ? ? backend_record_dict= json.loads(backend_record)
except Exception as e:
? ? ? ? ? ? print("\033[31;1mInput not a json data type!\033[0m")
continue
? ? ? ? continue_flag= True
? ? return backend_record_dict
def operate(action):
? ? global change_flag
if action == "fetch":
? ? ? ? backend_info= input("Input backend info:").strip()
result= fetch(backend_info)# 取出backend信息
? ? ? ? if result:
? ? ? ? ? ? output(result)# 輸出獲取到的server信息
? ? ? ? else:
? ? ? ? ? ? print("\033[31;1mNot a match is found!\033[0m")
elif action is None:
? ? ? ? print("\033[31;1mNothing to do!\033[0m")
else:
? ? ? ? backend_record_dict= input_json()# 要求輸入json格式
? ? ? ? for keyin backend_record_dict:
? ? ? ? ? ? backend= key
record= backend_record_dict[key]
if action == "add":
? ? ? ? ? ? ? ? add(backend, record)
elif action == "delete":
? ? ? ? ? ? ? ? delete(backend, record)
if change_flagis True:? # 文件有修改,才進(jìn)行文件更新
? ? ? ? ? ? # 將操作結(jié)果生效
? ? ? ? ? ? shutil.copy("haproxy.cfg", "old.cfg")
shutil.copy("new.cfg", "haproxy.cfg")
result= fetch(backend)
output(result)# 輸出獲取到的server信息
def judge_input():
? ? # 判斷輸入功能編號(hào),執(zhí)行相應(yīng)步驟
? ? input_info= input("Your input number:").strip()
if input_info== "1":? # 查詢指定backend記錄
? ? ? ? action= "fetch"
? ? elif input_info== "2":? # 添加backend記錄
? ? ? ? action= "add"
? ? elif input_info== "3":? # 刪除backend記錄
? ? ? ? action= "delete"
? ? elif input_info== "q" or input_info== "quit":
? ? ? ? exit("Bye,thanks!".center(50, "#"))
else:
? ? ? ? print("\033[31;1mInput error!\033[0m")
action= None
? ? return action
def main():
? ? exit_flag= False
? ? while exit_flagis not True:
? ? ? ? global change_flag
change_flag= False
? ? ? ? list_function()
action= judge_input()
operate(action)
if __name__== '__main__':
? ? main()