【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(' %s '% a) 73 f1.write(" %s " % 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 = ' '.join(r_list) 93 f2.write(' '* 8 + t + ' ') 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 ' % (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 %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 +' ') 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 +' ') 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 +' ') 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 %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()