feat(power): shut the firewall down through its firmware API (NetOrk/netork#2841) #52

Merged
christianmanivong merged 2 commits from feature/host-shutdown-2841 into main 2026-11-29 22:07:28 +00:00
5 changed files with 78 additions and 21 deletions

No files matched your search

+5
View File
@@ -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 `<system><webgui><authmode>` names, in their order, and the Local Database when it
+54
View File
@@ -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,
}
+2 -20
View File
@@ -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,26 +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 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.
+1 -1
View File
@@ -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"
+16
View File
@@ -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)."""