修改haproxy配置文件

 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.baidu.org
29         server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
haproxy配置文件
  1 #查找
  2 def search(website ):
  3     reault_list = []#存放查询结果
  4     search_flag = False #定义查询结果标志
  5 
  6     with open('haproxy','r',encoding= 'utf-8') as f:
  7         for line in f:#if语句顺序进行,当前条件判断完,进入下一判断
  8             if 'backend {}'.format(website) == line.strip():#找到backend www.oldboy.org 开始截取
  9                 search_flag = True
 10             if line.strip().startswith('backend') and 'backend {}'.format(website) != line.strip() :#找到下一个 backend www.xxx.com部分结束,截取其中部分
 11                  search_flag = False
 12             if search_flag:
 13                 reault_list.append(line .strip() )
 14         print(reault_list )
 15         f.close()
 16     if reault_list == []:
 17         print('对不起,您查询的网址不存在!')
 18     else:
 19         return reault_list
 20 
 21 #增加
 22 def add(website):
 23     arg = eval(website) #将输入的字符串转变为字典
 24     backend_title = 'backend {}'.format(arg['backend'])# 要插入backend的字段
 25     record_title = arg['record']
 26     context_record = 'server {0}{0} weight {1} maxconn {2}'.\
 27         format(record_title['server'],record_title['weight'],record_title['maxconn']   )
 28     add_flag = True#设置新增标志位
 29     with open('haproxy', 'r+', encoding='utf-8') as f:
 30         for line in f:
 31             if line.strip() == backend_title:
 32                 print("您新增的网址已经存在!")
 33                 add_flag = False
 34             if add_flag:
 35                 f.write('\n{}'.format(backend_title))
 36                 f.write('\n\t\t{}'.format(context_record))
 37         f.close()
 38 
 39 #删除
 40 def delete(website):
 41     delete_flag = False #设置删除标志位
 42     delete_result = True
 43     with open('haproxy', 'r', encoding='utf-8') as f,\
 44         open('haproxy_bak', 'w') as f1:
 45         for line in f:
 46             if 'backend {}'.format(website ) == line.strip():
 47                 delete_flag = True
 48                 delete_result = False
 49                 continue
 50             if line.strip().startswith('backend') and   line.strip() != website:
 51                 delete_flag = False
 52             if not delete_flag:
 53                 f1.write(line)
 54         if delete_result :
 55             print("您删除的网址不存在!")
 56     with open('haproxy', 'w') as f, \
 57         open('haproxy_bak', 'r', encoding='utf-8') as f1:
 58         for line in f1:
 59             f.write(line)
 60 
 61 #修改
 62 def update(website):
 63     arg = eval(website )
 64     #    print(arg)
 65     backend_title = "backend {}".format(arg['backend'])  # 要插入backend整个字段
 66     #    print(backend_title)
 67     record_title = arg["record"]
 68     context_record = "server {0} {0} weight {1} maxconn {2}". \
 69         format(record_title['server'], record_title['weight'], record_title['maxconn'])
 70     print(context_record)
 71 
 72     update_flag = False  #设置修改标志位
 73     update_re = False    #设置重复修改位
 74 
 75     with open('haproxy', 'r', encoding='utf-8') as f, \
 76             open('haproxy_bak', 'w') as f1:
 77         for line in f:
 78             if line.strip() == backend_title:
 79                update_flag = True
 80                continue                            #如果出现修改backend_title,设置修改标志位为True。
 81             if line.strip().startswith('backend') and line.strip() != backend_title :
 82                 update_flag = False                #捕捉到下一个backend_title,设置修改标志位为False。
 83             if not update_flag:
 84                 f1.write(line)
 85             if update_flag and  not update_re:    #修改标志位为True,并且没有被修改过
 86                 f1.write('\n{}'.format(backend_title))
 87                 f1.write('\n\t\t{}\n'.format(context_record))
 88                 update_re = True
 89 
 90     with open('haproxy', 'w') as f, \
 91             open('haproxy_bak', 'r', encoding='utf-8') as f1:
 92         for line in f1:
 93             f.write(line)
 94 
 95 
 96 
 97 #主函数
 98 while True:
 99     print('欢迎进入haproxy配置程序'.center(50,'-') )
100     print(  '\n1 查询\n')
101     print(' 2 新增\n')
102     print(' 3 删除\n')
103     print(' 4 修改\n')
104     op_haproxy = input('选择要进入模式的ID-》:')
105     if op_haproxy == '1':
106         website = input('请输入要查询的网址:\n'
107                         '例如www.oldboy.org')
108         search(website )
109     elif op_haproxy == '2':
110         website = input("请输入要新增的网址配置:"
111                         "例如:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8',"
112                "'weight': 20,'maxconn': 3000}}\n")
113         add(website)
114     elif op_haproxy == '3':
115         website = input("请输入要删除的网址配置:"
116                         "例如:www.baidu.com\n")
117 
118         delete(website)
119     elif op_haproxy == '4':
120         website = input("请输入要修改的网址配置:"
121                         "例如:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8',"
122                         "'weight': 20,'maxconn': 3000}}\n")
123         update(website)
124     elif op_haproxy == 'q':
125         break
126     else:
127         print("请检查您的输入!")
View Code

 

posted @ 2019-06-29 13:27  普罗米修斯-  阅读(248)  评论(0编辑  收藏  举报