refactor(power): reboot_host and shutdown_host in a mixin of their own (NetOrk/netork#2841)
driver / standard (pull_request) Successful in 13s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 52s
driver / lowest (pull_request) Successful in 28s
driver / test (3.13) (pull_request) Successful in 57s
driver / build (pull_request) Successful in 25s
driver / test (3.12) (pull_request) Successful in 1m9s
driver / security (pull_request) Successful in 1m0s
CI / driver (pull_request) Successful in 2m5s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 13s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 52s
driver / lowest (pull_request) Successful in 28s
driver / test (3.13) (pull_request) Successful in 57s
driver / build (pull_request) Successful in 25s
driver / test (3.12) (pull_request) Successful in 1m9s
driver / security (pull_request) Successful in 1m0s
CI / driver (pull_request) Successful in 2m5s
driver / publish (pull_request) Skipped
linux.py must not grow (python_file_lines, 1990): the power methods, their detached commands and _launch_detached move to _host_power.py (LinuxHostPowerMixin), and linux.py shrinks to 1972 lines, recorded in the baseline. Behaviour unchanged; the 480 tests pass as they were.
This commit is contained in:
1 parent
f876ac4598
commit
d0a86c1046
3 files changed
+62
-40
No files matched your search
@@ -0,0 +1,59 @@
|
||||
"""Restarting and shutting the host down, cleanly and detached (NetOrk/netork#2841).
|
||||
|
||||
``reboot_host`` and ``shutdown_host`` (napalm-device-types ``HostRebootMixin``,
|
||||
``HostShutdownMixin``) run their command as root two seconds late and detached
|
||||
from the session: the launcher's exit status comes back before the host goes
|
||||
down, and closing the session cannot take the restart or shutdown with it.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Callable
|
||||
|
||||
#: Restart the host two seconds later, detached from this session.
|
||||
_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 &'"
|
||||
|
||||
|
||||
class LinuxHostPowerMixin:
|
||||
if TYPE_CHECKING:
|
||||
_run_privileged: Callable[[str, int], str]
|
||||
|
||||
def reboot_host(self) -> None:
|
||||
"""Restart the host (``HostRebootMixin``); returns once the restart is under way.
|
||||
|
||||
:raises RuntimeError: when the host refuses -- sudo without a password,
|
||||
no ``reboot`` -- or its answer carried no exit status.
|
||||
"""
|
||||
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."""
|
||||
# Imported here: linux.py builds the driver from this mixin.
|
||||
from napalm_linux.linux import _RC_MARKER, _SUDO_PASSWORD_HINT, _split_status
|
||||
|
||||
output, status = _split_status(self._run_privileged(f"{command}; echo {_RC_MARKER}$?", 30))
|
||||
if status != 0:
|
||||
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)
|
||||
+2
-39
@@ -76,6 +76,7 @@ from netmiko import ConnectHandler
|
||||
from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutException
|
||||
|
||||
from napalm_linux._dpkg import DPKG_QUERY_COMMAND, parse_dpkg_query
|
||||
from napalm_linux._host_power import LinuxHostPowerMixin
|
||||
from napalm_linux._hostname import HostnameMixin
|
||||
from napalm_linux._local_accounts import LinuxLocalAccountsMixin
|
||||
from napalm_linux._open_port import OpenPortMixin
|
||||
@@ -143,16 +144,6 @@ _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).
|
||||
_PRIVILEGED_STDIN = (
|
||||
@@ -274,6 +265,7 @@ _USER_NAME = re.compile(r"^[a-z_][a-z0-9_.\-]{0,31}$")
|
||||
|
||||
|
||||
class LinuxDriver(
|
||||
LinuxHostPowerMixin,
|
||||
LinuxSudoHistoryMixin,
|
||||
LinuxLocalAccountsMixin,
|
||||
SnmpAgentMixin,
|
||||
@@ -573,35 +565,6 @@ class LinuxDriver(
|
||||
line, prefix = unprompted_stream(self._transport(), command, line, prefix)
|
||||
return open_stream_on_transport(self._transport(), line, stdin_prefix=prefix)
|
||||
|
||||
def reboot_host(self) -> None:
|
||||
"""Restart the host (``HostRebootMixin``); returns once the restart is under way.
|
||||
|
||||
:raises RuntimeError: when the host refuses -- sudo without a password,
|
||||
no ``reboot`` -- or its answer carried no exit status.
|
||||
"""
|
||||
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 {name} command exited with status {status}"
|
||||
if "password is required" in output:
|
||||
reason = f"{reason}\n{_SUDO_PASSWORD_HINT}"
|
||||
raise RuntimeError(reason)
|
||||
|
||||
def _run_host_status_command(self, command: str) -> str:
|
||||
"""The transport for ``HostStatusMixin.get_host_status``: read-only, no sudo."""
|
||||
return self._send(command, read_timeout=60)
|
||||
|
||||
Reference in new issue
Block a user