Пример #1
0
    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
Пример #2
0
 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
Пример #3
0
    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
Пример #4
0
    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
Пример #5
0
    def build_frontend(self, frontend_node):
        """parse `frontend` sections, and return a config.Frontend

        Args:
            frontend_node (TreeNode): Description

        Raises:
            Exception: Description

        Returns:
            config.Frontend: an object
        """
        proxy_name = frontend_node.frontend_header.proxy_name.text
        service_address_node = frontend_node.frontend_header.service_address

        # parse the config block
        config_block_lines = self.__build_config_block(
            frontend_node.config_block)

        # parse host and port
        host, port = '', ''
        if isinstance(service_address_node, pegnode.ServiceAddress):
            host = service_address_node.host.text
            port = service_address_node.port.text
        else:
            # use `bind` in config lines to fill in host and port
            # just use the first
            for line in config_block_lines:
                if isinstance(line, config.Bind):
                    host, port = line.host, line.port
                    break
            else:
                raise Exception(
                    'Not specify host and port in `frontend` definition')
        return config.Frontend(name=proxy_name,
                               host=host,
                               port=port,
                               config_block=config_block_lines)