HAproxy配置文件操作

要求

1. 根据用户输入输出对应的backend下的server信息
2. 可添加backend 和sever信息
3. 可修改backend 和sever信息
4. 可删除backend 和sever信息
5. 操作配置文件前进行备份
6 添加server信息时,如果ip已经存在则修改;如果backend不存在则创建;若信息与已有信息重复则不操作

  1 def find(backend):
  2     ‘‘‘
  3     查看backend下sever信息
  4     :param backend:
  5     :return:
  6     ‘‘‘
  7     ls = []
  8     with open(‘HA.txt‘, ‘r‘) as f:
  9         T = True
 10         for line in f:
 11             # 找到第一个backend,做记录 T = False
 12             if line.strip() == ‘backend %s‘ % (backend):
 13                 T = False
 14                 continue
 15             # 找到第二个backend,做记录 T = True
 16             if T == False and line.strip().startswith(‘backend‘):
 17                 T = True
 18                 break
 19             # 把关于False的加入到列表中
 20             if T == False and line.strip():
 21                 ls.append(line.strip())
 22     return ls
 23
 24
 25 def check_bk():
 26     ‘‘‘
 27     列出backend的信息
 28     :return: 选中的backend
 29     ‘‘‘
 30     ls = []
 31     T = True
 32     with open(‘HA.txt‘, ‘r‘) as f1:
 33          for line in f1:
 34             if line.strip().startswith(‘backend‘) == True:
 35                 to = line.split(‘ ‘)
 36                 ls.append(to[1])
 37          for i, v in enumerate(ls,1):
 38             print (i, v)
 39          while T:
 40              t1 = input(‘请输入查看的server的 backend信息‘)
 41              if t1.isdecimal() and int(t1) in range(0,len(ls)+1):
 42                 a = ls[int(t1)-1]
 43                 break
 44              else:
 45                 print(‘输入有误,重新输入‘)
 46     return a
 47
 48 def backup():
 49     ‘‘‘
 50     备份
 51     :return:
 52     ‘‘‘
 53     with open(‘HA.txt‘, ‘r‘) as f1, open(‘HA_old.txt‘, ‘w‘) as f2:
 54         for line in f1:
 55             f2.write(line)
 56
 57
 58 def add(bk, sr):
 59     ‘‘‘
 60     添加记录
 61     :param bk:
 62     :param sr:
 63     :return:
 64     ‘‘‘
 65     r_list = find(bk)
 66     # 没有关于bk的记录,直接添加内容
 67     T = True
 68     a = ‘backend %s‘ % bk
 69     backup()
 70     if not r_list:
 71         with open(‘HA.txt‘, ‘a‘) as f1:
 72             f1.write(‘\n%s\n‘% a)
 73             f1.write("        %s\n" % sr)
 74     # 有关于bk的记录
 75     else:
 76         # 没有sever记录
 77         backup()
 78         if sr not in r_list:
 79             with open(‘HA_old.txt‘, ‘r‘) as f1, open(‘HA.txt‘, ‘w‘) as f2:
 80                 for line in f1:
 81                     if line.strip() == ‘backend %s‘ % (bk):
 82                         T = False
 83                         f2.write(line)
 84                         continue
 85                     if T == False and line.strip().startswith(‘backend‘):
 86                         T = True
 87                     if T == True:
 88                         f2.write(line)
 89                     else:
 90                         if sr not in r_list:
 91                             r_list.append(sr)
 92                             t = ‘\n        ‘.join(r_list)
 93                             f2.write(‘ ‘* 8 + t + ‘\n\n‘)
 94     # 有sever记录,直接复制
 95         else:
 96             print(‘记录已存在,请重新选择‘)
 97             caidan()
 98
 99
