Flag hosts without room on disk for their pending upgrades
CI / tests (Python 3.10) (pull_request) Successful in 43s
CI / tests (Python 3.11) (pull_request) Successful in 44s
CI / tests (Python 3.12) (pull_request) Successful in 43s
CI / tests (Python 3.13) (pull_request) Successful in 42s
CI / lint (pull_request) Successful in 3s
CI / tests (Python 3.10) (pull_request) Successful in 43s
CI / tests (Python 3.11) (pull_request) Successful in 44s
CI / tests (Python 3.12) (pull_request) Successful in 43s
CI / tests (Python 3.13) (pull_request) Successful in 42s
CI / lint (pull_request) Successful in 3s
A filesystem that fills up while apt downloads or dpkg unpacks leaves the upgrade half-done and apt broken, the same failure as a full /boot but on / or /var. Nothing caught it until the host's updates stopped. The agent (0.11.0) now works out what the pending upgrades need. A simulation doesn't print apt's "Need to get" or "additional disk space" lines, so it sums the package records from apt-cache show for each version the simulation would install: Size for each .deb not already in /var/cache/apt/archives, and the growth in Installed-Size over the installed versions (from dpkg-query). Removals aren't subtracted, so it errs high. It also reports free space and the mount point of the filesystems holding /var/cache/apt/archives and /usr. The server puts a host in update when the upgrades would leave less than upgrade_min_free_mb (default 100, 0 disables) on either filesystem. The download and the install both count when they're on the same one. With nothing pending there is nothing to check, so this doesn't turn into general disk monitoring. The host page gets a "Pending upgrades size" row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b5be19f85d
commit
15b1f5be4f
13 files changed
+363
-7
No files matched your search
+114
-2
@@ -31,6 +31,37 @@ Inst curl [8.14.1-2] (8.14.1-2+deb13u1 Debian:13.2/stable [amd64])
|
||||
Conf libssl3t64 (3.5.1-1+deb13u1 Debian-Security:13/stable-security [amd64])
|
||||
"""
|
||||
|
||||
# `apt-cache show` records for the simulation's three new versions. curl's record
|
||||
# appears twice, as it does when two sources carry the same version.
|
||||
APT_CACHE_DEBIAN = """\
|
||||
Package: libssl3t64
|
||||
Architecture: amd64
|
||||
Version: 3.5.1-1+deb13u1
|
||||
Installed-Size: 9000
|
||||
Size: 2500000
|
||||
|
||||
Package: linux-image-6.12.60+deb13-amd64
|
||||
Architecture: amd64
|
||||
Version: 6.12.60-1
|
||||
Installed-Size: 400000
|
||||
Size: 100000000
|
||||
|
||||
Package: curl
|
||||
Architecture: amd64
|
||||
Version: 8.14.1-2+deb13u1
|
||||
Installed-Size: 500
|
||||
Size: 300000
|
||||
|
||||
Package: curl
|
||||
Architecture: amd64
|
||||
Version: 8.14.1-2+deb13u1
|
||||
Installed-Size: 500
|
||||
Size: 300000
|
||||
"""
|
||||
|
||||
# Installed-Size of the installed libssl3t64 and curl, the two upgrades.
|
||||
DPKG_SIZES_DEBIAN = "8900\n500\n"
|
||||
|
||||
|
||||
def stub(bindir, name, script):
|
||||
path = os.path.join(bindir, name)
|
||||
@@ -61,11 +92,21 @@ class AgentCase(unittest.TestCase):
|
||||
stub(self.bin, "hostname", "echo web01.example\n")
|
||||
# The full-database query (unfinished packages) prints $KERNELWATCH_ROOT/dpkg-all.
|
||||
# The unattended-upgrades query prints $KERNELWATCH_ROOT/dpkg-uu (its status, e.g. "ii ").
|
||||
stub(self.bin, "dpkg-query", 'case "$*" in *" unattended-upgrades") cat "$KERNELWATCH_ROOT/dpkg-uu" 2>/dev/null;; '
|
||||
# The Installed-Size query prints $KERNELWATCH_ROOT/dpkg-sizes.
|
||||
stub(self.bin, "dpkg-query", 'case "$*" in *Installed-Size*) cat "$KERNELWATCH_ROOT/dpkg-sizes" 2>/dev/null;; '
|
||||
'*" unattended-upgrades") cat "$KERNELWATCH_ROOT/dpkg-uu" 2>/dev/null;; '
|
||||
'*binary:Package*) cat "$KERNELWATCH_ROOT/dpkg-all" 2>/dev/null;; '
|
||||
'*Version*) echo "ii 6.12.48-2";; *) cat <<\'X\'\n'
|
||||
f"{DPKG_DEBIAN}X\n;; esac\n")
|
||||
self.stub_df("/dev/sda1 999320 187364 743144 21% /boot\n")
|
||||
# df answers per path: /boot and /usr on the root filesystem, apt's archives on /var.
|
||||
stub(self.bin, "df", "echo 'Filesystem 1024-blocks Used Available Capacity Mounted on'\n"
|
||||
'case "$2" in */var/cache/apt/archives) '
|
||||
"echo '/dev/sda2 4062912 3500000 358124 91% /var';; "
|
||||
"*) echo '/dev/sda1 999320 187364 743144 21% /';; esac\n")
|
||||
with open(os.path.join(self.root, "dpkg-sizes"), "w") as f:
|
||||
f.write(DPKG_SIZES_DEBIAN)
|
||||
# apt-cache records its arguments in $KERNELWATCH_ROOT/apt-cache-args.
|
||||
self.stub_apt_cache(APT_CACHE_DEBIAN)
|
||||
# `apt-config dump KEY` prints $KERNELWATCH_ROOT/apt-config (nothing: the key is unset).
|
||||
stub(self.bin, "apt-config", 'cat "$KERNELWATCH_ROOT/apt-config" 2>/dev/null; exit 0\n')
|
||||
stub(self.bin, "apt-get", f"cat <<'X'\n{APT_SIM_DEBIAN}X\n")
|
||||
@@ -77,6 +118,10 @@ class AgentCase(unittest.TestCase):
|
||||
stub(self.bin, "df", "echo 'Filesystem 1024-blocks Used Available Capacity Mounted on'\n"
|
||||
f"cat <<'X'\n{line}X\nexit {rc}\n")
|
||||
|
||||
def stub_apt_cache(self, records, rc=0):
|
||||
stub(self.bin, "apt-cache", 'printf "%s\\n" "$@" >"$KERNELWATCH_ROOT/apt-cache-args"\n'
|
||||
f"cat <<'X'\n{records}X\nexit {rc}\n")
|
||||
|
||||
def run_agent(self, *args, env=None):
|
||||
full_env = {
|
||||
"PATH": f"{self.bin}:{os.environ['PATH']}",
|
||||
@@ -173,6 +218,73 @@ class AgentReportTest(AgentCase):
|
||||
self.stub_df("/dev/sda1 - - - - /boot\n")
|
||||
self.assertIsNone(self.report()["boot_free_kb"])
|
||||
|
||||
def test_upgrade_space(self):
|
||||
r = self.report()
|
||||
# 102.8 MB of .debs, rounded up to KiB; 409500 KiB of new versions less the
|
||||
# 9400 KiB of the libssl3t64 and curl they replace.
|
||||
self.assertEqual(r["upgrade_download_kb"], 100391)
|
||||
self.assertEqual(r["upgrade_install_kb"], 400100)
|
||||
self.assertEqual((r["apt_archives_free_kb"], r["apt_archives_mount"]), (358124, "/var"))
|
||||
self.assertEqual((r["usr_free_kb"], r["usr_mount"]), (743144, "/"))
|
||||
with open(os.path.join(self.root, "apt-cache-args")) as f:
|
||||
args = f.read().split()
|
||||
self.assertEqual(args[-4:], ["show", "libssl3t64=3.5.1-1+deb13u1",
|
||||
"linux-image-6.12.60+deb13-amd64=6.12.60-1", "curl=8.14.1-2+deb13u1"])
|
||||
|
||||
def test_upgrade_space_skips_downloaded_archives(self):
|
||||
archives = os.path.join(self.root, "var/cache/apt/archives")
|
||||
os.makedirs(archives)
|
||||
open(os.path.join(archives, "curl_8.14.1-2+deb13u1_amd64.deb"), "w").close()
|
||||
open(os.path.join(archives, "libssl3t64_3.5.1-1_amd64.deb"), "w").close() # the old version
|
||||
self.assertEqual(self.report()["upgrade_download_kb"], 100098)
|
||||
|
||||
def test_upgrade_space_epoch_in_archive_name(self):
|
||||
stub(self.bin, "apt-get", "echo 'Inst tzdata [1:2024a-1] (1:2025b-1 Debian:13/stable [all])'\n")
|
||||
self.stub_apt_cache("Package: tzdata\nArchitecture: all\nVersion: 1:2025b-1\n"
|
||||
"Installed-Size: 3000\nSize: 2048\n")
|
||||
archives = os.path.join(self.root, "var/cache/apt/archives")
|
||||
os.makedirs(archives)
|
||||
open(os.path.join(archives, "tzdata_1%3a2025b-1_all.deb"), "w").close()
|
||||
with open(os.path.join(self.root, "dpkg-sizes"), "w") as f:
|
||||
f.write("3100\n")
|
||||
r = self.report()
|
||||
self.assertEqual((r["upgrade_download_kb"], r["upgrade_install_kb"]), (0, -100))
|
||||
|
||||
def test_upgrade_space_new_package_counts_in_full(self):
|
||||
stub(self.bin, "apt-get", "echo 'Inst linux-image-6.12.60+deb13-amd64 (6.12.60-1 Debian:13/stable [amd64])'\n")
|
||||
self.stub_apt_cache(APT_CACHE_DEBIAN.split("\n\n")[1] + "\n")
|
||||
os.remove(os.path.join(self.root, "dpkg-sizes"))
|
||||
r = self.report()
|
||||
self.assertEqual((r["upgrade_download_kb"], r["upgrade_install_kb"]), (97657, 400000))
|
||||
|
||||
def test_upgrade_space_nothing_pending(self):
|
||||
stub(self.bin, "apt-get", "echo 'NOTE: This is only a simulation!'\n")
|
||||
r = self.report()
|
||||
self.assertEqual((r["upgrade_download_kb"], r["upgrade_install_kb"]), (0, 0))
|
||||
self.assertFalse(os.path.exists(os.path.join(self.root, "apt-cache-args")), "apt-cache not run")
|
||||
|
||||
def test_upgrade_space_unknown_when_apt_cache_fails(self):
|
||||
self.stub_apt_cache("", rc=100)
|
||||
r = self.report()
|
||||
self.assertIsNone(r["upgrade_download_kb"])
|
||||
self.assertIsNone(r["upgrade_install_kb"])
|
||||
|
||||
def test_upgrade_space_unknown_when_simulation_fails(self):
|
||||
stub(self.bin, "apt-get", "echo 'E: Unmet dependencies.' >&2; exit 100\n")
|
||||
r = self.report()
|
||||
self.assertIsNone(r["upgrade_download_kb"])
|
||||
self.assertFalse(os.path.exists(os.path.join(self.root, "apt-cache-args")))
|
||||
|
||||
def test_filesystem_free_unknown_when_df_fails(self):
|
||||
self.stub_df("", rc=1)
|
||||
r = self.report()
|
||||
self.assertIsNone(r["apt_archives_free_kb"])
|
||||
self.assertIsNone(r["usr_mount"])
|
||||
|
||||
def test_mount_point_with_spaces(self):
|
||||
self.stub_df("/dev/sdb1 999320 187364 743144 21% /srv/my disk\n")
|
||||
self.assertEqual(self.report()["usr_mount"], "/srv/my disk")
|
||||
|
||||
def test_unfinished_packages(self):
|
||||
with open(os.path.join(self.root, "dpkg-all"), "w") as f:
|
||||
f.write("ii \tcurl\n"
|
||||
|
||||
Reference in new issue
Block a user