vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Publish apt and yum repositories with each release
73e98b74cf29c19adeeef3b4a277016838c3debe · Keenan Nemetz · 15d ago · parent eee732ec
Publish apt and yum repositories with each release A release tree and a channel give one stable URL, but apt needs a flat Packages/Release repository and dnf needs repodata. Build both from the release packages, attach the dist tree so they sit under apt/ and yum/, cover the metadata in SHA256SUMS, and document installing and upgrading from the stable channel.
scripts/build-package-repos.py +223
| @@ -0,0 +1,223 @@ | |||
| 1 | #!/usr/bin/env python3 | ||
| 2 | """Turn a release's packages into the repositories apt and dnf read. | ||
| 3 | |||
| 4 | `vole release attach` publishes a directory tree, and a channel gives one URL | ||
| 5 | that follows a release, but neither is a repository on its own: apt reads a flat | ||
| 6 | `Packages` file and the `Release` that hashes it, while dnf reads | ||
| 7 | `repodata/repomd.xml`. This script takes the `.deb` and `.rpm` files the release | ||
| 8 | workflow just built and writes both trees beside them: | ||
| 9 | |||
| 10 | dist/apt/ vole_<version>-1_<arch>.deb, Packages, Packages.gz, Release | ||
| 11 | dist/yum/ vole-<version>-1.<rpmarch>.rpm, repodata/ | ||
| 12 | |||
| 13 | The release attaches `dist`, so the release root keeps the binaries, the SBOM, | ||
| 14 | and SHA256SUMS beside `apt/` and `yum/`. A channel then makes one stable URL: | ||
| 15 | |||
| 16 | deb [trusted=yes] https://host/repo/channels/stable/apt ./ | ||
| 17 | baseurl=https://host/repo/channels/stable/yum/ | ||
| 18 | |||
| 19 | Both trees are byte-for-byte reproducible from a tag. `--epoch` is the tag's | ||
| 20 | commit time, and it supplies the `Date` in `Release`, the timestamps in | ||
| 21 | `repomd.xml`, and the copied packages' mtimes, which createrepo_c records as | ||
| 22 | each package's file time. The `Packages` stanzas are sorted rather than left in | ||
| 23 | directory order, and `Packages.gz` is written without a filename or clock in its | ||
| 24 | header. | ||
| 25 | |||
| 26 | It needs dpkg-dev (dpkg-scanpackages) and createrepo-c (createrepo_c), which the | ||
| 27 | release and verify images install. Nothing here signs anything: Vole does not | ||
| 28 | sign release artifacts yet, so the installation docs tell clients to trust the | ||
| 29 | host and to verify SHA256SUMS where that matters. | ||
| 30 | """ | ||
| 31 | |||
| 32 | import argparse | ||
| 33 | import email.utils | ||
| 34 | import gzip | ||
| 35 | import hashlib | ||
| 36 | import os | ||
| 37 | import shutil | ||
| 38 | import subprocess | ||
| 39 | import sys | ||
| 40 | import time | ||
| 41 | import xml.etree.ElementTree as ET | ||
| 42 | from pathlib import Path | ||
| 43 | |||
| 44 | # The architecture names differ between the two formats: nfpm writes Debian's | ||
| 45 | # `amd64` and RPM's `x86_64` for the same build. | ||
| 46 | DEB_ARCH = {"amd64": "amd64", "arm64": "arm64"} | ||
| 47 | RPM_ARCH = {"amd64": "x86_64", "arm64": "aarch64"} | ||
| 48 | |||
| 49 | REPOMD_NS = {"r": "http://linux.duke.edu/metadata/repo"} | ||
| 50 | |||
| 51 | |||
| 52 | def digest(name: str, data: bytes) -> str: | ||
| 53 | return hashlib.new(name, data, usedforsecurity=False).hexdigest() | ||
| 54 | |||
| 55 | |||
| 56 | def stanza_key(stanza: str) -> tuple[str, str, str]: | ||
| 57 | """Order stanzas by the fields a Packages file is read by. | ||
| 58 | |||
| 59 | dpkg-scanpackages emits stanzas in directory order, which is not stable | ||
| 60 | across machines. Sorting by package, architecture, and version makes one tag | ||
| 61 | produce one file. | ||
| 62 | """ | ||
| 63 | fields = {} | ||
| 64 | for line in stanza.splitlines(): | ||
| 65 | if line.startswith((" ", "\t")): | ||
| 66 | continue # a continuation line of a value such as Description | ||
| 67 | name, _, value = line.partition(":") | ||
| 68 | fields[name] = value.strip() | ||
| 69 | return fields.get("Package", ""), fields.get("Architecture", ""), fields.get("Version", "") | ||
| 70 | |||
| 71 | |||
| 72 | def write_apt(apt: Path, debs: list[tuple[Path, str]], epoch: int) -> None: | ||
| 73 | """Write Packages, Packages.gz, and Release for a flat Debian repository.""" | ||
| 74 | out = subprocess.run( | ||
| 75 | ["dpkg-scanpackages", "--multiversion", "."], | ||
| 76 | cwd=apt, | ||
| 77 | check=True, | ||
| 78 | capture_output=True, | ||
| 79 | text=True, | ||
| 80 | ) | ||
| 81 | stanzas = [s for s in out.stdout.strip().split("\n\n") if s.strip()] | ||
| 82 | if len(stanzas) != len(debs): | ||
| 83 | sys.exit( | ||
| 84 | f"dpkg-scanpackages described {len(stanzas)} package(s), " | ||
| 85 | f"but {len(debs)} were copied" | ||
| 86 | ) | ||
| 87 | stanzas.sort(key=stanza_key) | ||
| 88 | packages = ("\n\n".join(stanzas) + "\n").encode() | ||
| 89 | (apt / "Packages").write_bytes(packages) | ||
| 90 | # mtime=0 keeps the clock (and the filename) out of the gzip header. | ||
| 91 | (apt / "Packages.gz").write_bytes(gzip.compress(packages, compresslevel=9, mtime=0)) | ||
| 92 | |||
| 93 | # apt reads the hashes from Release and refuses a Packages file that does | ||
| 94 | # not match, so a truncated download or a half-written metadata file fails | ||
| 95 | # before it reaches a system. | ||
| 96 | lines = [ | ||
| 97 | "Origin: Vole", | ||
| 98 | "Label: Vole", | ||
| 99 | "Suite: vole", | ||
| 100 | "Codename: vole", | ||
| 101 | f"Date: {email.utils.formatdate(epoch, usegmt=True)}", | ||
| 102 | "Architectures: " + " ".join(sorted(arch for _, arch in debs)), | ||
| 103 | "Components: main", | ||
| 104 | "Description: Vole packages", | ||
| 105 | ] | ||
| 106 | for label, name in (("MD5Sum", "md5"), ("SHA1", "sha1"), ("SHA256", "sha256")): | ||
| 107 | lines.append(f"{label}:") | ||
| 108 | for member in ("Packages", "Packages.gz"): | ||
| 109 | data = (apt / member).read_bytes() | ||
| 110 | lines.append(f" {digest(name, data)} {len(data):16d} {member}") | ||
| 111 | (apt / "Release").write_text("\n".join(lines) + "\n", encoding="utf-8") | ||
| 112 | |||
| 113 | |||
| 114 | def write_yum(yum: Path, epoch: int) -> None: | ||
| 115 | """Write repodata for an RPM repository with a pinned revision and clock.""" | ||
| 116 | subprocess.run( | ||
| 117 | [ | ||
| 118 | "createrepo_c", | ||
| 119 | "--quiet", | ||
| 120 | "--no-database", | ||
| 121 | "--revision", str(epoch), | ||
| 122 | # The revision is an epoch because this flag says so; it is the tag's | ||
| 123 | # commit time, which is also what the packages carry. | ||
| 124 | "--set-timestamp-to-revision", | ||
| 125 | ".", | ||
| 126 | ], | ||
| 127 | cwd=yum, | ||
| 128 | check=True, | ||
| 129 | ) | ||
| 130 | repomd = yum / "repodata" / "repomd.xml" | ||
| 131 | if not repomd.is_file(): | ||
| 132 | sys.exit("createrepo_c produced no repodata/repomd.xml") | ||
| 133 | # A location that names a missing file is a repository dnf cannot install | ||
| 134 | # from, and the error would surface on a user's machine after the tag was | ||
| 135 | # pushed. Every referenced file is in the tree this release publishes. | ||
| 136 | for location in ET.parse(repomd).getroot().findall(".//r:location", REPOMD_NS): | ||
| 137 | href = location.get("href", "") | ||
| 138 | if not (yum / href).is_file(): | ||
| 139 | sys.exit(f"repomd.xml names a file that is not in the tree: {href}") | ||
| 140 | |||
| 141 | |||
| 142 | def main() -> None: | ||
| 143 | parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) | ||
| 144 | parser.add_argument("--dist", default="dist", help="directory holding the built packages (default: dist)") | ||
| 145 | parser.add_argument("--version", required=True, help="release version without the leading v, as nfpm was given it") | ||
| 146 | parser.add_argument( | ||
| 147 | "--epoch", | ||
| 148 | type=int, | ||
| 149 | default=int(os.environ.get("SOURCE_DATE_EPOCH") or time.time()), | ||
| 150 | help="tag commit time in seconds (default: SOURCE_DATE_EPOCH, else the clock)", | ||
| 151 | ) | ||
| 152 | parser.add_argument( | ||
| 153 | "--arch", | ||
| 154 | action="append", | ||
| 155 | choices=sorted(DEB_ARCH), | ||
| 156 | dest="architectures", | ||
| 157 | help="architecture to publish; repeatable (default: amd64 and arm64)", | ||
| 158 | ) | ||
| 159 | args = parser.parse_args() | ||
| 160 | architectures = args.architectures or sorted(DEB_ARCH) | ||
| 161 | |||
| 162 | for tool in ("dpkg-scanpackages", "createrepo_c"): | ||
| 163 | if shutil.which(tool) is None: | ||
| 164 | sys.exit( | ||
| 165 | f"{tool} is not installed; the workflow images carry " | ||
| 166 | "dpkg-dev and createrepo-c, which provide both tools" | ||
| 167 | ) | ||
| 168 | |||
| 169 | dist = Path(args.dist).resolve() | ||
| 170 | if not dist.is_dir(): | ||
| 171 | sys.exit(f"{args.dist} is not a directory") | ||
| 172 | # nfpm rewrites a prerelease hyphen to a tilde for both formats, so a tag | ||
| 173 | # v0.3.0-rc1 builds vole_0.3.0~rc1-1_amd64.deb. | ||
| 174 | package_version = args.version.replace("-", "~") | ||
| 175 | apt = dist / "apt" | ||
| 176 | yum = dist / "yum" | ||
| 177 | |||
| 178 | # A package missing here would publish a repository that cannot install the | ||
| 179 | # release on the architecture it names, and a tag cannot be rebuilt. | ||
| 180 | deb_sources = [ | ||
| 181 | (dist / f"vole_{package_version}-1_{DEB_ARCH[arch]}.deb", arch) | ||
| 182 | for arch in architectures | ||
| 183 | ] | ||
| 184 | rpm_sources = [ | ||
| 185 | dist / f"vole-{package_version}-1.{RPM_ARCH[arch]}.rpm" | ||
| 186 | for arch in architectures | ||
| 187 | ] | ||
| 188 | missing = [str(path) for path, _ in deb_sources if not path.is_file()] | ||
| 189 | missing += [str(path) for path in rpm_sources if not path.is_file()] | ||
| 190 | if missing: | ||
| 191 | sys.exit("the packages for this release are not in " + args.dist + ":\n " + "\n ".join(missing)) | ||
| 192 | |||
| 193 | for directory in (apt, yum): | ||
| 194 | if directory.is_dir(): | ||
| 195 | shutil.rmtree(directory) | ||
| 196 | directory.mkdir(parents=True) | ||
| 197 | |||
| 198 | debs = [] | ||
| 199 | for source, arch in deb_sources: | ||
| 200 | destination = apt / source.name | ||
| 201 | shutil.copyfile(source, destination) | ||
| 202 | destination.chmod(0o644) | ||
| 203 | os.utime(destination, (args.epoch, args.epoch)) | ||
| 204 | debs.append((destination, arch)) | ||
| 205 | |||
| 206 | for source in rpm_sources: | ||
| 207 | destination = yum / source.name | ||
| 208 | shutil.copyfile(source, destination) | ||
| 209 | destination.chmod(0o644) | ||
| 210 | # createrepo_c records this as the package's file time in primary.xml. | ||
| 211 | os.utime(destination, (args.epoch, args.epoch)) | ||
| 212 | |||
| 213 | write_apt(apt, debs, args.epoch) | ||
| 214 | write_yum(yum, args.epoch) | ||
| 215 | |||
| 216 | print( | ||
| 217 | f"apt: {len(debs)} package(s) in {apt}\n" | ||
| 218 | f"yum: {len(architectures)} package(s) in {yum}" | ||
| 219 | ) | ||
| 220 | |||
| 221 | |||
| 222 | if __name__ == "__main__": | ||
| 223 | main() | ||