100 def update_bk ():
101     ‘‘‘
102     需要修改或删除backend
103     :return:
104     ‘‘‘
105     backup()
106     T = True
107     bk = check_bk()
108     ls =[]
109     with open(‘HA.txt‘, ‘r‘) as f1:
110          for line in f1:
111             if line.strip().startswith(‘backend‘) == True:
112                 to = line.split(‘ ‘)
113                 ls.append(to[1].strip())
114     c = input(‘‘‘
115     请选择需要选项:
116     1 修改backend
117     2 删除backend
118     ‘‘‘)
119     while T:
120         #修改backend信息
121         if c == ‘1‘:
122             bk1 = input(‘请输入修改后的backend信息‘)
123             t3 = input(‘信息确认请按 y, 重新输入请按其他键‘)
124             if bk1 in ls:
125                 print(‘信息已存在,请重新选择‘)
126                 break
127             else:
128                 if t3.lower() == ‘y‘:
129                     with open(‘HA_old.txt‘, ‘r‘) as f1, open(‘HA.txt‘, ‘w‘) as f2:
130                         for line in f1:
131                             #需要修改的backend
132                             if line.strip() == ‘backend %s‘ % (bk.strip()):
133                                 f2.write(‘backend %s \n‘ % (bk1))
134                             else:
135                                 f2.write(line)
136                         print(‘修改成功‘)
137                         break
138                 else:
139                     continue
140             #删除backend信息
141         elif c == ‘2‘:
142             t = find(bk.strip())
143             while T:
144                 with open(‘HA_old.txt‘, ‘r‘) as f1, open(‘HA.txt‘, ‘w‘) as f2:
145                     for line in f1:
146                         if line.strip() == ‘backend %s‘ % (bk.strip()):
147                             continue
148                         if line.strip() in t:
149                             continue
150                         else:
151                             f2.write(line)
152                     print(‘删除成功‘)
153                     break
154             break
155
156 def check_sr():
157     T1 = True
158     while T1:
159         cont = 0
160         a1 = input(‘sever:‘)
161         ti = a1.split(‘.‘)
162         for ii in ti:
163             if ii.isdecimal() and int(ii) in range(0, 255) and len(ti) == 4:
164                 cont = cont + 1
165                 continue
166             else:
167                 print(‘输入有误,请输入由四组0-255组成的IP地址‘)
168                 break
169         if cont == 4:
170             break
171     return a1
172
173
174 def update_sr():
175     ‘‘‘
176     修改或删除sever
177     :return:
178     ‘‘‘
179     t = check_bk()
180     backup()
181     T = True
182     T1 = True
183     ls = find(t.strip())
184     cc = 0
185     with open(‘HA_old.txt‘, ‘r‘) as f1, open(‘HA.txt‘, ‘w‘) as f2:
186         for line in f1:
187             if line.strip() == ‘backend %s‘ %(t.strip()):
188                 T = False
189                 f2.write(line)
190                 continue
191             if T == False and line.strip().startswith(‘backend‘):
192                 T = True
193             if T == True:
194                 if line.strip() != cc and line.strip() not in ls:
195                     f2.write(line)
196
197             else:
198                 for i, v in enumerate(ls, 1):
199                     print (‘%s: %s‘ % (i, v))
200                 while T1:
201                     v1 = input(‘请输入要修改或删除的编号‘)
202                     if v1.isdecimal() and int(v1) in range(len(ls)+1):
203                         break
204                     else:
205                         print(‘输入有误,请重新输入‘)
206                         continue
207                 x = input(‘请选择 1 = 修改 其他键 = 删除‘)
208                 if x == ‘1‘:
209                     while T1:
210                         a1 = check_sr()
211                         a2 = input(‘weight:‘)
212                         a3 = input(‘maxconn:‘)
213                         if a2.isdecimal() and a3.isdecimal():
214                             t1 = ‘server %s %s weight %s maxconn %s‘ % (a1, a1, a2, a3)
215                             print(‘backend: %s \n%s‘ % (t, t1))
216                         else:
217                             print(‘所输入weight 和 maxconn 必须为数字,请重新输入‘)
218                             continue
219                         li_sr = ‘server %s %s weight %s maxconn %s‘ % (a1, a1, a2, a3)
220                         # server
221                         if li_sr in ls:
222                             tt = input(‘server已存在,请按 y = 重新输入,或按 other key = 返回‘)
223                             if tt == ‘y‘:
224                                 continue
225                             else:
226                                 break
227                         else:
228                             a4 = input(‘请确认信息正确,y = 确认, other key  = 重新输入‘)
229
230                             if a4 == ‘y‘:
231                                 f2.write(‘ ‘*8 + li_sr +‘\n‘)
232                             else:
233                                 continue
234                             print(‘修改成功‘)
235                             break
236                     cc = ls[int(v1)-1]
237                     del ls[int(v1)-1]
238                     for tt in ls:
239                         f2.write(‘ ‘*8 + tt +‘\n‘)
240                     T = True
241                 else:
242                     print(‘删除成功‘)
243                     cc = ls[int(v1)-1]
244                     del ls[int(v1)-1]
245                     for tt in ls:
246                         f2.write(‘ ‘*8 + tt +‘\n‘)
247                     T = True
248
249
250 def caidan ():
251     T = True
252     while T:
253         ls = input(‘‘‘请输入所需编号:
254         1 查看backend下sever信息
255         2 添加记录
256         3 修改或删除backend
257         4 修改或删除sever
258         5 退出
259         ‘‘‘)
260         if ls == ‘1‘:
261             i = check_bk()
262             l1 = find(i.strip())
263             for a in l1:
264                 print (a)
265         elif ls == ‘2‘:
266             while T:
267                 t = input(‘请输入backend信息‘)
268                 print(‘请输入sever信息‘)
269                 cont = 0
270                 while T:
271                     a1 = input(‘sever:‘)
272                     ti = a1.split(‘.‘)
273                     for ii in ti:
274                         if ii.isdecimal() and int(ii) in range(0, 255) and len(ti) == 4:
275                             cont = cont + 1
276                             continue
277                         else:
278                             print(‘输入有误,请输入由四组0-255组成的IP地址‘)
279                             break
280                     if cont == 4:
281                         break
282                 while T:
283                     a2 = input(‘weight:‘)
284                     a3 = input(‘maxconn:‘)
285                     if a2.isdecimal() and a3.isdecimal():
286                         t1 = ‘server %s %s weight %s maxconn %s‘ % (a1, a1, a2, a3)
287                         print(‘backend: %s \n%s‘ % (t, t1))
288                         break
289                     else:
290                         print(‘所输入weight 和 maxconn 必须为数字,请重新输入‘)
291                         continue
292                 t3 = input(‘信息确认请按 y, 重新输入请按其他键‘)
293                 if t3.lower() == ‘y‘:
294                     add (t, t1)
295                     print(‘添加成功‘)
296                     break
297                 else:
298                     continue
299         elif ls == ‘3‘:
300             update_bk()
301         elif ls == ‘4‘:
302             update_sr()
303         elif ls == ‘5‘:
304             exit()
305         else:
306             print(‘输入有误,请重新输入‘)
307
308 caidan()
def find(backend):    ‘‘‘    查看backend下sever信息:param backend::return:    ‘‘‘ls = []    with open(‘HA.txt‘, ‘r‘) as f:        T = True        for line in f:            # 找到第一个backend,做记录 T = Falseif line.strip() == ‘backend %s‘ % (backend):                T = False                continue# 找到第二个backend,做记录 T = Trueif T == False and line.strip().startswith(‘backend‘):                T = True                break# 把关于False的加入到列表中if T == False and line.strip():                ls.append(line.strip())    return ls

