From 5f0f73dc5edc6262cd2609dcfaab1414f7a74345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Zaj=C4=85c?= Date: Thu, 11 Jan 2024 08:50:18 +0100 Subject: [PATCH 01/13] Dashboard can be served on a non-root path --- karton/dashboard/app.py | 46 +++++++++++++----------- karton/dashboard/templates/analysis.html | 4 +-- karton/dashboard/templates/crashed.html | 2 +- karton/dashboard/templates/index.html | 2 +- karton/dashboard/templates/layout.html | 10 +++--- karton/dashboard/templates/queue.html | 2 +- karton/dashboard/templates/task.html | 4 +-- requirements.txt | 2 +- 8 files changed, 39 insertions(+), 33 deletions(-) diff --git a/karton/dashboard/app.py b/karton/dashboard/app.py index 0c642fb..2f2af84 100644 --- a/karton/dashboard/app.py +++ b/karton/dashboard/app.py @@ -14,7 +14,7 @@ Flask, jsonify, redirect, - render_template, + render_template as flask_render_template, request, send_from_directory, ) @@ -43,6 +43,8 @@ class KartonDashboard(KartonBase): karton = KartonDashboard() +base_path = karton.config.get("dashboard", "base_path") if karton.config.has_option("dashboard", "base_path") else "" + markdown = mistune.create_markdown( escape=True, renderer="html", @@ -50,6 +52,10 @@ class KartonDashboard(KartonBase): ) + +def render_template(template_name: str, **kwargs: Dict[str, Any]) -> str: + return flask_render_template(template_name, base_path=base_path, **kwargs) + def cancel_tasks(tasks: List[Task]) -> None: for task in tasks: karton.backend.set_task_status(task=task, status=TaskState.FINISHED) @@ -191,7 +197,7 @@ def add_metrics(state: KartonState, metric: KartonMetrics, key: str) -> None: karton_metrics.labels(key, name).set(value) -@app.route("/varz", methods=["GET"]) +@app.route(base_path + "/varz", methods=["GET"]) def varz(): """Update and get prometheus metrics""" @@ -226,18 +232,18 @@ def varz(): return generate_latest() -@app.route("/static/", methods=["GET"]) +@app.route(base_path + "/static/", methods=["GET"]) def static(path: str): return send_from_directory(static_folder, path) -@app.route("/", methods=["GET"]) +@app.route(base_path + "/", methods=["GET"]) def get_queues(): state = KartonState(karton.backend) return render_template("index.html", queues=state.queues) -@app.route("/services", methods=["GET"]) +@app.route(base_path + "/services", methods=["GET"]) def get_services(): aggregated_services = defaultdict(list) online_services = karton.backend.get_online_services() @@ -246,7 +252,7 @@ def get_services(): return render_template("services.html", services=aggregated_services) -@app.route("/api/queues", methods=["GET"]) +@app.route(base_path + "/api/queues", methods=["GET"]) def get_queues_api(): state = KartonState(karton.backend) return jsonify( @@ -257,7 +263,7 @@ def get_queues_api(): ) -@app.route("//restart_crashed", methods=["POST"]) +@app.route(base_path + "//restart_crashed", methods=["POST"]) def restart_crashed_queue_tasks(queue_name): state = KartonState(karton.backend) queue = state.queues.get(queue_name) @@ -269,7 +275,7 @@ def restart_crashed_queue_tasks(queue_name): return redirect(request.referrer) -@app.route("//cancel_crashed", methods=["POST"]) +@app.route(base_path + "//cancel_crashed", methods=["POST"]) def cancel_crashed_queue_tasks(queue_name): state = KartonState(karton.backend) queue = state.queues.get(queue_name) @@ -280,7 +286,7 @@ def cancel_crashed_queue_tasks(queue_name): return redirect(request.referrer) -@app.route("//cancel_pending", methods=["POST"]) +@app.route(base_path + "//cancel_pending", methods=["POST"]) def cancel_pending_queue_tasks(queue_name): state = KartonState(karton.backend) queue = state.queues.get(queue_name) @@ -291,7 +297,7 @@ def cancel_pending_queue_tasks(queue_name): return redirect(request.referrer) -@app.route("/restart_task//restart", methods=["POST"]) +@app.route(base_path + "/restart_task//restart", methods=["POST"]) def restart_task(task_id): task = karton.backend.get_task(task_id) if not task: @@ -301,7 +307,7 @@ def restart_task(task_id): return redirect(request.referrer) -@app.route("/cancel_task//cancel", methods=["POST"]) +@app.route(base_path + "/cancel_task//cancel", methods=["POST"]) def cancel_task(task_id): task = karton.backend.get_task(task_id) if not task: @@ -311,7 +317,7 @@ def cancel_task(task_id): return redirect(request.referrer) -@app.route("/queue/", methods=["GET"]) +@app.route(base_path + "/queue/", methods=["GET"]) def get_queue(queue_name): state = KartonState(karton.backend) queue = state.queues.get(queue_name) @@ -321,7 +327,7 @@ def get_queue(queue_name): return render_template("queue.html", name=queue_name, queue=queue) -@app.route("/queue//crashed", methods=["GET"]) +@app.route(base_path + "/queue//crashed", methods=["GET"]) def get_crashed_queue(queue_name): state = KartonState(karton.backend) queue = state.queues.get(queue_name) @@ -331,7 +337,7 @@ def get_crashed_queue(queue_name): return render_template("crashed.html", name=queue_name, queue=queue) -@app.route("/api/queue/", methods=["GET"]) +@app.route(base_path + "/api/queue/", methods=["GET"]) def get_queue_api(queue_name): state = KartonState(karton.backend) queue = state.queues.get(queue_name) @@ -340,7 +346,7 @@ def get_queue_api(queue_name): return jsonify(QueueView(queue).to_dict()) -@app.route("/task/", methods=["GET"]) +@app.route(base_path + "/task/", methods=["GET"]) def get_task(task_id): task = karton.backend.get_task(task_id) if not task: @@ -351,7 +357,7 @@ def get_task(task_id): ) -@app.route("/api/task/", methods=["GET"]) +@app.route(base_path + "/api/task/", methods=["GET"]) def get_task_api(task_id): task = karton.backend.get_task(task_id) if not task: @@ -359,7 +365,7 @@ def get_task_api(task_id): return jsonify(TaskView(task).to_dict()) -@app.route("/analysis/", methods=["GET"]) +@app.route(base_path + "/analysis/", methods=["GET"]) def get_analysis(root_id): state = KartonState(karton.backend) analysis = state.analyses.get(root_id) @@ -371,7 +377,7 @@ def get_analysis(root_id): ) -@app.route("/api/analysis/", methods=["GET"]) +@app.route(base_path + "/api/analysis/", methods=["GET"]) def get_analysis_api(root_id): state = KartonState(karton.backend) analysis = state.analyses.get(root_id) @@ -381,12 +387,12 @@ def get_analysis_api(root_id): return jsonify(AnalysisView(analysis).to_dict()) -@app.route("/graph", methods=["GET"]) +@app.route(base_path + "/graph", methods=["GET"]) def get_graph(): return render_template("graph.html") -@app.route("/graph/generate", methods=["GET"]) +@app.route(base_path + "/graph/generate", methods=["GET"]) def generate_graph(): state = KartonState(karton.backend) graph = KartonGraph(state) diff --git a/karton/dashboard/templates/analysis.html b/karton/dashboard/templates/analysis.html index 104adff..1a4e28c 100644 --- a/karton/dashboard/templates/analysis.html +++ b/karton/dashboard/templates/analysis.html @@ -32,10 +32,10 @@

