From 271af3b0716082bdb43dbf50825a108ab82afd60 Mon Sep 17 00:00:00 2001 From: patrick Date: Sat, 17 Oct 2026 12:03:02 -0400 Subject: [PATCH] Show Ubuntu phased updates separately from available updates apt lists updates deferred by phasing in an indented block rather than as Inst lines, so they were already excluded from the counts but invisible. The agent now parses that block into phased_packages; the dashboard shows "+N phased" in the Updates column (with a tooltip) and the list on the host page. Phasing never affects status: Ubuntu doesn't phase security updates, and there's nothing to act on until apt offers the update. Run the apt simulation with LC_ALL=C since we parse its messages (the systemd unit already runs in the C locale; manual --print may not). Agent and server 0.3.0. Co-Authored-By: Claude Opus 5.5 --- README.md | 6 ++++++ agent/kernelwatch-agent | 15 +++++++++++--- server/kernelwatch/__init__.py | 4 ++-- server/kernelwatch/report.py | 1 + server/kernelwatch/web.py | 9 ++++++++- tests/test_agent.py | 36 ++++++++++++++++++++++++++++++++++ tests/test_status.py | 4 ++++ tests/test_web.py | 8 +++++++- 8 files changed, 76 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3c6e93b..25e4f3d 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,12 @@ How the agent works it out: no locks and writes no cache files. `dist-upgrade` is used so that new-ABI kernel packages pulled in by the meta-package show up. Lines whose origin contains `-security` count as security updates. +- **Phased updates** (Ubuntu only): Ubuntu rolls some updates out gradually and + holds them back from a share of machines at first. apt doesn't offer these to + the host yet, so they aren't counted as available updates. They appear as + `+N phased` in the Updates column, with the package list on the host page. + They never affect status, because Ubuntu doesn't phase security updates and + there's nothing to act on until apt offers them. - **Package list freshness**: the mtime of apt's update stamps. The agent **doesn't run `apt update`** itself; it relies on the stock `apt-daily.timer`. If that's disabled, the host shows `update` with "package lists N days old". diff --git a/agent/kernelwatch-agent b/agent/kernelwatch-agent index d70ad2b..16e8797 100755 --- a/agent/kernelwatch-agent +++ b/agent/kernelwatch-agent @@ -6,7 +6,7 @@ # rules can change without redeploying agents. set -euo pipefail -AGENT_VERSION=0.2.0 +AGENT_VERSION=0.3.0 # Prefix for every file the agent reads. Only the test suite sets this. ROOT=${KERNELWATCH_ROOT:-} @@ -153,15 +153,23 @@ read_needrestart() { # Simulated dist-upgrade. Reads only: no locks, no cache files written. # dist-upgrade (not upgrade) so new-ABI kernel packages pulled in by the -# meta-package show up. +# meta-package show up. C locale because we parse apt's messages. apt_simulation() { - apt-get -s -q \ + LC_ALL=C apt-get -s -q \ -o Debug::NoLocking=true \ -o Dir::Cache::pkgcache= \ -o Dir::Cache::srcpkgcache= \ dist-upgrade 2>/dev/null || true } +# Packages Ubuntu is holding back from this host for now (phased updates). apt +# lists them in an indented block instead of as Inst lines, so they're not in +# the upgradable counts. Debian doesn't phase updates. +phased_packages() { + sed -n '/deferred due to phasing:$/,/^[^ ]/{/^ /p}' | + tr -s ' ' '\n' | sed '/^$/d' | sort -u +} + # Seconds since apt last refreshed its package lists, or empty if unknown. apt_lists_age() { local newest=0 f m @@ -223,6 +231,7 @@ build_report() { printf '"reboot_required_pkgs":%s,' "$(json_str_array <<<"$reboot_pkgs")" printf '"upgradable_count":%s,' "$(json_int "$(grep -c . <<<"$inst" || true)")" printf '"security_count":%s,' "$(json_int "$(grep -ci 'security' <<<"$inst" || true)")" + printf '"phased_packages":%s,' "$(phased_packages <<<"$sim" | json_str_array)" printf '"kernel_upgradable":%s,' "$(awk '$2 ~ /^linux-image-/ { print $2 }' <<<"$inst" | json_str_array)" printf '"apt_lists_age_seconds":%s,' "$(json_int "$(apt_lists_age)")" printf '"uptime_seconds":%s' "$(json_int "$(uptime_seconds)")" diff --git a/server/kernelwatch/__init__.py b/server/kernelwatch/__init__.py index deacf42..419fc53 100644 --- a/server/kernelwatch/__init__.py +++ b/server/kernelwatch/__init__.py @@ -1,7 +1,7 @@ """kernelwatch: fleet kernel and reboot status dashboard.""" -__version__ = "0.2.0" +__version__ = "0.3.0" # Version of agent/kernelwatch-agent shipped alongside this server; older agents # are noted on the dashboard. tests/test_status.py checks the two stay in sync. -AGENT_VERSION = "0.2.0" +AGENT_VERSION = "0.3.0" diff --git a/server/kernelwatch/report.py b/server/kernelwatch/report.py index 8fb1972..b0f5989 100644 --- a/server/kernelwatch/report.py +++ b/server/kernelwatch/report.py @@ -34,6 +34,7 @@ FIELDS: dict[str, str] = { "upgradable_count": "int", "security_count": "int", "kernel_upgradable": "strlist", + "phased_packages": "strlist", "apt_lists_age_seconds": "int", "uptime_seconds": "int", } diff --git a/server/kernelwatch/web.py b/server/kernelwatch/web.py index c571910..f2010b0 100644 --- a/server/kernelwatch/web.py +++ b/server/kernelwatch/web.py @@ -420,7 +420,13 @@ class Handler(BaseHTTPRequestHandler): name = h["host"] sec = r.get("security_count") total = r.get("upgradable_count") + phased = len(r.get("phased_packages") or []) updates = "–" if total is None else f"{total}" + (f" ({sec} sec)" if sec else "") + updates_title = "" + if phased: + updates += f" +{phased} phased" + updates_title = (f"{phased} more held back by Ubuntu's phased rollout: not offered to " + "this host yet, never security updates") services = services_summary(r) return ( "" @@ -429,7 +435,7 @@ class Handler(BaseHTTPRequestHandler): f"{e(r.get('os_pretty_name') or '–')}" f"{e(r.get('kernel_running') or '–')}" f"{e(r.get('kernel_newest_installed') or '–')}" - f"{e(updates)}" + f'{e(updates)}' f'{e(services[0])}' f"{e(duration(r.get('uptime_seconds')))}" f'{e(ago(h["received_at"], now))}' @@ -480,6 +486,7 @@ class Handler(BaseHTTPRequestHandler): field("Kernel packages upgradable", r.get("kernel_upgradable"), code=True), field("Updates pending", r.get("upgradable_count")), field("Security updates", r.get("security_count")), + field("Deferred by phasing", r.get("phased_packages"), code=True), field("Reboot-required packages", r.get("reboot_required_pkgs"), code=True), field("Services needing restart", services_summary(r)[1]), field("Sessions needing restart", r.get("sessions_need_restart")), diff --git a/tests/test_agent.py b/tests/test_agent.py index aba2377..b26a504 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -98,6 +98,7 @@ class AgentReportTest(AgentCase): self.assertEqual(r["upgradable_count"], 3) self.assertEqual(r["security_count"], 2) self.assertEqual(r["kernel_upgradable"], ["linux-image-6.12.60+deb13-amd64"]) + self.assertEqual(r["phased_packages"], [], "Debian: no phasing") self.assertAlmostEqual(r["apt_lists_age_seconds"], 7200, delta=60) self.assertEqual(r["uptime_seconds"], 93784) self.assertIsNone(r["services_need_restart"], "no needrestart results: unknown") @@ -168,6 +169,41 @@ class AgentReportTest(AgentCase): self.assertEqual(r["security_count"], 0) self.assertEqual(r["kernel_upgradable"], []) + def test_ubuntu_phased_updates(self): + # Shape of real `apt-get -s dist-upgrade` output on 24.04 with phasing. + stub(self.bin, "apt-get", """cat <<'X' +NOTE: This is only a simulation! + apt-get needs root privileges for real execution. + Keep also in mind that locking is deactivated, + so don't depend on the relevance to the real current situation! +Reading package lists... +Building dependency tree... +Reading state information... +Calculating upgrade... +The following upgrades have been deferred due to phasing: + libnss-systemd libpam-systemd libsystemd-shared libsystemd0 libudev1 systemd + systemd-resolved udev +The following packages will be upgraded: + curl libcurl4t64 +2 upgraded, 0 newly installed, 0 to remove and 8 not upgraded. +Inst libcurl4t64 [8.5.0-2ubuntu10.6] (8.5.0-2ubuntu10.7 Ubuntu:24.04/noble-security [amd64]) +Inst curl [8.5.0-2ubuntu10.6] (8.5.0-2ubuntu10.7 Ubuntu:24.04/noble-security [amd64]) +Conf libcurl4t64 (8.5.0-2ubuntu10.7 Ubuntu:24.04/noble-security [amd64]) +X +""") + r = self.report() + self.assertEqual(r["upgradable_count"], 2) + self.assertEqual(r["security_count"], 2) + self.assertEqual(r["phased_packages"], sorted( + ["libnss-systemd", "libpam-systemd", "libsystemd-shared", "libsystemd0", "libudev1", + "systemd", "systemd-resolved", "udev"])) + + def test_apt_runs_in_c_locale(self): + stub(self.bin, "apt-get", 'echo "Inst locale-$LC_ALL [1] (2 x [amd64])"\n') + self.assertEqual(self.report()["upgradable_count"], 1) + stub(self.bin, "apt-get", '[ "$LC_ALL" = C ] || exit 1; echo "Inst ok [1] (2 x [amd64])"\n') + self.assertEqual(self.report()["upgradable_count"], 1) + def test_hostile_strings_stay_valid_json(self): stub(self.bin, "hostname", "printf 'a\"b\\\\c\\tq'\n") self.assertEqual(self.report()["reported_hostname"], 'a"b\\cq') diff --git a/tests/test_status.py b/tests/test_status.py index b437a07..02f903d 100644 --- a/tests/test_status.py +++ b/tests/test_status.py @@ -169,6 +169,10 @@ class EvaluateTest(unittest.TestCase): self.assertEqual(s.level, "update") self.assertEqual(s.reasons, ["3 security updates pending"]) + def test_phased_updates_dont_affect_status(self): + s = self.level(report(phased_packages=["systemd", "udev"])) + self.assertEqual((s.level, s.reasons), ("ok", [])) + def test_non_security_updates_are_ok(self): self.assertEqual(self.level(report(upgradable_count=5)).level, "ok") diff --git a/tests/test_web.py b/tests/test_web.py index be97f5b..1c3b981 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -127,7 +127,9 @@ class DashboardTest(ServerCase): "os_pretty_name": "Ubuntu 24.04.3 LTS ", "apt_lists_age_seconds": 60, "agent_version": "0.1.0", - "services_need_restart": ["nginx.service"]}) + "services_need_restart": ["nginx.service"], + "upgradable_count": 2, "security_count": 1, + "phased_packages": ["systemd", "udev", "libudev1"]}) status, headers, _ = self.login("admin", "a-long-password") self.assertEqual(status, 303) @@ -143,6 +145,10 @@ class DashboardTest(ServerCase): self.assertIn("never reported", body) self.assertIn("1 service needs restart: nginx", body) self.assertIn("agent 0.1.0 is older", body) + self.assertIn("2 (1 sec) +3 phased", body) + web01_reasons = body[body.rindex(''):].split("")[0] # last row + self.assertIn("security update", web01_reasons) + self.assertNotIn("phas", web01_reasons, "phasing is never a status reason") self.assertIn("<b>", body, "report content must be escaped") self.assertNotIn("\n", body) # Worst first: stale db01 before reboot web01.