Python3.5 day3做業二:修改haproxy配置文件。

需求:python

      1.使python具體增刪查的功能。git

haproxy的配置文件。app

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

import  os,re,sys
from collections import defaultdict,OrderedDict
conf = "haproxy.cfg"
conf_new = "haproxy.cfg.new"
backend_list = []
server_dict= {}
show_dict = {}
backend_name_dict = {}
server_info= []
server_flag = False                                                     ###初始化server判斷標誌位###
with open(conf,'r') as ha:                                             ###打開haproxy配置文件###
    for line in ha:
        line = line.strip('\n')
        if re.match('backend',line):                                    ###匹配配置文件以backend開頭的行###
            backend_name = re.split('\s+',line)[1]
            backend_list.append(backend_name)
            server_info1 = []     
            server_flag = True                                           ###賦值標誌位爲真,用來與server關聯###
        elif server_flag and re.match('\s+server',line):               ###匹配配置文件以server開頭的行###
            server_info = re.split('\s+',line)
            server_info.remove('')
            server_info1.append(server_info)
            server_dict['server'] = server_info[1]
            server_dict['weight'] = server_info[4]
            server_dict['maxconn'] = server_info[5]
            backend_name_dict[backend_name] = "name"
            backend_name_dict[backend_name] = server_info1 
        else:
            server_flag = False

#循環
options_list = ["1.查詢backend","2.刪除backend","3.新增backend","5.退出"]
while True:
      for i in options_list:
        print(i)
      find = (input("\033[94m請選擇操做條目序號:\033[0m"))
      if find.isdigit():
            find = int((find))
            #1是查詢
            if find == 1:
               for i, ele in enumerate(backend_list):
                   print(i, ele)
               find1 = input("請輸入您要查詢的backend的序號:")
               find1 = int(find1)
               for j in range(1):
                   print("==========%s============" %backend_list[find1])
                   for i in backend_name_dict[backend_list[find1]]:
                      print("server %s weigt %s maxconn %s" %(i[1],i[3],i[5]))
            #2是刪除
            elif find == 2:
               for i, ele in enumerate(backend_list):
                       print(i, ele)
               find3 = int(input("請選擇您要刪除backend的序號:"))
               server_show = []
               for i, ele in enumerate(backend_name_dict[backend_list[find3]]):
                               server_show.append("server %s weight %s maxconn %s" % (ele[1], ele[3], ele[5]))
                               print(i, "server %s weight %s maxconn %s" % (ele[1], ele[3], ele[5]))
                               server_show1= str(server_show[0])
                               print(server_show1)
                               f = open(conf, "r")
                               f1 = open(conf_new, "a+")
                               for i in f:
                                   if server_show1 in i:
                                       i = i.replace(server_show1, "")
                                   f1.write(i)
                                   f1.flush()
                               f.close()
                               f1.close()
                               os.remove(conf)
                               os.rename(conf_new, conf)
                               backend_list1 = []
                               backend_list1.append("backend %s" %(backend_list[find3]))
                               backend_list2 = str(backend_list1[0])
                               f = open(conf, "r")
                               f1 = open(conf_new, "a+")
                               for i in f:
                                   if backend_list2 in i:
                                       i = i.replace(backend_list2, "")
                                   f1.write(i)
                                   f1.flush()
                               f.close()
                               f1.close()
                               os.remove(conf)
                               os.rename(conf_new, conf)
                               print("刪除成功!!!")
            #3是增長
            elif find == 3:
                   arg = []
                   backend_name1 = []
                   backend_list3 = []
                   input_back = input("\033[94m請輸入backend(www.orgboy.org):\033[0m")
                   arg.append("backend %s" %input_back)
                   input_server = input("\033[94m請輸入server(127.0.0.1):\033[0m")
                   input_weight = input("\033[94m請輸入weight(20):\033[0m")
                   # arg["weight"] = input_weight
                   input_maxconn = input("\033[94m請輸入maxconn(3000):\033[0m")
                   arg.append("        server %s weight %s maxconn %s" %(input_server,input_weight,input_maxconn))
                   backend_name1 = str(arg[0])
                   f = open(conf, "a+")
                   f.write("%s \n" %backend_name1)
                   f.flush()
                   f.close()
                   backend_list3 = arg[1]
                   backend_list3 = str(backend_list3)
                   f = open(conf, "a+")
                   f.write("%s \n" %backend_list3)
                   f.close()
                   print("添加成功!!!")
            #5是退出
            elif find == 5:
               exit()
      else:
          print("\033[91m請輸入正確的序號\033[0m")
          continue
相關文章
相關標籤/搜索