5.修改haproxy配置文件

 需求:

 1 1、查
 2     输入:www.oldboy.org
 3     获取当前backend下的所有记录
 4 
 5 2、新建
 6     输入:
 7         arg = {
 8             'backend': 'www.oldboy.org',
 9             'record':{
10                 'server': '100.1.7.9',
11                 'weight': 20,
12                 'maxconn': 30
13             }
14         }
15 
16 3、删除
17     输入:
18         arg = {
19             'backend': 'www.oldboy.org',
20             'record':{
21                 'server': '100.1.7.9',
22                 'weight': 20,
23                 'maxconn': 30
24             }
25         }
View Code
haproxy.cfg配置文件
 1 global       
 2         log 127.0.0.1 local2
 3         daemon
 4         maxconn 256
 5         log 127.0.0.1 local2 info
 6 defaults
 7         log global
 8         mode http
 9         timeout connect 5000ms
10         timeout client 50000ms
11         timeout server 50000ms
12         option  dontlognull
13 
14 listen stats :8888
15         stats enable
16         stats uri       /admin
17         stats auth      admin:1234
18 
19 frontend oldboy.org
20         bind 0.0.0.0:80
21         option httplog
22         option httpclose
23         option  forwardfor
24         log global
25         acl www hdr_reg(host) -i www.oldboy.org
26         use_backend www.oldboy.org if www
27 
28 backend www.oldboy.org
29         server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
View Code

 简易流程图:

 


 1 # -*- coding:utf-8 -*-
 2 # Author: JACK ZHAO
 3 
 4 #菜单列表
 5 operation_list = ["Search haproxy.cfg backend information","Add haproxy.cfg backend information",\
 6                   "Delete haproy.cfg backend information"]
 7 #状态判断标志
 8 flag_search = 0
 9 flag_add = 0
10 
11 while True:
12     flag_search = 0
13     print(" Haproy configuration file management ".center(50, "#"))
14     for index,i in enumerate(operation_list): #遍历菜单列表
15         print([index],i)
16     choice = input("Please enter your choice[Q:exit]:")
17     if choice.isdigit(): #判读输入是否为整数
18         choice = int(choice)
19         if choice >= 0 and choice <= 2: #判读选择是否在菜单对应的数字内
20             if choice == 0: #查询backend information
21                 url = input("Please enter URL Address:")
22                 with open("haproxy.cfg","r",encoding="utf-8") as f_search: #打开haproxy.cfg一次性读取到内存中
23                     for line in f_search:
24                         if flag_search == 1:
25                             if  "backend" in line:
26                                 flag_search = 0
27                                 continue
28                             else:
29                                 if len(line) <= 1: #不打印空白行
30                                     continue
31                                 else:
32                                     print("\033[32;1m%s\033[0m" % line.strip()) #打印符合条件的行,并去掉前后空格、换行符
33                                     continue
34                         if "backend %s"%url in line and "use_backend" not in line :
35                             flag_search = 1
36             elif choice == 1: #新增backend information
37                 arg = input("Pleas enter backend:")
38                 dic_add = eval('''%s'''%arg) #将输入的字符串转成字典
39                 with open("haproxy.cfg", "a+", encoding="utf-8") as f_add:
40                     f_add.seek(0) #将位置光标移动开始位置,a+模式默认光标在文件最后
41                     for line in f_add:
42                         if  dic_add["backend"] in line and "use_backend" not in line: #判读backend 是否已经存在
43                             print("\033[31;1m%s is exists.\033[0m" %dic_add["backend"])
44                             flag_add =1
45                             break
46                     if flag_add == 1:
47                         continue
48                     f_add.write("\nbackend %s\n" %dic_add["backend"])
49                     f_add.write("\t\tserver %s weight %s maxconn %s\n" % (dic_add["record"]["server"],\
50                                     dic_add["record"]["weight"],dic_add["record"]["maxconn"]))
51             elif choice == 2:
52                 arg = input("Pleas enter backend:")
53                 dic_delete = eval('''%s''' % arg) #将输入的字符串转为字典格式
54                 with open("haproxy.cfg", "r", encoding='utf-8') as f_delete:
55                     lines = f_delete.readlines()
56                 with open("haproxy.cfg", "w", encoding='utf-8') as f_delete: #覆盖源文件的方式删除内容
57                     for line in lines:
58                         if "backend %s" %dic_delete["backend"] in line or "server %s weight %s maxconn %s"\
59                             %(dic_delete["record"]["server"],dic_delete["record"]["weight"],dic_delete["record"]["maxconn"]) in line:
60                             continue
61                         f_delete.write(line)
62         else:
63             print("It's only going to be 0 to 2.") #必须是菜单对应的数字
64             continue
65     elif choice == "Q": #选择退出
66         exit()
67     else:
68         print("It has to be an integer.")

posted on 2017-11-17 09:38  ChangMingZhao  阅读(134)  评论(0)    收藏  举报

导航