脚本运行环境win7, python3.6, pycharm2017社区版
ha_config配置文件如下:
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 weight 20 maxconn 3000 backend www.test.org server 100.1.7.3 weight 20 maxconn 3000 server 100.1.7.6 weight 20 maxconn 3000
需求:
HAproxy配置文件操作:
1. 根据用户输入输出对应的backend下的server信息
2. 可添加backend 和sever信息
3. 可修改backend 和sever信息
4. 可删除backend 和sever信息
5. 操作配置文件前进行备份
6. 添加server信息时,如果ip已经存在则修改;
如果backend不存在则创建;若信息与已有信息重复则不操作
无论添加还是删除,首先要先判断是否存在相应的backend以及是否存在backend的配置信息:
如何取得的信息呢?
备份ha_config配置文件:
def backup(): with open('ha_config','r') as f ,open('ha_config'+'.bak','w') as bak_f: for line in f: bak_f.write(line) bak_f.flush()
获取backend 信息的程序:
def get_backend(backend): backend_info = [] with open('ha_config','r') as f: flag = False for line in f: if line.strip().startswith('backend') and line.strip() == 'backend ' + backend: backend_info.append(line.strip()) flag = True continue if line.strip().startswith('backend') and flag: break if flag and line.strip(): backend_info.append(line.strip()) # print(backend_info) return backend_info
主程序代码如下:
# -*- coding: utf-8 -*- def backup(): #备份ha_config配置文件 with open('ha_config','r') as f ,open('ha_config'+'.bak','w') as bak_f: for line in f: bak_f.write(line) bak_f.flush() def get_backend(backend): #输入backend,得到相应的backend 和 record with open('ha_config','r') as f: flag = False backend_info = [] for line in f: if line.strip().startswith('backend') and line.strip() == 'backend ' + backend: flag = True backend_info.append(line.strip().split()[-1]) continue if line.strip().startswith('backend') and flag: break if line.strip() and flag: backend_info.append(line.strip()) # print(backend_info) return backend_info def add_backend(backend,record): backend_info = get_backend(backend) print(backend_info) if backend not in backend_info:#backend 不存在时 backup()#备份-函数 print('ha_config,备份为ha_config.bak!') with open('ha_config','r') as f ,open('add_config','w') as add_f: for line in f: add_f.write(line) add_f.flush() #把不存在的backend 和record添加到文件结尾 add_f.write(' ') add_f.write('backend '+backend+' ') add_f.write(' '*8 + record) add_f.flush() print('添加完成,请查看:add_config') # if backend in backend_info: else: # backend存在,但record 不存在时 if record not in backend_info: backup() print('backend存在,record不存在,备份完成!') with open('ha_config','r') as f ,open('add22_config','w') as add_f: flag = True for line in f: if line.strip().startswith('backend') and line.strip() == 'backend ' + backend: flag = False continue if line.strip().startswith('backend') and not flag: flag = True if flag: add_f.write(line) add_f.flush() add_f.write(' ') add_f.write('backend ' + backend + ' ') add_f.write(' ' * 8 + record) add_f.flush() print('backend添加完成,请查看:add22_config') else: print(' 33[32mbackend 和 record 信息存在 33[0m 无需修改!') def delete_backend(backend): record = get_backend(backend) print(record) if backend in record: backup()#调用-备份ha_config 函数 print('删除backend前,备份,备份完成,备份名称为:ha_config.bak!') with open('ha_config','r') as f ,open('del_config','w') as del_f: flag = True for line in f: if line.strip().startswith('backend') and line.strip() == 'backend ' + backend: flag = False continue if line.strip().startswith('backend') and not flag: flag = True if flag: del_f.write(line) del_f.flush() print('文件删除完成,请查看新文件:del_config') else: print('不存在backend:%s'%backend) bk = input(" 33[31m请输入backend: 33[0m") backend_info = get_backend(bk) if bk in backend_info: print(backend_info) your_select = input('you want to delete or modify? 33[41m(del / mod) 33[0m') if your_select == 'del': delete_backend(bk) elif your_select == 'mod': recd = input('请输入 record:') add_backend(bk,recd) else: print('不能识别,即将退出……') exit() else: print('没有backend,你想添加吗?') your_choice = input('yes or no ?("y" or "n")') if your_choice == 'y': recd = input('输入record:') add_backend(bk,recd) elif your_choice == 'n': exit() else: print('只能识别 y or n !') exit()
运行主程序:
提示输入backend:例如www.oldboy.org
存在时,提示是否删除或修改,
不存在backend时,提示是否添加,
小小总结:
1、用到的知识点:if-elif-else循环,打开文件 with open('文件名','r') as f:, for line in f:(逐行打印文件内容,效率高),
字符串方法startswitch(‘XXX’) (以字符串‘XXX’开始);
2、感觉自己现在还是命令的堆叠吧,仅仅是知识点掌握的太少???(自我安慰一下)
继续加油吧,烧年!