版本:python3
功能:对haproxy配置文件进行简单的查询、添加以及删除功能
操作流程:
1.根据提示选择相应的选项
2.进入所选项后,根据提示写入相应的参数
3.查询功能会返回查询结果,添加、删除以及回退功能无回显

流程图:

 global
log 127.0.0.1 local2
daemon
maxconn 256
log 127.0.0.1 local2 info
defaults
log global
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option dontlognull listen stats :8888
stats enable
stats uri /admin
stats auth admin:1234 frontend oldboy.org
bind 0.0.0.0:80
option httplog
option httpclose
option forwardfor
log global
acl www hdr_reg(host) -i www.oldboy.org
use_backend www.oldboy.org if www backend www.oldboy.org
server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000 backend buy.oldboy.org
server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000 backend nb.oldboy.org
server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000 backend ttt.oldboy.org
server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000 backend www.oldboy.com
server 192.168.1.1 192.168.1.1 weight 30 maxcoon 3000

haproxy配置示例文件

 import os
import os.path
import time
def query(back_name):#定义查询函数
flage=False #[通过用户给定的backend,找到backend所在行,在寻找下一个以backend开头的行,将两个backend中间的内容添加进列表中]
query_list=[]
with open('ha','r') as ha: #打开配置文件
for line in ha: #获取配置文件的内容
if line.strip()=='backend %s'%(back_name): #获取要查询的backend所在行的内容
flage=True#善用标志位
continue
if line.strip().startswith('backend'):#判断是否行开头为backend
flage=False
if flage and line.strip(): #判断flage是否为真
query_list.append(line.strip())#将符合的内容添加单独进列表中
return query_list def add_backend(back_name,ip,weight,maxcoon):#定义添加函数
current_title='backend %s'%back_name #获得用户输入backend所在行内容
current_record='server %s %s weight %s maxcoon %s'%(ip,ip,weight,maxcoon) #获得用户所要添加的server
fetch_list=query(back_name) #获取指定的backend下的记录
if fetch_list: #如果添加的backend存在
if current_record in fetch_list: #如果添加的子项存在backend中,则不进行操作
pass # '''【主要思路:通过判断,将需要添加的backend下的内容和原来已经存在的内容存入一个列表,将需要添加的
# backend之前的内容写入新文件,再将backend和存入列表中需要添加的sever写入,最后添加其余内容】'''
else:
fetch_list.append(current_record)#如果不存在backend中,添加进指定的backend中
flage=False #定义标示位,用于判断前后backend之间需要写入的内容
has_write=False##定义标示位,用于判断需要添加的内容是否已存在
with open('ha','r') as ha,open('ha.new','w') as new_ha: #打开ha配置文件,创建一个新的文件
for line in ha: #迭代配置文件
if line.strip() == current_title: #判断文件中是否有用户输入的backend
flage=True
new_ha.write(line)#把backend写入到新配置文件内
continue
if flage and line.strip().startswith('backend'):
flage=False
if flage :
if not has_write: #通过标示位判断
for line in fetch_list:#获得需要添加的backend下的所以server
temp="%s%s\n" %(" "*8,line)
new_ha.write(temp) #写入新的文件中
has_write = True
else:
new_ha.write(line)#将其余内容写入到新的文件中
else:#如果用户输入的backend不存在配置文件中
with open('ha','r') as ha,open('ha.new','w') as new_ha:
for line in ha:
new_ha.write(line)#将配置文件的所以内容写入新的文件中
new_ha.write('\n'+current_title)#将用户输入的backend写入新的文件中
tem=''*8+current_record+'\n'
new_ha.write('\n'+tem)#将用户输入的sever写入文件中
os.rename('ha','ha_bak')
os.rename('ha.new','ha') def quit_time():#查询完成后退出
for i in [3,2,1]:
print('\033[32;1m查询完毕,正在返回主菜单.....\033[1m',i)
time.sleep(1) def del_backend(back_name,ip,weight,maxcoon): #与添加类似,通过将需要的server存入一个列表中,最后将其余内容和列表中的内容写入新的文件中
current_title='backend %s'%back_name #通过文件重命名的方式实现功能
current_record='server %s %s weight %s maxcoon %s'%(ip,ip,weight,maxcoon)
fetch_list=query(back_name) #获取指定的backend下的记录
if fetch_list: #如果添加的backend存在
if current_record in fetch_list: #如果添加的子项存在backend中
fetch_list.remove(current_record)
else:
pass
flage=False
has_write=False
with open('ha','r') as ha,open('ha.new','w') as new_ha:
for line in ha:
if line.strip() == current_title:
flage=True
new_ha.write(line)#把backend写入到新配置文件内
continue
if flage and line.strip().startswith('backend'):
flage=False if flage :
if not has_write:
for line in fetch_list:
temp="%s%s\n" %(" "*8,line)
new_ha.write(temp)
has_write = True
else:
new_ha.write(line)
else:
with open('ha','r') as ha,open('ha.new','w') as new_ha:
for line in ha:
new_ha.write(line)
new_ha.write('\n'+current_title)
tem=''*8+current_record+'\n'
new_ha.write('\n'+tem)
if os.path.isfile('ha_bak'): #文件重命名需要判断文件是否存在,当第一次时,备份文件不存在会报错
os.remove('ha_bak')
os.rename('ha','ha_bak')
os.rename('ha.new','ha') def fallback(): #回退代码,将备份文件和当前配置文件重命名的方式实现功能
os.rename('ha','ha_new')
os.rename('ha_bak','ha') while True:
print('\033[34;1m####################################### \033[1m') #打印选项
user_enter=input('''\033[32;1m 1.查询后端backend
2.添加后端backend
3.删除后端backend
4.回滚至前一版本
5.退出\033[1m
请输入你要选择的操作:''' )
if user_enter =='':
back_name=input('\033[32;1m请输入需要查询的backend名称,example:www.oldboy.org:\033[1m')
result=query(back_name) #调用查询函数
if result:
print('\033[33;1m查询结果如下:\033[1m')
for i in result:
print(i)
quit_time()
continue
else:
print('\033[34;1m查询错误,backend不存在,结果为空!\033[1m')
elif user_enter=='':
back_name=input('\033[32;1m请输入需要查询的backend名称,example:www.oldboy.org:\033[1m')
ip=input('\033[33;1m请输入要添加的serverIP地址,example192.168.1.1:\033[1m')
weight=input('\033[34;1m请输入该server的weight:\033[1m')
maxcoon=input('\033[35;1m请输入该server的maxcoon:\033[1m')
add_backend(back_name,ip,weight,maxcoon)
print('添加完成')
quit_time()
elif user_enter=='':
back_name=input('\033[32;1m请输入需要删除的backend名称,example:www.oldboy.org:\033[1m')
ip=input('\033[33;1m请输入要添加的serverIP地址,example192.168.1.1:\033[1m')
weight=input('\033[34;1m请输入该server的weight:\033[1m')
maxcoon=input('\033[35;1m请输入该server的maxcoon:\033[1m')
del_backend(back_name,ip,weight,maxcoon)
print('删除完成')
quit_time()
elif user_enter=='':
try:
fallback()
except FileNotFoundError: #回退时,备份文件不存在报错
print('\033[31;1m回退失败,当前为最新版本,无法回退!\033[1m')
except FileExistsError:#用户重复回退
print('已经是最新版本')
quit_time()
elif user_enter=='':
quit_time()
break
else:#非法输入时
print('\033[31;1m您输入的选项错误,请重新选择!\033[1m')

