def get_backend_str(): ''' 把所有backend转化为符合配置文件格式的字符串,用于输出显示和写入到文件 :return: # 返回经过格式化的字符串 ''' backends = haproxy.get_backends() backend_str = '' for backend in backends: # 遍历所有baekend backend_str = ''.join([backend_str, '%s %s\n' %('backend',backend['backend'])]) # 格式化backend for record in backend['record']: # 遍历backend下面的记录 server = record_to_str(record) # record_to_str函数返回record的字符串 backend_str = ''.join([backend_str, server, '\n']) return backend_str
def get_backend_str(): ''' 把所有backend转化为符合配置文件格式的字符串,用于输出显示和写入到文件 :return: # 返回经过格式化的字符串 ''' backends = haproxy.get_backends() backend_str = '' for backend in backends: # 遍历所有baekend backend_str = ''.join( [backend_str, '%s %s\n' % ('backend', backend['backend'])]) # 格式化backend for record in backend['record']: # 遍历backend下面的记录 server = record_to_str(record) # record_to_str函数返回record的字符串 backend_str = ''.join([backend_str, server, '\n']) return backend_str
def write_to_file(): ''' 将临时保存的haproxy配置文件信息保存到haproxy配置文件中 :return: ''' choose = input('确认保存修改y|n返回上层)(q退出程序): ') # 获取用户确认 if choose == 'y': backends = haproxy.get_backends() # 获取所有backend backend_str = get_backend_str() # 将所有backend信息转化为字符串形式 if haproxy.write_to_file( backend_str): # 调用haproxy的write_to_file方法,写入到文件,判断是否成功写入到文件 input('写入文件成功,按任意键继续') else: input('写入文件失败,按任意键继续') elif choose == 'q': sys.exit('已退出程序') elif choose == 'n': print('返回上层目录') pass
def write_to_file(): ''' 将临时保存的haproxy配置文件信息保存到haproxy配置文件中 :return: ''' chose = input('确认是否将修改写入到配置文件中(y): ') # 获取用户确认 if chose == 'y': backends = haproxy.get_backends() # 获取所有backend backend_str = get_backend_str() # 将所有backend信息转化为字符串形式 # backend_str = '' # for backend in backends: # backend_str = ''.join([backend_str, '%s %s\n' %('backend',backend['backend'])]) # for record in backend['record']: # server = record_to_str(record) # backend_str = ''.join([backend_str, server, '\n']) #print(backend_str) if haproxy.write_to_file(backend_str): # 调用haproxy的write_to_file方法,写入到文件,判断是否成功写入到文件 input('写入文件成功,按任意键继续') else: input('写入文件失败,按任意键继续')
def write_to_file(): ''' 将临时保存的haproxy配置文件信息保存到haproxy配置文件中 :return: ''' chose = input('确认是否将修改写入到配置文件中(y): ') # 获取用户确认 if chose == 'y': backends = haproxy.get_backends() # 获取所有backend backend_str = get_backend_str() # 将所有backend信息转化为字符串形式 # backend_str = '' # for backend in backends: # backend_str = ''.join([backend_str, '%s %s\n' %('backend',backend['backend'])]) # for record in backend['record']: # server = record_to_str(record) # backend_str = ''.join([backend_str, server, '\n']) #print(backend_str) if haproxy.write_to_file( backend_str): # 调用haproxy的write_to_file方法,写入到文件,判断是否成功写入到文件 input('写入文件成功,按任意键继续') else: input('写入文件失败,按任意键继续')