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:
parent
093b8c14d6
commit
83381446a0
|
@ -4,6 +4,7 @@
|
||||||
#
|
#
|
||||||
# This file may be distributed under the terms of the GNU GPLv3 license.
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
||||||
import logging
|
import logging
|
||||||
|
from tornado.ioloop import IOLoop
|
||||||
|
|
||||||
class Machine:
|
class Machine:
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
|
@ -12,6 +13,9 @@ class Machine:
|
||||||
"/machine/reboot", ['POST'], self._handle_machine_request)
|
"/machine/reboot", ['POST'], self._handle_machine_request)
|
||||||
self.server.register_endpoint(
|
self.server.register_endpoint(
|
||||||
"/machine/shutdown", ['POST'], self._handle_machine_request)
|
"/machine/shutdown", ['POST'], self._handle_machine_request)
|
||||||
|
self.server.register_endpoint(
|
||||||
|
"/machine/services/restart", ['POST'],
|
||||||
|
self._handle_service_restart)
|
||||||
|
|
||||||
# Register remote methods
|
# Register remote methods
|
||||||
self.server.register_remote_method(
|
self.server.register_remote_method(
|
||||||
|
@ -35,6 +39,18 @@ class Machine:
|
||||||
async def reboot_machine(self):
|
async def reboot_machine(self):
|
||||||
await self._execute_cmd("sudo shutdown -r now")
|
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):
|
async def _execute_cmd(self, cmd):
|
||||||
shell_command = self.server.lookup_plugin('shell_command')
|
shell_command = self.server.lookup_plugin('shell_command')
|
||||||
scmd = shell_command.build_shell_command(cmd, None)
|
scmd = shell_command.build_shell_command(cmd, None)
|
||||||
|
@ -42,6 +58,7 @@ class Machine:
|
||||||
await scmd.run(timeout=2., verbose=False)
|
await scmd.run(timeout=2., verbose=False)
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.exception(f"Error running cmd '{cmd}'")
|
logging.exception(f"Error running cmd '{cmd}'")
|
||||||
|
raise
|
||||||
|
|
||||||
def load_plugin(config):
|
def load_plugin(config):
|
||||||
return Machine(config)
|
return Machine(config)
|
||||||
|
|
Loading…
Reference in New Issue