def check_bk():    ‘‘‘    列出backend的信息:return: 选中的backend    ‘‘‘ls = []    T = True    with open(‘HA.txt‘, ‘r‘) as f1:         for line in f1:            if line.strip().startswith(‘backend‘) == True:                to = line.split(‘ ‘)                ls.append(to[1])         for i, v in enumerate(ls,1):            print (i, v)         while T:             t1 = input(‘请输入查看的server的 backend信息‘)             if t1.isdecimal() and int(t1) in range(0,len(ls)+1):                a = ls[int(t1)-1]                break             else:                print(‘输入有误,重新输入‘)    return a

def backup():    ‘‘‘    备份:return:    ‘‘‘with open(‘HA.txt‘, ‘r‘) as f1, open(‘HA_old.txt‘, ‘w‘) as f2:        for line in f1:            f2.write(line)

def add(bk, sr):    ‘‘‘    添加记录:param bk::param sr::return:    ‘‘‘r_list = find(bk)    # 没有关于bk的记录,直接添加内容T = Truea = ‘backend %s‘ % bk    backup()    if not r_list:        with open(‘HA.txt‘, ‘a‘) as f1:            f1.write(‘\n%s\n‘% a)            f1.write("        %s\n" % sr)    # 有关于bk的记录else:        # 没有sever记录backup()        if sr not in r_list:            with open(‘HA_old.txt‘, ‘r‘) as f1, open(‘HA.txt‘, ‘w‘) as f2:                for line in f1:                    if line.strip() == ‘backend %s‘ % (bk):                        T = Falsef2.write(line)                        continue                    if T == False and line.strip().startswith(‘backend‘):                        T = True                    if T == True:                        f2.write(line)                    else:                        if sr not in r_list:                            r_list.append(sr)                            t = ‘\n        ‘.join(r_list)                            f2.write(‘ ‘* 8 + t + ‘\n\n‘)    # 有sever记录,直接复制else:            print(‘记录已存在,请重新选择‘)            caidan()

