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>
100 lines
3.2 KiB
Python
100 lines
3.2 KiB
Python
"""Validation of agent reports. Unknown fields are dropped; known ones are type-checked."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
MAX_BODY_BYTES = 64 * 1024
|
|
_MAX_STR = 256
|
|
_MAX_LIST = 200
|
|
|
|
|
|
class ReportError(ValueError):
|
|
pass
|
|
|
|
|
|
# field -> kind. "str", "int", "bool", "strlist". All fields are optional.
|
|
FIELDS: dict[str, str] = {
|
|
"agent_version": "str",
|
|
"reported_hostname": "str",
|
|
"os_id": "str",
|
|
"os_version_id": "str",
|
|
"os_pretty_name": "str",
|
|
"kernel_running": "str",
|
|
"kernel_build": "str",
|
|
"kernel_running_pkg_version": "str",
|
|
"kernels_installed": "strlist",
|
|
"kernel_newest_installed": "str",
|
|
"needrestart_ksta": "int",
|
|
"needrestart_kexp": "str",
|
|
"needrestart_ucsta": "int",
|
|
"needrestart_uccur": "str",
|
|
"needrestart_ucexp": "str",
|
|
"services_need_restart": "strlist",
|
|
"sessions_need_restart": "int",
|
|
"reboot_required": "bool",
|
|
"reboot_required_pkgs": "strlist",
|
|
"upgradable_count": "int",
|
|
"security_count": "int",
|
|
"kernel_upgradable": "strlist",
|
|
"phased_packages": "strlist",
|
|
"livepatch_state": "str",
|
|
"livepatch_check_state": "str",
|
|
"livepatch_supported": "str",
|
|
"livepatch_upgrade_required": "str",
|
|
"livepatch_fix_count": "int",
|
|
"apt_simulation_ok": "bool",
|
|
"apt_simulation_error": "str",
|
|
"boot_size_kb": "int",
|
|
"boot_free_kb": "int",
|
|
"upgrade_download_kb": "int",
|
|
"upgrade_install_kb": "int",
|
|
"apt_archives_free_kb": "int",
|
|
"apt_archives_mount": "str",
|
|
"usr_free_kb": "int",
|
|
"usr_mount": "str",
|
|
"dpkg_unfinished_packages": "strlist",
|
|
"held_packages": "strlist",
|
|
"ubuntu_pro_attached": "bool",
|
|
"ubuntu_esm_infra": "str",
|
|
"unattended_upgrades": "str",
|
|
"unattended_upgrades_success_age_seconds": "int",
|
|
"unattended_upgrades_run_age_seconds": "int",
|
|
"unattended_upgrades_kept_back": "strlist",
|
|
"apt_lists_age_seconds": "int",
|
|
"security_repo_suite": "str",
|
|
"security_repo_age_seconds": "int",
|
|
"helper_units": "strlist",
|
|
"uptime_seconds": "int",
|
|
}
|
|
|
|
|
|
def _check(name: str, kind: str, value: Any) -> Any:
|
|
if value is None:
|
|
return None
|
|
if kind == "str":
|
|
if not isinstance(value, str):
|
|
raise ReportError(f"{name} must be a string")
|
|
return value[:_MAX_STR]
|
|
if kind == "int":
|
|
if not isinstance(value, int) or isinstance(value, bool):
|
|
raise ReportError(f"{name} must be an integer")
|
|
return value
|
|
if kind == "bool":
|
|
if not isinstance(value, bool):
|
|
raise ReportError(f"{name} must be a boolean")
|
|
return value
|
|
if kind == "strlist":
|
|
if not isinstance(value, list) or not all(isinstance(v, str) for v in value):
|
|
raise ReportError(f"{name} must be a list of strings")
|
|
return [v[:_MAX_STR] for v in value[:_MAX_LIST]]
|
|
raise AssertionError(kind)
|
|
|
|
|
|
def sanitize(raw: Any) -> dict[str, Any]:
|
|
if not isinstance(raw, dict):
|
|
raise ReportError("report must be a JSON object")
|
|
if not isinstance(raw.get("kernel_running"), str) or not raw["kernel_running"]:
|
|
raise ReportError("kernel_running is required")
|
|
return {name: _check(name, kind, raw[name]) for name, kind in FIELDS.items() if name in raw}
|