haproxy 文件操作,操作属于简单操作,不复杂
1 # -*- coding:utf-8 -*- 2 # LC 3 4 def search(*args): #查找Haproxy文件中的服务器 5 list1 = [] 6 with open("haproxyfile","r") as f: 7 flag = False 8 for line in f: 9 if line.strip() == "backend %s" % args: #查找第一个以backend + 输入域名的起头的backend,将相关信息记录list中 10 flag = True 11 list1.append(line.strip()) 12 continue #并继续循环,为了将backend下一行执行,即按着flag = True执行 13 if line.strip().startswith("backend") or line.strip() == "": #查到第二个backend这将flag置于Fasle,如果是空行,也将flag置于Fasle中,(防止文章末尾有多个空行) 14 flag = False 15 if flag: 16 list1.append(line.strip()) #将正确backend的后续信息继续记录至list中 17 return list1 18 19 20 def delete(string): 21 dict = eval(string) 22 backend = dict["backend"] 23 record = dict["record"] 24 new_file_list = [] 25 if backend in domain_list(): #判读要删除的域名是否在文件内 26 with open("haproxyfile","r",encoding="utf-8") as f_read: 27 for line in f_read: 28 new_file_list.append(line) #读取文件的每行,将每行写入列表 29 if line.strip() == "backend %s" %backend: #如果有符合的backend,则将最近的写入列表弹出 30 new_file_list.pop() 31 if line.strip() == "server %s weight %s maxconn %s" %(record["server"],record["weight"],record["maxconn"]):#如果有符合的server信息,则将最近的写入列表弹出 32 new_file_list.pop() 33 with open("haproxyfile1", "a") as f_write: #将删除后的写入文件中 34 for line in new_file_list: 35 f_write.write(line) 36 else: 37 print("The Domain not in this file!") 38 39 def add(string): #增加ha文件配置服务器信息 40 dict = eval(string) 41 backend = dict["backend"] 42 record = dict["record"] 43 with open("haproxyfile","a") as f_write: 44 f_write.write("backend %s "%backend) 45 f_write.write(" server %s %s weight %s maxconn %s"%(record["server"][0],record["server"][1], 46 record["weight"], 47 record["maxconn"])) 48 49 50 def domain_list(): #将文件中所有的domain都摘出来 51 backend_name = [] 52 with open("haproxyfile","r") as f_read: 53 for line in f_read: 54 if line.startswith("backend"): 55 domain_name = line.split()[1] 56 backend_name.append(domain_name) 57 return backend_name 58 59 #域名信息查找 60 domain_search_name = input("Please input the domain your search:") 61 if domain_search_name in domain_list(): 62 domain_info = search(domain_search_name) 63 print(domain_info) 64 65 #域名信息删除 66 67 string = "{'backend': 'www.yst.com.cn','record':{'server':'1.1.1.1','weight': 30,'maxconn': 2300}}" 68 delete(string) 69 70 #域名信息增加 71 string = "{'backend': 'www.yst.com.cn','record':{'server':'1.1.1.1','weight': 30,'maxconn': 2300}}" 72 add(string)
收集backend信息和对应server的信息
1 def backend_info(): 2 with open("haproxyfile","r") as f_read: 3 backend_list = [] 4 dict_all = {} 5 dict_ser = {} 6 server_list = [] 7 for line in f_read: 8 if line.strip().startswith("backend"): 9 backend_name = line.split()[1] 10 backend_list.append(backend_name) 11 server_list = [] 12 if line.strip().startswith("server"): 13 dict_ser['server'] = line.strip().split()[1] 14 dict_ser['weight'] = line.strip().split()[3] 15 dict_ser['maxconn'] = line.strip().split()[5] 16 server_list.append(dict_ser) 17 dict_all[backend_name] = server_list 18 return backend_list,dict_all #返回backend的所有域名信息和域名包含的服务器信息