feat: guest uid, credential expiry and update history contracts (NetOrk/netork#2728)
VMDict.uid is an identity of the guest that survives a migration, a restore and a changed vmid. CredentialExpiryMixin reads when the credential a driver logs in with expires. UpdateMixin.get_update_history lists the package manager's past transactions. resume_vm documents that the guest clock is set after a resume where the platform can.
This commit is contained in:
1 parent
818016b717
commit
07fc7d0dd3
7 files changed
+249
-1
No files matched your search
@@ -41,6 +41,7 @@ instead of being restated on every role that happens to need it:
|
||||
* :class:`~napalm_device_types.config_revisions.ConfigRevisionsMixin`
|
||||
* :class:`~napalm_device_types.config_sync.ConfigSyncMixin`
|
||||
* :class:`~napalm_device_types.container_engine.ContainerEngineMixin`
|
||||
* :class:`~napalm_device_types.credential_expiry.CredentialExpiryMixin`
|
||||
* :class:`~napalm_device_types.dhcp.DhcpServerMixin`
|
||||
* :class:`~napalm_device_types.firewall_rules.FirewallRuleMixin`
|
||||
* :class:`~napalm_device_types.health_metrics.HealthMetricsMixin`
|
||||
@@ -143,6 +144,11 @@ from napalm_device_types.container_engine import (
|
||||
ContainerEngineMixin,
|
||||
ContainerEngineUnavailable,
|
||||
)
|
||||
from napalm_device_types.credential_expiry import (
|
||||
CredentialExpiryDict,
|
||||
CredentialExpiryMixin,
|
||||
CredentialKind,
|
||||
)
|
||||
from napalm_device_types.dhcp import DhcpApplyError, DhcpServerMixin, normalize_cidr, normalize_mac
|
||||
from napalm_device_types.dns_forward import DnsForwardMixin
|
||||
from napalm_device_types.firewall import FirewallDriver
|
||||
@@ -226,6 +232,9 @@ from napalm_device_types.models import (
|
||||
PowerAction,
|
||||
PowerStateDict,
|
||||
SnmpAgentSettingsDict,
|
||||
UpdateHistoryAction,
|
||||
UpdateHistoryDict,
|
||||
UpdateHistoryPackageDict,
|
||||
UpdateProgressDict,
|
||||
)
|
||||
from napalm_device_types.nat_vpn import NatVpnMixin
|
||||
@@ -406,6 +415,12 @@ __all__ = [
|
||||
"OspfInterfaceDict",
|
||||
"OspfMixin",
|
||||
"UpdateProgressDict",
|
||||
"UpdateHistoryAction",
|
||||
"UpdateHistoryDict",
|
||||
"UpdateHistoryPackageDict",
|
||||
"CredentialExpiryDict",
|
||||
"CredentialExpiryMixin",
|
||||
"CredentialKind",
|
||||
"FirmwareOfferMixin",
|
||||
"FirmwareUpgradeMixin",
|
||||
"LicenseDict",
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
"""When the credential a driver logs in with stops working.
|
||||
|
||||
An API token can be given an expiry, and a platform can require one (Proxmox VE
|
||||
9.3's datacenter ``token-policy``). Once it has passed, every login fails with
|
||||
an authentication error, and an application that polls the device learns of it
|
||||
only then. A driver that can read its own credential's expiry says so here, so
|
||||
the application can warn before, the way it warns of an expiring certificate.
|
||||
|
||||
Which credential is read is the driver's: the one this connection logged in
|
||||
with, never another.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, Literal
|
||||
|
||||
from typing_extensions import TypedDict
|
||||
|
||||
#: ``"api-token"``: a token of an account; ``"account"``: the account a password logs in to.
|
||||
CredentialKind = Literal["api-token", "account"]
|
||||
|
||||
|
||||
class CredentialExpiryDict(TypedDict):
|
||||
"""The credential this connection logged in with, and when it expires."""
|
||||
|
||||
kind: CredentialKind
|
||||
#: The credential as the platform names it, e.g. ``"netork@pve!poll"``.
|
||||
name: str
|
||||
#: Unix seconds; ``None`` when it never expires.
|
||||
expires: int | None
|
||||
|
||||
|
||||
class CredentialExpiryMixin:
|
||||
"""Reads the expiry of the credential the driver logged in with.
|
||||
|
||||
Declared under ``if TYPE_CHECKING``: a contract, not a placeholder.
|
||||
"""
|
||||
|
||||
if TYPE_CHECKING:
|
||||
|
||||
def get_credential_expiry(self) -> CredentialExpiryDict:
|
||||
"""
|
||||
The credential this connection logged in with, and when it expires.
|
||||
|
||||
``expires`` is ``None`` for one that never expires. A credential
|
||||
whose expiry has passed cannot log in, so a driver that returns at
|
||||
all reports one that still works.
|
||||
|
||||
:raises PermissionError: The platform will not tell this credential
|
||||
its own expiry (a token whose privileges do not reach it). Not a
|
||||
fault of the connection; a caller then knows nothing either way.
|
||||
:raises RuntimeError: The platform could not be read.
|
||||
|
||||
Example::
|
||||
|
||||
{"kind": "api-token", "name": "netork@pve!poll", "expires": 1767225600}
|
||||
"""
|
||||
...
|
||||
@@ -82,6 +82,10 @@ class HypervisorDriver(PackageManagementMixin, HealthMetricsMixin, DeviceTypeDri
|
||||
* memory_usage (int) - current RAM usage in megabytes
|
||||
* uptime (int) - uptime in seconds (0 if not running)
|
||||
* node (string) - cluster node this VM lives on (empty string for standalone)
|
||||
* uid (string, optional) - an identity of the guest that survives a
|
||||
migration, a restore and a changed ``vmid`` (Proxmox VE 9.3's ``uid``,
|
||||
VMware's instance UUID). Left out where the platform has none, or has
|
||||
not given this guest one yet; a caller then knows it by ``vmid`` alone.
|
||||
|
||||
Example::
|
||||
|
||||
@@ -272,6 +276,11 @@ class HypervisorDriver(PackageManagementMixin, HealthMetricsMixin, DeviceTypeDri
|
||||
counterpart of :meth:`suspend_vm`. The method blocks until the
|
||||
hypervisor took the request.
|
||||
|
||||
The guest's clock is set to the host's time after it resumes, where the
|
||||
platform can do so (Proxmox VE: through the guest agent): a guest that
|
||||
stood still for hours would otherwise run behind. No caller wants
|
||||
otherwise, so it is the driver's to do, not a choice.
|
||||
|
||||
Optional, like every power action: a driver whose platform cannot
|
||||
resume a guest does not implement it, and a caller then offers no
|
||||
resume. A driver that can resume some guests but not others (a
|
||||
|
||||
@@ -835,6 +835,9 @@ class VMDict(TypedDict):
|
||||
memory_usage: int
|
||||
uptime: int
|
||||
node: str
|
||||
# An identity of the guest that survives migration, restore and a changed
|
||||
# ``vmid``; absent where the platform has none or has not assigned it yet.
|
||||
uid: NotRequired[str]
|
||||
|
||||
|
||||
class VMPassthroughDict(TypedDict):
|
||||
@@ -1141,6 +1144,39 @@ class UpdateProgressDict(TypedDict):
|
||||
log: str # the device's own log of it, whole; a caller follows it by line count
|
||||
|
||||
|
||||
UpdateHistoryAction = Literal[
|
||||
"install", "reinstall", "upgrade", "downgrade", "remove", "purge", "disappear"
|
||||
]
|
||||
|
||||
|
||||
class UpdateHistoryPackageDict(TypedDict):
|
||||
"""One package a past transaction changed (``UpdateHistoryDict.packages``)."""
|
||||
|
||||
name: str
|
||||
action: UpdateHistoryAction # "disappear": dpkg dropped it, its files taken by another
|
||||
old_version: str | None # None where nothing was installed before
|
||||
new_version: str | None # None where nothing is installed after
|
||||
|
||||
|
||||
class UpdateHistoryDict(TypedDict):
|
||||
"""A past run of the host's package manager (``UpdateMixin.get_update_history``).
|
||||
|
||||
A fact of the host's own log: what it changed, when, and who asked, also for
|
||||
a run that no application of the caller started.
|
||||
"""
|
||||
|
||||
#: Stable for the same transaction across reads, unique on the host: apt's
|
||||
#: start time with its index among those starting in the same second, dnf's
|
||||
#: transaction ID.
|
||||
id: str
|
||||
start: int # unix seconds
|
||||
end: int | None # None: still running, or interrupted
|
||||
packages: list[UpdateHistoryPackageDict]
|
||||
requested_by: str | None # the user behind sudo, pkexec or PackageKit; None when unknown
|
||||
command: str | None # the command line, where the log keeps it
|
||||
error: str | None # the error the run ended with; None for none
|
||||
|
||||
|
||||
class DnsForwardDict(TypedDict):
|
||||
"""A domain the device's resolver forwards to another server
|
||||
(``DnsForwardMixin.get_dns_forwards``)."""
|
||||
|
||||
@@ -14,7 +14,12 @@ from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from napalm_device_types.models import ApplyUpdatesResultDict, UpdateDict, UpdateProgressDict
|
||||
from napalm_device_types.models import (
|
||||
ApplyUpdatesResultDict,
|
||||
UpdateDict,
|
||||
UpdateHistoryDict,
|
||||
UpdateProgressDict,
|
||||
)
|
||||
|
||||
|
||||
class PackageIndexMissing(RuntimeError):
|
||||
@@ -83,6 +88,44 @@ class UpdateMixin:
|
||||
"""
|
||||
...
|
||||
|
||||
def get_update_history(self, since: int | None = None) -> list[UpdateHistoryDict]:
|
||||
"""
|
||||
The transactions the host's package manager ran, newest first: what
|
||||
each changed, when, and who asked for it -- also those no caller of
|
||||
this driver started (an operator's ``apt upgrade``, unattended-upgrades).
|
||||
|
||||
:param since: Unix seconds; only transactions that started at or after
|
||||
it. ``None`` for as far back as the host's log reaches.
|
||||
|
||||
**An empty list means no transaction since.** A reader that cannot
|
||||
read raises, as :meth:`get_available_updates` does: a caller keeps what
|
||||
it read before, and an empty list for "don't know" would read as a
|
||||
host that never changed. ``NotImplementedError`` where the package
|
||||
manager keeps no history.
|
||||
|
||||
Example::
|
||||
|
||||
[
|
||||
{
|
||||
"id": "1764158400-0",
|
||||
"start": 1764158400,
|
||||
"end": 1764158460,
|
||||
"packages": [
|
||||
{
|
||||
"name": "openssh-server",
|
||||
"action": "upgrade",
|
||||
"old_version": "1:9.2p1-2+deb12u1",
|
||||
"new_version": "1:9.2p1-2+deb12u2",
|
||||
},
|
||||
],
|
||||
"requested_by": "admin",
|
||||
"command": "apt-get -y upgrade",
|
||||
"error": None,
|
||||
},
|
||||
]
|
||||
"""
|
||||
...
|
||||
|
||||
def refresh_available_updates(self) -> dict[str, Any]:
|
||||
"""
|
||||
Refreshes the host's package index, so that :meth:`get_available_updates`
|
||||
|
||||
Reference in new issue
Block a user