From a2af7623affbc905c6d14734e0241c68da994b7a Mon Sep 17 00:00:00 2001 From: Daniel Markstedt Date: Sat, 27 Nov 2021 17:17:36 -0800 Subject: [PATCH] =?UTF-8?q?Make=20the=20restart=20rascsi=20service=20endpo?= =?UTF-8?q?int=20actually=20check=20for=20systemd=20s=E2=80=A6=20(#487)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Make the restart rascsi service endpoint actually check for systemd service status before doing anything. Introduced error handling and more verbose messages. * Cleanup --- src/web/pi_cmds.py | 11 ++++++++--- src/web/web.py | 29 ++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/web/pi_cmds.py b/src/web/pi_cmds.py index 0612fa96..246d5b73 100644 --- a/src/web/pi_cmds.py +++ b/src/web/pi_cmds.py @@ -11,10 +11,15 @@ from settings import AUTH_GROUP def systemd_service(service, action): """ Takes (str) service and (str) action - Action can be one of start/stop/restart + Action can be any that systemctl supports, ex. start/stop/restart/show + Returns (dict) with (bool) status, (str) msg, (str) err """ - proc = asyncio.run(run_async("sudo /bin/systemctl {action} {service}")) - return proc["returncode"] == 0 + proc = asyncio.run(run_async(f"sudo /bin/systemctl {action} {service}")) + return { + "status": proc["returncode"] == 0, + "msg": proc["stdout"], + "err": proc["stderr"], + } def reboot_pi(): diff --git a/src/web/web.py b/src/web/web.py index c9f45d73..577068c2 100644 --- a/src/web/web.py +++ b/src/web/web.py @@ -717,11 +717,30 @@ def rascsi_restart(): flash(auth["msg"], "error") return redirect(url_for("index")) - detach_all() - flash("Safely detached all devices.") - flash("Restarting RaSCSI Service...") - systemd_service("rascsi.service", "restart") - systemd_service("monitor_rascsi.service", "restart") + service = "rascsi.service" + monitor_service = "monitor_rascsi.service" + rascsi_status = systemd_service(service, "show") + if rascsi_status["status"] and "ActiveState=active" not in rascsi_status["msg"]: + flash( + f"Failed to restart {service} because it is inactive. " + "You are probably running RaSCSI as a regular process.", "error" + ) + return redirect(url_for("index")) + + monitor_status = systemd_service(monitor_service, "show") + restart_proc = systemd_service(service, "restart") + if restart_proc["status"]: + flash(f"Restarted {service}") + restart_monitor = systemd_service(monitor_service, "restart") + if restart_monitor["status"] and "ActiveState=active" in monitor_status["msg"]: + flash(f"Restarted {monitor_service}") + elif not restart_monitor["status"] and "ActiveState=active" in monitor_status["msg"]: + flash(f"Failed to restart {monitor_service}:", "error") + return redirect(url_for("index")) + + restart_monitor = systemd_service("monitor_rascsi.service", "restart") + flash(f"Failed to restart {service}:", "error") + flash(restart_proc["err"], "error") return redirect(url_for("index"))