程序代码

haproxy配置文件简单管理的更多相关文章

  1. Service系统服务(一):安装一个KVM服务器、KVM平台构建及简单管理、virsh基本管理操作、xml配置文件的应用、为虚拟机制作快照备份、快建新虚拟机

    一.安装一个KVM服务器 目标: 本例要求准备一台 RHEL7.2 服务器,将其搭建为KVM平台,主要完成下列操作: 1> 关闭本机的SELinux保护.防火墙服务   2> 挂载RHEL ...

  2. haproxy配置文件详解和ACL功能

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  3. ssdb主从及双主模型配置和简单管理

    ssdb主从及双主模型配置和简单管理 levelDB是一个key->value 的数据存储库,其只能在本地保存数据,支持持久化,并且支持保存非常大的数据,单机redis在保存较大数据的时候数十G ...

  4. 8 -- 深入使用Spring -- 4...6 AOP代理:基于注解的XML配置文件的管理方式

    8.4.6 基于XML配置文件的管理方式 Spring 2.x 提供一个新的aop:命名空间来定义切面.切入点和增强处理. XML配置方式优点: ⊙ 如果应用没有使用JDK 1.5 以上版本,那么应用 ...

  5. 如何在 NetCore 中定义我们自己的JSON配置文件的管理器。

    一.介绍 微软已经对外提供了新的平台,我们叫它们是 Net Core 平台,这个平台和 Net Framework 平台有本质的区别,这个最本质的区别就是微软的C#代码可以跨平台了.当前我们主流的3大 ...

  6. Python3.5 day3作业二:修改haproxy配置文件。

    需求: 1.使python具体增删查的功能. haproxy的配置文件. global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 lo ...

  7. python之haproxy配置文件操作(第三天)

    作业: 对haproxy配置文件进行操作 要求: 对haproxy配置文件中backend下的server实现增删改查的功能 一.这个程序有二个版本 1. python2.7版本见haproxy_py ...

  8. haproxy配置文件

    haproxy配置文件   思路:读一行.写一行 global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info de ...

  9. 利用 ssh 的用户配置文件 config 管理 ssh 会话

    http://dhq.me/use-ssh-config-manage-ssh-session 利用 ssh 连接远程服务器,一般都要输入以下类似命令: 1 ssh user@hostname -p ...

