diff --git a/moonraker/components/machine.py b/moonraker/components/machine.py index 12e4111..09c1c7b 100644 --- a/moonraker/components/machine.py +++ b/moonraker/components/machine.py @@ -74,6 +74,7 @@ SERVICE_PROPERTIES = [ "User" ] USB_IDS_URL = "http://www.linux-usb.org/usb.ids" +HAS_SYSCTL = shutil.which("systemctl", os.F_OK) is not None class Machine: def __init__(self, config: ConfigHelper) -> None: @@ -932,17 +933,12 @@ class Machine: class BaseProvider: def __init__(self, config: ConfigHelper) -> None: self.server = config.get_server() - self.shutdown_action = config.get("shutdown_action", "poweroff") - self.shutdown_action = self.shutdown_action.lower() - if self.shutdown_action not in ["halt", "poweroff"]: - raise config.error( - "Section [machine], Option 'shutdown_action':" - f"Invalid value '{self.shutdown_action}', must be " - "'halt' or 'poweroff'" - ) + shutdown_choices = ["halt", "poweroff"] + self.shutdown_action = config.getchoice( + "shutdown_action", shutdown_choices, "poweroff", force_lowercase=True + ) self.available_services: Dict[str, Dict[str, str]] = {} - self.shell_cmd: SCMDComp = self.server.load_component( - config, 'shell_command') + self.shell_cmd: SCMDComp = self.server.load_component(config, 'shell_command') async def initialize(self) -> None: pass @@ -952,10 +948,13 @@ class BaseProvider: return await machine.exec_sudo_command(command) async def shutdown(self) -> None: - await self._exec_sudo_command(f"systemctl {self.shutdown_action}") + act = self.shutdown_action + cmd = f"systemctl {act}" if HAS_SYSCTL else act + await self._exec_sudo_command(cmd) async def reboot(self) -> None: - await self._exec_sudo_command("systemctl reboot") + cmd = "systemctl reboot" if HAS_SYSCTL else "reboot" + await self._exec_sudo_command(cmd) async def do_service_action(self, action: str,