feat: the Supervisor as a read-only container engine
driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Successful in 18s
driver / lowest (pull_request) Successful in 23s
driver / types (pull_request) Successful in 54s
driver / test (3.12) (pull_request) Successful in 56s
driver / test (3.13) (pull_request) Successful in 49s
driver / build (pull_request) Successful in 31s
driver / security (pull_request) Successful in 1m0s
CI / driver (pull_request) Successful in 1m41s
driver / publish (pull_request) Skipped
driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Successful in 18s
driver / lowest (pull_request) Successful in 23s
driver / types (pull_request) Successful in 54s
driver / test (3.12) (pull_request) Successful in 56s
driver / test (3.13) (pull_request) Successful in 49s
driver / build (pull_request) Successful in 31s
driver / security (pull_request) Successful in 1m0s
CI / driver (pull_request) Successful in 1m41s
driver / publish (pull_request) Skipped
container_engines() names the Supervisor (engine supervisor, API
ha-supervisor) and open_container_engine("supervisor") returns read
access to its API, so netOrk can read Core, the Supervisor, its plugins
and the apps as containers (NetOrk/netork#3148). Nothing can be written
through it; without a Supervisor there is no engine.
This commit is contained in:
1 parent
a0a3dd9554
commit
7f60d43732
4 files changed
+105
-1
No files matched your search
@@ -17,3 +17,7 @@ All notable changes to this driver. The format follows
|
||||
shut down the host.
|
||||
- Discovery: Home Assistant's web page on port 8123.
|
||||
- Type label `Home Assistant OS`, so a driver picker names it.
|
||||
- Container engine access: `container_engines()` names the Supervisor (engine
|
||||
`supervisor`, API `ha-supervisor`) and `open_container_engine("supervisor")` returns
|
||||
read access to its API, through which netOrk reads Core, the Supervisor, its plugins
|
||||
and the apps as containers (NetOrk/netork#3148).
|
||||
@@ -52,6 +52,7 @@ with driver("192.0.2.21", "", "<long-lived access token>") as device:
|
||||
| `refresh_available_updates` | the Supervisor's `/reload_updates` |
|
||||
| `get_services`, `manage_service` | add-ons by slug: start, stop, restart; enable/disable sets their start at boot |
|
||||
| `get_running_backups` | the Supervisor's backup jobs in progress |
|
||||
| `container_engines`, `open_container_engine` | the Supervisor as engine `supervisor` (API `ha-supervisor`): read access to its API (`get`), through which netOrk reads Core, the Supervisor, its plugins and the apps as containers; nothing is written through it. None without a Supervisor |
|
||||
| `reboot_host`, `shutdown_host` | the host, through the Supervisor |
|
||||
| Discovery | Home Assistant's web page on port 8123 |
|
||||
|
||||
|
||||
@@ -30,9 +30,10 @@ from typing import Any, Protocol
|
||||
|
||||
from napalm.base.exceptions import ConnectionException
|
||||
from napalm.base.models import FactsDict, InterfaceDict, InterfacesIPDict
|
||||
from napalm_device_types import FingerprintRule, OSDriver, PortSpec
|
||||
from napalm_device_types import ContainerEngineUnavailable, FingerprintRule, OSDriver, PortSpec
|
||||
from napalm_device_types.models import (
|
||||
ApplyUpdatesResultDict,
|
||||
ContainerEngineDict,
|
||||
HostStatusDict,
|
||||
ServiceDict,
|
||||
UpdateDict,
|
||||
@@ -77,6 +78,10 @@ BOOT_BY_ACTION = {"enable": "auto", "disable": "manual"}
|
||||
#: The order updates run in: add-ons, then what restarts more and more of the host.
|
||||
UPDATE_ORDER = {SUPERVISOR: 1, CORE: 2, OS: 3}
|
||||
CORE_ONLY_MODEL = "Home Assistant (without Supervisor)"
|
||||
#: The container engine this driver offers: the Supervisor, which runs Core, its plugins
|
||||
#: and the apps as Docker containers and is the only way to them (NetOrk/netork#3148).
|
||||
ENGINE = "supervisor"
|
||||
ENGINE_API = "ha-supervisor"
|
||||
|
||||
|
||||
class SupervisorApi(Protocol):
|
||||
@@ -96,6 +101,24 @@ class SupervisorApi(Protocol):
|
||||
def close(self) -> None: ...
|
||||
|
||||
|
||||
class SupervisorEngine:
|
||||
"""Read access to the Supervisor, as a container engine connection (``ha-supervisor``).
|
||||
|
||||
``get(endpoint)`` is the ``data`` of the Supervisor's answer to a GET of *endpoint*.
|
||||
There is no way to write through it: what runs on the engine is only read. The
|
||||
connection is the driver's; closing this leaves it open.
|
||||
"""
|
||||
|
||||
def __init__(self, get: Callable[[str], Any]) -> None:
|
||||
self._read = get
|
||||
|
||||
def get(self, endpoint: str) -> Any:
|
||||
return self._read(endpoint)
|
||||
|
||||
def close(self) -> None:
|
||||
"""Nothing of its own to release."""
|
||||
|
||||
|
||||
class HomeAssistantDriver(RunningBackupsMixin, OSDriver):
|
||||
"""Home Assistant OS (or a Supervised install) through Home Assistant's WebSocket."""
|
||||
|
||||
@@ -238,6 +261,31 @@ class HomeAssistantDriver(RunningBackupsMixin, OSDriver):
|
||||
def get_running_backups(self) -> list[RunningBackupDict]:
|
||||
return parse_running_backups(self._get("/jobs/info"))
|
||||
|
||||
# --- container engine -------------------------------------------------------------
|
||||
|
||||
def container_engines(self) -> list[ContainerEngineDict]:
|
||||
"""
|
||||
Returns the container engines the host offers: the Supervisor, which speaks
|
||||
``ha-supervisor``; none where Home Assistant runs without one.
|
||||
|
||||
* engine (string) - "supervisor"
|
||||
* api (string) - "ha-supervisor": the Supervisor's API over Home Assistant's
|
||||
WebSocket, read through :class:`SupervisorEngine`
|
||||
"""
|
||||
try:
|
||||
self._get("/supervisor/info")
|
||||
except HomeAssistantError as exc:
|
||||
if exc.code != UNKNOWN_COMMAND:
|
||||
raise
|
||||
return []
|
||||
return [{"engine": ENGINE, "api": ENGINE_API}]
|
||||
|
||||
def open_container_engine(self, engine: str) -> SupervisorEngine:
|
||||
"""Read access to *engine*; :class:`ContainerEngineUnavailable` for any other."""
|
||||
if engine != ENGINE:
|
||||
raise ContainerEngineUnavailable(f"no way to reach container engine {engine!r}")
|
||||
return SupervisorEngine(self._get)
|
||||
|
||||
# --- add-ons ----------------------------------------------------------------------
|
||||
|
||||
def manage_service(self, name: str, action: str) -> dict[str, Any]:
|
||||
|
||||
@@ -405,3 +405,54 @@ def test_closing_a_dropped_connection_does_not_raise(
|
||||
driver.close()
|
||||
|
||||
assert driver.transport is None
|
||||
|
||||
|
||||
# --- container engine (NetOrk/netork#3148) ---------------------------------------------
|
||||
|
||||
|
||||
def test_the_supervisor_is_the_container_engine(driver: HomeAssistantDriver) -> None:
|
||||
assert driver.container_engines() == [{"engine": "supervisor", "api": "ha-supervisor"}]
|
||||
|
||||
|
||||
def test_without_a_supervisor_there_is_no_engine(fake: FakeSupervisor) -> None:
|
||||
fake.answers[("get", "/supervisor/info")] = HomeAssistantError(
|
||||
"unknown_command", "Unknown command."
|
||||
)
|
||||
device = HomeAssistantDriver("192.0.2.21", "", "tok")
|
||||
device.transport = fake
|
||||
|
||||
assert device.container_engines() == []
|
||||
|
||||
|
||||
def test_a_failed_read_of_the_engines_raises(
|
||||
driver: HomeAssistantDriver, fake: FakeSupervisor
|
||||
) -> None:
|
||||
fake.answers[("get", "/supervisor/info")] = REFUSED
|
||||
|
||||
with pytest.raises(HomeAssistantError):
|
||||
driver.container_engines()
|
||||
|
||||
|
||||
def test_the_engine_connection_only_reads(
|
||||
driver: HomeAssistantDriver, fake: FakeSupervisor
|
||||
) -> None:
|
||||
engine = driver.open_container_engine("supervisor")
|
||||
|
||||
assert engine.get("/core/info") == load("core-info")
|
||||
assert not hasattr(engine, "post")
|
||||
assert fake.calls[-1]["method"] == "get"
|
||||
|
||||
|
||||
def test_closing_the_engine_leaves_the_drivers_connection_open(
|
||||
driver: HomeAssistantDriver, fake: FakeSupervisor
|
||||
) -> None:
|
||||
driver.open_container_engine("supervisor").close()
|
||||
|
||||
assert fake.closed is False
|
||||
|
||||
|
||||
def test_an_unknown_engine_is_refused() -> None:
|
||||
from napalm_device_types import ContainerEngineUnavailable
|
||||
|
||||
with pytest.raises(ContainerEngineUnavailable):
|
||||
HomeAssistantDriver("ha", "", "tok").open_container_engine("docker")
|
||||
Reference in new issue
Block a user