feat(power): shutdown_host through HostShutdownMixin, 0.25.0 (NetOrk/netork#2841)
The host's clean shutdown next to reboot_host: /sbin/poweroff as root, detached and two seconds late like the restart, so the exit status comes back before the session drops. A refusal raises RuntimeError with what the host said, plus the sudo password hint where sudo wanted one. poweroff rather than shutdown -h now: systemd links it to systemctl poweroff, sysvinit and BusyBox ship it themselves, while BusyBox has no shutdown and sysvinit's -h may only halt.
This commit is contained in:
1 parent
a8c57a61e4
commit
f876ac4598
5 files changed
+83
-8
No files matched your search
+26
-7
@@ -138,15 +138,20 @@ _YUM_SECURITY_COMMAND = (
|
||||
#: The exit status ``DNF_SECURITY_COMMAND`` and ``_YUM_SECURITY_COMMAND`` report.
|
||||
_DNF_RC_RE = re.compile(r"^__DNF_RC=(\d+)\s*$", re.MULTILINE)
|
||||
|
||||
#: Restart the host two seconds later, detached from this session: the launcher's
|
||||
#: exit status comes back before the host goes down, and closing the session
|
||||
#: cannot take the restart with it.
|
||||
#: ``running`` when systemd has lldpd active or a process of that name runs, else ``stopped``.
|
||||
_LLDPD_STATE_COMMAND = (
|
||||
"if systemctl is-active --quiet lldpd 2>/dev/null || pgrep -x lldpd >/dev/null 2>&1; "
|
||||
"then echo running; else echo stopped; fi"
|
||||
)
|
||||
#: Restart the host two seconds later, detached from this session: the launcher's
|
||||
#: exit status comes back before the host goes down, and closing the session
|
||||
#: cannot take the restart with it.
|
||||
_REBOOT_DETACHED = "sh -c '(trap \"\" HUP; sleep 2; /sbin/reboot) </dev/null >/dev/null 2>&1 &'"
|
||||
#: Power the host off the same way, two seconds later and detached. ``poweroff``
|
||||
#: rather than ``shutdown -h now``: systemd links it to ``systemctl poweroff``,
|
||||
#: sysvinit and BusyBox (Alpine) ship it themselves, while BusyBox has no
|
||||
#: ``shutdown`` and sysvinit's ``-h`` may only halt, leaving the machine on.
|
||||
_POWEROFF_DETACHED = "sh -c '(trap \"\" HUP; sleep 2; /sbin/poweroff) </dev/null >/dev/null 2>&1 &'"
|
||||
|
||||
#: Why ``run_command`` refuses stdin to a privileged command while a sudo
|
||||
#: password is set (NetOrk/netork#1029).
|
||||
@@ -574,11 +579,25 @@ class LinuxDriver(
|
||||
:raises RuntimeError: when the host refuses -- sudo without a password,
|
||||
no ``reboot`` -- or its answer carried no exit status.
|
||||
"""
|
||||
output, status = _split_status(
|
||||
self._run_privileged(f"{_REBOOT_DETACHED}; echo {_RC_MARKER}$?", 30)
|
||||
)
|
||||
self._launch_detached(_REBOOT_DETACHED, "reboot")
|
||||
|
||||
def shutdown_host(self) -> None:
|
||||
"""Shut the host down cleanly (``HostShutdownMixin``); returns once it is under way.
|
||||
|
||||
Runs ``/sbin/poweroff`` as root, detached and two seconds late like the
|
||||
restart, so the exit status comes back before the session drops. A
|
||||
clean shutdown only: cutting the power is the BMC's business.
|
||||
|
||||
:raises RuntimeError: when the host refuses -- sudo without a password,
|
||||
no ``poweroff`` -- or its answer carried no exit status.
|
||||
"""
|
||||
self._launch_detached(_POWEROFF_DETACHED, "poweroff")
|
||||
|
||||
def _launch_detached(self, command: str, name: str) -> None:
|
||||
"""Launch a detached power *command* as root; raise what the host said on refusal."""
|
||||
output, status = _split_status(self._run_privileged(f"{command}; echo {_RC_MARKER}$?", 30))
|
||||
if status != 0:
|
||||
reason = output or f"the reboot command exited with status {status}"
|
||||
reason = output or f"the {name} command exited with status {status}"
|
||||
if "password is required" in output:
|
||||
reason = f"{reason}\n{_SUDO_PASSWORD_HINT}"
|
||||
raise RuntimeError(reason)
|
||||
|
||||
Reference in new issue
Block a user