def update_bk ():    ‘‘‘    需要修改或删除backend:return:    ‘‘‘backup()    T = Truebk = check_bk()    ls =[]    with open(‘HA.txt‘, ‘r‘) as f1:         for line in f1:            if line.strip().startswith(‘backend‘) == True:                to = line.split(‘ ‘)                ls.append(to[1].strip())    c = input(‘‘‘    请选择需要选项:    1 修改backend    2 删除backend    ‘‘‘)    while T:        #修改backend信息if c == ‘1‘:            bk1 = input(‘请输入修改后的backend信息‘)            t3 = input(‘信息确认请按 y, 重新输入请按其他键‘)            if bk1 in ls:                print(‘信息已存在,请重新选择‘)                break            else:                if t3.lower() == ‘y‘:                    with open(‘HA_old.txt‘, ‘r‘) as f1, open(‘HA.txt‘, ‘w‘) as f2:                        for line in f1:                            #需要修改的backendif line.strip() == ‘backend %s‘ % (bk.strip()):                                f2.write(‘backend %s \n‘ % (bk1))                            else:                                f2.write(line)                        print(‘修改成功‘)                        break                else:                    continue#删除backend信息elif c == ‘2‘:            t = find(bk.strip())            while T:                with open(‘HA_old.txt‘, ‘r‘) as f1, open(‘HA.txt‘, ‘w‘) as f2:                    for line in f1:                        if line.strip() == ‘backend %s‘ % (bk.strip()):                            continue                        if line.strip() in t:                            continue                        else:                            f2.write(line)                    print(‘删除成功‘)                    break            break

def check_sr():    T1 = True    while T1:        cont = 0a1 = input(‘sever:‘)        ti = a1.split(‘.‘)        for ii in ti:            if ii.isdecimal() and int(ii) in range(0, 255) and len(ti) == 4:                cont = cont + 1continue            else:                print(‘输入有误,请输入由四组0-255组成的IP地址‘)                break        if cont == 4:            break    return a1

def update_sr():    ‘‘‘    修改或删除sever:return:    ‘‘‘t = check_bk()    backup()    T = TrueT1 = Truels = find(t.strip())    cc = 0with open(‘HA_old.txt‘, ‘r‘) as f1, open(‘HA.txt‘, ‘w‘) as f2:        for line in f1:            if line.strip() == ‘backend %s‘ %(t.strip()):                T = Falsef2.write(line)                continue            if T == False and line.strip().startswith(‘backend‘):                T = True            if T == True:                if line.strip() != cc and line.strip() not in ls:                    f2.write(line)

else:                for i, v in enumerate(ls, 1):                    print (‘%s: %s‘ % (i, v))                while T1:                    v1 = input(‘请输入要修改或删除的编号‘)                    if v1.isdecimal() and int(v1) in range(len(ls)+1):                        break                    else:                        print(‘输入有误,请重新输入‘)                        continuex = input(‘请选择 1 = 修改 其他键 = 删除‘)                if x == ‘1‘:                    while T1:                        a1 = check_sr()                        a2 = input(‘weight:‘)                        a3 = input(‘maxconn:‘)                        if a2.isdecimal() and a3.isdecimal():                            t1 = ‘server %s %s weight %s maxconn %s‘ % (a1, a1, a2, a3)                            print(‘backend: %s \n%s‘ % (t, t1))                        else:                            print(‘所输入weight 和 maxconn 必须为数字,请重新输入‘)                            continueli_sr = ‘server %s %s weight %s maxconn %s‘ % (a1, a1, a2, a3)                        # serverif li_sr in ls:                            tt = input(‘server已存在,请按 y = 重新输入,或按 other key = 返回‘)                            if tt == ‘y‘:                                continue                            else:                                break                        else:                            a4 = input(‘请确认信息正确,y = 确认, other key  = 重新输入‘)

if a4 == ‘y‘:                                f2.write(‘ ‘*8 + li_sr +‘\n‘)                            else:                                continueprint(‘修改成功‘)                            breakcc = ls[int(v1)-1]                    del ls[int(v1)-1]                    for tt in ls:                        f2.write(‘ ‘*8 + tt +‘\n‘)                    T = True                else:                    print(‘删除成功‘)                    cc = ls[int(v1)-1]                    del ls[int(v1)-1]                    for tt in ls:                        f2.write(‘ ‘*8 + tt +‘\n‘)                    T = True

