day_ha配置文件

流程图:

 

 

代码

 1 #!/sur/bin/env python
 2 # -*- coding: utf-8 -*-
 3 #{"backend": "www.oldboy.org","record":{"server": "100.1.7.90","weight": 20,"maxconn": 30}}
 4 import json
 5 import sys
 6 
 7 def add_ha(backend_de):
 8     backend_dic = json.loads(backend_de)
 9     backend_add = 'server %s weight %s maxconn %s'%(backend_dic['record']['server'],backend_dic['record']['weight'],+backend_dic['record']['maxconn'])
10     with open("haproxy.conf") as read_f,open("ceshi","w") as write_f :
11         for lines in read_f.readlines():
12             write_f.write(lines)
13         write_f.write('backend'' '+backend_dic['backend']+'\n')
14         write_f.write(' '*8+backend_add) #
15 
16 def delet_ha(backend_de):
17     backend_dic = json.loads(backend_de) ##将字符串转换为相印的数据类型
18     backned_check=list(bakcned_dic['backend']) #www.oldboy.org
19     backend_del = '        server %s weight %s maxconn %s\n'%(backend_dic['record']['server'],backend_dic['record']['weight'],+backend_dic['record']['maxconn']) ##数据类容
20     with open("haproxy.conf") as read_f,open("ceshi","w") as write_f :
21         for lines in read_f.readline():
22             backend_list=[]
23             backend_list.append(lines.strip())
24             backend_list.append(lines)
25             if lines.strip().split()[2] == backned_check:
26                 continue
27             elif backend_del == lines :
28                 backend_list.remove(lines)
29         write_f.write(backend_list)
30 
31 def change_ha(backend_de):
32     backend_dic = json.loads(backend_de) ##将字符串转换为相印的数据类型
33     backned_check=list(bakcned_dic['backend']) #www.oldboy.org
34     backend_del = '        server %s weight %s maxconn %s\n'%(backend_dic['record']['server'],backend_dic['record']['weight'],+backend_dic['record']['maxconn']) ##数据类容
35     with open("haproxy.conf") as read_f,open("ceshi","w") as write_f :
36         for lines in read_f.readline():
37             backend_list=[]
38             backend_list.append(lines.strip())
39             backend_list.append(lines)
40             if lines.strip().split()[2] == backned_check:
41                 write_f.write(backend_del)
42                 continue
43             elif backend_del == lines :
44                 print("类容已存在,请确定后再修改")
45                 sys.exit()
46         write_f.write(backend_list)
47 
48 while True:
49     number_ha = int(input("1、添加\n2、删除\n3、修改\n请输入以上阿拉伯数字:"))
50     if number_ha == 1:
51         backend_de=input("请输入backend:") #如输入:["bakcend","www.oldboy.org"]
52         add_ha(add_file)
53     elif number_ha == 2:
54         backend_de=input("请输入要删除的bakcned:") #如 www.oldboy.org
55         delet_ha(delet_file)
56     elif number_ha == 3:
57         backend_de=input("请输入要删除的bakcned:")
58         change_ha()
59     else:
60         print("你输入的功能有误,请重新输入")
61         continue
代码

ha配置文件

1 frontend oldboy.org
2         bind 0.0.0.0:80
3 
4 backend www.oldboy.org
5         server 100.1.7.9 1020.1.7.9 weight 20 maxconn 3000
6 
7 backend buy.oldboy.org
8         server 100.1.7.90 100.1.7.90 weight 20 maxconn 30000
HA配置文件代码

 

posted @ 2016-04-28 18:10  清风徐来#  阅读(217)  评论(0编辑  收藏  举报