feat(system-identity): read the system firmware with the identity
driver / lint (pull_request) Successful in 13s
driver / standard (pull_request) Failing after 15s
driver / types (pull_request) Successful in 52s
driver / test (3.13) (pull_request) Successful in 1m3s
driver / test (3.12) (pull_request) Successful in 1m11s
driver / lowest (pull_request) Successful in 39s
driver / build (pull_request) Successful in 28s
driver / security (pull_request) Successful in 58s
CI / driver (pull_request) Failing after 2m17s
driver / publish (pull_request) Skipped
driver / lint (pull_request) Successful in 13s
driver / standard (pull_request) Failing after 15s
driver / types (pull_request) Successful in 52s
driver / test (3.13) (pull_request) Successful in 1m3s
driver / test (3.12) (pull_request) Successful in 1m11s
driver / lowest (pull_request) Successful in 39s
driver / build (pull_request) Successful in 28s
driver / security (pull_request) Successful in 58s
CI / driver (pull_request) Failing after 2m17s
driver / publish (pull_request) Skipped
get_system_identity() also reads bios_vendor, bios_version and bios_date from /sys/class/dmi/id, in the same round trip as the SMBIOS identity. The date is given as ISO 8601; anything that is no MM/DD/YYYY reads as empty. Only the vendors' text defaults are placeholders for the firmware: a version of "1" is a version, unlike a serial of one repeated character. Output of the three identity fields alone still parses, with the firmware empty. NetOrk/netork#2518
This commit is contained in:
1 parent
e43926f1d5
commit
9ecc6823c8
5 files changed
+135
-18
No files matched your search
@@ -1443,7 +1443,7 @@ class ManagementProtocolsDict(TypedDict):
|
||||
|
||||
|
||||
class SystemIdentityDict(TypedDict):
|
||||
"""A host's SMBIOS identity, what ties it to its BMC.
|
||||
"""A host's SMBIOS identity, what ties it to its BMC, and its system firmware.
|
||||
|
||||
See :mod:`napalm_device_types.system_identity`.
|
||||
|
||||
@@ -1452,3 +1452,6 @@ class SystemIdentityDict(TypedDict):
|
||||
system_uuid: str # lowercase
|
||||
product_serial: str
|
||||
board_serial: str
|
||||
bios_vendor: str # the system firmware (BIOS or UEFI): its vendor,
|
||||
bios_version: str # its version,
|
||||
bios_date: str # its release date, YYYY-MM-DD
|
||||
@@ -1,6 +1,7 @@
|
||||
"""The SMBIOS identity of a host: system UUID, product serial, board serial.
|
||||
"""The SMBIOS identity of a host: system UUID, product serial, board serial -- and
|
||||
its system firmware (BIOS or UEFI): vendor, version, release date.
|
||||
|
||||
What ties a server to its BMC. The BMC reports the server's serial number and
|
||||
The identity is what ties a server to its BMC. The BMC reports the server's serial number and
|
||||
system UUID; the host reads the same values from its firmware tables. Both sides
|
||||
read them the same way on every Linux host, so the command, its parse and the
|
||||
placeholder filter live here once, and a driver only carries the command across.
|
||||
@@ -13,11 +14,17 @@ Matching on those would join unrelated machines, so they read as empty.
|
||||
|
||||
**Root.** ``product_uuid`` and ``product_serial`` are readable by root only; without
|
||||
it they come back empty, which is "not known", not a mismatch.
|
||||
|
||||
**The firmware** comes from the same tables in the same round trip, so a driver that
|
||||
reads the identity reads it too. It is a fact about the host, not part of its identity:
|
||||
its placeholders are the vendors' text defaults, never a short value (a version of
|
||||
``1`` is a version), and sysfs's ``MM/DD/YYYY`` release date is given as ISO 8601.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from datetime import date
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from napalm_device_types.models import SystemIdentityDict
|
||||
@@ -27,13 +34,21 @@ _BEGIN = "IDBEGIN"
|
||||
_END = "IDEND"
|
||||
_DMI = "/sys/class/dmi/id"
|
||||
|
||||
#: Reads the three files in one round trip, framed so a banner or prompt around the
|
||||
#: output cannot shift a field.
|
||||
SYSTEM_IDENTITY_COMMAND = (
|
||||
f"printf '{_BEGIN}\\n%s\\n%s\\n%s\\n{_END}\\n' "
|
||||
f'"$(cat {_DMI}/product_uuid 2>/dev/null)" '
|
||||
f'"$(cat {_DMI}/product_serial 2>/dev/null)" '
|
||||
f'"$(cat {_DMI}/board_serial 2>/dev/null)"'
|
||||
#: The files read, in the order the output carries them.
|
||||
_FILES = (
|
||||
"product_uuid",
|
||||
"product_serial",
|
||||
"board_serial",
|
||||
"bios_vendor",
|
||||
"bios_version",
|
||||
"bios_date",
|
||||
)
|
||||
|
||||
#: Reads the files in one round trip, framed so a banner or prompt around the output
|
||||
#: cannot shift a field.
|
||||
_FIELDS = "%s\\n" * len(_FILES)
|
||||
SYSTEM_IDENTITY_COMMAND = f"printf '{_BEGIN}\\n{_FIELDS}{_END}\\n' " + " ".join(
|
||||
f'"$(cat {_DMI}/{name} 2>/dev/null)"' for name in _FILES
|
||||
)
|
||||
|
||||
#: Lowercased defaults board vendors leave in place.
|
||||
@@ -80,6 +95,22 @@ def _clean(value: str) -> str:
|
||||
return "" if is_placeholder_identity(value) else value
|
||||
|
||||
|
||||
def _clean_text(value: str) -> str:
|
||||
"""A firmware text, empty for a vendor's text default; a short value stays."""
|
||||
value = value.strip()
|
||||
return "" if value.lower() in _PLACEHOLDERS else value
|
||||
|
||||
|
||||
def _iso_date(value: str) -> str:
|
||||
"""sysfs's ``MM/DD/YYYY`` as ``YYYY-MM-DD``; anything else is no date."""
|
||||
month, _, rest = value.strip().partition("/")
|
||||
day, _, year = rest.partition("/")
|
||||
try:
|
||||
return date(int(year), int(month), int(day)).isoformat()
|
||||
except ValueError:
|
||||
return ""
|
||||
|
||||
|
||||
def parse_system_identity(output: str) -> SystemIdentityDict:
|
||||
"""The identity from :data:`SYSTEM_IDENTITY_COMMAND`'s output, placeholders empty.
|
||||
|
||||
@@ -91,12 +122,16 @@ def parse_system_identity(output: str) -> SystemIdentityDict:
|
||||
end = lines.index(_END, start)
|
||||
except ValueError:
|
||||
raise ValueError("system identity: no intact IDBEGIN/IDEND frame") from None
|
||||
fields = (lines[start:end] + ["", "", ""])[:3]
|
||||
uuid, product_serial, board_serial = (_clean(f) for f in fields)
|
||||
fields = (lines[start:end] + [""] * len(_FILES))[: len(_FILES)]
|
||||
uuid, product_serial, board_serial = (_clean(f) for f in fields[:3])
|
||||
bios_vendor, bios_version, bios_date = fields[3:]
|
||||
return {
|
||||
"system_uuid": uuid.lower(),
|
||||
"product_serial": product_serial,
|
||||
"board_serial": board_serial,
|
||||
"bios_vendor": _clean_text(bios_vendor),
|
||||
"bios_version": _clean_text(bios_version),
|
||||
"bios_date": _iso_date(bios_date),
|
||||
}
|
||||
|
||||
|
||||
@@ -119,12 +154,15 @@ class SystemIdentityMixin:
|
||||
|
||||
def get_system_identity(self) -> SystemIdentityDict:
|
||||
"""
|
||||
Returns the host's SMBIOS identity, each value empty when unknown or a vendor
|
||||
placeholder:
|
||||
Returns the host's SMBIOS identity and system firmware, each value empty when
|
||||
unknown or a vendor placeholder:
|
||||
|
||||
* system_uuid (str) - lowercase
|
||||
* product_serial (str)
|
||||
* board_serial (str)
|
||||
* bios_vendor (str) - the system firmware's vendor
|
||||
* bios_version (str)
|
||||
* bios_date (str) - its release date, ``YYYY-MM-DD``
|
||||
|
||||
:raises ValueError: if the output carried no intact reading.
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user