feat: apt's errors come through, unreadable sources raise PackageSourcesUnreadable, 4.57.0 (NetOrk/netork#3087)
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 16s
driver / lowest (pull_request) Successful in 45s
driver / types (pull_request) Successful in 1m9s
driver / test (3.12) (pull_request) Successful in 1m19s
driver / test (3.13) (pull_request) Successful in 1m19s
driver / build (pull_request) Successful in 37s
driver / security (pull_request) Successful in 1m10s
CI / driver (pull_request) Successful in 2m14s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 16s
driver / lowest (pull_request) Successful in 45s
driver / types (pull_request) Successful in 1m9s
driver / test (3.12) (pull_request) Successful in 1m19s
driver / test (3.13) (pull_request) Successful in 1m19s
driver / build (pull_request) Successful in 37s
driver / security (pull_request) Successful in 1m10s
CI / driver (pull_request) Successful in 2m14s
driver / publish (pull_request) Skipped
One bad apt source line, two Signed-By for one repository, makes apt refuse every source. The update read then failed with "exit status 100" and nothing else, because APT_UPGRADABLE_COMMAND threw stderr away. The command now passes apt's E: lines on, marked __APT_E:, and still keeps its warnings out. parse_apt_upgradable raises PackageSourcesUnreadable, a ValueError, with apt's errors when apt could not read its sources, and names them after the exit status for any other failure.
This commit is contained in:
1 parent
6eec94830a
commit
9efc3275b1
6 files changed
+111
-7
No files matched your search
@@ -499,6 +499,7 @@ from napalm_device_types.time_sync import (
|
||||
from napalm_device_types.time_sync_posix import PosixTimeSyncMixin, parse_time_sync
|
||||
from napalm_device_types.updates import (
|
||||
PackageIndexMissing,
|
||||
PackageSourcesUnreadable,
|
||||
UpdateHistoryAction,
|
||||
UpdateHistoryDict,
|
||||
UpdateHistoryPackageDict,
|
||||
@@ -813,6 +814,7 @@ __all__ = [
|
||||
"SystemdUnavailable",
|
||||
"parse_systemd_services",
|
||||
"PackageIndexMissing",
|
||||
"PackageSourcesUnreadable",
|
||||
"UpdateMixin",
|
||||
"VIRTUAL_IPS_COMMAND",
|
||||
"VirtualIpsMixin",
|
||||
|
||||
@@ -18,13 +18,23 @@ import re
|
||||
|
||||
from napalm_device_types.models import UpdateDict
|
||||
from napalm_device_types.terminal import strip_terminal_codes
|
||||
from napalm_device_types.updates import PackageSourcesUnreadable
|
||||
|
||||
#: Read-only, no root needed, in a fixed language so the parse holds, and
|
||||
#: through a pipe: without a terminal apt draws no progress and no terminal
|
||||
#: codes, one of which (``ESC >``) a screen-scraping transport took for a shell
|
||||
#: prompt and stopped reading at. Its exit status is printed inside the group,
|
||||
#: so it is apt's, not cat's.
|
||||
APT_UPGRADABLE_COMMAND = "{ LC_ALL=C apt list --upgradable 2>/dev/null; echo __APT_RC=$?; } | cat"
|
||||
#: so it is apt's, not cat's. Its errors (``E:``) come through marked
|
||||
#: ``__APT_E:``, so a failed read says why (netOrk #3087); the rest of what it
|
||||
#: writes to stderr, its warnings, stays out. Plain ``sh``: stdout goes round
|
||||
#: the pipe on descriptor 3.
|
||||
APT_UPGRADABLE_COMMAND = (
|
||||
"{ { LC_ALL=C apt list --upgradable 2>&1 1>&3 3>&-; echo __APT_RC=$? 1>&3; }"
|
||||
" | sed -n 's/^E: /__APT_E: /p'; } 3>&1 | cat"
|
||||
)
|
||||
|
||||
#: apt's last word when it cannot read its sources, in ``LC_ALL=C``.
|
||||
_SOURCES_UNREADABLE = "The list of sources could not be read."
|
||||
|
||||
#: Read-only. Lists the packages that a pending security advisory covers. A
|
||||
#: failed dnf prints nothing, the same as no advisories, so its exit status is
|
||||
@@ -52,6 +62,7 @@ DNF_INSTALLED_CVES_COMMAND = (
|
||||
_APT_LINE = re.compile(r"^(\S+)/(\S+)\s+(\S+)\s+\S+\s+\[upgradable from:\s+(\S+)\]")
|
||||
_SECURITY_SUITE = "-security"
|
||||
_APT_STATUS = re.compile(r"^__APT_RC=(\d+)\s*$", re.MULTILINE)
|
||||
_APT_ERROR = re.compile(r"^__APT_E: (.*?)\s*$", re.MULTILINE)
|
||||
_DNF_STATUS = re.compile(r"^__DNF_RC=(\d+)\s*$", re.MULTILINE)
|
||||
_CVE_ID = re.compile(r"^CVE-\d{4}-\d{4,}$")
|
||||
|
||||
@@ -71,14 +82,19 @@ def _joined_lines(output: str) -> list[str]:
|
||||
def _listing(output: str, status: re.Pattern[str], what: str) -> str:
|
||||
"""The listing without its exit status, or ``ValueError`` when the command
|
||||
failed or the output was cut short -- "could not read" must never look like
|
||||
"nothing pending"."""
|
||||
"nothing pending". apt's marked errors go into the failure's message, and
|
||||
unreadable sources raise :exc:`PackageSourcesUnreadable`."""
|
||||
text = strip_terminal_codes(output)
|
||||
statuses = status.findall(text)
|
||||
if not statuses:
|
||||
raise ValueError(f"{what} reported no exit status; the output was cut short")
|
||||
errors = [e for e in _APT_ERROR.findall("\n".join(_joined_lines(text))) if e]
|
||||
if statuses[-1] != "0":
|
||||
raise ValueError(f"{what} failed with exit status {statuses[-1]}")
|
||||
return status.sub("", text)
|
||||
if _SOURCES_UNREADABLE in errors:
|
||||
raise PackageSourcesUnreadable("; ".join(errors))
|
||||
said = f": {'; '.join(errors)}" if errors else ""
|
||||
raise ValueError(f"{what} failed with exit status {statuses[-1]}{said}")
|
||||
return _APT_ERROR.sub("", status.sub("", text))
|
||||
|
||||
|
||||
def parse_apt_upgradable(output: str) -> list[UpdateDict]:
|
||||
@@ -87,7 +103,8 @@ def parse_apt_upgradable(output: str) -> list[UpdateDict]:
|
||||
A package listed for several architectures (``libc6`` for amd64 and i386)
|
||||
is one entry; it counts as a security update if any of its lines does.
|
||||
|
||||
:raises ValueError: when apt failed or its exit status never arrived.
|
||||
:raises PackageSourcesUnreadable: when apt could not read its list of sources.
|
||||
:raises ValueError: when apt failed otherwise or its exit status never arrived.
|
||||
"""
|
||||
by_name: dict[str, UpdateDict] = {}
|
||||
for line in _joined_lines(_listing(output, _APT_STATUS, "apt list --upgradable")):
|
||||
|
||||
@@ -68,6 +68,16 @@ class PackageIndexMissing(RuntimeError):
|
||||
"""
|
||||
|
||||
|
||||
class PackageSourcesUnreadable(ValueError):
|
||||
"""apt could not read its list of sources, so it reads no package at all.
|
||||
|
||||
One bad line -- two ``Signed-By`` for one repository, say -- makes apt refuse
|
||||
every source, until someone fixes the line (netOrk #3087). The message is
|
||||
what apt said, its errors joined by ``"; "``. A ``ValueError``, as every
|
||||
failed read of :func:`parse_apt_upgradable` is.
|
||||
"""
|
||||
|
||||
|
||||
class UpdateMixin:
|
||||
if TYPE_CHECKING:
|
||||
|
||||
|
||||
Reference in new issue
Block a user