def caidan ():    T = True    while T:        ls = input(‘‘‘请输入所需编号:        1 查看backend下sever信息        2 添加记录        3 修改或删除backend        4 修改或删除sever        5 退出        ‘‘‘)        if ls == ‘1‘:            i = check_bk()            l1 = find(i.strip())            for a in l1:                print (a)        elif ls == ‘2‘:            while T:                t = input(‘请输入backend信息‘)                print(‘请输入sever信息‘)                cont = 0while T:                    a1 = input(‘sever:‘)                    ti = a1.split(‘.‘)                    for ii in ti:                        if ii.isdecimal() and int(ii) in range(0, 255) and len(ti) == 4:                            cont = cont + 1continue                        else:                            print(‘输入有误,请输入由四组0-255组成的IP地址‘)                            break                    if cont == 4:                        break                while T:                    a2 = input(‘weight:‘)                    a3 = input(‘maxconn:‘)                    if a2.isdecimal() and a3.isdecimal():                        t1 = ‘server %s %s weight %s maxconn %s‘ % (a1, a1, a2, a3)                        print(‘backend: %s \n%s‘ % (t, t1))                        break                    else:                        print(‘所输入weight 和 maxconn 必须为数字,请重新输入‘)                        continuet3 = input(‘信息确认请按 y, 重新输入请按其他键‘)                if t3.lower() == ‘y‘:                    add (t, t1)                    print(‘添加成功‘)                    break                else:                    continue        elif ls == ‘3‘:            update_bk()        elif ls == ‘4‘:            update_sr()        elif ls == ‘5‘:            exit()        else:            print(‘输入有误,请重新输入‘)

caidan()
时间: 2024-09-30 10:19:59

HAproxy配置文件操作的相关文章

初学 python 之 HAproxy配置文件操作

HAproxy配置文件操作: 1. 根据用户输入输出对应的backend下的server信息 2. 可添加backend 和sever信息 3. 可修改backend 和sever信息 4. 可删除backend 和sever信息 5. 操作配置文件前进行备份 6 添加server信息时,如果ip已经存在则修改;如果backend不存在则创建:若信息与已有信息重复则不操作 配置文件 参考 http://www.cnblogs.com/alex3714/articles/5717620.html

第三周:HAproxy配置文件操作

作业 HAproxy配置文件操作: 1. 根据用户输入输出对应的backend下的server信息 2. 可添加backend 和sever信息 3. 可修改backend 和sever信息 4. 可删除backend 和sever信息 5. 操作配置文件前进行备份 6 添加server信息时,如果ip已经存在则修改;如果backend不存在则创建:若信息与已有信息重复则不操作 #Author xq import os import shutil import time def f_new(f_

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

作业: 对haproxy配置文件进行操作 要求: 对haproxy配置文件中backend下的server实现增删改查的功能 一.这个程序有二个版本 1. python2.7版本见haproxy_python27.py 2. python3.4版本见haproxy_python34.py 二.具体实现了如下功能: 1.输入1,进入backend菜单,查询server信息 2.输入2,进入backend菜单,添加server条目 3.输入3,进入backend菜单,选择server条目,进入修改环

修改haproxy配置文件

HAproxy配置文件操作: 1. 根据用户输入输出对应的backend下的server信息 2. 可添加backend 和sever信息,如果存在则不添加,不修改 4. 可删除backend 和sever信息 5. 首先选择要进入哪种模式(查询,增加,删除),按q退出整个操作,在某种模式内时按b退出重新选择,不退出则 一直处于某种模式内 #Auther:He Jianhan #查def hanshu(names,fliens): print(fliens[names].rstrip()) if

python练习_module01-3-haproxy配置文件操作

haproxy配置文件操作 haproxy 配置文件示例: global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 infodefaults log global mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms option dontlognull listen stats :8888 stats enabl

作业:老板现在给你任务,公司有haproxy配置文件,希望通过python程序可以对ha配置文件进行增删改

1 # 老板现在给你任务,公司有haproxy配置文件,希望通过python程序可以对ha配置文件进行增删改 2 #分析:对文件进行增删改,首先需要找到需要修改文件的位置,即必须先把文件读取出来,找到对应 3 #位置,进行内容的修改,增加和删除: 4 import json,os #json模块用于将用户输入的字符串转换为字典 5 6 #首先定义fetch函数,同时传入指定参数backend,用来将修改的地方找出来 7 def fetch(backend): 8 flag=False #定义fl

haproxy配置文件解释

haproxy配置文件各项解释 ####################全局配置信息######################## #######参数是进程级的,通常和操作系统(OS)相关######### global log 127.0.0.1 local0 info      #日志格式 chroot /var/haproxy              #chroot运行的路径 group haproxy                   #所属运行的用户 user haproxy  

使用python更新haproxy配置文件

老板现在给你任务,公司有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 50000m

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

需求: 1.使python具体增删查的功能. haproxy的配置文件. 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 sta