def add(operation):
"增加,增加内容存在的情况(可以通过search()函数进行查询),增加内容不存在的情况"
backend_url = operation["backend"]
record_list = serach(backend_url)
backend_data = "backend %s" %backend_url
dict_string = "sever %s weight %s maxconn %s" %(operation["record"]["server"],\
operation["record"]["weight"],\
operation["record"]["maxconn"],
)
if not record_list:
record_list.append(backend_data)
record_list.append(dict_string)
with open("haproxy.conf",encoding="utf-8") as file_read,\
open("haproxy_new.conf","w+",encoding="utf-8") as file_write:
for line_read in file_read:
file_write.write(line_read)
for new_line in record_list:
if new_line.startswith("backend"):
file_write.write(new_line+"\n")
else:
file_write.write("%s%s\n" %(" "*8,new_line))
import os
os.rename("haproxy.conf","haproxy_bak.conf")
os.rename("haproxy_new.conf","haproxy.conf")