From a5c85494d151e724e9bde57ea8c5e2b7fd3257b6 Mon Sep 17 00:00:00 2001 From: j4n Date: Thu, 26 Nov 2026 16:01:06 +0100 Subject: [PATCH] dovecot: support Debian 13 (trixie) --- cmdeploy/src/cmdeploy/dovecot/deployer.py | 37 +++-- cmdeploy/src/cmdeploy/pins.py | 25 ++- .../cmdeploy/tests/test_dovecot_deployer.py | 150 ++++++++++++++---- 3 files changed, 158 insertions(+), 54 deletions(-) diff --git a/cmdeploy/src/cmdeploy/dovecot/deployer.py b/cmdeploy/src/cmdeploy/dovecot/deployer.py index 284cd5e8..08185597 100644 --- a/cmdeploy/src/cmdeploy/dovecot/deployer.py +++ b/cmdeploy/src/cmdeploy/dovecot/deployer.py @@ -5,7 +5,7 @@ from chatmaild.config import Config from pyinfra import host from pyinfra.facts.deb import DebPackages from pyinfra.facts.server import Arch, Command, Sysctl -from pyinfra.operations import apt, files, server +from pyinfra.operations import files, server from cmdeploy.basedeploy import ( Deployer, @@ -16,9 +16,6 @@ from cmdeploy.basedeploy import ( ) from cmdeploy.pins import DOVECOT_SHA256, DOVECOT_VERSION -DOVECOT_STAMPED_VERSION = f"{DOVECOT_VERSION}+deb12u1" -DOVECOT_PACKAGE_VERSION = f"1:{DOVECOT_STAMPED_VERSION}" - class DovecotDeployer(Deployer): daemon_reload = False @@ -30,10 +27,12 @@ class DovecotDeployer(Deployer): def install(self): arch = host.get_fact(Arch) + version_id = host.get_fact(Command, "grep '^VERSION_ID=' /etc/os-release") + deb_release = _parse_version_id(version_id) with blocked_service_startup(): debs = [] for pkg in ("core", "imapd", "lmtpd", "auth-lua"): - deb, changed = _download_dovecot_package(pkg, arch) + deb, changed = _download_dovecot_package(pkg, arch, deb_release) self.need_restart |= changed if deb: debs.append(deb) @@ -85,6 +84,15 @@ class DovecotDeployer(Deployer): ) +def _parse_version_id(version_line: str) -> int: + """Debian major release from an /etc/os-release VERSION_ID line.""" + _, _, raw = (version_line or "").strip().partition("=") + try: + return int(raw.strip('"')) + except ValueError: + raise ValueError(f"cannot determine Debian release from {version_line!r}") + + def _pick_url(primary, fallback): try: req = urllib.request.Request(primary, method="HEAD") @@ -94,22 +102,27 @@ def _pick_url(primary, fallback): return fallback -def _download_dovecot_package(package: str, arch: str) -> tuple[str | None, bool]: +def _download_dovecot_package( + package: str, arch: str, deb_release: int +) -> tuple[str | None, bool]: """Download a dovecot .deb if needed, return (path, changed).""" arch = "amd64" if arch == "x86_64" else arch arch = "arm64" if arch == "aarch64" else arch pkg_name = f"dovecot-{package}" - sha256 = DOVECOT_SHA256.get((package, arch)) - if sha256 is None: - op = apt.packages(packages=[pkg_name]) - return None, bool(getattr(op, "changed", False)) + # never fall back to the distro package + try: + sha256 = DOVECOT_SHA256[(arch, deb_release, package)] + except KeyError: + raise ValueError(f"no dovecot build for {pkg_name} on deb{deb_release}/{arch}") + # chatmail/dovecot CI stamps the distro suffix into package version and filename + stamped_version = f"{DOVECOT_VERSION}+deb{deb_release}u1" installed_versions = host.get_fact(DebPackages).get(pkg_name, []) - if DOVECOT_PACKAGE_VERSION in installed_versions: + if f"1:{stamped_version}" in installed_versions: return None, False - url_version = DOVECOT_STAMPED_VERSION.replace("+", "%2B") + url_version = stamped_version.replace("+", "%2B") tag = DOVECOT_VERSION.replace("+", "%2B") deb_base = f"{pkg_name}_{url_version}_{arch}.deb" primary_url = f"https://download.delta.chat/dovecot/{deb_base}" diff --git a/cmdeploy/src/cmdeploy/pins.py b/cmdeploy/src/cmdeploy/pins.py index fc65c5c1..70b9fa64 100644 --- a/cmdeploy/src/cmdeploy/pins.py +++ b/cmdeploy/src/cmdeploy/pins.py @@ -28,16 +28,25 @@ MTAIL_ARTIFACTS = { ), } +# distro-neutral base version, as committed in chatmail/dovecot debian/changelog DOVECOT_VERSION = "2.3.21+dfsg1-3+chatmail3" DOVECOT_SHA256 = { - ("core", "amd64"): "8064ca9a940684007063d4a4f60ec265b364f07d26cb4a7ffec83fc775812ebe", - ("core", "arm64"): "5f5665735a01c70fb22ac0415b3aea00d23c1301b52effab96bf92c1b1e4366c", - ("imapd", "amd64"): "1175a1dafb755e393385306bbe109fccacd31d5177a72db16c0f0b1731a83221", - ("imapd", "arm64"): "48399b20ab600a20eaf377f8de34eb8e2a393954e53c6636b1e37c7ea99fd8a6", - ("lmtpd", "amd64"): "d0681cd5ae6680d13338ee2e2bad290f929cac8b62d913d842576f2413cde48e", - ("lmtpd", "arm64"): "3ad2a4484b2fa52f33e536cb8e36d13d9a86cb5c98732d5fe0f5e4898af03de5", - ("auth-lua", "amd64"): "d2764bd247abb6dd90ee8026cea277cb044f9ac02348abd45c692ce52a2ed6f4", - ("auth-lua", "arm64"): "5164b74a47a70aa265097f66a7f38985ebd6c575f8432ba64122ae6267749286", + ("amd64", 12, "auth-lua"): "d2764bd247abb6dd90ee8026cea277cb044f9ac02348abd45c692ce52a2ed6f4", + ("arm64", 12, "auth-lua"): "5164b74a47a70aa265097f66a7f38985ebd6c575f8432ba64122ae6267749286", + ("amd64", 13, "auth-lua"): "09db7adf07948d897f7f9e282ef274de256b6b6db6b0d5fcda283545766ed707", + ("arm64", 13, "auth-lua"): "215627e7874006e2072df488a04e2413415fcd2f7a77a8ff5cdfd647bc33825d", + ("amd64", 12, "core"): "8064ca9a940684007063d4a4f60ec265b364f07d26cb4a7ffec83fc775812ebe", + ("arm64", 12, "core"): "5f5665735a01c70fb22ac0415b3aea00d23c1301b52effab96bf92c1b1e4366c", + ("amd64", 13, "core"): "0e51cf819be1a06cde3cb9cafc6ce545919ca9af78355b7aa53168e607b0a539", + ("arm64", 13, "core"): "33a9494092668726272332ba59deb89f71e0c8b16004f1742db2db6ab1cda565", + ("amd64", 12, "imapd"): "1175a1dafb755e393385306bbe109fccacd31d5177a72db16c0f0b1731a83221", + ("arm64", 12, "imapd"): "48399b20ab600a20eaf377f8de34eb8e2a393954e53c6636b1e37c7ea99fd8a6", + ("amd64", 13, "imapd"): "cdd052b7702006102a0a473c26cc5c2fda45e62bb858717dc39d49abf3543152", + ("arm64", 13, "imapd"): "b4b14017ae2fd4f5e4de661f9e0e034a10390730dba76a00636a72e44051b6e7", + ("amd64", 12, "lmtpd"): "d0681cd5ae6680d13338ee2e2bad290f929cac8b62d913d842576f2413cde48e", + ("arm64", 12, "lmtpd"): "3ad2a4484b2fa52f33e536cb8e36d13d9a86cb5c98732d5fe0f5e4898af03de5", + ("amd64", 13, "lmtpd"): "627175c4d76bfc15e2458d07d0fc173f804ea2781ccb418c133eecd5d7cdeab2", + ("arm64", 13, "lmtpd"): "bdbedaa701f65dafa9f31aff40e79d2c61fa79a6c4268457db177a0b92186add", } TURN_VERSION = "v0.4" TURN_ARTIFACTS = { diff --git a/cmdeploy/src/cmdeploy/tests/test_dovecot_deployer.py b/cmdeploy/src/cmdeploy/tests/test_dovecot_deployer.py index 45886e6b..68e3e465 100644 --- a/cmdeploy/src/cmdeploy/tests/test_dovecot_deployer.py +++ b/cmdeploy/src/cmdeploy/tests/test_dovecot_deployer.py @@ -3,9 +3,13 @@ from types import SimpleNamespace import pytest from pyinfra.facts.deb import DebPackages +from pyinfra.facts.server import Command from cmdeploy.dovecot import deployer as dovecot_deployer +# what dpkg reports after installing our deb12 build +INSTALLED_DEB12 = f"1:{dovecot_deployer.DOVECOT_VERSION}+deb12u1" + def make_host(*fact_pairs): """Build a mock host; get_fact(cls) dispatches to the provided facts mapping. @@ -19,7 +23,7 @@ def make_host(*fact_pairs): """ facts = dict(fact_pairs) - def get_fact(cls): + def get_fact(cls, *args): if cls not in facts: registered = ", ".join(c.__name__ for c in facts) raise LookupError( @@ -64,7 +68,7 @@ def track_shell(monkeypatch): def test_download_dovecot_package_skips_epoch_matched_install(monkeypatch): - epoch_version = dovecot_deployer.DOVECOT_PACKAGE_VERSION + epoch_version = INSTALLED_DEB12 downloads = [] monkeypatch.setattr( dovecot_deployer, @@ -82,15 +86,18 @@ def test_download_dovecot_package_skips_epoch_matched_install(monkeypatch): lambda **kwargs: downloads.append(kwargs), ) - deb, changed = dovecot_deployer._download_dovecot_package("core", "amd64") + deb, changed = dovecot_deployer._download_dovecot_package( + "core", "amd64", deb_release=12 + ) assert deb is None, f"expected no deb path when version matches, got {deb!r}" assert changed is False, "should not flag changed when version already installed" assert downloads == [], "should not download when version already installed" +@pytest.mark.parametrize(("arch", "deb_release"), [("amd64", 12), ("arm64", 13)]) def test_download_dovecot_package_uses_archive_version_for_url_and_filename( - monkeypatch, + monkeypatch, deb_release, arch ): downloads = [] monkeypatch.setattr( @@ -111,10 +118,13 @@ def test_download_dovecot_package_uses_archive_version_for_url_and_filename( lambda **kwargs: downloads.append(kwargs), ) - deb, changed = dovecot_deployer._download_dovecot_package("core", "amd64") + deb, changed = dovecot_deployer._download_dovecot_package( + "core", arch, deb_release=deb_release + ) version = dovecot_deployer.DOVECOT_VERSION - deb_base = f"dovecot-core_{version}+deb12u1_amd64.deb".replace("+", "%2B") + deb_base = f"dovecot-core_{version}+deb{deb_release}u1_{arch}.deb" + deb_base = deb_base.replace("+", "%2B") tag = version.replace("+", "%2B") # the release tag carries no distro suffix and filenames carry no epoch @@ -127,6 +137,39 @@ def test_download_dovecot_package_uses_archive_version_for_url_and_filename( ) assert len(downloads) == 1, "files.download should be called exactly once" assert downloads[0]["src"] == urls["primary"] + assert ( + downloads[0]["sha256sum"] + == dovecot_deployer.DOVECOT_SHA256[(arch, deb_release, "core")] + ), "must pass the sha256 matching (arch, release, package)" + + +def test_download_dovecot_package_replaces_deb12_build_after_upgrade_to_13( + monkeypatch, +): + downloads = [] + monkeypatch.setattr( + dovecot_deployer, + "host", + make_host((DebPackages, {"dovecot-core": [INSTALLED_DEB12]})), + ) + monkeypatch.setattr( + dovecot_deployer, + "_pick_url", + lambda primary, fallback: primary, + ) + monkeypatch.setattr( + dovecot_deployer.files, + "download", + lambda **kwargs: downloads.append(kwargs), + ) + + deb, changed = dovecot_deployer._download_dovecot_package( + "core", "amd64", deb_release=13 + ) + + assert changed is True, "a deb12 build on a deb13 host must be replaced" + assert "deb13u1" in deb, f"expected the deb13 build, got {deb!r}" + assert len(downloads) == 1, "files.download should be called exactly once" def test_install_skips_dpkg_path_when_epoch_matched_packages_present( @@ -139,13 +182,14 @@ def test_install_skips_dpkg_path_when_epoch_matched_packages_present( ( dovecot_deployer.DebPackages, { - "dovecot-core": [dovecot_deployer.DOVECOT_PACKAGE_VERSION], - "dovecot-imapd": [dovecot_deployer.DOVECOT_PACKAGE_VERSION], - "dovecot-lmtpd": [dovecot_deployer.DOVECOT_PACKAGE_VERSION], - "dovecot-auth-lua": [dovecot_deployer.DOVECOT_PACKAGE_VERSION], + "dovecot-core": [INSTALLED_DEB12], + "dovecot-imapd": [INSTALLED_DEB12], + "dovecot-lmtpd": [INSTALLED_DEB12], + "dovecot-auth-lua": [INSTALLED_DEB12], }, ), (dovecot_deployer.Arch, "x86_64"), + (Command, 'VERSION_ID="12"'), ), ) downloads = [] @@ -164,39 +208,23 @@ def test_install_skips_dpkg_path_when_epoch_matched_packages_present( ) -def test_install_unsupported_arch_falls_back_to_apt( +def test_install_unsupported_arch_raises( deployer, patch_blocked, mock_files_put, track_shell, monkeypatch ): - # For unsupported architectures, all fact lookups return the arch string. monkeypatch.setattr( dovecot_deployer, "host", - SimpleNamespace(get_fact=lambda cls: "riscv64"), + make_host( + (dovecot_deployer.Arch, "riscv64"), + (Command, 'VERSION_ID="12"'), + ), ) - apt_calls = [] - # Mirrors apt.packages() return value: OperationMeta with .changed property. - # Only lmtpd triggers a change to verify |= accumulation of changed flags. - def fake_apt(**kwargs): - apt_calls.append(kwargs) - changed = "lmtpd" in kwargs["packages"][0] - return SimpleNamespace(changed=changed) + # we never fall back to the pinned distro package + with pytest.raises(ValueError, match="no dovecot build for dovecot-core"): + deployer.install() - monkeypatch.setattr(dovecot_deployer.apt, "packages", fake_apt) - - deployer.install() - - actual_pkgs = [c["packages"] for c in apt_calls] - assert actual_pkgs == [ - ["dovecot-core"], - ["dovecot-imapd"], - ["dovecot-lmtpd"], - ["dovecot-auth-lua"], - ], f"expected apt install of core/imapd/lmtpd/auth-lua, got {actual_pkgs}" assert track_shell == [], "should not run dpkg for unsupported arch" - assert deployer.need_restart is True, ( - "need_restart should be True when apt installed a package" - ) def test_install_runs_dpkg_when_packages_need_download( @@ -208,6 +236,7 @@ def test_install_runs_dpkg_when_packages_need_download( make_host( (dovecot_deployer.DebPackages, {}), (dovecot_deployer.Arch, "x86_64"), + (Command, 'VERSION_ID="12"'), ), ) monkeypatch.setattr( @@ -245,3 +274,56 @@ def test_pick_url_falls_back_on_primary_error(monkeypatch): assert result == "http://fallback", ( f"should fall back when primary fails, got {result!r}" ) + + +def test_install_fails_on_unsupported_debian_version( + deployer, patch_blocked, monkeypatch +): + monkeypatch.setattr( + dovecot_deployer, + "host", + make_host( + (dovecot_deployer.Arch, "x86_64"), + (Command, 'VERSION_ID="99"'), + ), + ) + + with pytest.raises(ValueError, match="no dovecot build for dovecot-core on deb99"): + deployer.install() + + +@pytest.mark.parametrize( + "version_line", + ["", None, "ID=debian", "VERSION_ID="], + ids=["empty", "none", "no-version-id", "empty-value"], +) +def test_parse_version_id_raises_without_version_id(version_line): + with pytest.raises(ValueError, match="cannot determine Debian release"): + dovecot_deployer._parse_version_id(version_line) + + +@pytest.mark.parametrize( + ("version_line", "deb_release"), + [('VERSION_ID="12"\n', 12), ('VERSION_ID="13"\n', 13), ("VERSION_ID=13", 13)], +) +def test_parse_version_id(version_line, deb_release): + assert dovecot_deployer._parse_version_id(version_line) == deb_release + + +def test_dovecot_sha256_covers_all_packages_per_release(): + """Every release in the table needs all four packages on both arches.""" + table = dovecot_deployer.DOVECOT_SHA256 + expected = { + (arch, pkg) + for arch in ("amd64", "arm64") + for pkg in ("core", "imapd", "lmtpd", "auth-lua") + } + for release in {r for _, r, _ in table}: + got = {(arch, pkg) for arch, r, pkg in table if r == release} + assert got == expected, f"deb{release} incomplete: {sorted(expected - got)}" + + +def test_dovecot_sha256_has_no_duplicate_hashes(): + """Each build is a distinct file, so a repeated hash is a copy-paste error.""" + table = dovecot_deployer.DOVECOT_SHA256 + assert len(set(table.values())) == len(table)