def remove_server(self, endpoint_addr, endpoint_port, port): endpoint_port = str(endpoint_port) port = str(port) cfg_parser = Parser(self.cf_path) configuration = cfg_parser.build_configuration() name = HAProxyCtrl.get_backend_name(port) backend = configuration.backend(name) if not backend: return servers = backend.servers() # check server exist_server = next( (s for s in servers if s.host == endpoint_addr and s.port == endpoint_port), None) if exist_server is not None: servers.remove(exist_server) # check server empty if len(servers) == 0: # xoa backend va frontend configuration.backends.remove(backend) frontend_name = HAProxyCtrl.get_frontend_name(port) frontend = configuration.frontend(frontend_name) if frontend: configuration.frontends.remove(frontend) cfg_render = Render(configuration) cfg_render.dumps_to(self.cf_path) HAProxyCtrl.restart_service()
def add_server(self, endpoint_addr, endpoint_port, port): endpoint_port = str(endpoint_port) port = str(port) cfg_parser = Parser(self.cf_path) configuration = cfg_parser.build_configuration() backend = self.get_backend(configuration, port) servers = backend.servers() # check server exist_server = next( (s for s in servers if s.host == endpoint_addr and s.port == endpoint_port), None) if exist_server is None: # get available name idx = max([ int(c[1]) for c in [s.name.split('_') for s in servers] if len(c) == 2 and c[0] == 'web' and c[1].isdigit() ] or [ 0, ]) idx = idx + 1 sname = 'web_%s' % idx backend.servers().append( config.Server(sname, endpoint_addr, endpoint_port, [ 'check', ])) cfg_render = Render(configuration) cfg_render.dumps_to(self.cf_path) HAProxyCtrl.restart_service()
def main(): parser = argparse.ArgumentParser(__doc__) parser.set_defaults(command=None) cmd_subparsers = parser.add_subparsers() add_parser = cmd_subparsers.add_parser('add') add_parser.set_defaults(command='add') add_parser.add_argument('config_file') add_parser.add_argument('frontend') add_parser.add_argument('domain') add_parser.add_argument('subdomain') add_parser.add_argument('--host', default='127.0.0.1') add_parser.add_argument('port', type=int) del_parser = cmd_subparsers.add_parser('del') del_parser.set_defaults(command='del') del_parser.add_argument('config_file') del_parser.add_argument('frontend') del_parser.add_argument('subdomain') args = parser.parse_args() if not args.command: return config = Parser(args.config_file).build_configuration() if args.command == 'add': config = add(config, args.frontend, args.domain, args.subdomain, args.host, args.port) elif args.command == 'del': config = remove(config, args.frontend, args.subdomain) Render(config).dumps_to(args.config_file)
def save_config(self): # Render new cfg file Render(self.proxy_config).dumps_to(self.proxy_config_file) host.service_reload('haproxy.service') # Check the juju ports match the config self.update_ports()
def save_config(self): """Save the updated configuration.""" # Render new cfg file Render(self.proxy_config).dumps_to(self.proxy_config_file) host.service_reload("haproxy.service") # Check the juju ports match the config self.update_ports()
def set_containers(containers): # Read the current configuration and parse it parser = Parser(CONFIG_PATH) configuration = parser.build_configuration() backend = configuration.backend(BACKEND_NAME) # Remove all current servers from backend configuration configured_servers = backend.servers() for server in configured_servers: backend.remove_server(server.name) # Add servers according to the running docker containers for container in containers: server = create_server(name=container['id'], host=container['ip']) backend.add_server(server) # Update the ha proxy configuration config_render = Render(configuration) config_render.dumps_to(CONFIG_PATH) ## Restart the service for changes to take effect os.system(INVALIDATE_CONFIG_COMMAND)
def refresh_loadbalancer(): print("Refreshing loadbalancer") haproxy_config = Parser(haproxy_config_path).build_configuration() webapp_backend = haproxy_config.backend("webapp") for server in webapp_backend.servers(): webapp_backend.remove_server(server.name) for container in containers: webapp_backend.add_server(pyhaproxy.config.Server( name = container.name, host = container.get_ips(timeout = 5000)[0], port = "8000", attributes = ["check"] )) Render(haproxy_config).dumps_to(haproxy_config_path) loadbalancer_container.attach_wait(lxc.attach_run_command, ["service", "haproxy", "restart"])
def writeConfFile(configuration): cfg_render = Render(configuration) cfg_render.dumps_to('config.cfg')
docker_list.append(lis) i += 1 #print(cmd) # count = newDocsN #multiprocessing.cpu_count() # BUILD AND RUN DOCKER IMAGE pool = multiprocessing.Pool() pool.map_async(addDocker, cmd) #ADD DOCKER IMAGE SERVER TO HAPROXY CONFIG FILE for c in cmd: addServer_HAproxyCFG(_name=c[0], _port=c[1], the_be_section=the_be_section) cfg_render = Render(configuration) cfg_render.dumps_to('/etc/haproxy/haproxy.cfg') subprocess.run(" sudo service haproxy reload", shell=True) #docker_list.append(docker) #pool.close() #pool.join() # if cpu load decreases, decrease the number of containers if (n < docker_count and len(docker_list) > 0): delDocsN = docker_count - n docker_count = n i = 0 cmd = [] while (i < delDocsN): initial_im -= 1
if args.delete is not True: host1 = '10.0.0.116' name1 = 'po.tater.io' host2 = '10.0.0.240' name2 = 'tots01.tater.io' port = args.port attributes = ['check'] server1 = pyhaproxy.config.Server(name1, host1, port, attributes) server2 = pyhaproxy.config.Server(name2, host2, port, attributes) config_block = defaultdict(list) config_block['servers'] = [server1, server2] backend = pyhaproxy.config.Backend(args.name, config_block) configuration.backends.append(backend) #Render and write out new config file cfg_render = Render(configuration) cfg_render.dumps_to(args.config)
operator = 'if' use_backend = pyhaproxy.config.UseBackend(be_name, operator, be_condition) frontend.config_block['usebackends'].append(use_backend) else: for line in frontend.config_block['usebackends']: if line.backend_name == args.name: frontend.config_block['usebackends'].remove(line) print 'Removed %s from use_backend list' % args.name break #backend config section if args.delete is not True: host1 = '10.0.0.116' name1 = 'po.tater.io' host2 = '10.0.0.240' name2 = 'tots01.tater.io' port = args.port attributes = ['check'] server1 = pyhaproxy.config.Server(name1, host1, port, attributes) server2 = pyhaproxy.config.Server(name2, host2, port, attributes) config_block = defaultdict(list) config_block['servers'] = [server1, server2] backend = pyhaproxy.config.Backend(args.name, config_block) configuration.backends.append(backend) #Render and write out new config file cfg_render = Render(configuration) cfg_render.dumps_to(args.config)