Make the restart rascsi service endpoint actually check for systemd s… (#487)

* Make the restart rascsi service endpoint actually check for systemd service status before doing anything. Introduced error handling and more verbose messages.

* Cleanup
This commit is contained in:
Daniel Markstedt 2021-11-27 17:17:36 -08:00 committed by GitHub
parent 7818552ca9
commit a2af7623af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 8 deletions

View File

@ -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():

View File

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