def enable_stats(self, save=True): """Enable the stats enpoint.""" # Remove any previous stats self.disable_stats(save=False) # Check that no frontend exists with conflicting port if (self.get_frontend(port=self.charm_config["stats-port"], create=False) is not None): hookenv.log( "Stats port {} already in use".format( self.charm_config["stats-port"]), "ERROR", ) if save: self.save_config() return False # Generate new front end for stats user_string = "{}:{}".format(self.charm_config["stats-user"], self.charm_config["stats-passwd"]) config_block = [] config_block.append( haproxy_config.Bind("0.0.0.0", self.charm_config["stats-port"], None)) config_block.append(haproxy_config.Config("stats enable", "")) config_block.append( haproxy_config.Config("stats auth {}".format(user_string), "")) config_block.append( haproxy_config.Config( "stats uri {}".format(self.charm_config["stats-url"]), "")) if self.charm_config["stats-local"]: config_block.append( haproxy_config.Acl( "local", ("src 10.0.0.0/8 " "172.16.0.0/12 " "192.168.0.0/16 " "127.0.0.0/8 " "fd00::/8 " "fe80::/10 " "::1/128"), )) config_block.append( haproxy_config.Config("http-request deny if !local", "")) frontend = haproxy_config.Frontend( "stats", "0.0.0.0", str(self.charm_config["stats-port"]), config_block) self.proxy_config.frontends.append(frontend) if save: self.save_config() return True
def get_frontend(self, port=None, create=True): port = str(port) frontend = None for fe in self.proxy_config.frontends: hookenv.log("Checking frontend for port {}".format(port), "DEBUG") hookenv.log("Port is: {}".format(fe.port), "DEBUG") if fe.port == port: hookenv.log("Using previous frontend", "DEBUG") frontend = fe break if frontend is None and create: hookenv.log("Creating frontend for port {}".format(port), "INFO") config_block = [Config.Bind('0.0.0.0', port, None)] frontend = Config.Frontend('relation-{}'.format(port), '0.0.0.0', port, config_block) self.proxy_config.frontends.append(frontend) return frontend
def enable_stats(self, save=True): # Remove any previous stats self.disable_stats(save=False) # Check that no frontend exists with conflicting port if self.get_frontend(port=self.charm_config['stats-port'], create=False) is not None: hookenv.log( "Stats port {} already in use".format( self.charm_config['stats-port']), 'ERROR') if save: self.save_config() return False # Generate new front end for stats user_string = '{}:{}'.format(self.charm_config['stats-user'], self.charm_config['stats-passwd']) config_block = [] config_block.append( Config.Bind('0.0.0.0', self.charm_config['stats-port'], None)) config_block.append(Config.Config('stats enable', '')) config_block.append( Config.Config('stats auth {}'.format(user_string), '')) config_block.append( Config.Config( 'stats uri {}'.format(self.charm_config['stats-url']), '')) if self.charm_config['stats-local']: config_block.append( Config.Acl('local', 'src 10.0.0.0/8 192.168.0.0/16 127.0.0.0/8')) config_block.append( Config.Config('http-request deny if !local', '')) frontend = Config.Frontend('stats', '0.0.0.0', str(self.charm_config['stats-port']), config_block) self.proxy_config.frontends.append(frontend) if save: self.save_config() return True
def get_backend(self, configuration, port): # cfg_parser = Parser(cf_path) # configuration = cfg_parser.build_configuration() name = HAProxyCtrl.get_frontend_name(port) exist_frontend = configuration.frontend(name) if exist_frontend: backend_name = exist_frontend.config_block['usebackends'][ 0].backend_name return configuration.backend(backend_name) backend_name = HAProxyCtrl.get_backend_name(port) exist_backend = configuration.backend(backend_name) if exist_backend: newbackend = exist_backend else: newbackend = config.Backend( backend_name, { 'configs': [('mode', 'http'), ('balance', 'roundrobin')], 'options': [('forwardfor', '')], 'servers': [] }) configuration.backends.append(newbackend) usebackend = config.UseBackend(newbackend.name, "", "", True) frontends = configuration.frontends newfrontend = config.Frontend( name, '*', port, { 'usebackends': [ usebackend, ], 'binds': [ config.Bind('*', port, None), ], 'configs': [('mode', 'http')], 'default_backend': [] }) frontends.append(newfrontend) return newbackend
def __build_bind(self, bind_node): service_address = bind_node.service_address return config.Bind(host=service_address.host.text, port=service_address.port.text, attributes=bind_node.value.text.split(' \t'))