feat(power): shut the firewall down through its firmware API (NetOrk/netork#2841)
driver / standard (pull_request) Failing after 14s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 50s
driver / test (3.12) (pull_request) Successful in 1m2s
driver / lowest (pull_request) Successful in 32s
driver / test (3.13) (pull_request) Successful in 1m2s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 55s
CI / driver (pull_request) Failing after 1m59s
driver / publish (pull_request) Skipped

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.
This commit is contained in:
Christian Manivong committed 2026-11-29 11:32:33 +01:00
1 parent 2374d57b7d
commit 94279de382
3 files changed
+37

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
+16
View File
@@ -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."""
+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)."""