码迷,mamicode.com
首页 > 编程语言 > 详细

使用python更新haproxy配置文件

时间:2016-11-28 01:12:45      阅读:386      评论:0      收藏:0      [点我收藏+]

标签:null   复制   continue   type   not   listen   frontend   exp   打开文件   

老板现在给你任务,公司有haproxy配置文件,希望通过python程序可以对ha配置文件进行增删改,不再是以往的打开文件进行直接操作了。

现有ha配置文件如下:

技术分享
 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
30 
31 backend buy.oldboy.org
32         server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000
33 原配置文件
View Code

 

实现python代码如下,python3.X

技术分享
 1 # -*- coding:utf-8 -*-
 2 def fetch(backend):
 3     result = []
 4     with open(ha.conf, r) as f:
 5         flag = False
 6         for line in f:
 7             if line.strip().startswith(backend) and line.strip() == "backend " + backend:
 8                 flag = True
 9                 continue
10             if flag and line.strip().startswith(backend):
11                 break
12             if flag and line.strip():
13                 result.append(line.strip())
14 
15     return result
16 def add(backend,record):
17     record_list = fetch(backend)
18     #print (type(record_list))
19     if not record_list:
20     #1、无backend时,将旧文件全部复制到新文件中,再将查询的记录添加入新文件。
21         with open(ha.conf, r) as old,open(newha.conf,w) as new:
22             for line in old:
23                 new.write(line)
24                 #复制
25             new.write(\nbackend  + backend + \n)
26             new.write(  * 8 + record + \n)
27             #此两行为添加新记录(未查找到的)
28     else:
29         if record in record_list:
30         #backend存在,record也存在
31             pass
32         else:
33         #backend存在,record不存在
34             record_list.append(record)
35             #将输入的不存在的record添加入record_list
36             with open(ha.conf, r) as old, open(newha.conf, w) as new:
37                 flag = False
38                 for line in old:
39                     if line.strip().startswith(backend) and line.strip() == "backend " + backend:
40                         flag = True
41                         new.write(line)
42                         #将查找到的backend写入新文件
43                         for new_line in record_list:
44                             new.write(  * 8 + new_line + \n)
45                             #将新记录写入新文件
46                     if flag and line.strip().startswith(backend):
47                     #下一个backend,此时flas为True
48                         flag = False
49                         #将flag重置为False
50                         new.write(line)
51                         #仅为写入其他backend文件
52                         continue
53                         #防止重复写入
54                     if line.strip() and not flag:
55                         new.write(line)
56 ret = add("www.oldboy.org",server 222.2.2.2 222.2.2.2 weight 22 maxconn 2222)
View Code

 

使用python更新haproxy配置文件

标签:null   复制   continue   type   not   listen   frontend   exp   打开文件   

原文地址:http://www.cnblogs.com/wangpangpang/p/6107590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!