machine: support reboot and shutdown on more distros
Previously a change was made away from the standard "reboot" and "poweroff" commands as not every systemd distro had the correct symbolic links available. However, there are other distros that do not use systemd. To support the maximum number of distros detect if systemctl is available and prefix it to the command if so. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
1 file changed
+11
-12
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user