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"))