refactor(power): the firmware's reboot, power-off and status in a mixin of their own (NetOrk/netork#2841)
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 54s
driver / lowest (pull_request) Successful in 27s
driver / test (3.13) (pull_request) Successful in 59s
driver / test (3.12) (pull_request) Successful in 1m4s
driver / build (pull_request) Successful in 24s
driver / security (pull_request) Successful in 55s
CI / driver (pull_request) Successful in 2m4s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 54s
driver / lowest (pull_request) Successful in 27s
driver / test (3.13) (pull_request) Successful in 59s
driver / test (3.12) (pull_request) Successful in 1m4s
driver / build (pull_request) Successful in 24s
driver / security (pull_request) Successful in 55s
CI / driver (pull_request) Successful in 2m4s
driver / publish (pull_request) Skipped
opnsense.py must not grow (python_file_lines, 3022): reboot_host, shutdown_host and get_host_status, all three the firmware controller's, move to firmware_power.py (OPNsenseFirmwarePowerMixin), and opnsense.py shrinks to 3019 lines, recorded in the baseline. Behaviour unchanged; the 645 tests pass as they were.
This commit is contained in:
1 parent
94279de382
commit
02336c20ed
3 files changed
+57
-37
No files matched your search
@@ -0,0 +1,54 @@
|
||||
"""Restarting and shutting the firewall down, through its firmware API (NetOrk/netork#2841).
|
||||
|
||||
``reboot_host`` and ``shutdown_host`` (napalm-device-types ``HostRebootMixin``,
|
||||
``HostShutdownMixin``) and ``get_host_status`` (``HostStatusMixin``) are the
|
||||
firmware controller's: ``POST /api/core/firmware/reboot`` and ``…/poweroff``, which
|
||||
OPNsense has had since 16.1.15, and ``GET /api/core/firmware/status``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
|
||||
class OPNsenseFirmwarePowerMixin:
|
||||
if TYPE_CHECKING: # pragma: no cover - the driver's REST helpers
|
||||
|
||||
def _get(self, path: str) -> dict[str, Any]: ...
|
||||
|
||||
def _post(self, path: str, data: dict[str, Any] | None = None) -> dict[str, Any]: ...
|
||||
|
||||
def reboot_host(self) -> None:
|
||||
"""Restart the firewall through its firmware API (``HostRebootMixin``).
|
||||
|
||||
:raises RuntimeError: when the API refuses the request.
|
||||
"""
|
||||
try:
|
||||
self._post("/api/core/firmware/reboot")
|
||||
except Exception as exc:
|
||||
raise RuntimeError(f"The firewall refused to reboot: {exc}") from exc
|
||||
|
||||
def shutdown_host(self) -> None:
|
||||
"""Halt the firewall and power it off through its firmware API
|
||||
(``HostShutdownMixin``).
|
||||
|
||||
Calls ``POST /api/core/firmware/poweroff``, which runs configd's ``firmware
|
||||
poweroff`` detached, the clean shutdown of *Power → Power Off*, and answers
|
||||
before the firewall goes down.
|
||||
|
||||
:raises RuntimeError: when the API refuses the request.
|
||||
"""
|
||||
try:
|
||||
self._post("/api/core/firmware/poweroff")
|
||||
except Exception as exc:
|
||||
raise RuntimeError(f"The firewall refused to shut down: {exc}") from exc
|
||||
|
||||
def get_host_status(self) -> dict[str, Any]:
|
||||
"""Whether the firewall needs a reboot to finish an update; it does not
|
||||
patch itself as far as netOrk can tell."""
|
||||
pending = self._get("/api/core/firmware/status").get("needs_reboot") == "1"
|
||||
return {
|
||||
"reboot_required": pending,
|
||||
"reboot_reason": "the firmware status reports a pending reboot" if pending else None,
|
||||
"auto_updates": None,
|
||||
}
|
||||
@@ -63,6 +63,7 @@ from requests.exceptions import RequestException
|
||||
|
||||
from napalm_opnsense.api_endpoint import ALIVE_TIMEOUT, api_endpoint
|
||||
from napalm_opnsense.config_revisions import OPNsenseConfigRevisionsMixin
|
||||
from napalm_opnsense.firmware_power import OPNsenseFirmwarePowerMixin
|
||||
from napalm_opnsense.ha import config_sync, dhcp_ha
|
||||
from napalm_opnsense.kea_reservations import (
|
||||
lease_held,
|
||||
@@ -109,6 +110,7 @@ def _search_rows(data: Any, search: str) -> list[dict[str, Any]]:
|
||||
|
||||
|
||||
class OPNsenseDriver(
|
||||
OPNsenseFirmwarePowerMixin,
|
||||
OPNsenseConfigRevisionsMixin,
|
||||
OPNsenseLoginAuthMixin,
|
||||
OPNsenseTimeSyncMixin,
|
||||
@@ -2182,42 +2184,6 @@ class OPNsenseDriver(
|
||||
),
|
||||
}
|
||||
|
||||
def reboot_host(self) -> None:
|
||||
"""Restart the firewall through its firmware API (``HostRebootMixin``).
|
||||
|
||||
:raises RuntimeError: when the API refuses the request.
|
||||
"""
|
||||
try:
|
||||
self._post("/api/core/firmware/reboot")
|
||||
except Exception as exc:
|
||||
raise RuntimeError(f"The firewall refused to reboot: {exc}") from exc
|
||||
|
||||
def shutdown_host(self) -> None:
|
||||
"""Halt the firewall and power it off through its firmware API
|
||||
(``HostShutdownMixin``).
|
||||
|
||||
Calls ``POST /api/core/firmware/poweroff``, which OPNsense has had since
|
||||
16.1.15, next to ``firmware/reboot``. It runs configd's ``firmware poweroff``
|
||||
detached, the clean shutdown of *Power → Power Off*, and answers before the
|
||||
firewall goes down.
|
||||
|
||||
:raises RuntimeError: when the API refuses the request.
|
||||
"""
|
||||
try:
|
||||
self._post("/api/core/firmware/poweroff")
|
||||
except Exception as exc:
|
||||
raise RuntimeError(f"The firewall refused to shut down: {exc}") from exc
|
||||
|
||||
def get_host_status(self) -> dict[str, Any]:
|
||||
"""Whether the firewall needs a reboot to finish an update; it does not
|
||||
patch itself as far as netOrk can tell."""
|
||||
pending = self._get("/api/core/firmware/status").get("needs_reboot") == "1"
|
||||
return {
|
||||
"reboot_required": pending,
|
||||
"reboot_reason": "the firmware status reports a pending reboot" if pending else None,
|
||||
"auto_updates": None,
|
||||
}
|
||||
|
||||
def apply_updates(self, packages: list[str]) -> dict[str, Any]:
|
||||
"""Start the firmware upgrade of *packages*, or of everything for none.
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@
|
||||
"napalm_opnsense.resolver_mixin"
|
||||
],
|
||||
"python_file_lines": {
|
||||
"napalm_opnsense/opnsense.py": 3022
|
||||
"napalm_opnsense/opnsense.py": 3019
|
||||
},
|
||||
"untyped_public": [
|
||||
"napalm_opnsense/opnsense.py::OPNsenseDriver.get_snmp_config"
|
||||
|
||||
Reference in new issue
Block a user