machine: add shutdown action option

Signed-off-by:  Eric Callahan <arkisne.code@gmail.com>
This commit is contained in:
Eric Callahan 2023-02-08 06:21:09 -05:00
parent f0b82afde4
commit 8010f1dda2
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 33 additions and 11 deletions

View File

@ -777,6 +777,14 @@ class Machine:
class BaseProvider: class BaseProvider:
def __init__(self, config: ConfigHelper) -> None: def __init__(self, config: ConfigHelper) -> None:
self.server = config.get_server() 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'"
)
self.available_services: Dict[str, Dict[str, str]] = {} self.available_services: Dict[str, Dict[str, str]] = {}
self.shell_cmd: SCMDComp = self.server.load_component( self.shell_cmd: SCMDComp = self.server.load_component(
config, 'shell_command') config, 'shell_command')
@ -789,7 +797,7 @@ class BaseProvider:
return await machine.exec_sudo_command(command) return await machine.exec_sudo_command(command)
async def shutdown(self) -> None: async def shutdown(self) -> None:
await self._exec_sudo_command("systemctl halt") await self._exec_sudo_command(f"systemctl {self.shutdown_action}")
async def reboot(self) -> None: async def reboot(self) -> None:
await self._exec_sudo_command("systemctl reboot") await self._exec_sudo_command("systemctl reboot")
@ -1011,6 +1019,7 @@ class SystemdDbusProvider(BaseProvider):
"org.freedesktop.systemd1.manage-units", "org.freedesktop.systemd1.manage-units",
"System Service Management (start, stop, restart) " "System Service Management (start, stop, restart) "
"will be disabled") "will be disabled")
if self.shutdown_action == "poweroff":
await self.dbus_mgr.check_permission( await self.dbus_mgr.check_permission(
"org.freedesktop.login1.power-off", "org.freedesktop.login1.power-off",
"The shutdown API will be disabled" "The shutdown API will be disabled"
@ -1020,6 +1029,16 @@ class SystemdDbusProvider(BaseProvider):
"The shutdown API will be disabled if multiple user " "The shutdown API will be disabled if multiple user "
"sessions are open." "sessions are open."
) )
else:
await self.dbus_mgr.check_permission(
"org.freedesktop.login1.halt",
"The shutdown API will be disabled"
)
await self.dbus_mgr.check_permission(
"org.freedesktop.login1.halt-multiple-sessions",
"The shutdown API will be disabled if multiple user "
"sessions are open."
)
try: try:
# Get the login manaager interface # Get the login manaager interface
self.login_mgr = await self.dbus_mgr.get_interface( self.login_mgr = await self.dbus_mgr.get_interface(
@ -1053,7 +1072,10 @@ class SystemdDbusProvider(BaseProvider):
async def shutdown(self) -> None: async def shutdown(self) -> None:
if self.login_mgr is None: if self.login_mgr is None:
await super().shutdown() await super().shutdown()
if self.shutdown_action == "poweroff":
await self.login_mgr.call_power_off(False) # type: ignore await self.login_mgr.call_power_off(False) # type: ignore
else:
await self.login_mgr.call_halt(False) # type: ignore
async def do_service_action(self, async def do_service_action(self,
action: str, action: str,