コード例 #1
0
def proxy_update_config(reload_proxy=True):
    """Regenerate the proxy configuration file."""
    @transactional
    def write_config():
        allowed_subnets = Subnet.objects.filter(allow_proxy=True)
        cidrs = [subnet.cidr for subnet in allowed_subnets]

        http_proxy = Config.objects.get_config("http_proxy")
        upstream_proxy_enabled = (Config.objects.get_config("use_peer_proxy")
                                  and http_proxy)
        context = {
            'allowed': allowed_subnets,
            'modified': str(datetime.date.today()),
            'fqdn': socket.getfqdn(),
            'cidrs': cidrs,
            'running_in_snap': snappy.running_in_snap(),
            'snap_path': snappy.get_snap_path(),
            'snap_data_path': snappy.get_snap_data_path(),
            'snap_common_path': snappy.get_snap_common_path(),
            'upstream_peer_proxy': upstream_proxy_enabled,
        }

        proxy_enabled = Config.objects.get_config("enable_http_proxy")
        if proxy_enabled and upstream_proxy_enabled:
            http_proxy_hostname = urlparse(http_proxy).hostname
            http_proxy_port = urlparse(http_proxy).port
            context.update({
                'upstream_proxy_address': http_proxy_hostname,
                'upstream_proxy_port': http_proxy_port,
            })

        template_path = locate_template('proxy', MAAS_PROXY_CONF_TEMPLATE)
        template = tempita.Template.from_filename(template_path,
                                                  encoding="UTF-8")
        try:
            content = template.substitute(context)
        except NameError as error:
            raise ProxyConfigFail(*error.args)
        # Squid prefers ascii.
        content = content.encode("ascii")
        target_path = get_proxy_config_path()
        atomic_write(content, target_path, overwrite=True, mode=0o644)

    if is_proxy_enabled():
        d = deferToDatabase(write_config)
        if reload_proxy:
            # XXX: andreserl 2016-05-09 bug=1687620. When running in a snap,
            # supervisord tracks services. It does not support reloading.
            # Instead, we need to restart the service.
            if snappy.running_in_snap():
                d.addCallback(lambda _: service_monitor.restartService(
                    "proxy", if_on=True))
            else:
                d.addCallback(lambda _: service_monitor.reloadService(
                    "proxy", if_on=True))
        return d
    else:
        return succeed(None)
コード例 #2
0
def proxy_update_config(reload_proxy=True):
    """Regenerate the proxy configuration file."""

    @transactional
    def _write_config():
        allowed_subnets = Subnet.objects.filter(allow_proxy=True)
        cidrs = [subnet.cidr for subnet in allowed_subnets]
        config = Config.objects.get_configs(
            [
                "http_proxy",
                "maas_proxy_port",
                "use_peer_proxy",
                "prefer_v4_proxy",
                "enable_http_proxy",
            ]
        )

        kwargs = {
            "prefer_v4_proxy": config["prefer_v4_proxy"],
            "maas_proxy_port": config["maas_proxy_port"],
        }
        if (
            config["enable_http_proxy"]
            and config["http_proxy"]
            and config["use_peer_proxy"]
        ):
            kwargs["peer_proxies"] = [config["http_proxy"]]
        write_config(cidrs, **kwargs)

    if is_proxy_enabled():
        d = deferToDatabase(_write_config)
        if reload_proxy:
            # XXX: andreserl 2016-05-09 bug=1687620. When running in a snap,
            # supervisord tracks services. It does not support reloading.
            # Instead, we need to restart the service.
            if snappy.running_in_snap():
                d.addCallback(
                    lambda _: service_monitor.restartService(
                        "proxy", if_on=True
                    )
                )
            else:
                d.addCallback(
                    lambda _: service_monitor.reloadService(
                        "proxy", if_on=True
                    )
                )
        return d
    else:
        return succeed(None)