Haproxy configuration file
Idea: Read a line, write a line
Global log 127.0.0.1 local2 daemon maxconn log 127.0.0.1 local2 infodefaults Log Global Mode HTTP timeout connect 5000ms timeout client 50000ms timeout server 50000ms option Dontlognulllisten stats:8888 stats enable stats uri /admin stats auth admin:1234frontend 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 Wwwbackend www.oldboy.org Server 100.1.7.9 100.1.7.9 weight maxconn backend buy.oldboy.org server 100.1.7.90 100.1.7.90 weight 20 Maxconn 3000 configuration file
Requires that the specified backend content can be extracted, or the specified backend content can be added
#!/usr/bin/env python#-*-coding:utf-8-*-def fetch (backend): result = [] with open (' ha.conf ', ' R ') as F: Flag = False for line in F:if Line.strip (). StartsWith (' backend ') and line.strip () = = "Backend" + Backe Nd:flag = True Continue if flag and Line.strip (). StartsWith (' backend '): Break if Flag and Line.strip (): Result.append (Line.strip ()) return result def add (BA Ckend, record): result = Fetch (backend) If not result: # no backend, no record pass else: # with Back End If record in result: # Records record pass Else:result.append With open (' ha.conf ', ' R ') as-old, open (' new.conf ', ' W ') as New:continue_flag = False F or line in Old:if Line.strip (). StartsWith (' backend ') and line.strip () = = "Backend" + backend: ContinuE_flag = True New.write (line) for temp in result: New.write ("" *8 + temp + "\ n") continue if Continue_flag and Line.strip (). Star Tswith (' backend '): Continue_flag = False if Continue_flag: Pass Else:new.write (line) def ADD2 (backend, record): With open (' ha.conf ' , ' R ') as old, open (' new.conf ', ' W ') as New:in_backend = False Has_backend = False Has_record = Fals E for line in Old:if Line.strip (). StartsWith (' backend ') and line.strip () = = "Backend" + backend: Has_backend = True In_backend = True New.write (line) continue If In_backend and Line.strip (). StartsWith (' backend '): If not Has_record:new.writ E ("" *8 + record + ' \ n ') New.write (line) In_backend = False Continue if In_backend and Li Ne.strip () = = Record:has_record = True New.write (line) continue If Line.strip (): New.write (line) if not has_backend: # write backend, write record new.w Rite (' backend ' + backend + ' \ n ') new.write (' *8 + record + ' \ n ') # ret = Fetch ("www.oldboy.org") # Print (ret) # Add (' www.oldboy.org ', "Server 100.1.7.10 100.1.7.10 Weight maxconn") # add2 (' www.oldboy.org ', "Server 100.1.7.11 100.1.7.11 weight maxconn 3000 ")
Python string combat