#!/usr/bin/env python3
# -*-conding:utf-8-**
# __Author__:‘liudong‘
import json, shutil
def fetch(backend):
"""
根据用户输入站点查找对应配置项目
:param backend: 从主程序读取的用户输入的站点配置条目
:return: 返回配置的值
"""
config_list = []
with open(‘ha.conf‘, ‘r‘, encoding=‘utf-8‘) as f:
flag = False # 用于标志所读内容是否为需要的数据
for line in f:
if line.strip().startswith(‘backend‘) and line.strip() == "backend " + backend:
# 判断以backend开始,且后面内容和用户输入的域名相同,即所需要查询的配置条目
flag = True
continue
if line.strip().startswith(‘backend‘):
# 判断以backend开头,与用户输入域名不相同、即不是要查询的配置项目
flag = False
# break
if flag and line.strip():
config_list.append(line.strip())
# print(confg_list)
return config_list
def add(backend, record):
"""
用于增加用户输入的配置项目
:param backend: 用户输入的要增加的backend和域名
:param record: 用户输入的服务器记录
:return:
"""
record_list = fetch(backend)
if not record_list:
# backend不存在,没找到要修改的backend
with open(‘ha.conf‘, ‘r‘) as old, open("new.conf", ‘w‘) as new:
for line in old:
new.write(line)
new.write("\nbackend " + backend + "\n")
new.write(" " * 8 + record + "\n")
# print(‘Added sucessed!‘)
else: # 用户输入的backend存在
if record in record_list: # 用户输入的backend项和record均已经存在
shutil.copy(‘ha.conf‘, ‘new.conf‘) # 复制配置文件到新文件中
print(‘The config list you added is exits,No configure file was changed!‘)
else: # 用户输入的backend已经存在,record不存在
record_list.append(record) # 把用户输入的配置记录内容,添加到列表record_list中
with open(‘ha.conf‘, ‘r‘) as old, open(‘new.conf‘, ‘w‘) as new:
flag = False # 判断是否读取到backend开关的标识符
for line in old: # 一行一行读取原配置文件
if line.strip().startswith("backend ") and line.strip() == "backend " + backend:
# 如果该行以backend开始并且等于用户输入的backend域名
flag = True
new.write(line)
for new_line in record_list:
new.write(" " * 8 + new_line)
continue
if flag and line.strip().startswith("backend"):
flag = False
new.write(line)
continue
if line.strip() and not flag:
new.write(line)
print(‘modified successed! new record is %s %s‘ % (backend, record))
shutil.copy(‘ha.conf‘, ‘ha.bak‘) # /备份原文件
shutil.copy(‘new.conf‘, ‘ha.conf‘) # 把新修改好的文件改成正式文件
def del_Item(backend):
"""
用于删除用户输入的配置项目
:param backend: 用户输入的要删除的backend和域名
:param record: 用户输入的要删除的服务器记录
:return:
"""
record_list = fetch(backend)
print(record_list)
if not record_list:
# 没找到要删除的配置项目
with open(‘ha.conf‘, ‘r‘) as old, open("new.conf", ‘w‘) as new:
for line in old:
new.write(line)
print(‘There is no item, No need to delete!‘)
else: # 用户输入的backend存在
with open(‘ha.conf‘, ‘r‘) as old, open(‘new.conf‘, ‘w‘) as new:
flag = False # 判断是否读取到backend开关的标识符
for line in old: # 一行一行读取原配置文件
if line.strip().startswith("backend ") and line.strip() == "backend " + backend:
# 如果该行以backend开始并且等于用户输入的backend域名
flag = True
continue
# new.write(line)
# for new_line in record_list:
# new.write(" " * 8 + new_line + "\n")
# continue
if flag and line.strip().startswith("backend"):
flag = False
new.write(line)
continue
if line.strip() and not flag:
new.write(line)
print(‘%s is deleted!‘ % backend)
shutil.copy(‘ha.conf‘, ‘ha.bak‘) # /备份原文件
shutil.copy(‘new.conf‘, ‘ha.conf‘) # 把新修改好的文件改成正式文件
if __name__ == ‘__main__‘:
while True:
print(‘Welcome to ha config tools‘.center(50, ‘-‘))
# 读取用户第一项选择内容
start_choice = input(‘1.获取ha记录\n2.增加ha记录\n3.删除ha记录\nPlease input what you want to do:[q=quit]‘)
print(‘‘.center(50, ‘-‘))
if start_choice == ‘q‘:
exit()
elif start_choice == ‘1‘:
option1_choice = input(‘1.查询配置,请输入backend项目域名:‘) # 用户输入要查询的域名
print_conf = fetch(option1_choice) # 保存返回的内容
print(‘你要查询的配置为:‘)
print(print_conf) # 打印返回的配置内容:列表形式
# print(‘‘.center(50, ‘-‘))
elif start_choice == ‘2‘:
str_exa = ‘{"backend":"ttt.jachy.org","record":{"server":"100.1.7.9","weight":"20","maxconn":3000}}‘
mod_config = input(‘2.选择要修改的配置项目及配置条目,格式%s:‘ % str_exa)
dic = json.loads(mod_config)
bk = dic["backend"]
rd = "server %s %s weight %s maxconn %s" % (dic[‘record‘][‘server‘],
dic[‘record‘][‘server‘],
dic[‘record‘][‘weight‘],
dic[‘record‘][‘maxconn‘])
# print(bk + ‘\n‘)
# print(rd)
add(bk, rd)
elif start_choice == ‘3‘:
str_del = ‘{"backend":"ttt.jachy.org","record":{"server":"100.1.7.9","weight":"20","maxconn":3000}}‘
del_config = input(‘2.选择要删除的配置项目及配置条目,格式%s:‘ % str_del)
del_dic = json.loads(del_config)
del_bk = del_dic["backend"]
# del_rd = "server %s %s weight %s maxconn %s" % (dic[‘record‘][‘server‘],
# dic[‘record‘][‘server‘],
# dic[‘record‘][‘weight‘],
# dic[‘record‘][‘maxconn‘])
del_Item(del_bk)
else:
print(‘input error!‘)