예제 #1
0
def _sql_profiler_view(request: pyramid.request.Request) -> Mapping[str, Any]:
    auth.auth_view(request)
    enable = request.params.get("enable")
    if enable is not None:
        broadcast.broadcast("c2c_sql_profiler",
                            params={"enable": enable},
                            expect_answers=True)
    return {"status": 200, "enabled": repository is not None}
예제 #2
0
def _sql_profiler_view(request: pyramid.request.Request) -> Mapping[str, Any]:
    global repository
    auth.auth_view(request)
    enable = request.params.get('enable')
    if enable is not None:
        broadcast.broadcast('c2c_sql_profiler',
                            params={'enable': enable},
                            expect_answers=True)
    return {'status': 200, 'enabled': repository is not None}
예제 #3
0
def memory(request):
    auth_view(request)
    return _memory(
        with_others=request.params.get("with_others",
                                       "false").lower() in ["1", "on", "true"],
        with_all=request.params.get("with_all",
                                    "false").lower() in ["1", "on", "true"],
        with_repr=request.params.get("with_repr", "false").lower()
        in ["1", "on", "true"],
    )
def _db_maintenance(request: pyramid.request.Request) -> Mapping[str, Any]:
    auth.auth_view(request)
    readonly_param = cast(str, request.params.get("readonly"))
    if readonly_param is not None:
        readonly = readonly_param.lower() == "true"

        LOG.critical("Readonly DB status changed from %s to %s", db.force_readonly, readonly)
        _set_readonly(value=readonly)
        _store(request.registry.settings, readonly)
        return {"status": 200, "readonly": readonly}
    else:
        readonly = _get_redis_value(request.registry.settings)
        if readonly is not None:
            readonly = readonly == "true"
        return {"status": 200, "current_readonly": readonly}
예제 #5
0
    def run(self):
        auth_view(self.request)

        if "command" not in self.request.POST:
            raise pyramid.httpexceptions.HTTPBadRequest(
                "The POST argument 'command' is required")

        command = shlex.split(self.request.POST["command"])

        if command[0] not in self.gene.config.get("server", {}).get(
                "allowed_commands", ["generate_tiles", "generate_controller"]):
            raise pyramid.httpexceptions.HTTPBadRequest(
                "The given executable '{}' is not allowed".format(command[0]))
        log_thread = LogThread(command)
        log_thread.start()
        return pyramid.httpexceptions.HTTPFound(
            self.request.route_url("admin"))
예제 #6
0
    def run(self):
        auth_view(self.request)

        if 'command' not in self.request.POST:
            raise pyramid.httpexceptions.HTTPBadRequest("The POST argument 'command' is required")

        command = shlex.split(self.request.POST['command'])

        if command[0] not in self.gene.config.get('server', {}).get('allowed_commands', [
            'generate_tiles', 'generate_controller'
        ]):
            raise pyramid.httpexceptions.HTTPBadRequest(
                "The given executable '{}' is not allowed".format(command[0])
            )
        lt = LogThread(command)
        lt.start()
        return pyramid.httpexceptions.HTTPFound(self.request.route_url('admin'))
예제 #7
0
def _dump_memory_diff(request: pyramid.request.Request) -> List[Any]:
    auth.auth_view(request)
    limit = int(request.params.get("limit", "30"))
    if "path" in request.matchdict:
        # deprecated
        path = "/" + "/".join(request.matchdict["path"])
    else:
        path = request.params["path"]

    sub_request = request.copy()
    split_path = path.split("?")
    sub_request.path_info = split_path[0]
    if len(split_path) > 1:
        sub_request.query_string = split_path[1]

    # warmup run
    try:
        if "no_warmup" not in request.params:
            request.invoke_subrequest(sub_request)
    except Exception:  # nosec  # pylint: disable=broad-except
        pass

    LOG.debug("checking memory growth for %s", path)

    peak_stats: Dict[Any, Any] = {}
    for i in range(3):
        gc.collect(i)

    objgraph.growth(limit=limit, peak_stats=peak_stats, shortnames=False)

    response = None
    try:
        response = request.invoke_subrequest(sub_request)
        LOG.debug("response was %d", response.status_code)

    except HTTPException as ex:
        LOG.debug("response was %s", str(ex))

    del response

    for i in range(3):
        gc.collect(i)

    return objgraph.growth(limit=limit,
                           peak_stats=peak_stats,
                           shortnames=False)  # type: ignore