Tasks

{% for task in queue.pending_tasks %} - {{identity}} + {{identity}} - {{ task.uid }} + {{ task.uid }} {% if task.priority.value != 'normal' %} diff --git a/karton/dashboard/templates/crashed.html b/karton/dashboard/templates/crashed.html index d1fd9e6..39aae0e 100644 --- a/karton/dashboard/templates/crashed.html +++ b/karton/dashboard/templates/crashed.html @@ -28,7 +28,7 @@

{% for task in queue.crashed_tasks|sort(attribute='last_update', reverse=True) %} - {{ task.uid }} + {{ task.uid }}
{{ task.last_update|render_timestamp }}
diff --git a/karton/dashboard/templates/index.html b/karton/dashboard/templates/index.html index dab7727..5dd2269 100644 --- a/karton/dashboard/templates/index.html +++ b/karton/dashboard/templates/index.html @@ -17,7 +17,7 @@

binds

{% for (queue_name, queue) in queues|dictsort %} - {{ queue_name }} + {{ queue_name }}
diff --git a/karton/dashboard/templates/layout.html b/karton/dashboard/templates/layout.html index 1b10c76..d3c7fab 100644 --- a/karton/dashboard/templates/layout.html +++ b/karton/dashboard/templates/layout.html @@ -4,24 +4,24 @@ {% block title %}karton-dashboard{% endblock %} - +