From bdab198369f9a237377fbd7a76cdc6c0896fed4d Mon Sep 17 00:00:00 2001 From: Ulincsys Date: Mon, 20 Mar 2023 18:39:33 -0500 Subject: [PATCH 01/12] (WIP) Re-enable admin dashboard Add @requires_admin decorator Add clarity to backend start error status Signed-off-by: Ulincsys --- collectoss/api/view/api_view.py | 30 +++++++++++++++++++++++------- collectoss/api/view/routes.py | 4 ++++ collectoss/templates/navbar.j2 | 4 +++- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/collectoss/api/view/api_view.py b/collectoss/api/view/api_view.py index b2f5a2925..35da0fe1a 100644 --- a/collectoss/api/view/api_view.py +++ b/collectoss/api/view/api_view.py @@ -1,10 +1,12 @@ from flask import render_template, redirect, url_for, session, request, jsonify -from flask_login import LoginManager +from flask_login import LoginManager, current_user, login_required from io import StringIO from .utils import * from .init import logger from .url_converters import * +from functools import wraps + # from .server import User from ..server import app, db_session from collectoss.application.db.models import User, UserSessionToken @@ -38,6 +40,13 @@ def unsupported_method(error): return render_message("405 - Method not supported", "The resource you are trying to access does not support the request method used"), 405 +@app.errorhandler(403) +def forbidden(error): + if AUGUR_API_VERSION in str(request.url_rule): + return jsonify({"status": "Forbidden"}), 403 + + return render_message("403 - Forbidden", "You do not have permission to view this page"), 403 + @app.errorhandler(500) def internal_server_error(error): if API_VERSION in str(request.path): @@ -68,6 +77,16 @@ def unauthorized(): session["login_next"] = url_for(request.endpoint, **request.args) return redirect(url_for('user_login')) +def admin_required(func): + @login_required + @wraps(func) + def inner_function(*args, **kwargs): + if current_user.admin: + return func(*args, **kwargs) + else: + forbidden(None) + return inner_function + @login_manager.user_loader def load_user(user_id): @@ -98,19 +117,16 @@ def load_user(user_id): @login_manager.request_loader def load_user_request(request): token = get_bearer_token() - current_time = int(time.time()) - token = db_session.query(UserSessionToken).filter(UserSessionToken.token == token, UserSessionToken.expiration >= current_time).first() - if token: - print("Valid user") + token = db_session.query(UserSessionToken).filter(UserSessionToken.token == token, UserSessionToken.expiration >= current_time).first() + if token: user = token.user user._is_authenticated = True user._is_active = True - return user - + return None @app.template_filter('as_datetime') diff --git a/collectoss/api/view/routes.py b/collectoss/api/view/routes.py index 15ab991b3..373a104bc 100644 --- a/collectoss/api/view/routes.py +++ b/collectoss/api/view/routes.py @@ -5,6 +5,7 @@ import math from flask import render_template, request, redirect, url_for, session, flash from .utils import * +from .augur_view import admin_required from flask_login import login_user, logout_user, current_user, login_required from collectoss.application.db.models import User, Repo, ClientApplication @@ -326,6 +327,7 @@ def throw_exception(): View the admin dashboard. """ @app.route('/dashboard') +@admin_required def dashboard_view(): empty = [ { "title": "Placeholder", "settings": [ @@ -339,4 +341,6 @@ def dashboard_view(): backend_config = requestJson("config/get", False) + logger.info(backend_config) + return render_template('admin-dashboard.j2', sections = empty, config = backend_config) diff --git a/collectoss/templates/navbar.j2 b/collectoss/templates/navbar.j2 index 8250ea634..c5c636829 100644 --- a/collectoss/templates/navbar.j2 +++ b/collectoss/templates/navbar.j2 @@ -22,7 +22,9 @@ From c210c10cf8fcd0d8f311aad2051ac86718ea03ba Mon Sep 17 00:00:00 2001 From: Ulincsys <28362836a@gmail.com> Date: Mon, 17 Apr 2023 10:31:49 -0500 Subject: [PATCH 02/12] Admin updates - Add ssl decorator to config endpoints - Fix syntax error in admin_required decorator - Update dashboard endpoint to use config class directly - Update dashboard styles with more consistent colors - Implement config update functionality in admin dashboard Signed-off-by: Ulincsys <28362836a@gmail.com> --- collectoss/api/routes/config.py | 9 ++--- collectoss/api/util.py | 1 - collectoss/api/view/api_view.py | 2 +- collectoss/api/view/routes.py | 4 +-- collectoss/static/css/dashboard.css | 22 ++++++++++++ collectoss/templates/admin-dashboard.j2 | 48 +++++++++++++++++++++++-- collectoss/templates/settings.j2 | 2 +- 7 files changed, 73 insertions(+), 15 deletions(-) diff --git a/collectoss/api/routes/config.py b/collectoss/api/routes/config.py index 8c66d8b22..aad308bd6 100644 --- a/collectoss/api/routes/config.py +++ b/collectoss/api/routes/config.py @@ -9,13 +9,13 @@ # Disable the requirement for SSL by setting env["AUGUR_DEV"] = True from collectoss.application.config import get_development_flag from collectoss.application.db.lib import get_session +from collectoss.api.util import ssl_required from collectoss.application.db.models import Config from collectoss.application.config import SystemConfig from collectoss.application.db.session import DatabaseSession from ..server import app logger = logging.getLogger(__name__) -development = get_development_flag() from collectoss.api.routes import API_VERSION @@ -28,6 +28,7 @@ def generate_upgrade_request(): return response, 426 @app.route(f"/{API_VERSION}/config/get", methods=['GET', 'POST']) +@ssl_required def get_config(): if not development and not request.is_secure: return generate_upgrade_request() @@ -40,10 +41,8 @@ def get_config(): @app.route(f"/{API_VERSION}/config/update", methods=['POST']) +@ssl_required def update_config(): - if not development and not request.is_secure: - return generate_upgrade_request() - update_dict = request.get_json() with get_session() as session: @@ -64,5 +63,3 @@ def update_config(): session.commit() return jsonify({"status": "success"}), 200 - - diff --git a/collectoss/api/util.py b/collectoss/api/util.py index f2dea539b..662b58f13 100644 --- a/collectoss/api/util.py +++ b/collectoss/api/util.py @@ -112,7 +112,6 @@ def get_client_token(): return token - # usage: """ @app.route("/path") diff --git a/collectoss/api/view/api_view.py b/collectoss/api/view/api_view.py index 35da0fe1a..23d1b9a65 100644 --- a/collectoss/api/view/api_view.py +++ b/collectoss/api/view/api_view.py @@ -84,7 +84,7 @@ def inner_function(*args, **kwargs): if current_user.admin: return func(*args, **kwargs) else: - forbidden(None) + return forbidden(None) return inner_function @login_manager.user_loader diff --git a/collectoss/api/view/routes.py b/collectoss/api/view/routes.py index 373a104bc..9d07aa8db 100644 --- a/collectoss/api/view/routes.py +++ b/collectoss/api/view/routes.py @@ -339,8 +339,6 @@ def dashboard_view(): ]} ] - backend_config = requestJson("config/get", False) - - logger.info(backend_config) + backend_config = AugurConfig(logger, db_session).load_config() return render_template('admin-dashboard.j2', sections = empty, config = backend_config) diff --git a/collectoss/static/css/dashboard.css b/collectoss/static/css/dashboard.css index ef111c32a..f8354c49b 100644 --- a/collectoss/static/css/dashboard.css +++ b/collectoss/static/css/dashboard.css @@ -1,7 +1,9 @@ :root { --color-bg: #1A233A; --color-bg-light: #272E48; + --color-bg-contrast: #646683; --color-fg: white; + --color-fg-dark: #b0bdd6; --color-fg-contrast: black; --color-accent: #6f42c1; --color-accent-dark: #6134b3; @@ -25,6 +27,26 @@ body { margin-bottom: 10px; } +.input-textbox { + color: var(--color-fg); + background-color: var(--color-bg); + border-color: var(--color-accent-dark); +} + +.input-textbox::placeholder { + color: var(--color-fg-dark); +} + +.input-textbox:focus { + color: var(--color-fg); + background-color: var(--color-bg); + border-color: var(--color-accent-dark); +} + +.input-textbox:focus::placeholder { + color: var(--color-fg-dark); +} + .nav-pills .nav-link.active, .nav-pills .show > .nav-link { background-color: var(--color-accent) } diff --git a/collectoss/templates/admin-dashboard.j2 b/collectoss/templates/admin-dashboard.j2 index ee547b46d..d6f12362e 100644 --- a/collectoss/templates/admin-dashboard.j2 +++ b/collectoss/templates/admin-dashboard.j2 @@ -77,7 +77,7 @@
- +
{{ setting.description or "No description available" }}
@@ -106,7 +106,7 @@
- +
{{ setting.description or "No description available" }}
@@ -123,6 +123,7 @@