예제 #8
0
def _dump_memory(request: pyramid.request.Request) -> List[Mapping[str, Any]]:
    auth.auth_view(request)
    limit = int(request.params.get("limit", "30"))
    analyze_type = request.params.get("analyze_type")
    python_internals_map = request.params.get("python_internals_map",
                                              "0").lower() in ("", "1", "true",
                                                               "on")
    result = broadcast.broadcast(
        "c2c_dump_memory",
        params={
            "limit": limit,
            "analyze_type": analyze_type,
            "python_internals_map": python_internals_map
        },
        expect_answers=True,
        timeout=70,
    )
    assert result is not None
    return result
예제 #9
0
def _headers(request: pyramid.request.Request) -> Mapping[str, Any]:
    auth.auth_view(request)
    result = {
        "headers": dict(request.headers),
        "client_info": {
            "client_addr": request.client_addr,
            "host": request.host,
            "host_port": request.host_port,
            "http_version": request.http_version,
            "path": request.path,
            "path_info": request.path_info,
            "remote_addr": request.remote_addr,
            "remote_host": request.remote_host,
            "scheme": request.scheme,
            "server_name": request.server_name,
            "server_port": request.server_port,
        },
    }
    if "status" in request.params:
        raise exception_response(int(request.params["status"]), detail=result)
    else:
        return result
예제 #10
0
def _show_refs(request: pyramid.request.Request) -> pyramid.response.Response:
    auth.auth_view(request)
    for generation in range(3):
        gc.collect(generation)

    objs: List[Any] = []
    if "analyze_type" in request.params:
        objs = objgraph.by_type(request.params["analyze_type"])
    elif "analyze_id" in request.params:
        objs = [objgraph.by(int(request.params["analyze_id"]))]

    args: Dict[str, Any] = {
        "refcounts": True,
    }
    if request.params.get("max_depth", "") != "":
        args["max_depth"] = int(request.params["max_depth"])
    if request.params.get("too_many", "") != "":
        args["too_many"] = int(request.params["too_many"])
    if request.params.get("min_size_kb", "") != "":
        args["filter"] = lambda obj: get_size(obj) > (int(request.params[
            "min_size_kb"]) * 1024)
    if request.params.get("no_extra_info", "") == "":
        args[
            "extra_info"] = lambda obj: f"{get_size(obj) / 1024:.3f} kb\n{id(obj)}"

    result = StringIO()
    if request.params.get("backrefs", "") != "":
        objgraph.show_backrefs(objs, output=result, **args)
    else:
        objgraph.show_refs(objs,
                           output=result,
                           filter=lambda x: not objgraph.inspect.isclass(x),
                           **args)

    request.response.content_type = "text/vnd.graphviz"
    request.response.text = result.getvalue()
    result.close()
    return request.response
예제 #11
0
def _logging_change_level(
        request: pyramid.request.Request) -> Mapping[str, Any]:
    auth.auth_view(request)
    name = request.params.get('name')
    if name is not None:
        level = request.params.get('level')
        logger = logging.getLogger(name)
        if level is not None:
            LOG.critical("Logging of %s changed from %s to %s", name,
                         logging.getLevelName(logger.level), level)
            _set_level(name=name, level=level)
            _store_override(request.registry.settings, name, level)
        return {
            'status': 200,
            'name': name,
            'level': logging.getLevelName(logger.level),
            'effective_level': logging.getLevelName(logger.getEffectiveLevel())
        }
    else:
        return {
            'status': 200,
            'overrides': dict(_list_overrides(request.registry.settings))
        }
예제 #12
0
def memory(request: pyramid.request.Request) -> Dict[str, Any]:
    auth_view(request)
    return cast(Dict[str, Any], _memory())
예제 #13
0
def _dump_stacks(request: pyramid.request.Request) -> List[Mapping[str, Any]]:
    auth.auth_view(request)
    result = broadcast.broadcast("c2c_dump_stacks", expect_answers=True)
    assert result is not None
    return _beautify_stacks(result)