随机推荐

  1. 黄聪:wordpress源码解析-数据库表结构(转)

    如果是一个普通的用户,不需要了解wordpress数据库的结构.但是,如果你正在写一个插件,你应该会对wordpress如何处理它的数据和关系感兴趣.如果你已经尝试使用已经存在的wordpress a ...

  2. (VS) TFS lost mapping suddenly.

    家里的网络不是很稳定.今天突然发现 TFS 上所有的 mapping都突然没有了. 尝试去remapping,在Source Control Explorer 中右击源文件,然后选择 Advanced ...

  3. JavaScript中文字符验证的函数/正则

    /** * 中文字符验证 * @param {} str * @return {Boolean} */ function checkChinese(str) { var re = /[^\u4e00- ...

  4. GL_GL系列 - 预算管理分析(案例)

    2014-07-09 Created By BaoXinjian

  5. NYOJ16 矩形嵌套(DAG最长路)

    矩形嵌套 紫书P262 这是有向无环图DAG(Directed Acyclic Graph)上的动态规划,是DAG最长路问题 [题目链接]NYOJ16-矩形嵌套 [题目类型]DAG上的dp & ...

  6. hdu 5774 Where Amazing Happens

    Where Amazing Happens 题意: 让你输出各个队名的出现次数. 题解: 打表题,好坑,相同的没有放在一起,需要认真找,否则容易错. 代码: #include<iostream& ...

  7. HTML document对象(2)

    五.相关元素操作: var a = document.getElementById("id");找到a: var b = a.nextSibling,找a的下一个同辈元素,注意包含 ...

  8. Windows2008防火墙封ip

    http://www.bitscn.com/os/windows/201411/406212.html

  9. iframe替代方案

    自己写一个pagelet框架.封装成JSP的Taglib. <tms:view header="common-header" footer="common-foot ...

  10. netty中LengthFieldBasedFrameDecoder的使用

    在org.jboss.netty.handler.codec.frame包中,有LengthFieldBasedFrameDecoder类用来解析带有长度属性的包,只要我们在传输协议中加入包的总长度就 ...