machine: add service "stop" and "start" endpoints

This allows clients to toggle specific services on/off.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-03-03 17:21:54 -05:00
parent 1f9d896e17
commit e32c14b1a7
1 changed files with 18 additions and 6 deletions

View File

@ -17,7 +17,13 @@ class Machine:
"/machine/shutdown", ['POST'], self._handle_machine_request)
self.server.register_endpoint(
"/machine/services/restart", ['POST'],
self._handle_service_restart)
self._handle_service_request)
self.server.register_endpoint(
"/machine/services/stop", ['POST'],
self._handle_service_request)
self.server.register_endpoint(
"/machine/services/start", ['POST'],
self._handle_service_request)
# Register remote methods
self.server.register_remote_method(
@ -41,15 +47,21 @@ class Machine:
async def reboot_machine(self):
await self._execute_cmd("sudo shutdown -r now")
async def restart_service(self, service_name):
await self._execute_cmd(f'sudo systemctl restart {service_name}')
async def do_service_action(self, action, service_name):
await self._execute_cmd(
f'sudo systemctl {action} {service_name}')
async def _handle_service_restart(self, web_request):
async def _handle_service_request(self, web_request):
name = web_request.get('service').lower()
action = web_request.get_endpoint().split('/')[-1]
if name == "moonraker":
IOLoop.current().spawn_callback(self.restart_service, name)
if action != "restart":
raise self.server.error(
f"Service action '{action}' not available for moonraker")
IOLoop.current().spawn_callback(
self.do_service_action, action, name)
elif name in ALLOWED_SERVICES:
await self.restart_service(name)
await self.do_service_action(action, name)
else:
raise self.server.error(
f"Invalid argument recevied for 'name': {name}")