예제 #14
0
def _dump_memory_maps(
        request: pyramid.request.Request) -> List[Dict[str, Any]]:
    auth.auth_view(request)
    return sorted(dump_memory_maps(),
                  key=lambda i: cast(int, -i.get("pss_kb", 0)))
예제 #15
0
def _error(request: pyramid.request.Request) -> Any:
    auth.auth_view(request)
    raise exception_response(int(request.params["status"]), detail="Test")
예제 #16
0
def _sleep(request: pyramid.request.Request) -> pyramid.response.Response:
    auth.auth_view(request)
    timeout = float(request.params["time"])
    time.sleep(timeout)
    request.response.status_code = 204
    return request.response
예제 #17
0
    def run(self) -> pyramid.response.Response:
        """Run the command given by the user."""
        assert self.gene
        auth_view(self.request)

        if "command" not in self.request.POST:
            self.request.response.status_code = 400
            return {"error": "The POST argument 'command' is required"}

        commands = shlex.split(self.request.POST["command"])
        command = commands[0].replace("_", "-")

        allowed_commands = (self.gene.get_main_config().config.get(
            "server", {}).get(
                "allowed_commands",
                ["generate-tiles", "generate-controller", "generate-cost"]))
        if command not in allowed_commands:
            return {
                "error":
                f"The given command '{command}' is not allowed, allowed command are: "
                f"{', '.join(allowed_commands)}"
            }
        add_role = False
        arguments = {c.split("=")[0]: c.split("=")[1:] for c in commands[1:]}
        if command == "generate-tiles":
            add_role = "--get-hash" not in arguments and "--get-bbox" not in arguments

        allowed_arguments = (self.gene.get_main_config().config.get(
            "server", {}).get(
                "allowed_arguments",
                [
                    "--layer",
                    "--get-hash",
                    "--generate-legend-images",
                    "--dump-config",
                    "--get-bbox",
                ],
            ))
        for arg in arguments.keys():
            if arg.startswith("-") and arg not in allowed_arguments:
                self.request.response.status_code = 400
                return {
                    "error":
                    (f"The argument {arg} is not allowed, allowed arguments are: "
                     f"{', '.join(allowed_arguments)}")
                }

        final_command = [
            command,
            f"--config={self.gene.get_host_config_file(self.request.host)}"
        ]
        if add_role:
            final_command += ["--role=master"]
        final_command += commands[1:]

        display_command = " ".join([shlex.quote(arg) for arg in final_command])
        LOG.info("Run the command `%s`", display_command)
        env: Dict[str, str] = {}
        env.update(os.environ)
        env["FRONTEND"] = "noninteractive"

        completed_process = subprocess.run(  # nosec # pylint: disable=subprocess-run-check
            final_command,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            env=env,
        )

        if completed_process.returncode != 0:
            LOG.warning(
                "The command `%s` exited with an error code: %s\nstdout:\n%s\nstderr:\n%s",
                display_command,
                completed_process.returncode,
                completed_process.stdout.decode(),
                completed_process.stderr.decode(),
            )

        return {
            "stdout":
            _limit_length(
                completed_process.stdout.decode(),
                int(os.environ.get("TILECLOUD_CHAIN_MAX_OUTPUT_LENGTH", 1000)),
            ),
            "stderr":
            _limit_length(
                completed_process.stderr.decode(),
                int(os.environ.get("TILECLOUD_CHAIN_MAX_OUTPUT_LENGTH", 1000)),
            ),
            "returncode":
            completed_process.returncode,
        }
예제 #18
0
 def invalidate_cache(self) -> Dict[str, bool]:
     auth_view(self.request)
     models.cache_invalidate_cb()
     return {"success": True}
예제 #19
0
 def invalidate_cache(self):  # pragma: no cover
     auth_view(self.request)
     main.cache_invalidate_cb()
     return {"success": True}
예제 #20
0
def memory(request: pyramid.request.Request) -> Dict[str, Any]:
    """Offer an authenticated view throw c2cwsgiutils to provide some memory information."""
    auth_view(request)
    return cast(Dict[str, Any], _memory())