machine: add "/machine/services/restart" endpoint

This makes it possible for clients to hard restart an service in the event it becomes unresponsive.

Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-11-18 19:29:25 -05:00
parent 093b8c14d6
commit 83381446a0
1 changed files with 17 additions and 0 deletions

View File

@ -4,6 +4,7 @@
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import logging
from tornado.ioloop import IOLoop
class Machine:
def __init__(self, config):
@ -12,6 +13,9 @@ class Machine:
"/machine/reboot", ['POST'], self._handle_machine_request)
self.server.register_endpoint(
"/machine/shutdown", ['POST'], self._handle_machine_request)
self.server.register_endpoint(
"/machine/services/restart", ['POST'],
self._handle_service_restart)
# Register remote methods
self.server.register_remote_method(
@ -35,6 +39,18 @@ class Machine:
async def reboot_machine(self):
await self._execute_cmd("sudo shutdown -r now")
async def _handle_service_restart(self, web_request):
name = web_request.get('service')
if name == "klipper":
await self._execute_cmd(f'sudo systemctl restart {name}')
elif name == "moonraker":
IOLoop.current().spawn_callback(
self._execute_cmd, f'sudo systemctl restart {name}')
else:
raise self.sever.error(
f"Invalid argument recevied for 'name': {name}")
return "ok"
async def _execute_cmd(self, cmd):
shell_command = self.server.lookup_plugin('shell_command')
scmd = shell_command.build_shell_command(cmd, None)
@ -42,6 +58,7 @@ class Machine:
await scmd.run(timeout=2., verbose=False)
except Exception:
logging.exception(f"Error running cmd '{cmd}'")
raise
def load_plugin(config):
return Machine(config)