Configuration

{# Start content card #} +

Double-click an empty input field to automatically populate it with the placeholder value

@@ -135,7 +136,7 @@
- +
No description available
@@ -163,6 +164,47 @@ range(elements.length).forEach((i) => { }) }); +document.getElementById("settings-form").addEventListener("submit", (event) => { + event.preventDefault(); + + var data = {}; + + for(var value of event.target.elements) { + if(value.value != "") { + var section = value.getAttribute("section"); + var setting = value.getAttribute("setting"); + + if(!(section in data)) { + data[section] = {}; + } + data[section][setting] = value.value; + } + } + + fetch("{{ url_for('update_config') }}", { + method: "POST", + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify(data) + }) + .then(response => response.json()) + .then(data => { + if(data.status == "success") { + window.location.replace("{{ url_for('dashboard_view') }}"); + } + }) + .catch(reason => { + alert("An error occurred: " + reason); + }); +}); + +for(var box of document.getElementsByClassName("input-textbox")) { + box.addEventListener("dblclick", (event) => { + if(event.target.value == "") { + event.target.value = event.target.placeholder; + } + }); +} + function setActive(navLink) { var elements = document.getElementsByClassName("nav-link"); range(elements.length).forEach((i) => { diff --git a/collectoss/templates/settings.j2 b/collectoss/templates/settings.j2 index cdedd6b8c..014da2d67 100644 --- a/collectoss/templates/settings.j2 +++ b/collectoss/templates/settings.j2 @@ -530,7 +530,7 @@ fetch("{{ url_for("toggle_user_group_favorite") }}?group_name=" + group) .then((response) => response.json()) .then((data) => { - if (data.status == "Success") { + if(data.status == "Success") { if (button.classList.contains("bi-star-fill")) { button.classList.remove("bi-star-fill"); button.classList.add("bi-star"); From c5efe69bc2d501a2df45ebdf12bd83ad9af32f3d Mon Sep 17 00:00:00 2001 From: Ulincsys Date: Mon, 20 May 2024 17:00:44 -0500 Subject: [PATCH 03/12] Clean up after merge Signed-off-by: Ulincsys --- collectoss/api/routes/config.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/collectoss/api/routes/config.py b/collectoss/api/routes/config.py index aad308bd6..bc2e8afce 100644 --- a/collectoss/api/routes/config.py +++ b/collectoss/api/routes/config.py @@ -30,9 +30,6 @@ def generate_upgrade_request(): @app.route(f"/{API_VERSION}/config/get", methods=['GET', 'POST']) @ssl_required def get_config(): - if not development and not request.is_secure: - return generate_upgrade_request() - with DatabaseSession(logger, engine=current_app.engine) as session: config_dict = SystemConfig(logger, session).config.load_config() From 1935bd24311f7c2d5f851d9939d27a084b103573 Mon Sep 17 00:00:00 2001 From: Ulincsys Date: Mon, 20 May 2024 17:19:37 -0500 Subject: [PATCH 04/12] Fix missing import Signed-off-by: Ulincsys --- collectoss/api/view/routes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/collectoss/api/view/routes.py b/collectoss/api/view/routes.py index 9d07aa8db..7cadc2d8a 100644 --- a/collectoss/api/view/routes.py +++ b/collectoss/api/view/routes.py @@ -12,6 +12,7 @@ from .server import LoginException from collectoss.application.util import * from collectoss.application.db.lib import get_value +from collectoss.application.config import SystemConfig from ..server import app, db_session logger = logging.getLogger(__name__) From 195439df90a8970aba66b9e13faa62dbe3513fa0 Mon Sep 17 00:00:00 2001 From: Ulincsys Date: Mon, 24 Jun 2024 11:02:57 -0500 Subject: [PATCH 05/12] implement config updating and user table view Signed-off-by: Ulincsys --- collectoss/api/routes/config.py | 24 +- collectoss/api/util.py | 24 +- collectoss/api/view/api_view.py | 15 +- collectoss/api/view/routes.py | 10 +- collectoss/static/css/dashboard.css | 109 ++++++- collectoss/templates/admin-dashboard.j2 | 368 +++++++++++++++++------- collectoss/templates/settings.j2 | 22 +- 7 files changed, 439 insertions(+), 133 deletions(-) diff --git a/collectoss/api/routes/config.py b/collectoss/api/routes/config.py index bc2e8afce..e18fc157b 100644 --- a/collectoss/api/routes/config.py +++ b/collectoss/api/routes/config.py @@ -9,7 +9,7 @@ # Disable the requirement for SSL by setting env["AUGUR_DEV"] = True from collectoss.application.config import get_development_flag from collectoss.application.db.lib import get_session -from collectoss.api.util import ssl_required +from collectoss.api.util import ssl_required, admin_required from collectoss.application.db.models import Config from collectoss.application.config import SystemConfig from collectoss.application.db.session import DatabaseSession @@ -36,6 +36,28 @@ def get_config(): return jsonify(config_dict), 200 +@app.route(f"/{AUGUR_API_VERSION}/config/set", methods=['GET', 'POST']) +@ssl_required +@admin_required +def set_config_item(): + setting = request.args.get("setting") + section = request.args.get("section") + value = request.values.get("value") + + result = { + "section_name": section, + "setting_name": setting, + "value": value + } + + if not setting or not section or not value: + return jsonify({"status": "Missing argument"}), 400 + + with get_session() as session: + config = AugurConfig(logger, session) + config.add_or_update_settings([result]) + + return jsonify({"status": "success"}) @app.route(f"/{API_VERSION}/config/update", methods=['POST']) @ssl_required diff --git a/collectoss/api/util.py b/collectoss/api/util.py index 662b58f13..44aaadf3d 100644 --- a/collectoss/api/util.py +++ b/collectoss/api/util.py @@ -6,7 +6,7 @@ import re import beaker -from flask import request, jsonify, current_app +from flask import request, jsonify, current_app, abort from collectoss.application.db import get_session from functools import wraps @@ -14,6 +14,8 @@ from collectoss.application.config import get_development_flag from collectoss.application.db.models import ClientApplication +from flask_login import login_required, current_user + development = get_development_flag() __ROOT = os.path.abspath(os.path.dirname(__file__)) @@ -154,4 +156,22 @@ def wrapper(*args, **kwargs): return generate_upgrade_request() return fun(*args, **kwargs) - return wrapper \ No newline at end of file + return wrapper + +def admin_required(func): + @login_required + @wraps(func) + def inner_function(*args, **kwargs): + if current_user.admin: + return func(*args, **kwargs) + else: + abort(403) + return inner_function + +def development_required(func): + @wraps(func) + def inner_function(*args, **kwargs): + if not development: + abort(403) + return func(*args, **kwargs) + return inner_function \ No newline at end of file diff --git a/collectoss/api/view/api_view.py b/collectoss/api/view/api_view.py index 23d1b9a65..d03c90fcc 100644 --- a/collectoss/api/view/api_view.py +++ b/collectoss/api/view/api_view.py @@ -64,6 +64,11 @@ def internal_server_error(error): return render_message("500 - Internal Server Error", "An error occurred while trying to service your request. Please try again, and if the issue persists, please file a GitHub issue with the below error message:", error=stacktrace), 500 +@app.template_filter("escape_ID") +def escape_HTML_ID(data: str) -> str: + data = data.replace(".", "\\.") + return data + @login_manager.unauthorized_handler def unauthorized(): if API_VERSION in str(request.path): @@ -77,16 +82,6 @@ def unauthorized(): session["login_next"] = url_for(request.endpoint, **request.args) return redirect(url_for('user_login')) -def admin_required(func): - @login_required - @wraps(func) - def inner_function(*args, **kwargs): - if current_user.admin: - return func(*args, **kwargs) - else: - return forbidden(None) - return inner_function - @login_manager.user_loader def load_user(user_id): diff --git a/collectoss/api/view/routes.py b/collectoss/api/view/routes.py index 7cadc2d8a..4ae1c85c8 100644 --- a/collectoss/api/view/routes.py +++ b/collectoss/api/view/routes.py @@ -5,7 +5,7 @@ import math from flask import render_template, request, redirect, url_for, session, flash from .utils import * -from .augur_view import admin_required +from augur.api.util import admin_required, development_required from flask_login import login_user, logout_user, current_user, login_required from collectoss.application.db.models import User, Repo, ClientApplication @@ -15,6 +15,8 @@ from collectoss.application.config import SystemConfig from ..server import app, db_session +from augur.application.db.lib import get_session + logger = logging.getLogger(__name__) @@ -320,6 +322,7 @@ def user_group_view(group = None): return render_module("user-group-repos-table", title="Repos", repos=data, query_key=query, activePage=params["page"], pages=page_count, offset=pagination_offset, PS="user_group_view", reverse = rev, sorting = params.get("sort"), group=group) @app.route('/error') +@development_required def throw_exception(): raise Exception("This Exception intentionally raised") @@ -341,5 +344,8 @@ def dashboard_view(): ] backend_config = AugurConfig(logger, db_session).load_config() + + with get_session() as session: + users = session.query(User).all() - return render_template('admin-dashboard.j2', sections = empty, config = backend_config) + return render_template('admin-dashboard.j2', sections = empty, config = backend_config, users = users) diff --git a/collectoss/static/css/dashboard.css b/collectoss/static/css/dashboard.css index f8354c49b..d213434ca 100644 --- a/collectoss/static/css/dashboard.css +++ b/collectoss/static/css/dashboard.css @@ -33,6 +33,13 @@ body { border-color: var(--color-accent-dark); } +.input-group-text { + color: var(--color-fg); + background-color: var(--color-bg-light); + border-color: var(--color-accent-dark); + border-right: none; +} + .input-textbox::placeholder { color: var(--color-fg-dark); } @@ -47,7 +54,8 @@ body { color: var(--color-fg-dark); } -.nav-pills .nav-link.active, .nav-pills .show > .nav-link { +.nav-pills .nav-link.active, +.nav-pills .show>.nav-link { background-color: var(--color-accent) } @@ -84,17 +92,59 @@ body { color: #bcd0f7; } +.contrast-card { + background-color: var(--color-bg); +} + +.card:has(.contrast-card) { + border: none; +} + +.accordion-item { + background-color: var(--color-bg-light); + color: var(--color-fg); +} + +.accordion-button { + background-color: var(--color-accent-dark); + color: var(--color-fg); +} + +.accordion-button:not(.collapsed) { + background-color: var(--color-accent); + color: var(--color-fg); +} + +.accordion-button::after { + filter: saturate(0%) brightness(10); +} + +.accordion-button:not(.collapsed)::after { + filter: saturate(0%) brightness(10); +} + +.accordion-button:focus { + box-shadow: none; + border-color: var(--color-accent-dark); +} + .circle-opaque { - border-radius: 50%; /* Make it a circle */ - display: inline-block; - position: absolute; /* Able to position it, overlaying the other image */ - left:0px; /* Customise the position, but make sure it */ - top:0px; /* is the same as .circle-transparent */ - z-index: -1; /* Makes the image sit *behind* .circle-transparent */ + border-radius: 50%; + /* Make it a circle */ + display: inline-block; + position: absolute; + /* Able to position it, overlaying the other image */ + left: 0px; + /* Customise the position, but make sure it */ + top: 0px; + /* is the same as .circle-transparent */ + z-index: -1; + /* Makes the image sit *behind* .circle-transparent */ } .circle-opaque img { - border-radius: 50%; /* Make it a circle */ + border-radius: 50%; + /* Make it a circle */ z-index: -1; } @@ -117,4 +167,47 @@ table { #toast-placeholder { display: none; z-index: 100; +} + +@-webkit-keyframes rotating + +/* Safari and Chrome */ + { + from { + -webkit-transform: rotate(0deg); + -o-transform: rotate(0deg); + transform: rotate(0deg); + } + + to { + -webkit-transform: rotate(360deg); + -o-transform: rotate(360deg); + transform: rotate(360deg); + } +} + +@keyframes rotating { + from { + -ms-transform: rotate(0deg); + -moz-transform: rotate(0deg); + -webkit-transform: rotate(0deg); + -o-transform: rotate(0deg); + transform: rotate(0deg); + } + + to { + -ms-transform: rotate(360deg); + -moz-transform: rotate(360deg); + -webkit-transform: rotate(360deg); + -o-transform: rotate(360deg); + transform: rotate(360deg); + } +} + +.rotating { + -webkit-animation: rotating 1s linear infinite; + -moz-animation: rotating 1s linear infinite; + -ms-animation: rotating 1s linear infinite; + -o-animation: rotating 1s linear infinite; + animation: rotating 1s linear infinite; } \ No newline at end of file diff --git a/collectoss/templates/admin-dashboard.j2 b/collectoss/templates/admin-dashboard.j2 index d6f12362e..1b1bb5279 100644 --- a/collectoss/templates/admin-dashboard.j2 +++ b/collectoss/templates/admin-dashboard.j2 @@ -1,5 +1,6 @@ + @@ -16,14 +17,16 @@ + Dasboard - CollectOSS View - + + {% include 'notifications.j2' %}
@@ -33,8 +36,10 @@
#} + + Augur logo +
- {# Start dashboard content #} -
-

Stats

- {# Start content card #} -
-
- {# Start form body #} - - {% for section in sections %} -
-
-
{{ section.title }}
-
- {% for setting in section.settings %} -
-
- - -
{{ setting.description or "No description available" }}
+
+ {# Start dashboard content #} +
+
+

Stats

+ {# Start content card #} +
+
+ {# Start form body #} + + {% for section in sections %} +
+
+
{{ section.title }}
+
+ {% for setting in section.settings %} +
+
+ + +
{{ setting.description or "No description available" }}
+
+
+ {% endfor %}
-
- {% endfor %} + {% endfor %} + {#
+
+ +
+
#} +
- {% endfor %} - {#
-
- -
-
#} - +
-
-

User Accounts

- {# Start content card #} -
-
-
- {% for section in sections %} -
-
-
{{ section.title }}
-
- {% for setting in section.settings %} -
-
- - -
{{ setting.description or "No description available" }}
+ + -
-

Configuration

- {# Start content card #} -

Double-click an empty input field to automatically populate it with the placeholder value

-
-
-
- {% for section in config.items() %} -
-
-
{{ section[0] }}
+
@@ -157,14 +249,41 @@ + + diff --git a/collectoss/templates/settings.j2 b/collectoss/templates/settings.j2 index 014da2d67..267c7f2f1 100644 --- a/collectoss/templates/settings.j2 +++ b/collectoss/templates/settings.j2 @@ -414,8 +414,6 @@
-
-
{#
#} - + + diff --git a/scripts/control/restart.py b/scripts/control/restart.py new file mode 100644 index 000000000..4f0382c9e --- /dev/null +++ b/scripts/control/restart.py @@ -0,0 +1,29 @@ +from subprocess import run, PIPE, Popen +import signal, time, os + +# Ignore SIGTERM from parent process (since we're terminating our parent) +signal.signal(signal.SIGTERM, lambda signum, frame: None) + +run("augur backend stop-collection-blocking", shell=True, stderr=PIPE, stdout=PIPE, stdin=PIPE) +Popen("augur backend stop", shell=True, stderr=PIPE, stdout=PIPE, stdin=PIPE).wait() + +time.sleep(5) + +cmd = "augur backend start" + +if os.environ.get("AUGUR_DEV") == "1": + cmd += " --development" + +if os.environ.get("AUGUR_DISABLE_COLLECTION") == "1": + cmd += " --disable-collection" + +if os.environ.get("AUGUR_PORT"): + cmd += f" --port {os.environ['AUGUR_PORT']}" + +if os.environ.get("AUGUR_PIDFILE"): + cmd += f" --pidfile {os.environ['AUGUR_PIDFILE']}" + +if signal.getsignal(signal.SIGHUP) != signal.SIG_DFL: + cmd = "nohup " + cmd + +Popen(cmd, shell=True) From 7a97cc9941940862f46184bd7b9b1883a177c253 Mon Sep 17 00:00:00 2001 From: Ulincsys Date: Sun, 14 Jul 2024 23:29:45 -0500 Subject: [PATCH 09/12] further work needed in CLI Signed-off-by: Ulincsys --- collectoss/api/view/api_view.py | 10 +++- collectoss/api/view/routes.py | 10 +++- collectoss/static/css/dashboard.css | 4 ++ collectoss/templates/admin-dashboard.j2 | 75 ++++--------------------- collectoss/templates/ping.j2 | 9 +-- 5 files changed, 38 insertions(+), 70 deletions(-) diff --git a/collectoss/api/view/api_view.py b/collectoss/api/view/api_view.py index d03c90fcc..f0fd57cce 100644 --- a/collectoss/api/view/api_view.py +++ b/collectoss/api/view/api_view.py @@ -61,14 +61,22 @@ def internal_server_error(error): errout.close() except Exception as e: logger.error(e) + raise e - return render_message("500 - Internal Server Error", "An error occurred while trying to service your request. Please try again, and if the issue persists, please file a GitHub issue with the below error message:", error=stacktrace), 500 + return render_message("500 - Internal Server Error", """An error occurred while trying to service your request. + Please try again, and if the error persists, please file a GitHub issue with a description + of what you were doing before this error occurred accompanied by the below error message:""", error=stacktrace), 500 @app.template_filter("escape_ID") def escape_HTML_ID(data: str) -> str: + # Done this way in case we want to add more replacements in the future data = data.replace(".", "\\.") return data +@app.template_filter("quoted") +def quote_surrounded(data: str) -> str: + return '"' + data + '"' + @login_manager.unauthorized_handler def unauthorized(): if API_VERSION in str(request.path): diff --git a/collectoss/api/view/routes.py b/collectoss/api/view/routes.py index 025330094..7ee2678a5 100644 --- a/collectoss/api/view/routes.py +++ b/collectoss/api/view/routes.py @@ -3,10 +3,13 @@ """ import logging import math +import os +import signal from flask import render_template, request, redirect, url_for, session, flash from .utils import * from augur.api.util import admin_required, development_required from flask_login import login_user, logout_user, current_user, login_required +from sqlalchemy.exc import OperationalError from collectoss.application.db.models import User, Repo, ClientApplication from .server import LoginException @@ -350,6 +353,11 @@ def dashboard_view(): backend_config = AugurConfig(logger, db_session).load_config() with get_session() as session: - users = session.query(User).all() + try: + users = session.query(User).all() + except OperationalError as e: + # Instruct Gunicorn to reboot workers to resolve database connection instability + os.kill(os.getpid(), signal.SIGTERM) + return "reloading" return render_template('admin-dashboard.j2', sections = empty, config = backend_config, users = users) diff --git a/collectoss/static/css/dashboard.css b/collectoss/static/css/dashboard.css index d213434ca..c89815488 100644 --- a/collectoss/static/css/dashboard.css +++ b/collectoss/static/css/dashboard.css @@ -82,6 +82,10 @@ body { padding-right: 10px !important; } +.modal-dialog { + color: var(--color-fg-contrast); +} + .dashboard-form-control { border: 1px solid #596280; -webkit-border-radius: 2px; diff --git a/collectoss/templates/admin-dashboard.j2 b/collectoss/templates/admin-dashboard.j2 index 473e214aa..facf3649d 100644 --- a/collectoss/templates/admin-dashboard.j2 +++ b/collectoss/templates/admin-dashboard.j2 @@ -78,9 +78,14 @@ {# Start content card #}
- {# Start form body #} - - +
+
+ +
+
+ +
+
@@ -310,7 +257,7 @@ function restart_confirm() { var confirmModal = new bootstrap.Modal(document.getElementById('confirmation-modal')); var confirmButton = document.getElementById("confirmation-button"); confirmButton.onclick = () => { - fetch("{{ url_for('restart_system') }}").then(response => { + fetch({{ url_for('restart_system') | quoted }}).then(response => { if(response.ok) { window.location.replace("{{ url_for('server_ping_frontend') }}"); } else { @@ -326,7 +273,7 @@ function shutdown_confirm() { var confirmModal = new bootstrap.Modal(document.getElementById('confirmation-modal')); var confirmButton = document.getElementById("confirmation-button"); confirmButton.onclick = () => { - fetch("{{ url_for('shutdown_system') }}").then(response => { + fetch({{ url_for('shutdown_system') | quoted }}).then(response => { if(response.ok) { flashToast("Shutdown request acknowledged"); } else { diff --git a/collectoss/templates/ping.j2 b/collectoss/templates/ping.j2 index 69755cea5..10663ac4f 100644 --- a/collectoss/templates/ping.j2 +++ b/collectoss/templates/ping.j2 @@ -1,6 +1,7 @@

Connection Status

This page will automatically update

-

+

If a restart was recently requested, it may take several minutes for Augur to finish shutting down.

+

- diff --git a/scripts/control/restart.py b/scripts/control/restart.py deleted file mode 100644 index 4f0382c9e..000000000 --- a/scripts/control/restart.py +++ /dev/null @@ -1,29 +0,0 @@ -from subprocess import run, PIPE, Popen -import signal, time, os - -# Ignore SIGTERM from parent process (since we're terminating our parent) -signal.signal(signal.SIGTERM, lambda signum, frame: None) - -run("augur backend stop-collection-blocking", shell=True, stderr=PIPE, stdout=PIPE, stdin=PIPE) -Popen("augur backend stop", shell=True, stderr=PIPE, stdout=PIPE, stdin=PIPE).wait() - -time.sleep(5) - -cmd = "augur backend start" - -if os.environ.get("AUGUR_DEV") == "1": - cmd += " --development" - -if os.environ.get("AUGUR_DISABLE_COLLECTION") == "1": - cmd += " --disable-collection" - -if os.environ.get("AUGUR_PORT"): - cmd += f" --port {os.environ['AUGUR_PORT']}" - -if os.environ.get("AUGUR_PIDFILE"): - cmd += f" --pidfile {os.environ['AUGUR_PIDFILE']}" - -if signal.getsignal(signal.SIGHUP) != signal.SIG_DFL: - cmd = "nohup " + cmd - -Popen(cmd, shell=True) From 99a6d91413fde59965195b5ba27fef6bb470458f Mon Sep 17 00:00:00 2001 From: Adrian Edwards Date: Wed, 24 Jun 2026 19:16:35 -0400 Subject: [PATCH 12/12] rename some additional things Signed-off-by: Adrian Edwards --- collectoss/api/routes/config.py | 2 +- collectoss/api/view/routes.py | 2 +- collectoss/application/db/lib.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/collectoss/api/routes/config.py b/collectoss/api/routes/config.py index e18fc157b..55c2b02fa 100644 --- a/collectoss/api/routes/config.py +++ b/collectoss/api/routes/config.py @@ -54,7 +54,7 @@ def set_config_item(): return jsonify({"status": "Missing argument"}), 400 with get_session() as session: - config = AugurConfig(logger, session) + config = SystemConfig(logger, session) config.add_or_update_settings([result]) return jsonify({"status": "success"}) diff --git a/collectoss/api/view/routes.py b/collectoss/api/view/routes.py index c0ae76efc..a017e5f73 100644 --- a/collectoss/api/view/routes.py +++ b/collectoss/api/view/routes.py @@ -346,7 +346,7 @@ def dashboard_view(): ]} ] - backend_config = AugurConfig(logger, db_session).load_config() + backend_config = SystemConfig(logger, db_session).load_config() with get_session() as session: try: diff --git a/collectoss/application/db/lib.py b/collectoss/application/db/lib.py index c5394365d..a459c7a64 100644 --- a/collectoss/application/db/lib.py +++ b/collectoss/application/db/lib.py @@ -20,7 +20,7 @@ logger = logging.getLogger("db_lib") -@deprecated("This is a legacy method. Use AugurConfig.get_value instead") +@deprecated("This is a legacy method. Use SystemConfig.get_value instead") def get_value(section_name: str, setting_name: str) -> Optional[Any]: """Get the value of a setting from the config.