machine: add shutdown action option
Signed-off-by: Eric Callahan <arkisne.code@gmail.com>
This commit is contained in:
parent
f0b82afde4
commit
8010f1dda2
|
@ -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,15 +1019,26 @@ 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")
|
||||||
await self.dbus_mgr.check_permission(
|
if self.shutdown_action == "poweroff":
|
||||||
"org.freedesktop.login1.power-off",
|
await self.dbus_mgr.check_permission(
|
||||||
"The shutdown API will be disabled"
|
"org.freedesktop.login1.power-off",
|
||||||
)
|
"The shutdown API will be disabled"
|
||||||
await self.dbus_mgr.check_permission(
|
)
|
||||||
"org.freedesktop.login1.power-off-multiple-sessions",
|
await self.dbus_mgr.check_permission(
|
||||||
"The shutdown API will be disabled if multiple user "
|
"org.freedesktop.login1.power-off-multiple-sessions",
|
||||||
"sessions are open."
|
"The shutdown API will be disabled if multiple user "
|
||||||
)
|
"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()
|
||||||
await self.login_mgr.call_power_off(False) # type: ignore
|
if self.shutdown_action == "poweroff":
|
||||||
|
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,
|
||||||
|
|
Loading…
Reference in New Issue