From 94279de3823a4b815d2dce49dfae91dcd4a43e0b Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 29 Nov 2026 11:32:33 +0100 Subject: [PATCH 1/2] feat(power): shut the firewall down through its firmware API (NetOrk/netork#2841) shutdown_host() posts /api/core/firmware/poweroff, the clean halt with power off that OPNsense has had since 16.1.15, next to the firmware/reboot that reboot_host() uses. A refused request raises RuntimeError with the firewall's answer. --- CHANGELOG.md | 5 +++++ napalm_opnsense/opnsense.py | 16 ++++++++++++++++ tests/unit/test_updates.py | 16 ++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b5d5cd..c68f033 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ All notable changes to this driver. The format follows ## [Unreleased] ### Added +- Shutting the firewall down (NetOrk/netork#2841): `shutdown_host()` from + napalm-device-types' `HostShutdownMixin` halts the firewall and powers it off through + `POST /api/core/firmware/poweroff` (OPNsense 16.1.15 and later), the clean shutdown of + *Power → Power Off*, as `reboot_host()` restarts it through `firmware/reboot`. A + refused request raises `RuntimeError` with the firewall's answer. - How people log in (NetOrk/netork#662), in napalm-device-types 4.50.0's shapes, read from config.xml over `/api/core/backup/download/this`. `get_web_login_auth()` returns the auth servers `` names, in their order, and the Local Database when it diff --git a/napalm_opnsense/opnsense.py b/napalm_opnsense/opnsense.py index 3dba009..3bb65ab 100644 --- a/napalm_opnsense/opnsense.py +++ b/napalm_opnsense/opnsense.py @@ -2192,6 +2192,22 @@ class OPNsenseDriver( 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.""" diff --git a/tests/unit/test_updates.py b/tests/unit/test_updates.py index b8c8b4f..3a005e9 100644 --- a/tests/unit/test_updates.py +++ b/tests/unit/test_updates.py @@ -140,6 +140,22 @@ class TestRebootHost: driver.reboot_host() +class TestShutdownHost: + """``shutdown_host`` halts and powers the firewall off through its firmware API + (netOrk #2841).""" + + def test_the_firmware_api_is_asked(self, driver): + with patch.object(driver, "_post", return_value={"status": "ok"}) as post: + driver.shutdown_host() + + post.assert_called_once_with("/api/core/firmware/poweroff") + + def test_a_refusal_is_raised_with_the_answer(self, driver): + with patch.object(driver, "_post", side_effect=ConnectionError("403 Forbidden")): + with pytest.raises(RuntimeError, match="refused to shut down: 403 Forbidden"): + driver.shutdown_host() + + def test_no_warnings_of_its_own(): """The pending updates are get_available_updates; the caller names updates_available from them (netOrk #1111).""" -- 2.54.1 From 02336c20ed62f6b0d11d56eeb999697b7cbd2581 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 29 Nov 2026 23:00:57 +0100 Subject: [PATCH 2/2] refactor(power): the firmware's reboot, power-off and status in a mixin of their own (NetOrk/netork#2841) 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. --- napalm_opnsense/firmware_power.py | 54 +++++++++++++++++++++++++++++++ napalm_opnsense/opnsense.py | 38 ++-------------------- tests/quality_baseline.json | 2 +- 3 files changed, 57 insertions(+), 37 deletions(-) create mode 100644 napalm_opnsense/firmware_power.py diff --git a/napalm_opnsense/firmware_power.py b/napalm_opnsense/firmware_power.py new file mode 100644 index 0000000..82dc202 --- /dev/null +++ b/napalm_opnsense/firmware_power.py @@ -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, + } diff --git a/napalm_opnsense/opnsense.py b/napalm_opnsense/opnsense.py index 3bb65ab..3cb7269 100644 --- a/napalm_opnsense/opnsense.py +++ b/napalm_opnsense/opnsense.py @@ -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. diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index 99918ad..5da3d22 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -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" -- 2.54.1