pythonでhaproxyプロファイルを変更する

4670 ワード

必要:
ユーザーがドメイン名を入力すると、次のレコードが表示されます.
ユーザーが追加レコードを入力する必要がある場合は、必要なドメイン名の下に追加します.
# !/usr/bin/env python
# -*- coding:utf-8 -*-

def select(arg):
    """
            ,    webserver     
      haproxy     
      :
    a.      
    b.    backend +        ,                   
    c.      backend           ok 
    d.               ,     ,         ,     ,                     
    :param arg:
    :return:
    """
    result = []
    flag = False
    with open("haproxy.txt", "r", encoding="utf-8") as f:
        for line in f :
            if line.strip() == "backend %s" % arg:
                flag = True
                continue
            if line.strip().startswith("backend"):
                flag  = False
            if flag:
                result.append(line)
    return result
ret = select("www.oldboy.org")

for i in ret :
    print(i)
#++++++++++++++++++++++++++++
def sele_add(yuming, record):
    """
      :
    a.          ,             
    b.   ,                  ,
    c.   , pass,            

       :
    a.  webserver           
    b.                    
    c.                            
       :
    a.                        
    #             backend
    b.         
    c.           ,      ,          ,                       backend
         ,                   
    :param arg:
    :return:
    """

    """
    biaozhiwei = False
    with open("haproxy.txt", "r", encoding="utf-8") as old, open("haproxy2.txt", "w", encoding="utf-8") as new:
        res_list = select(yuming)
        res_list.append(" "*8 + record + "
") for line in old: if line.strip() == "backend %s" % yuming: biaozhiwei = True# new.write(line) for i in res_list: new.write(i ) continue if line.strip().startswith("backend"): biaozhiwei = False if not biaozhiwei: new.write(line) """ #No.2 # : # a. # backend # b. #---------------- # , , , pass, bzw = 2 result = [] a = 0 with open ("haproxy.txt", "r", encoding="utf-8") as old , open("haproxy2.txt", "w", encoding="utf-8") as new: if ("backend %s" % yuming ) in old: print("cunz") for line in old: if line.strip() == "backend %s" % yuming: bzw = 0 result.append(line) new.write(line) continue if line.strip().startswith("backend"): bzw += 1 # backend, 1 if bzw == 1: if (" "*8 + record + "
") in result: pass else: new.write(" "*8 + record + "
") new.write(line) else: print("bucz") with open("haproxy.txt", "r", encoding="utf-8") as old, open("haproxy2.txt", "w", encoding="utf-8") as new: for line in old: new.write(line) new.write("
backend %s
" % yuming) new.write(" "*8 + record + "
") sele_add("www.oldboy.orgt","1233446")

プロファイル

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
        server 100.1.7.92 100.1.7.93 weight 20 maxconn 3000
        server 100.1.7.94 100.1.7.93 weight 20 maxconn 3000

backend buy.oldboy.org
        server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000