From f876ac4598c6850fcd44fd895e1ed912766231b1 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 29 Nov 2026 11:28:38 +0100 Subject: [PATCH] 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. --- CHANGELOG.md | 10 +++++++ napalm_linux/linux.py | 33 ++++++++++++++++----- pyproject.toml | 2 +- tests/test_linux.py | 45 +++++++++++++++++++++++++++++ tests/test_sudo_without_password.py | 1 + 5 files changed, 83 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e09335..cbe6c7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.25.0] - 2026-11-29 + +### Added +- `shutdown_host()` (napalm-device-types' `HostShutdownMixin`): the host's clean shutdown, + next to `reboot_host` (NetOrk/netork#2841). `/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. `poweroff` rather than + `shutdown -h now`, because BusyBox (Alpine) has no `shutdown` and sysvinit's `-h` may + only halt. OpenMediaVault and QNAP have it through `LinuxDriver`. + ## [0.24.0] - 2026-11-28 ### Added diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index fe67862..beb58cb 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -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 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 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) diff --git a/pyproject.toml b/pyproject.toml index 71ce50a..5902f75 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "napalm-linux" -version = "0.24.0" +version = "0.25.0" description = "NAPALM driver for generic Linux systems via SSH" readme = "README.md" requires-python = ">=3.12" diff --git a/tests/test_linux.py b/tests/test_linux.py index 27e1725..efa1ee5 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -1896,6 +1896,51 @@ class TestRebootHost: driver.reboot_host() +class TestShutdownHost: + """``shutdown_host`` (napalm-device-types' ``HostShutdownMixin``) powers the host off. + + netOrk offers a clean shutdown where the driver defines ``shutdown_host`` + (NetOrk/netork#2841). Like the restart it is detached and a moment late, so + the launcher's exit status comes back before the host goes down. + """ + + def test_the_driver_can_shut_its_host_down(self): + assert callable(getattr(LinuxDriver, "shutdown_host", None)) + + def test_the_shutdown_is_detached_and_privileged(self, driver): + driver._root = False + driver._device.send_command.return_value = "\n__NETORK_RC=0" + + driver.shutdown_host() + + sent = driver._device.send_command.call_args[0][0] + assert sent.startswith("sudo -n sh -c ") + assert "/sbin/poweroff" in sent and "trap" in sent and "&" in sent + assert "reboot" not in sent + assert sent.endswith("echo __NETORK_RC=$?") + + def test_a_refusal_is_raised_with_what_the_host_said(self, driver): + driver._root = False + driver._device.send_command.return_value = "sudo: a password is required\n__NETORK_RC=1" + + with pytest.raises(RuntimeError, match="password is required"): + driver.shutdown_host() + + def test_a_nonzero_status_without_output_names_the_status(self, driver): + driver._root = True + driver._device.send_command.return_value = "__NETORK_RC=127" + + with pytest.raises(RuntimeError, match="status 127"): + driver.shutdown_host() + + def test_no_exit_status_is_no_success(self, driver): + driver._root = True + driver._device.send_command.return_value = "something else" + + with pytest.raises(RuntimeError): + driver.shutdown_host() + + # --------------------------------------------------------------------------- # Listening sockets: ss and the processes' cgroups, as root when it can # --------------------------------------------------------------------------- diff --git a/tests/test_sudo_without_password.py b/tests/test_sudo_without_password.py index b895475..43e523b 100644 --- a/tests/test_sudo_without_password.py +++ b/tests/test_sudo_without_password.py @@ -88,6 +88,7 @@ class TestTheSecurityAdvisories: #: Every caller of ``_run_privileged``, with an answer that satisfies it. _CALLERS = { "reboot_host": (lambda d: d.reboot_host(), ["__NETORK_RC=0"]), + "shutdown_host": (lambda d: d.shutdown_host(), ["__NETORK_RC=0"]), "refresh apt": (lambda d: d.refresh_available_updates(), ["__NETORK_RC=0"]), "check-update": ( lambda d: d.get_available_updates(),