Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f876ac4598 |
No files matched your search
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [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
|
## [0.24.0] - 2026-11-28
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
+26
-7
@@ -138,15 +138,20 @@ _YUM_SECURITY_COMMAND = (
|
|||||||
#: The exit status ``DNF_SECURITY_COMMAND`` and ``_YUM_SECURITY_COMMAND`` report.
|
#: The exit status ``DNF_SECURITY_COMMAND`` and ``_YUM_SECURITY_COMMAND`` report.
|
||||||
_DNF_RC_RE = re.compile(r"^__DNF_RC=(\d+)\s*$", re.MULTILINE)
|
_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``.
|
#: ``running`` when systemd has lldpd active or a process of that name runs, else ``stopped``.
|
||||||
_LLDPD_STATE_COMMAND = (
|
_LLDPD_STATE_COMMAND = (
|
||||||
"if systemctl is-active --quiet lldpd 2>/dev/null || pgrep -x lldpd >/dev/null 2>&1; "
|
"if systemctl is-active --quiet lldpd 2>/dev/null || pgrep -x lldpd >/dev/null 2>&1; "
|
||||||
"then echo running; else echo stopped; fi"
|
"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 &'"
|
_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
|
#: Why ``run_command`` refuses stdin to a privileged command while a sudo
|
||||||
#: password is set (NetOrk/netork#1029).
|
#: password is set (NetOrk/netork#1029).
|
||||||
@@ -574,11 +579,25 @@ class LinuxDriver(
|
|||||||
:raises RuntimeError: when the host refuses -- sudo without a password,
|
:raises RuntimeError: when the host refuses -- sudo without a password,
|
||||||
no ``reboot`` -- or its answer carried no exit status.
|
no ``reboot`` -- or its answer carried no exit status.
|
||||||
"""
|
"""
|
||||||
output, status = _split_status(
|
self._launch_detached(_REBOOT_DETACHED, "reboot")
|
||||||
self._run_privileged(f"{_REBOOT_DETACHED}; echo {_RC_MARKER}$?", 30)
|
|
||||||
)
|
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:
|
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:
|
if "password is required" in output:
|
||||||
reason = f"{reason}\n{_SUDO_PASSWORD_HINT}"
|
reason = f"{reason}\n{_SUDO_PASSWORD_HINT}"
|
||||||
raise RuntimeError(reason)
|
raise RuntimeError(reason)
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "napalm-linux"
|
name = "napalm-linux"
|
||||||
version = "0.24.0"
|
version = "0.25.0"
|
||||||
description = "NAPALM driver for generic Linux systems via SSH"
|
description = "NAPALM driver for generic Linux systems via SSH"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.12"
|
requires-python = ">=3.12"
|
||||||
|
|||||||
@@ -1896,6 +1896,51 @@ class TestRebootHost:
|
|||||||
driver.reboot_host()
|
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
|
# Listening sockets: ss and the processes' cgroups, as root when it can
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ class TestTheSecurityAdvisories:
|
|||||||
#: Every caller of ``_run_privileged``, with an answer that satisfies it.
|
#: Every caller of ``_run_privileged``, with an answer that satisfies it.
|
||||||
_CALLERS = {
|
_CALLERS = {
|
||||||
"reboot_host": (lambda d: d.reboot_host(), ["__NETORK_RC=0"]),
|
"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"]),
|
"refresh apt": (lambda d: d.refresh_available_updates(), ["__NETORK_RC=0"]),
|
||||||
"check-update": (
|
"check-update": (
|
||||||
lambda d: d.get_available_updates(),
|
lambda d: d.get_available_updates(),
|
||||||
|
|||||||
Reference in new issue
Block a user