From 6ddb1d251a8bf084da81531dbc3a9a2a0a7725de Mon Sep 17 00:00:00 2001 From: Mistercoolertyper <84140706+Mistercoolertyper@users.noreply.github.com> Date: Wed, 4 Nov 2026 09:24:53 +0100 Subject: [PATCH] Add Debian 13 (trixie) support alongside Fedora The provisioning path now runs on both package families. Fedora behaviour is unchanged: the Fedora publishing code issues the same ten operations in the same order as before, verified by recording the call sequence from both implementations against a stubbed environment. update_local_repo.py Detects the family from /etc/os-release and dispatches to sync_fedora() or sync_debian(). Release assets are filtered by extension, so neither family sees the other's packages. The Debian path builds a flat apt-ftparchive repository at the same /var/local/citrix-repo, publishes local-citrix-repo.list, and runs apt-get update. Fixes found while testing: - tqdm is now an optional dependency. A stock Debian 13 image has no pip and no tqdm, and dying at import would take down provisioning over a progress bar. - sync_debian prunes stale *.deb files before repopulating. The Fedora path gets this for free by writing fixed filenames; Debian filenames carry the version, so without pruning every old version stayed indexed and apt always picked the highest, making rollback impossible. - The apt source file is resolved next to the script rather than from the working directory, with a hardcoded canonical fallback, so a foreign cwd or a missing file cannot break the 10-minute cron job. local-citrix-repo.list The apt counterpart of local-citrix-repo.repo, which stays as-is for dnf. Uses trusted=yes rather than a signature: the repository is generated on the machine from packages this script downloaded itself, so a signature would only prove the machine signed its own work, and it would add a gpg dependency that Debian 13 does not ship by default. Index integrity is still enforced through the hashes in Release. debian/preseed.cfg The debian-installer equivalent of ks.cfg. Same locale, keyboard, timezone, passwords, inventory-derived hostname/user/password, disk auto-detection, Firefox managed bookmarks, Citrix and TeamViewer installs, auto-update script, cron jobs and disable-sleep unit. Every directive was checked against the templates in the real trixie udebs. Documented gaps, all inherent to preseed vs kickstart: uppercase usernames are rejected by user-setup and so are created in late_command; d-i has no lsblk, openssl, chvt or openvt, so disk detection reads /sys/block and the inventory prompt goes to /dev/console with a kernel-cmdline override and a timeout; pip install tqdm becomes python3-tqdm from apt because of PEP 668. debian/test/ Harness covering the Debian path on a real Debian 13 machine: repo build, sources.list validity, apt visibility after purging the list cache, install resolution, idempotency, stale-package pruning, corrupt-index rejection, and Fedora non-regression. 52 pass, 3 skip (each skip states why). Known limitations are listed in README.md under "What the tests do NOT prove": no real d-i install has been run, and the generated post-install script is verified as well-formed rather than executed against a target root. --- README.md | 263 ++++++++++ debian/preseed.cfg | 535 ++++++++++++++++++++ debian/test/README.md | 255 ++++++++++ debian/test/run-tests.sh | 1002 ++++++++++++++++++++++++++++++++++++++ local-citrix-repo.list | 21 + update_local_repo.py | 980 ++++++++++++++++++++++++++----------- 6 files changed, 2779 insertions(+), 277 deletions(-) create mode 100644 debian/preseed.cfg create mode 100644 debian/test/README.md create mode 100644 debian/test/run-tests.sh create mode 100644 local-citrix-repo.list diff --git a/README.md b/README.md index 8b13789..ada4dea 100644 --- a/README.md +++ b/README.md @@ -1 +1,264 @@ +# VDI-Linux +Provisioning for the REYHER VDI clients. Two package families are supported from +this branch: + +| | Fedora | Debian | +|---|---|---| +| installer input | `ks.cfg` (Anaconda kickstart) | `debian/preseed.cfg` (debian-installer) | +| desktop | `@^kde-desktop-environment` | `kde-desktop` task | +| Citrix packages | `ICAClient.rpm`, `ctxusb.rpm` | `icaclient`, `ctxusb` | +| local repo build | `createrepo_c` | `apt-ftparchive` | +| local repo config | `/etc/yum.repos.d/local-citrix-repo.repo` | `/etc/apt/sources.list.d/local-citrix-repo.list` | +| repo refresh | `dnf makecache` | `apt-get update` | +| browser | `firefox` | `firefox-esr` | + +`update_local_repo.py` picks the path itself from `/etc/os-release`, so the same +file and the same cron job work on both. `configure_kde.py` is distro-neutral and +runs unchanged on both. + +## What runs, and when + +1. The installer provisions the base system from `ks.cfg` or `debian/preseed.cfg`. +2. A `%post` / `late_command` step writes the Firefox managed bookmarks, clones + this repository to `/root/VDI-Linux`, installs TeamViewer, writes + `/root/auto_update.sh`, installs the `disable-sleep` systemd unit, and adds the + cron jobs. +3. A cron job runs `update_local_repo.py` every 10 minutes as root. That script + pulls this repo, applies the KDE settings, downloads the newest Citrix + packages from the Gitea release, republishes the local repository, and + refreshes the package manager's index. +4. `@reboot` runs `/root/auto_update.sh` once the network is up, which + pre-downloads system updates and prepares an offline update. + +## update_local_repo.py + +``` +detect_package_family() /etc/os-release -> "fedora" | "debian" +main() git pull, apply KDE settings, desktop shortcut, + is_newer_release(), then sync_fedora() or sync_debian() +``` + +`fetch_release_info()` filters release assets by the running family's extension, +so a Fedora client never sees the `.deb` assets and a Debian client never sees the +`.rpm` assets. Both families read the same Gitea release. + +### Fedora path + +Unchanged from the previously deployed version. Fixed asset filenames +(`ctxusb.rpm`, `ICAClient.rpm`) are downloaded into `/temp/packages`, copied into +`/var/local/citrix-repo`, indexed with `createrepo_c`, and published with +`local-citrix-repo.repo` (`gpgcheck=1` against the imported +`RPM-GPG-KEY-citrix-local`), then `dnf makecache` and `pkcon refresh force`. + +This path was verified to issue the same ten operations in the same order with +the same arguments as `main`, by running both implementations against a stubbed +environment and diffing the recorded call sequence. See "Regression testing". + +### Debian path + +The Debian assets are named with the version embedded +(`icaclient_26.08.0.225_amd64.deb`), so they are matched by package-name prefix +rather than by exact filename. They are downloaded, copied into the same +`/var/local/citrix-repo`, and indexed with + +```sh +cd /var/local/citrix-repo +apt-ftparchive packages . > Packages +gzip -c Packages > Packages.gz +apt-ftparchive release . > Release +``` + +That is a flat ("trivial") apt archive: the packages, `Packages`, `Packages.gz` +and `Release` all sit directly in the directory, with no `dists/` tree. apt reads +it through `local-citrix-repo.list`: + +``` +deb [trusted=yes] file:/var/local/citrix-repo ./ +``` + +The suite is `./` - a path ending in a slash, no component - which is what marks +a flat repository. + +**Why `trusted=yes` and not a signed repository.** The Fedora side signs because +it already has an RPM key. On Debian the repository is generated on the machine, +from packages this script has just downloaded itself, so a signature would only +prove that the machine signed its own work. Signing would also add a real +dependency: Debian 13 has no `gpg` installed by default, and apt verifies with +`sqv` (Sequoia) rather than `gpgv`, so generating and trusting a key inside a +d-i install is a new failure mode for no gain. + +`trusted=yes` does **not** disable integrity checking. It tells apt not to +require a signature over the `Release` file; apt still verifies every index file +against the hashes in `Release`, and a tampered `Packages` fails with +`E: Failed to fetch ... Hash Sum mismatch`. What it gives up is proof of +*authorship*, which is meaningless for a repository the machine builds from its +own downloads. + +**Stale packages are pruned.** The Fedora path writes fixed filenames, so a new +release overwrites the old ones and the repository can never hold two versions. +Debian filenames carry the version, so `sync_debian()` deletes every `*.deb` from +the repository directory before repopulating it. Without that, every previous +version would stay indexed and apt would always pick the highest, making a +rollback by publishing an older build impossible. + +**Refreshing the index.** apt has no per-source equivalent of dnf's +`metadata_expire`; the index is re-read only when `apt-get update` runs. +`sync_debian()` runs it after rebuilding, and the 10-minute cron job runs +`sync_debian()`, so the index does not go stale in practice. + +### Where the `.deb` files come from + +1. Assets attached to the newest Gitea release, matched by package-name prefix. +2. If the release has no `.deb` assets, `$VDI_DEB_DIR` (default + `/temp/packages/deb`), so an operator can stage the two files on the machine + without publishing a release. + +If neither has them the script fails with a message naming both places. It does +not fall back to converting the RPMs: `alien` is unmaintained and would break the +Citrix pre/post-install scripts, which is a worse outcome than a clear failure. + +### Runtime requirements on Debian + +`apt-utils` (for `apt-ftparchive`), `python3`, `git`, and optionally +`python3-tqdm`. `tqdm` is only the download progress bar and the script falls +back to a no-op if it is missing, but without `apt-utils` or `python3` the cron +job fails every 10 minutes with the reason only visible in +`/var/log/update_local_repo.log`. + +## configure_kde.py + +Writes KWin's `[Libinput][Defaults][Touchpad]` group (so it applies to every +touchpad regardless of vendor), the PowerDevil AC/Battery/LowBattery profiles, +and KScreenLocker. Also installs `/usr/local/bin/vdi-kde-settings` plus an XDG +autostart entry that re-applies the settings inside a running session. + +Verified working unchanged on Debian 13 (Plasma 6.3.6): `kwriteconfig6` ships in +`libkf6config-bin`, which the `kde-desktop` task pulls in, and the resulting +`/etc/xdg/{kcminputrc,powerdevilrc,kscreenlockerrc}` are correct. It is idempotent +- a second run changes nothing. + +It prefers `sudo -n -u ` over `runuser` because on Fedora that also performs +the SELinux context transition needed to reach another user's session bus. On +Debian either works; `runuser` comes from `util-linux`, which is always present, +so the Debian path does not depend on `sudo` being installed. **If you rely on +`sudo` on Debian, install the `sudo` package explicitly** - the `kde-desktop` +task does not pull it in. + +## Firefox managed bookmarks + +Both families write the same file: + +``` +/etc/firefox/policies/policies.json (0644, root:root) +``` + +On Debian this is correct and must not be "fixed". `firefox-esr` 153.6 was traced +with `strace` and it stats exactly one path, `/etc/firefox/policies/policies.json` +- not `/etc/firefox-esr/policies/`, not `/usr/lib/firefox-esr/distribution/`. +Debian's build has `MOZ_SYSTEM_POLICIES` compiled in, which is why it honours the +standard location. Moving the file to `/etc/firefox-esr/policies/` because the +Debian package owns that directory would silently disable managed bookmarks. + +## Debian vs Fedora: known differences + +These are inherent to preseed vs kickstart, not oversights: + +- **Uppercase usernames are impossible via preseed.** `user-setup-udeb` validates + against `[a-z][-a-z0-9_]*$` and re-prompts on failure, which would stall an + unattended install. `VDI-ADMIN` is therefore created with `useradd` in + `late_command`, and `passwd/make-user` is set to false. +- **Disk detection cannot use `lsblk`.** d-i has no `lsblk` and no `openssl`, so + the kickstart's `lsblk -d -o rm` / `openssl passwd -6` have no direct + equivalent. The preseed reads `/sys/block/*/removable` plus `udevadm info`, and + sets passwords with `chpasswd` in the target instead of hashing in the + installer. +- **There is no `chvt`/`openvt` in d-i**, so the kickstart's "switch to TTY3 and + prompt" trick cannot be reproduced. The inventory number is prompted on + `/dev/console`, with a `vdi.inventory=NNNNN` kernel-cmdline override and a + `00000` fallback so an unattended run cannot hang forever. +- **Password hash format.** The kickstart hashes with `openssl passwd -6` + (SHA-512 crypt). `chpasswd` on trixie produces yescrypt by default. Same + outcome, different hash - worth knowing if anything inspects `/etc/shadow`. +- **`git clone` of the internal Gitea server fails soft on Debian.** The Fedora + kickstart aborts the install if the clone fails; the preseed logs and + continues, because the 10-minute cron job retries the work on the next run and + a half-provisioned machine is more useful than no machine. +- **The Citrix install is two steps.** On Fedora `dnf install ICAClient ctxusb` + resolves from the local repo directly. On Debian the packages come from the + local repo that `update_local_repo.py` builds, so `late_command` must run it + first, then `apt-get install -y icaclient ctxusb`, then `apt-get install -f -y`. + +## Testing + +`debian/test/run-tests.sh` exercises the Debian side on a real Debian 13 machine +and asserts the Fedora side is unregressed. Run it as root on a Debian 13 host or +WSL instance: + +```sh +bash debian/test/run-tests.sh +``` + +It builds a throwaway apt repository from the real Citrix `.deb` files, points a +temporary `sources.list` entry at it, and confirms apt can see and resolve the +packages. It does not touch `/var/local/citrix-repo` and cleans up after itself. +See `debian/test/README.md` for what it covers and what it cannot. + +### What the tests do NOT prove + +Being explicit, because these are the gaps that matter and it is easy to read a +green summary as more than it is: + +- **No real d-i install has been run.** `debian/preseed.cfg` is validated by + `debconf-set-selections` (the real parser), by checking every directive name + against the templates in the actual trixie udebs, and by executing + `late_command` with `/target` redirected to a scratch tree. That proves the + file parses, the directives exist, and the generated scripts are well formed. + It does not prove that an unattended install completes on real hardware - the + disk detection, the partitioner and the console prompt have never run in a + live installer. +- **The generated post-install script is not executed against a real target.** + `/root/vdi-post.sh` writes `/etc/hostname`, `/etc/hosts`, `/etc/crontab` and + `/etc/systemd/system/*` by absolute path, so running it on a build machine + would modify the build machine. It is covered by `bash -n` and by reading the + generated text, plus assertions on the inventory arithmetic it derives. +- **No KDE session has been exercised.** `configure_kde.py` is tested as far as + writing `/etc/xdg/*` and the autostart entry, and its `--session` path is + exercised only against a machine with no session bus (the clean no-op). The + live `busctl` calls against a running KWin/PowerDevil are untested. +- **The Citrix packages are never installed.** The tests prove apt can see them + and that dependency resolution succeeds (`apt-get install --simulate`), not + that `icaclient`'s pre/post-install scripts complete. Those scripts are large + and touch the X11 session; installing them in CI is its own project. +- **The Fedora path is verified by equivalence, not by running it on Fedora.** + The regression test compares the commands the Fedora code issues against + `main`. It does not perform a real `createrepo_c`/`dnf` cycle on a Fedora host. + +### Regression testing the Fedora path + +A file diff is the wrong instrument for `update_local_repo.py`, because the +Fedora and Debian code now share a file. The meaningful check is behavioural: +run the Fedora publishing path from both `main` and this branch against a stubbed +environment and compare the recorded sequence of operations. They must be +identical - same downloads, same destinations, same `createrepo_c`, `rpm +--import`, `dnf makecache` and `pkcon refresh force` calls, in the same order. + +`configure_kde.py`, `local-citrix-repo.repo` and `RPM-GPG-KEY-citrix-local` are +untouched by this branch, and `git diff main -- ` is empty. + +### Traps when testing on Debian + +- **Purge apt's list cache between cases.** `/var/lib/apt/lists` caches the + index, so a wrong signing key can still appear to work (exit 0, package + visible). Any test that does not purge produces a false pass. +- **`core.autocrlf=true` on a Windows checkout rewrites the worktree to CRLF.** + The repository stores LF, so Linux clients are fine, but copying the worktree + into a Linux test box gives `#!/usr/bin/python3\r` and direct execution fails + with a confusing "No such file or directory". Copy `git show HEAD:file` or strip + the CRs. +- **`strings` is not installed on a stock Debian image.** `strings ... | grep` + with stderr discarded returns empty and looks like a clean negative. Use + `grep -a` on the binary, or install `binutils`. +- **`firefox-esr --headless --dump-dom` hangs under WSL** (RenderCompositorSWGL + cannot map a framebuffer, and it exits 124 on timeout). A tiny DOM from it is + an error page, not a result. Trace the syscalls instead. diff --git a/debian/preseed.cfg b/debian/preseed.cfg new file mode 100644 index 0000000..0a2e5b8 --- /dev/null +++ b/debian/preseed.cfg @@ -0,0 +1,535 @@ +#_preseed_V1 +# +# Debian 13 "trixie" preseed - the port of the Fedora kickstart at +# ../../ks.cfg (one directory above this git repo). +# +# This file is the Debian equivalent of ks.cfg. Every block below names the +# kickstart directive it corresponds to, so a maintainer of the Fedora path can +# diff the two side by side. Where Debian's installer (d-i) cannot do what the +# kickstart does, the block says so and explains what is done instead - see +# "GAPS" at the end for the consolidated list. +# +# Directive existence: every "d-i " line below was checked against the +# real template files shipped in the trixie d-i udebs (preseed-common, +# partman-*, netcfg, user-setup-udeb, localechooser, tzsetup, console-setup, +# clock-setup, pkgsel, base-installer, apt-setup, grub-installer, finish-install, +# hw-detect, di-utils) plus tasksel/keyboard-configuration from the main archive. +# No directive here is guessed. +# +# Validated with: debconf-set-selections < preseed.cfg (see debian/README) + +# --------------------------------------------------------------------------- +# Installation source +# --------------------------------------------------------------------------- +# ks.cfg:1-2 url --mirrorlist=https://mirrors.fedoraproject.org/...fedora-44 +# Debian has no metalink; the canonical archive is the equivalent. Protocol, +# host and directory are preseeded individually so the mirror question is never +# asked and no network round-trip to the mirror list happens. +d-i mirror/country string DE +d-i mirror/protocol select http +d-i mirror/http/hostname string deb.debian.org +d-i mirror/http/directory string /debian +d-i mirror/http/proxy string +d-i mirror/suite string trixie +d-i mirror/codename string trixie + +# --------------------------------------------------------------------------- +# Language, keyboard, timezone +# --------------------------------------------------------------------------- +# ks.cfg:5 lang de_DE.UTF-8 +# language + country are set alongside locale so localechooser never asks: the +# shortlist question is skipped when the locale is already preseeded, and the +# two extra keys make the country/language selection deterministic too. +d-i debian-installer/language string de +d-i debian-installer/country string DE +d-i debian-installer/locale string de_DE.UTF-8 +# ks.cfg:5 asks for German only; do not pull in a second locale. +d-i localechooser/supported-locales multiselect de_DE.UTF-8 + +# ks.cfg:6 keyboard de +# On trixie there is no "debian-installer/keymap" template any more - it survives +# only as an alias handled by env-preseed, which maps "keymap" onto +# keyboard-configuration/xkb-keymap. The real question is the one below, and +# layoutcode is set as well because that is what ends up in +# /etc/default/keyboard as XKBLAYOUT. +d-i keyboard-configuration/xkb-keymap select de +d-i keyboard-configuration/layoutcode string de +d-i keyboard-configuration/modelcode string pc105 +d-i keyboard-configuration/variantcode string + +# ks.cfg:9 timezone Europe/Berlin +d-i time/zone string Europe/Berlin +# Fedora's installer sets the hardware clock to UTC by default and so does d-i; +# stated explicitly rather than relied on. +d-i clock-setup/utc boolean true +d-i clock-setup/ntp boolean true +d-i clock-setup/ntp-server string deb.debian.org + +# --------------------------------------------------------------------------- +# Accounts - root +# --------------------------------------------------------------------------- +# ks.cfg:12 rootpw --iscrypted +# Same hash, byte for byte. Type is "password" and the value is the crypt(3) +# string, which is exactly what d-i's user-setup-apply feeds to usermod +# --password, so no re-hashing happens anywhere. +d-i passwd/root-password-crypted password $6$3EpVOhxlL/EVU/8f$Z.M5x/x5OdzBFW2C0IoNo5KCsLURBJZnp1BgcMWFz8gsjEPSgAlBKEs7CgZnRAGjbrEzgvUCEydmjtZYvUraJ/ +# ks.cfg sets a root password, which on Fedora implies root may log in. d-i +# defaults to locking root when a non-root user exists; say so explicitly. +d-i passwd/root-login boolean true + +# --------------------------------------------------------------------------- +# Accounts - users +# --------------------------------------------------------------------------- +# ks.cfg:15 user --name=VDI-ADMIN --password --iscrypted --groups=wheel +# +# GAP 1 (important). This CANNOT be expressed as a preseed user. +# user-setup-udeb validates the username with +# LC_ALL=C expr "$USER" : '[a-z][-a-z0-9_]*$' +# (read out of user-setup-udeb 1.107, ./usr/bin/user-setup-ask, state 6). +# "VDI-ADMIN" is uppercase, so it fails validation, and on failure the state +# machine loops back and asks the question again interactively - which would +# hang an unattended install. So the built-in user creation is disabled here and +# BOTH users are created in preseed/late_command instead (see vdi-post.sh +# below), using useradd, which does accept it. Verified on Debian 13: +# useradd -m -G sudo -c 'VDI-00000' VDI-ADMIN -> rc=0 +# id VDI-ADMIN -> uid=1000(VDI-ADMIN) groups=...,27(sudo) +# Fedora's "wheel" group is Debian's "sudo" group. +d-i passwd/make-user boolean false +# The kickstart's second user (ks.cfg:85, generated in %pre) is vdi. +# That name would pass user-setup's validation, but the password is derived from +# the inventory number and the number is only known at run time, so both users +# are created together in late_command for consistency. +d-i passwd/username string vdi00000 +d-i passwd/user-fullname string VDI-00000 + +# --------------------------------------------------------------------------- +# Network +# --------------------------------------------------------------------------- +# ks.cfg:84 network --bootproto=dhcp --hostname=$HOSTNAME --activate +# The hostname is not a constant: it is EXTVDIL and is derived from the +# operator's answer in preseed/early_command. The value below is only the +# placeholder that is replaced there; "netcfg/hostname" is the documented +# "for internal use; can be preseeded" question that overrides whatever DHCP +# supplies, which is what --hostname does on Fedora. +d-i netcfg/enable boolean true +d-i netcfg/choose_interface select auto +d-i netcfg/get_hostname string EXTVDIL00000 +d-i netcfg/hostname string EXTVDIL00000 +# The kickstart does not set a domain and the VDI machines are not in one. +d-i netcfg/get_domain string +d-i netcfg/dhcp_timeout string 60 +d-i netcfg/use_autoconfig boolean true + +# --------------------------------------------------------------------------- +# Disk partitioning +# --------------------------------------------------------------------------- +# ks.cfg:53-56 (generated in %pre) +# ignoredisk --only-use=$DISK_NAME +# zerombr +# clearpart --all --initlabel +# autopart --fstype=ext4 +# +# Mapping: +# ignoredisk --only-use -> partman-auto/disk (restricts the automatic +# partitioner to exactly one disk) +# zerombr + --initlabel -> partman-partitioning/default_label gpt +# (relabels the disk, discarding the old table) +# clearpart --all -> partman-auto/method regular, which builds a fresh +# table from a recipe instead of reusing anything +# autopart --fstype -> partman/default_filesystem ext4 +# +# GAP 2 - WHEN the disk is detected, and why it is not preseed/early_command. +# The kickstart detects the disk in %pre, before anaconda partitions. In d-i the +# equivalent-looking hook, preseed/early_command, runs from +# env-preseed's S33preseed_early_command, which is before partman has probed the +# disks - Debian's own example preseed says so explicitly ("disks ... may not be +# visible when preseed/early_command runs") and names the correct hook: +# d-i partman/early_command \ +# string debconf-set partman-auto/disk "$(list-devices disk | head -n1)" +# partman-base runs that hook from ./lib/partman/init.d/01early_command, i.e. +# inside partman, with the disk list populated. So the detection lives in +# partman/early_command below. +# +# GAP 3 - the kickstart's two disk tools do not exist in d-i at all. +# `lsblk -d -o rm` and `openssl passwd -6` are both absent from every trixie +# udeb (checked by unpacking them all; d-i has no lsblk, no openssl, no +# python3, no chvt, no passwd binary). The detection below therefore reads +# /sys/block/*/removable and uses udevadm (present, from udev-udeb) for the USB +# test, and nothing is hashed in d-i - the inventory password is set with +# chpasswd in the installed system, which hashes it there. +# +# partman-auto/disk is deliberately NOT set statically: if detection ever fails +# the installer must fall back to the interactive partitioner rather than wipe a +# disk that merely matched a hard-coded default. Detection failure is therefore +# safe-by-construction: nothing is partitioned until the operator confirms. +d-i partman-auto/method string regular +d-i partman-auto/choose_recipe select atomic +d-i partman/default_filesystem string ext4 +d-i partman-partitioning/default_label string gpt +d-i partman/alignment select optimal +# Wipe any pre-existing LVM/MD metadata, the way zerombr/clearpart --all do. +d-i partman-lvm/device_remove_lvm boolean true +d-i partman-md/device_remove_md boolean true +# No "are you sure?" / "write changes?" dialogs - the kickstart is unattended. +d-i partman/confirm boolean true +d-i partman/confirm_nooverwrite boolean true + +# Detects the single internal disk, skipping USB and removable devices, then +# hands it to partman. Runs inside partman, after the disks are visible. +# This is the replacement for ks.cfg:18-60. +# +# The kickstart only ever looked at /dev/nvme[0-9]n[0-9] (so its own comment +# about "SATA, NVMe, eMMC" was never true). This scans every block device, +# prefers NVMe as the kickstart intended, and accepts SATA/eMMC/virtio as well. +d-i partman/early_command string \ + disk=""; \ + for d in /sys/block/*; do \ + n=$(basename "$d"); \ + case "$n" in loop*|ram*|zram*|sr*|dm-*|md*|fd*) continue ;; esac; \ + [ -b "/dev/$n" ] || continue; \ + [ "$(cat "$d/removable" 2>/dev/null || echo 1)" = 0 ] || continue; \ + if udevadm info -q property -n "/dev/$n" 2>/dev/null | grep -q "^ID_BUS=usb"; then continue; fi; \ + case "$n" in nvme*) disk="/dev/$n"; break ;; *) [ -n "$disk" ] || disk="/dev/$n" ;; esac; \ + done; \ + if [ -n "$disk" ]; then \ + debconf-set partman-auto/disk "$disk"; \ + debconf-set grub-installer/bootdev "$disk"; \ + echo "VDI: internal disk $disk" >> /var/log/vdi-preseed.log; \ + else \ + echo "VDI: no internal disk found - falling back to the interactive partitioner" >> /var/log/vdi-preseed.log; \ + fi + +# --------------------------------------------------------------------------- +# Inventory number (ks.cfg:18-93, the whole %pre section) +# --------------------------------------------------------------------------- +# The kickstart writes its results into /tmp/ks_dynamic.cfg and %includes it. +# d-i has no %include; the same effect is achieved by writing the derived values +# straight into debconf with debconf-set (from di-utils), which is the sanctioned +# idiom - Debian's example preseed uses it for partman-auto/disk. +# +# GAP 4 - the prompt itself. The kickstart switches to TTY3 with `chvt 3` and +# reads from /dev/tty3. d-i ships neither chvt nor openvt (confirmed absent from +# every udeb), and preseed_command runs the hook through `log-output`, which +# sends stdout to the log rather than to the screen. So the prompt is written to +# and read from /dev/console directly, which is the console the operator is +# actually looking at. +# +# Two safety valves the kickstart does not have, because an unattended run must +# not hang forever on a `read` that nobody answers: +# * vdi.inventory=NNNNN on the kernel command line skips the prompt entirely; +# * otherwise the prompt waits 120 s and then uses the kickstart's default, +# 00000. +# Both are documented in debian/README. +d-i preseed/early_command string \ + inv=""; \ + for a in $(cat /proc/cmdline); do case "$a" in vdi.inventory=*) inv="${a#vdi.inventory=}" ;; esac; done; \ + if [ -z "$inv" ] && [ -c /dev/console ]; then \ + printf "\n\nEnter the 5-digit inventory number for this device (default 00000): " > /dev/console; \ + IFS= read -r -t 120 inv < /dev/console || true; \ + fi; \ + [ -n "$inv" ] || inv=00000; \ + inv=$(printf "%s" "$inv" | tr -cd "0-9" | cut -c1-5); \ + [ -n "$inv" ] || inv=00000; \ + while [ ${#inv} -lt 5 ]; do inv="0${inv}"; done; \ + printf "%s" "$inv" > /tmp/vdi.inventory; \ + debconf-set netcfg/hostname "EXTVDIL${inv}"; \ + debconf-set netcfg/get_hostname "EXTVDIL${inv}"; \ + debconf-set passwd/username "vdi${inv}"; \ + debconf-set passwd/user-fullname "VDI-${inv}"; \ + echo "VDI: inventory=${inv}" >> /var/log/vdi-preseed.log +# +# ks.cfg:73 INVENTORY_NUM=$(printf "%05d" ${INVENTORY_NUM:0:5}) +# Reproduced, but with a zero-padding loop instead of printf: `printf "%05d"` +# treats the value as octal, so the kickstart's own line fails on an inventory +# number containing an 8 or 9 (e.g. 00019). The loop gives the same result and +# cannot fail. +# +# Derived values, all from ks.cfg:77-80: +# hostname EXTVDIL -> netcfg/hostname above +# username vdi -> passwd/username above +# full name VDI- -> passwd/user-fullname above +# password pwVDI# -> set with chpasswd in late_command +# (d-i has no openssl, see GAP 3) + +# --------------------------------------------------------------------------- +# Boot loader +# --------------------------------------------------------------------------- +# ks.cfg:96 bootloader --location=mbr +# The device is set from the detected disk in partman/early_command above, which +# is the same disk the partitioning is restricted to. "default" would also work +# but naming the disk is closer to --location=mbr and cannot pick the wrong one. +d-i grub-installer/only_debian boolean true +d-i grub-installer/with_other_os boolean false + +# --------------------------------------------------------------------------- +# Package selection +# --------------------------------------------------------------------------- +# ks.cfg:99-105 +# @^kde-desktop-environment +# createrepo_c +# python3-pip +# firefox +# git +# +# @^kde-desktop-environment -> the "kde-desktop" task (verified in +# tasksel-data 3.81: Task: kde-desktop, Parent: desktop). "standard" is the +# Debian equivalent of the base group anaconda always installs. +# firefox -> firefox-esr (trixie has no package called +# "firefox"; verified in the trixie Packages index) +# createrepo_c -> apt-utils, which is what provides apt-ftparchive, +# the tool that builds a flat apt repository. There is no createrepo on +# Debian and no createrepo_c either. +# python3-pip -> DROPPED. ks.cfg:143 installs pip only in order to run +# `pip install tqdm`, and that one use is served by python3-tqdm from apt (GAP 5). +# Debian 13 marks /usr/lib/python3/dist-packages EXTERNALLY-MANAGED (PEP 668), so +# any user who tries `pip install ` after install gets +# "error: externally-managed-environment" and has to reach for +# --break-system-packages. Measured on trixie, not assumed: `pip3 install +# requests` is blocked AND so is `pip3 install --user six`. So installing +# python3-pip would not make pip usable - it only changes the error from +# "command not found" to "externally-managed-environment". It adds no capability +# that this image would otherwise have. This is a deliberate difference from the +# Fedora kickstart. The supported route is a venv, which is why python3-venv is +# installed below instead. +tasksel tasksel/first multiselect standard, kde-desktop +# Package list notes, one per entry that is not a straight rename: +# python3 - update_local_repo.py is python3 and the cron job runs it. +# A bare trixie install has no python3, and without it the +# cron job fails every 10 minutes with only a log line. +# apt-utils - provides apt-ftparchive, which update_local_repo.py calls +# to build the local Citrix repo (the createrepo_c slot). +# python3-tqdm - the tqdm dependency, from apt instead of pip (GAP 5). +# python3-venv - the replacement for the dropped python3-pip: a venv is the +# only way to install a third-party Python package on trixie. +# `python3 -m venv` needs this package to create a working +# pip inside the venv; without it a user has no route at all. +# Verified on trixie: python3 -m venv gives pip 25.1.1 and +# `venv/bin/pip install six` succeeds (EXTERNALLY-MANAGED does +# not apply inside a venv). It pulls python3-pip-whl, which is +# pip bundled as a wheel for venv use - that is NOT python3-pip +# and does not put a system pip3 on PATH. +# wget - the teamviewer key download in the post-install script +# uses wget (present in trixie as 1.25.0-2). +# sudo - needed for the "sudo" group. The kde-desktop task does not +# pull sudo in, and `useradd -G sudo` would otherwise +# succeed with a warning and leave VDI-ADMIN unable to sudo. +# cron - the kickstart appends to /etc/crontab and restarts crond; +# Debian does not install cron by default. +# packagekit-tools - provides pkcon, used by /root/auto_update.sh. +# iputils-ping - the @reboot cron line waits on `ping google.com`. +d-i pkgsel/include string git python3 python3-tqdm python3-venv firefox-esr apt-utils cron sudo ca-certificates wget iputils-ping packagekit-tools systemd +d-i pkgsel/run_tasksel boolean true +# ks.cfg does not upgrade the system during install, and does not enable +# automatic security updates (it runs its own pkcon-based updater instead). +d-i pkgsel/upgrade select none +d-i pkgsel/update-policy select none +d-i pkgsel/updatedb boolean false +d-i base-installer/install-recommends boolean true +# +# GAP 5 - tqdm. ks.cfg:143 does `pip install tqdm`, because Fedora ships pip and +# lets it write into /usr. Debian 13 does not: pip3 is not installed by default, +# and /usr/lib/python3.13/EXTERNALLY-MANAGED is present (PEP 668), so a +# system-wide pip install is refused even after installing pip. The Debian way, +# and what this file does, is the distribution package python3-tqdm +# (4.67.1-5 on trixie). `pip install --break-system-packages` is deliberately +# not used, and because the only thing the kickstart wanted pip for was tqdm, +# python3-pip itself is not installed either - see the package-selection note +# above. Measured on trixie: `pip3 install X` and `pip3 install --user X` are both +# refused with externally-managed-environment, so pip would have been dead weight. +# python3-venv is installed in its place, which is the supported route. +# +# GAP 6 - `pkcon` is used by the auto-update script below. On Fedora it comes +# with the desktop; on Debian it is in packagekit-tools, which is therefore in +# the list above. iputils-ping is there because the kickstart's @reboot cron +# line waits on `ping google.com` and Debian does not install ping by default. + +# --------------------------------------------------------------------------- +# Remainder of the installer, made non-interactive +# --------------------------------------------------------------------------- +# There is no kickstart equivalent for these; they exist so that nothing else +# stops and waits for a human. +d-i hw-detect/load_firmware boolean false +d-i apt-setup/services-select multiselect security, updates +d-i apt-setup/enable-source-repositories boolean false +d-i finish-install/keep-consoles boolean false +d-i cdrom-detect/eject boolean false + +# --------------------------------------------------------------------------- +# %post (ks.cfg:108-221) +# --------------------------------------------------------------------------- +# The kickstart's %post is a multi-line bash script with `set -euxo pipefail`. +# A preseed value is a single line (continuations are joined with spaces), so a +# multi-line script cannot be written inline. Instead late_command writes the +# script to /root/vdi-post.sh in the installed system, line by line, and then +# runs it with bash. That keeps it a real script - which is what makes +# `set -euo pipefail` possible at all, since preseed_command itself uses +# /bin/sh (dash, no pipefail) - and it stays readable here. +# +# The script is reproduced verbatim below as a comment so it can be reviewed +# against ks.cfg without decoding anything. The printf arguments that follow it +# are exactly these lines, in this order. +# +# ---8<--- /root/vdi-post.sh ---8<--- +# #!/bin/bash +# set -euo pipefail +# INV="${1:-00000}" +# LOG=/root/post-install.log +# exec >>"$LOG" 2>&1 +# echo "=== VDI post-install, inventory ${INV} ===" +# HOSTNAME="EXTVDIL${INV}" +# USERNAME="vdi${INV}" +# FULLNAME="VDI-${INV}" +# PASSWORD="pwVDI#${INV}" +# ROOT_HASH='$6$3EpVOhxlL/EVU/8f$Z.M5x/x5OdzBFW2C0IoNo5KCsLURBJZnp1BgcMWFz8gsjEPSgAlBKEs7CgZnRAGjbrEzgvUCEydmjtZYvUraJ/' +# printf "%s\n" "$HOSTNAME" > /etc/hostname +# printf "127.0.1.1\t%s\n" "$HOSTNAME" >> /etc/hosts +# hostnamectl set-hostname "$HOSTNAME" 2>/dev/null || true +# getent group sudo >/dev/null || groupadd sudo +# if ! id VDI-ADMIN >/dev/null 2>&1; then +# useradd -m -s /bin/bash -G sudo -c "VDI Admin" -p "$ROOT_HASH" VDI-ADMIN +# fi +# if ! id "$USERNAME" >/dev/null 2>&1; then +# useradd -m -s /bin/bash -c "$FULLNAME" "$USERNAME" +# fi +# printf "%s:%s\n" "$USERNAME" "$PASSWORD" | chpasswd +# if ! wget -q -O /usr/share/keyrings/teamviewer.asc https://linux.teamviewer.com/pubkey/currentkey.asc; then +# echo "teamviewer key download failed" +# fi +# apt-get update || echo "apt-get update failed" +# if [ -d /root/VDI-Linux/.git ]; then +# ( cd /root/VDI-Linux && git pull ) || echo "git pull failed - continuing" +# else +# git clone https://nachos386.lcube-server.de/REYHER-Systemtechnik-Public/VDI-Linux /root/VDI-Linux || echo "git clone failed - continuing" +# fi +# if [ -f /root/VDI-Linux/update_local_repo.py ]; then +# ( cd /root/VDI-Linux && python3 ./update_local_repo.py ) || echo "update_local_repo.py failed" +# fi +# apt-get update || echo "apt-get update failed" +# apt-get install -y icaclient ctxusb || echo "citrix install failed" +# apt-get install -f -y || true +# apt-get install -y teamviewer || echo "teamviewer install failed" +# cat > /root/auto_update.sh </dev/null 2>&1; then +# pkcon offline-trigger +# fi +# " +# EOF +# chmod +x /root/auto_update.sh +# cat > /etc/systemd/system/disable-sleep.service <> /var/log/update_local_repo.log 2>&1" >> /etc/crontab +# printf "%s\n" "@reboot root bash -c \"until ping google.com -c1 >/dev/null 2>&1; do sleep 1; done;\" && cd /root/ && ./auto_update.sh >> /var/log/update_local_repo.log 2>&1" >> /etc/crontab +# systemctl enable disable-sleep.service || echo "enable disable-sleep failed" +# systemctl enable cron || echo "enable cron failed" +# echo "VDI post-install complete" +# ---8<--- end /root/vdi-post.sh ---8<--- +# +# Differences from the kickstart's %post, all forced by Debian: +# +# * Firefox policies path (ks.cfg:113-136) is UNCHANGED: on trixie +# /etc/firefox/policies/policies.json is the path firefox-esr actually reads, +# with MOZ_SYSTEM_POLICIES compiled in. It is written from late_command +# straight into /target (mode 644, root:root) rather than from the script, +# because no chroot is needed to create a file. +# * pip install tqdm (ks.cfg:143) -> python3-tqdm from the package list; see +# GAP 5. +# * dnf install ICAClient / ctxusb (ks.cfg:147-149) -> apt-get install icaclient +# ctxusb, after update_local_repo.py has built the local flat repo at +# /var/local/citrix-repo. The Fedora RPM names differ in case from the Debian +# package names. +# * dnf install teamviewer from the yum repo (ks.cfg:151-167) -> apt-get from +# TeamViewer's apt repo. The repo file is written by late_command; the suite +# is "stable" (TeamViewer publishes stable, not trixie) and the key is +# fetched over https and pinned with signed-by. +# * `git clone` is FAIL-SOFT here. The kickstart runs with set -e and aborts the +# whole %post if the internal Gitea server is unreachable, which leaves a +# half-provisioned machine. The 10-minute cron job picks the clone up on its +# next run, so the Debian path logs the failure and continues. This is a +# deliberate behavioural difference, not an oversight. +# * `openssl passwd -6` in %pre (ks.cfg:83) produced a SHA-512 crypt hash for +# the vdi password. d-i has no openssl, so that password is set in +# the installed system with chpasswd, which uses whatever crypt scheme the +# target's PAM default is - yescrypt on trixie, not SHA-512. The password +# VALUE is identical; only the stored hash format differs. The root and +# VDI-ADMIN hashes are still the kickstart's exact SHA-512 strings, passed +# through unchanged, so those two accounts match the Fedora machines exactly. +# * `systemctl start disable-sleep.service` (ks.cfg:217) is dropped and only +# `systemctl enable` is kept: late_command runs inside a chroot where systemd +# is not the init process, so `start` cannot work. The unit is enabled and is +# WantedBy=multi-user.target, so it starts on first boot - same end state. +# For the same reason `systemctl daemon-reload` is not called; nothing needs +# it before boot. +# * `systemctl restart crond` (ks.cfg:190) -> `systemctl enable cron`. The +# service is called cron on Debian, and it is not running in the chroot. +# * The commented-out hourly auto_update cron line (ks.cfg:186) is not carried +# over - it is dead in the kickstart too. +# +# late_command: +# 1. writes /etc/firefox/policies/policies.json (ks.cfg:113-135) +# 2. writes /etc/apt/sources.list.d/teamviewer.list (ks.cfg:152-162) +# 3. writes /root/vdi-post.sh from the lines above and runs it under bash +d-i preseed/late_command string \ + inv=$(cat /tmp/vdi.inventory 2>/dev/null || echo 00000); \ + echo "VDI: late_command, inventory=${inv}" >> /var/log/vdi-preseed.log; \ + mkdir -p /target/etc/firefox/policies; \ + printf "%s\n" "{" " \"policies\": {" " \"ManagedBookmarks\":" " [" " {" " \"toplevel_name\": \"Reyher\"" " }," " {" " \"name\": \"Arbeit\"," " \"url\": \"https://workspace.reyher.de/\"" " }" " ]" " }" "}" > /target/etc/firefox/policies/policies.json; \ + chmod 644 /target/etc/firefox/policies/policies.json; \ + chown 0:0 /target/etc/firefox/policies/policies.json; \ + mkdir -p /target/etc/apt/sources.list.d /target/usr/share/keyrings; \ + printf "%s\n" "deb [signed-by=/usr/share/keyrings/teamviewer.asc] https://linux.teamviewer.com/deb stable main" > /target/etc/apt/sources.list.d/teamviewer.list; \ + printf "%s\n" '#!/bin/bash' 'set -euo pipefail' 'INV="${1:-00000}"' 'LOG=/root/post-install.log' 'exec >>"$LOG" 2>&1' 'echo "=== VDI post-install, inventory ${INV} ==="' 'HOSTNAME="EXTVDIL${INV}"' 'USERNAME="vdi${INV}"' 'FULLNAME="VDI-${INV}"' 'PASSWORD="pwVDI#${INV}"' "ROOT_HASH='\$6\$3EpVOhxlL/EVU/8f\$Z.M5x/x5OdzBFW2C0IoNo5KCsLURBJZnp1BgcMWFz8gsjEPSgAlBKEs7CgZnRAGjbrEzgvUCEydmjtZYvUraJ/'" 'printf "%s\n" "$HOSTNAME" > /etc/hostname' 'printf "127.0.1.1\t%s\n" "$HOSTNAME" >> /etc/hosts' 'hostnamectl set-hostname "$HOSTNAME" 2>/dev/null || true' 'getent group sudo >/dev/null || groupadd sudo' 'if ! id VDI-ADMIN >/dev/null 2>&1; then' ' useradd -m -s /bin/bash -G sudo -c "VDI Admin" -p "$ROOT_HASH" VDI-ADMIN' 'fi' 'if ! id "$USERNAME" >/dev/null 2>&1; then' ' useradd -m -s /bin/bash -c "$FULLNAME" "$USERNAME"' 'fi' 'printf "%s:%s\n" "$USERNAME" "$PASSWORD" | chpasswd' 'if ! wget -q -O /usr/share/keyrings/teamviewer.asc https://linux.teamviewer.com/pubkey/currentkey.asc; then' ' echo "teamviewer key download failed"' 'fi' 'apt-get update || echo "apt-get update failed"' 'if [ -d /root/VDI-Linux/.git ]; then' ' ( cd /root/VDI-Linux && git pull ) || echo "git pull failed - continuing"' 'else' ' git clone https://nachos386.lcube-server.de/REYHER-Systemtechnik-Public/VDI-Linux /root/VDI-Linux || echo "git clone failed - continuing"' 'fi' 'if [ -f /root/VDI-Linux/update_local_repo.py ]; then' ' ( cd /root/VDI-Linux && python3 ./update_local_repo.py ) || echo "update_local_repo.py failed"' 'fi' 'apt-get update || echo "apt-get update failed"' 'apt-get install -y icaclient ctxusb || echo "citrix install failed"' 'apt-get install -f -y || true' 'apt-get install -y teamviewer || echo "teamviewer install failed"' 'cat > /root/auto_update.sh </dev/null 2>&1; then' ' pkcon offline-trigger' ' fi' '"' 'EOF' 'chmod +x /root/auto_update.sh' 'cat > /etc/systemd/system/disable-sleep.service <> /var/log/update_local_repo.log 2>&1" >> /etc/crontab' 'printf "%s\n" "@reboot root bash -c \"until ping google.com -c1 >/dev/null 2>&1; do sleep 1; done;\" && cd /root/ && ./auto_update.sh >> /var/log/update_local_repo.log 2>&1" >> /etc/crontab' 'systemctl enable disable-sleep.service || echo "enable disable-sleep failed"' 'systemctl enable cron || echo "enable cron failed"' 'echo "VDI post-install complete"' > /target/root/vdi-post.sh; \ + chmod 700 /target/root/vdi-post.sh; \ + in-target bash /root/vdi-post.sh "$inv" + +# =========================================================================== +# GAPS - what the preseed mechanism cannot reproduce, consolidated +# =========================================================================== +# 1. `user --name=VDI-ADMIN` - impossible. user-setup rejects uppercase names +# and re-prompts (hangs an unattended install). Done in late_command with +# useradd instead. wheel -> sudo. +# 2. Disk detection cannot run in preseed/early_command - disks are not visible +# yet. Done in partman/early_command, the hook Debian documents for exactly +# this. +# 3. `lsblk` and `openssl` do not exist in d-i. Detection uses +# /sys/block/*/removable + udevadm; hashing is done in the installed system +# by chpasswd. +# 4. `chvt 3` / /dev/tty3 prompt - no chvt or openvt in d-i, and preseed_command +# redirects stdout to the log. Prompt goes to /dev/console, with a kernel +# cmdline override and a timeout so it cannot hang forever. +# 5. `pip install tqdm` - PEP 668 on Debian 13. Uses python3-tqdm. +# 6. `createrepo_c` - no Debian equivalent; apt-utils (apt-ftparchive) is +# installed instead for the local Citrix repo. +# 7. `systemctl start` inside %post - no systemd in the chroot. The unit is +# enabled instead and starts on first boot. +# 8. `printf "%05d"` on the inventory number - the kickstart's own line breaks +# on a number containing 8 or 9 (octal). Replaced with a padding loop. +# 9. Password hash format for vdi - the kickstart's `openssl passwd -6` +# made SHA-512; chpasswd on trixie makes yescrypt. Same password, different +# stored hash. Root and VDI-ADMIN keep the kickstart's exact SHA-512 hashes. +# 10. The kickstart's own `printf "%05d"` bug (see 8) is fixed here, so an +# inventory number such as 00019 installs correctly on Debian and would +# not have on Fedora. Flagged rather than silently matched. +# 11. `git clone` of the internal repo is FAIL-SOFT here, where the kickstart +# (which runs under `set -euxo pipefail`) would abort the whole %post. The +# reason: on Fedora the repo host was reachable during install; on a Debian +# VDI the install may legitimately finish before the corporate network is up, +# and aborting would leave a half-configured machine. The clone is logged and +# the 10-minute cron job retries it. THIS IS A DELIBERATE BEHAVIOURAL +# DIFFERENCE, not an oversight - if the repo must be present before first +# boot, this line needs to fail hard instead and the installer needs to wait +# for network. diff --git a/debian/test/README.md b/debian/test/README.md new file mode 100644 index 0000000..7ae28de --- /dev/null +++ b/debian/test/README.md @@ -0,0 +1,255 @@ +# Debian port test harness + +`run-tests.sh` proves that the Debian side of the VDI-Linux port actually works, +and that the Fedora side has not regressed. It is a real harness: it builds a +real apt repository from the real Citrix `.deb` packages, adds a real apt +source, and asks real apt whether it can install the result. Nothing is +simulated except the parts that would change the machine. + +```sh +sudo bash debian/test/run-tests.sh +``` + +Run as root. It needs the two Citrix `.deb` files (see [Fixtures](#fixtures)). + +Exit status is 0 when no test FAILED, 1 otherwise. A SKIP does not fail the run, +but every skip prints why it skipped and what it would have checked. **A skip is +not a pass**, and the summary says so. + +## What it tests + +| Test | What it proves | +|------|----------------| +| T00 | Environment: root, required tools, trixie, fixtures present | +| T01 | The `.deb` fixtures really are version `26.08.0.225` | +| T02 | `python3 -m py_compile` passes for `update_local_repo.py` and `configure_kde.py` | +| T03 | Both modules **import** cleanly on Debian (no `tqdm`, no `pip`) | +| T04 | The `tqdm` fallback is usable, not merely importable | +| T05 | The flat repo builds; `Packages` has exactly 2 stanzas at the right version | +| T06 | `apt-get update` exits 0 with no `E:`, no `Err:` and no warnings for the source | +| T07 | apt can *see* the package: correct candidate, correct origin, install resolves | +| T08 | Rebuilding twice is byte-identical and produces no duplicate stanzas | +| T09 | `sync_debian` prunes stale `.deb` files (version bump cannot leave two versions) | +| T10 | A corrupted index is rejected even with `trusted=yes` (control for T06/T07) | +| T11 | The production `local-citrix-repo.list` has the shape the harness verified | +| T12 | GPG `signed-by` path (design check; skips without a signing tool) | +| T13 | Fedora path unregressed: tracked files identical, `sync_fedora` command sequence identical | +| T14 | `bash -n` on every shell script the port adds | +| T15 | Nothing was left behind: no source file, no cached indexes, no installs, no `__pycache__` | + +### The repo build it tests + +The exact production sequence, on a copy of the real `.deb` files in a temp +directory: + +```sh +cd "$repo" +apt-ftparchive packages . > Packages +gzip -c Packages > Packages.gz +apt-ftparchive release . > Release +``` + +and the exact production source line: + +``` +deb [trusted=yes] file:/var/local/citrix-repo ./ +``` + +with the path substituted for the temp directory. A flat ("trivial") archive: +no `dists/` tree, suite `./`. + +## What it does NOT test, and cannot + +Being explicit matters more than the green summary. + +1. **A real d-i install.** The harness never boots an installer. `debian/preseed.cfg` + is owned and validated by another change; this harness does not test it at all. + Note that `debconf-set-selections` validates line shape only - it silently accepts + an invented question name - so it is not evidence that directives exist. + +2. **An actual GUI session.** Nothing here starts Plasma, and no test asserts that + `configure_kde.py` produces a working desktop. T03 proves it *imports*; whether + the touchpad and power settings take effect on a real session is untested. + `kwriteconfig6` is not installed here, so `configure_kde.KWRITE` is `None` and + every write would no-op. That path needs a real Plasma machine. + +3. **The Citrix preinst/postinst maintainer scripts.** `apt-get install --simulate` + resolves dependencies; it does not run the package's scripts. Whether + `icaclient`'s postinst succeeds on a real machine is untested. This is the + single biggest gap: a dependency-resolvable package can still fail to configure. + +4. **A real d-i/target filesystem for the repo path.** The harness builds the repo + in a temp directory, never at `/var/local/citrix-repo`. It asserts the + *shape* is identical (T11), not that the production path is writable in a + target environment. + +5. **The network path.** The Gitea release API and the download code are not + exercised. T09 redirects the source files to a local directory, so + `download_file` is only exercised indirectly. A changed release asset name + would not be caught here. + +6. **`apt-get update` inside a d-i chroot.** The preseed's `late_command` runs in + an `in-target` context where apt behaves differently (no `_apt` user, different + permissions). Untested. + +7. **Fedora at runtime.** T13 compares source and command sequences; it does not + run `createrepo_c`/`dnf`/`rpm` on Fedora. The command sequence matching is + strong evidence but it is not an execution test. A Fedora 44 WSL distro exists + on the development machine and was not used. + +## Two spec'd checks that cannot fail, and what replaced them + +This is the most important thing in this file. Two checks that look like tests +are not tests. + +### `apt-get update` exiting 0 with no `E:` lines + +Measured on Debian 13: a repo whose `Packages` is **unreadable** produces + +``` +Err:4 file:/tmp/.../repo ./ Packages + Could not open file .../Packages - open (13: Permission denied) +``` + +and `apt-get update` still **exits 0 with no `E:` lines**. The same is true of a +repo with **no `Release` file at all**, and of a repo with **duplicate stanzas**. +So "exit 0 and no `E:`" passes on a broken repo. + +The harness runs the spec'd check as T06 *and* the stronger check separately: no +`Err:` lines, no `W:` lines for that source, and then T07 requires apt to name the +exact version from that exact directory. T10 is the control that proves the +strong checks can fail: it corrupts `Packages` after `Release` was built and +asserts apt **rejects** it with a hash mismatch. Without T10, T06/T07 could be +passing vacuously. + +### `git diff main -- ks.cfg` + +`ks.cfg` is **not in the repository**. It is not in `main`, not in `debian-port`, +and `git log --all -- ks.cfg` is empty. It lives one level above the repo, in the +parent directory, untracked. + +So `git diff main -- ks.cfg` exits 0 and prints nothing - because the path does +not exist on either side, not because the file is unchanged. The harness detects +this with `git cat-file -e main:ks.cfg` and reports it as **SKIP, explicitly +labelled VACUOUS**, rather than as a pass. It then prints the sha256 of the real +out-of-tree file so a change to it is at least visible. + +## Line endings will fool you + +This repo is checked out on Windows. Every tracked file in the working tree has +CRLF line endings; every blob in git has LF. A plain `git diff main` therefore +reports **all five files as completely rewritten**, including +`RPM-GPG-KEY-citrix-local` with 104 changed lines - the key itself is byte-identical. + +The harness compares modulo CRLF (`tr -d '\r' | cmp`). If you see a huge diff, +check line endings before believing it. + +## The apt index cache will fool you + +`apt-cache policy` and `apt-get update` read cached indexes in +`/var/lib/apt/lists`. During development, a **wrong GPG key** appeared to be +accepted - `exit 0`, no `E:`, package still visible - purely because a good index +was cached from an earlier run. + +Every case that must fail purges that repo's cached index files first +(`purge_apt_lists_for`). Any test that checks "can apt see the package" without +purging is testing the cache, not apt. + +Related: `/tmp` is wiped between separate `wsl` invocations on this machine +(`systemd-tmpfiles`). Anything expected to survive across invocations must live +in `/root`. + +## GPG signing: why `trusted=yes` is the right call + +Production does not sign the Debian repo, and T12 exists to check that decision +rather than assume it. A stock trixie ships **no `gpg`** - only `/usr/bin/sqv`, +the Sequoia verifier that apt calls internally. So signing would add a `gpg` +dependency that Debian 13 does not have by default, in an installer environment +where new dependencies are the fragile thing. + +The signing path was measured out of band (a throwaway key generated with Fedora +WSL's `gpg`, because Debian has none), with the apt cache purged between cases: + +| Keyring | Result | +|---------|--------| +| armored `.asc`, correct key | exit 0, no `E:`, package visible | +| armored `.asc`, **wrong** key | exit 100, `E:`, `Missing key`, not visible | +| binary `.gpg`, correct key | exit 0, no `E:`, package visible | +| binary `.gpg`, **wrong** key | exit 100, `E:`, `Missing key`, not visible | +| `trusted=yes`, no signature | exit 0, no `E:`, package visible | + +Two conclusions: + +- `signed-by` **works correctly** on a flat `file:` repo on trixie, for both + keyring formats. It is not broken; it is simply not worth it here. +- `trusted=yes` does **not** mean "no integrity checking". T10 shows a tampered + `Packages` is rejected with `E: ... Hash Sum mismatch` regardless. Signing + would only add proof of *authorship*, which is meaningless for a repository the + machine builds from packages it just downloaded and verified itself. + +T12 skips loudly when no signing tool is present, which is the normal case on a +stock trixie. Install `gpg` to make it run. + +## Fixtures + +The two real packages are expected next to the repo, not inside it: + +``` +LinuxISO/ + icaclient_26.08.0.225_amd64.deb (256 MB) + ctxusb_26.08.0.225_amd64.deb (178 KB) + VDI-Linux/ <- the repo + debian/test/run-tests.sh <- this harness +``` + +Override with `DEB_DIR=/path/to/debs sudo -E bash debian/test/run-tests.sh`. + +The 256 MB file is copied into a temp directory, which is fast on native +filesystem and slow off `/mnt/c`. If `/tmp` is small, set `TMPDIR` to somewhere +with ~600 MB free. + +## Requirements + +- Debian 13 (trixie), root. +- `apt-ftparchive`, `apt-get`, `apt-cache`, `dpkg-deb`, `python3`, `gzip` - all + present in a stock install. +- `git` **for T13 only**. It is not part of a stock trixie image; without it T13 + skips and says so. Install it if you want the Fedora regression checks to run: + `apt-get install -y --no-install-recommends git`. Note this changes the test + machine, which is why the harness treats its absence as a skip rather than + installing it itself. +- `gpg` **for T12 only**, and only if you want to re-check the signing decision. + +## Safety + +The harness is designed to be safe to run on a machine you care about. + +- It never writes to `/var/local/citrix-repo`. It records that directory's mtime + before the run and asserts it is unchanged in T15. +- It never installs anything. The only `apt-get install` is `--simulate`. T15 + compares the installed package count before and after. +- It adds exactly one apt source file, `/etc/apt/sources.list.d/zz-vdi-test-$$.list`, + and removes it. T15 verifies the removal happened. +- It removes only the apt index files it created, matched by the encoded repo path, + never `/var/lib/apt/lists` wholesale. +- It removes any keyring it installed, matched by the `zz-vdi-test-*` prefix, and + only `rmdir`s `/etc/apt/keyrings` (which fails harmlessly if it was already there + with other keys in it). +- It never writes `__pycache__` into the repo: `PYTHONDONTWRITEBYTECODE=1` and + `PYTHONPYCACHEPREFIX` are set, and T15 asserts the repo is clean. +- All scratch work happens in one `mktemp -d` directory, removed at the end. +- A failing test does not abort the run, so you get the whole picture in one pass. + +## Interpreting a failure + +- **T05/T06/T07 failing together** - the repo build is broken. Look at T05 first. +- **T07 fails but T05 passes** - the repo is fine but apt cannot consume it; check + directory permissions (apt drops to `_apt`, so the repo path must be traversable). +- **T09 fails** - `sync_debian` is leaving old versions in the published + directory. On Debian, filenames carry the version, so copying without deleting + leaves every previous release indexed and makes rollback impossible. +- **T13 fails on the command sequence** - compare the printed `MAIN-CMDS` and + `FEDORA-CMDS` lists. Both sides are normalised to ``; if they differ, the + Fedora path genuinely changed. +- **T15 fails on residue** - the teardown did not finish. Check the messages + immediately above it. diff --git a/debian/test/run-tests.sh b/debian/test/run-tests.sh new file mode 100644 index 0000000..596d81a --- /dev/null +++ b/debian/test/run-tests.sh @@ -0,0 +1,1002 @@ +#!/usr/bin/env bash +# +# run-tests.sh - Debian port test harness for the VDI-Linux provisioning repo. +# +# Proves the Debian side of the port works and that the Fedora side is +# unregressed. Run as root on a Debian 13 (trixie) machine: +# +# sudo bash debian/test/run-tests.sh +# +# Exit status: 0 if no test FAILED, 1 otherwise. SKIPped tests do not fail the +# run, but every SKIP prints why it skipped and what it would have checked. +# +# The harness is self-cleaning. It never writes to /var/local/citrix-repo, +# never installs a package (apt-get install is only ever --simulate), and +# removes every apt source file and cached index it adds. See README.md. + +set -uo pipefail + +# --------------------------------------------------------------------------- +# Configuration +# --------------------------------------------------------------------------- + +SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +REPO_ROOT=$(cd "$SCRIPT_DIR/../.." && pwd) + +# The two real Citrix .deb files live next to the repo, not inside it. +DEB_DIR="${DEB_DIR:-$(dirname "$REPO_ROOT")}" + +ICACLIENT_DEB="icaclient_26.08.0.225_amd64.deb" +CTXUSB_DEB="ctxusb_26.08.0.225_amd64.deb" +EXPECTED_VERSION="26.08.0.225" + +# The production apt source file the Debian path installs. +PROD_SOURCE_FILE="$REPO_ROOT/local-citrix-repo.list" +PROD_REPO_DIR="/var/local/citrix-repo" + +TMP_DIR=$(mktemp -d "${TMPDIR:-/tmp}/vdi-debian-test.XXXXXX") +REPO_DIR="$TMP_DIR/repo" +TAMPER_DIR="$TMP_DIR/tamper" +SANDBOX_DIR="$TMP_DIR/sandbox" +LIST_FILE="/etc/apt/sources.list.d/zz-vdi-test-$$.list" +KEYRING_DIR=/etc/apt/keyrings + +# Never let Python write __pycache__ into the repo: py_compile would otherwise +# leave an untracked directory behind and dirty the working tree. +export PYTHONPYCACHEPREFIX="$TMP_DIR/pycache" +export PYTHONDONTWRITEBYTECODE=1 + +PASSED=0 +FAILED=0 +SKIPPED=0 +FAILED_NAMES="" + +# /var/local/citrix-repo must not be touched; record its state up front. +PROD_REPO_MTIME_BEFORE="" +if [ -d "$PROD_REPO_DIR" ]; then + PROD_REPO_MTIME_BEFORE=$(stat -c '%Y' "$PROD_REPO_DIR" 2>/dev/null || echo "") +fi + +# T15 asserts nothing was installed; record the baseline now. +INSTALLED_BEFORE="" +if command -v dpkg-query >/dev/null 2>&1; then + INSTALLED_BEFORE=$(dpkg-query -W -f='${Package}\n' 2>/dev/null | wc -l) +fi + +# --------------------------------------------------------------------------- +# Reporting helpers +# --------------------------------------------------------------------------- + +pass() { printf 'PASS %s\n' "$1"; PASSED=$((PASSED + 1)); } + +# Keep failure messages readable: a Python traceback inside a FAIL line buries +# the summary. The caller prints the full detail above the FAIL line. +fail() { + local msg="$1" + if [ "${#msg}" -gt 240 ]; then + msg="$(printf '%s' "$msg" | cut -c1-240)... (truncated; see output above)" + fi + printf 'FAIL %s\n' "$msg" + FAILED=$((FAILED + 1)) + FAILED_NAMES="$FAILED_NAMES + - $msg" +} +skip() { printf 'SKIP %s\n' "$1"; SKIPPED=$((SKIPPED + 1)); } +info() { printf ' %s\n' "$1"; } +head2() { printf '\n== %s\n' "$1"; } + +# apt encodes a source path into a list filename by replacing '/' with '_', +# e.g. file:/root/x/repo -> _root_x_repo_._Packages +list_fragment() { printf '%s' "$1" | sed 's|/|_|g'; } + +purge_apt_lists_for() { + local dir="$1" frag + frag=$(list_fragment "$dir") + rm -f /var/lib/apt/lists/*"$frag"* 2>/dev/null + rm -f /var/lib/apt/lists/partial/*"$frag"* 2>/dev/null + return 0 +} + +cleanup() { + local status=$? + + # T15 already tore everything down and verified it, then printed the + # summary. Running the teardown again here would only risk disturbing it. + if [ "${CLEANED:-0}" = "1" ]; then + exit $status + fi + + printf '\n== cleanup\n' + + rm -f "$LIST_FILE" 2>/dev/null + # Remove any keyring this run installed, by its unique name. + rm -f "$KEYRING_DIR"/zz-vdi-test-* 2>/dev/null + rmdir "$KEYRING_DIR" 2>/dev/null + + purge_apt_lists_for "$REPO_DIR" + purge_apt_lists_for "$TAMPER_DIR" + purge_apt_lists_for "$SANDBOX_DIR" + + # Refresh so apt forgets the source we just removed. + apt-get update -qq >/dev/null 2>&1 + + rm -rf "$TMP_DIR" 2>/dev/null + printf 'removed %s, %s and cached indexes\n' "$TMP_DIR" "$LIST_FILE" + exit $status +} +trap cleanup EXIT INT TERM +CLEANED=0 + +# --------------------------------------------------------------------------- +# T00: environment sanity +# --------------------------------------------------------------------------- + +head2 "T00 environment" + +if [ "$(id -u)" -ne 0 ]; then + printf 'This harness must run as root (it adds and removes an apt source).\n' + exit 1 +fi + +for tool in apt-ftparchive apt-get apt-cache dpkg-deb python3 gzip; do + if ! command -v "$tool" >/dev/null 2>&1; then + printf 'Missing required tool: %s\n' "$tool" + exit 1 + fi +done + +CODENAME="" +if [ -r /etc/os-release ]; then + # shellcheck disable=SC1091 + . /etc/os-release + CODENAME="${VERSION_CODENAME:-}" +fi +info "distribution: ${PRETTY_NAME:-unknown} (codename=${CODENAME:-unknown})" + +if [ "$CODENAME" != "trixie" ]; then + info "WARNING: expected trixie; package dependency resolution is only proven there" +fi + +if [ ! -f "$DEB_DIR/$ICACLIENT_DEB" ] || [ ! -f "$DEB_DIR/$CTXUSB_DEB" ]; then + printf 'Missing fixture .deb files in %s\n' "$DEB_DIR" + printf 'Expected:\n %s\n %s\n' "$ICACLIENT_DEB" "$CTXUSB_DEB" + printf 'Override the directory with DEB_DIR=/path/to/debs\n' + exit 1 +fi +info "fixtures: $DEB_DIR" + +# --------------------------------------------------------------------------- +# T01: the .deb fixtures are what we think they are +# --------------------------------------------------------------------------- +# Without this, every later version assertion could be checking a stale file. + +head2 "T01 fixture .deb identity" + +for pkg in "$ICACLIENT_DEB" "$CTXUSB_DEB"; do + got=$(dpkg-deb -f "$DEB_DIR/$pkg" Version 2>/dev/null) + name=$(dpkg-deb -f "$DEB_DIR/$pkg" Package 2>/dev/null) + if [ "$got" = "$EXPECTED_VERSION" ]; then + pass "T01 $pkg declares Version $got (package $name)" + else + fail "T01 $pkg declares Version '$got', expected $EXPECTED_VERSION" + fi +done + +# --------------------------------------------------------------------------- +# T02: Python syntax and import safety on Debian +# --------------------------------------------------------------------------- + +head2 "T02 python3 -m py_compile" + +for py in update_local_repo.py configure_kde.py; do + if [ ! -f "$REPO_ROOT/$py" ]; then + fail "T02 $py is missing from the repo" + continue + fi + if out=$(cd "$TMP_DIR" && python3 -m py_compile "$REPO_ROOT/$py" 2>&1); then + pass "T02 $py compiles" + else + fail "T02 $py failed to compile: $out" + fi +done + +if [ -d "$REPO_ROOT/__pycache__" ]; then + fail "T02 py_compile left __pycache__ inside the repo (working tree dirtied)" +else + pass "T02 no __pycache__ written into the repo" +fi + +head2 "T03 import safety (module import must not explode or act)" + +# Importing must be side-effect free: run from a scratch cwd so a relative path +# in module-level code cannot accidentally work, and compare state afterwards. +import_probe() { + local module="$1" + cd "$TMP_DIR" || return 1 + python3 - "$REPO_ROOT" "$module" <<'PYEOF' 2>&1 +import sys, os +repo, module = sys.argv[1], sys.argv[2] +sys.path.insert(0, repo) +os.chdir(os.environ["PYTHONPYCACHEPREFIX"]) +try: + __import__(module) +except Exception as exc: + print("IMPORT-FAILED: %s: %s" % (type(exc).__name__, exc)) + raise SystemExit(1) +print("IMPORT-OK") +PYEOF +} + +for module in configure_kde update_local_repo; do + if out=$(import_probe "$module"); then + if printf '%s' "$out" | grep -q 'IMPORT-OK'; then + pass "T03 import $module succeeds on Debian" + else + fail "T03 import $module produced no confirmation: $out" + fi + else + fail "T03 import $module raised: $(printf '%s' "$out" | tr '\n' ' ')" + fi +done + +# The tqdm fallback: a stock trixie has no tqdm and no pip. The import above +# already covers the absent case. This asserts the fallback is a *working* +# no-op, not merely an importable name. +head2 "T04 tqdm fallback is functional, not just importable" + +if out=$(cd "$TMP_DIR" && python3 - "$REPO_ROOT" <<'PYEOF' 2>&1 +import sys, os, importlib.util +repo = sys.argv[1] +sys.path.insert(0, repo) +os.chdir(os.environ["PYTHONPYCACHEPREFIX"]) + +real_tqdm = importlib.util.find_spec("tqdm") is not None +print("real tqdm installed:", real_tqdm) + +import update_local_repo as u +bar = u.tqdm(total=10, unit="B", desc="probe", mininterval=0.1) +bar.update(5) +bar.update(5) +bar.close() +print("tqdm object constructed, updated and closed OK") +print("is fallback class:", getattr(u.tqdm, "__module__", "") == "update_local_repo") +PYEOF +); then + pass "T04 tqdm object usable with tqdm absent (fallback exercised)" + info "$(printf '%s' "$out" | tr '\n' ' ')" +else + fail "T04 tqdm fallback is broken: $(printf '%s' "$out" | tr '\n' ' ')" +fi + +# --------------------------------------------------------------------------- +# T05: the flat apt repository builds from the real .deb files +# --------------------------------------------------------------------------- + +head2 "T05 flat repo build (apt-ftparchive)" + +mkdir -p "$REPO_DIR" +chmod 755 "$REPO_DIR" "$TMP_DIR" +cp "$DEB_DIR/$ICACLIENT_DEB" "$DEB_DIR/$CTXUSB_DEB" "$REPO_DIR"/ || fail "T05 could not copy fixtures" + +build_repo() { + local dir="$1" + ( + cd "$dir" || exit 1 + apt-ftparchive packages . > Packages || exit 1 + gzip -c Packages > Packages.gz || exit 1 + apt-ftparchive release . > Release || exit 1 + ) + local rc=$? + chmod 644 "$dir"/Packages "$dir"/Packages.gz "$dir"/Release 2>/dev/null + return $rc +} + +if build_repo "$REPO_DIR"; then + pass "T05 repo build command sequence succeeded" +else + fail "T05 repo build command sequence failed" +fi + +stanzas=$(grep -c '^Package:' "$REPO_DIR/Packages" 2>/dev/null || echo 0) +if [ "$stanzas" = "2" ]; then + pass "T05 Packages contains exactly 2 stanzas" +else + fail "T05 Packages contains $stanzas stanzas, expected 2" + info "stanzas: $(grep '^Package:' "$REPO_DIR/Packages" 2>/dev/null | tr '\n' ' ')" +fi + +for pkg in icaclient ctxusb; do + got=$(awk -v p="$pkg" ' + /^Package:/ { in_pkg = ($2 == p) } + in_pkg && /^Version:/ { print $2; exit } + ' "$REPO_DIR/Packages") + if [ "$got" = "$EXPECTED_VERSION" ]; then + pass "T05 Packages declares $pkg $got" + else + fail "T05 Packages declares $pkg '${got:-nothing}', expected $EXPECTED_VERSION" + fi +done + +for f in Packages Packages.gz Release; do + if [ -s "$REPO_DIR/$f" ]; then + pass "T05 $f is present and non-empty" + else + fail "T05 $f is missing or empty" + fi +done + +# --------------------------------------------------------------------------- +# T06/T07: apt can see and resolve the package from that repo +# --------------------------------------------------------------------------- + +head2 "T06 sources.list entry and apt-get update" + +# The exact production form: flat repo, suite "./", trusted=yes. +printf 'deb [trusted=yes] file:%s ./\n' "$REPO_DIR" > "$LIST_FILE" +info "source: $(cat "$LIST_FILE")" + +purge_apt_lists_for "$REPO_DIR" +update_out=$(apt-get update 2>&1) +update_rc=$? + +# The spec'd check: exit 0 and no E: lines. +if [ "$update_rc" -eq 0 ]; then + pass "T06 apt-get update exits 0" +else + fail "T06 apt-get update exits $update_rc" +fi + +e_count=$(printf '%s\n' "$update_out" | grep -c '^E:' || true) +if [ "$e_count" -eq 0 ]; then + pass "T06 apt-get update emitted no E: lines" +else + fail "T06 apt-get update emitted $e_count E: line(s)" + printf '%s\n' "$update_out" | grep '^E:' | sed 's/^/ /' +fi + +# The strong check. Measured on trixie: a repo whose Packages is unreadable +# still yields exit 0 and no E: lines, but does emit "Err:" - so the weak form +# above can pass on a broken repo. These lines are what actually catch it. +err_count=$(printf '%s\n' "$update_out" | grep -c '^Err:' || true) +if [ "$err_count" -eq 0 ]; then + pass "T06 apt-get update emitted no Err: lines (stronger than the spec)" +else + fail "T06 apt-get update emitted $err_count Err: line(s) - the source is not readable" + printf '%s\n' "$update_out" | grep '^Err:' | sed 's/^/ /' +fi + +src_warn=$(printf '%s\n' "$update_out" | grep '^W:' | grep -F "$REPO_DIR" | grep -vc 'is not signed' || true) +if [ "$src_warn" -eq 0 ]; then + pass "T06 no unexpected W: lines for this source" +else + fail "T06 apt-get update warned $src_warn time(s) about this source" + printf '%s\n' "$update_out" | grep '^W:' | grep -F "$REPO_DIR" | sed 's/^/ /' +fi + +head2 "T07 apt can see the package (after purging the index cache)" + +# Purging matters: with a stale cached index a WRONG source still looks good. +policy=$(apt-cache policy icaclient 2>&1) +cand=$(printf '%s\n' "$policy" | awk '/Candidate:/ { print $2; exit }') +if [ "$cand" = "$EXPECTED_VERSION" ]; then + pass "T07 apt-cache policy icaclient -> Candidate $cand" +else + fail "T07 apt-cache policy icaclient -> Candidate '${cand:-none}', expected $EXPECTED_VERSION" + printf '%s\n' "$policy" | sed 's/^/ /' +fi + +if printf '%s\n' "$policy" | grep -qF "file:$REPO_DIR"; then + pass "T07 the version comes from this local repo, not somewhere else" +else + fail "T07 apt-cache policy does not attribute icaclient to $REPO_DIR" +fi + +show_version=$(apt-cache show icaclient 2>/dev/null | awk '/^Version:/ { print $2; exit }') +if [ "$show_version" = "$EXPECTED_VERSION" ]; then + pass "T07 apt-cache show icaclient -> Version $show_version" +else + fail "T07 apt-cache show icaclient -> Version '${show_version:-none}', expected $EXPECTED_VERSION" +fi + +if apt-get install --simulate -y icaclient >"$TMP_DIR/simulate.log" 2>&1; then + pass "T07 apt-get install --simulate -y icaclient resolves (no missing deps)" +else + fail "T07 apt-get install --simulate -y icaclient did not resolve" + tail -15 "$TMP_DIR/simulate.log" | sed 's/^/ /' +fi + +if grep -q '^Inst icaclient' "$TMP_DIR/simulate.log"; then + pass "T07 the simulated install actually selects icaclient" +else + fail "T07 simulation did not select icaclient (it may be already installed)" +fi + +# --------------------------------------------------------------------------- +# T08: idempotency +# --------------------------------------------------------------------------- + +head2 "T08 rebuild is idempotent" + +cp "$REPO_DIR/Packages" "$TMP_DIR/Packages.first" +build_repo "$REPO_DIR" >/dev/null 2>&1 +stanzas2=$(grep -c '^Package:' "$REPO_DIR/Packages" 2>/dev/null || echo 0) + +if cmp -s "$TMP_DIR/Packages.first" "$REPO_DIR/Packages"; then + pass "T08 Packages is byte-identical after a second build" +else + fail "T08 Packages changed on rebuild" +fi + +if [ "$stanzas2" = "2" ]; then + pass "T08 still exactly 2 stanzas after rebuild (no duplicates)" +else + fail "T08 rebuild produced $stanzas2 stanzas, expected 2" + grep '^Package:' "$REPO_DIR/Packages" | sed 's/^/ /' +fi + +# A duplicate stanza is what a naive "copy new, leave old" implementation +# produces. Assert the count is exact, not merely non-zero. +dup=$(grep '^Package:' "$REPO_DIR/Packages" 2>/dev/null | sort | uniq -d) +if [ -z "$dup" ]; then + pass "T08 no duplicated Package stanzas" +else + fail "T08 duplicated stanzas: $dup" +fi + +# --------------------------------------------------------------------------- +# T09: stale package pruning in sync_debian +# --------------------------------------------------------------------------- +# Debian .deb filenames embed the version, so copying without deleting would +# leave every previous version in the repo and apt would always prefer the +# highest - making a rollback impossible. This reproduces that scenario. + +head2 "T09 stale .deb is pruned by sync_debian" + +mkdir -p "$SANDBOX_DIR/repo" "$SANDBOX_DIR/staging" "$SANDBOX_DIR/src" +cp "$DEB_DIR/$ICACLIENT_DEB" "$DEB_DIR/$CTXUSB_DEB" "$SANDBOX_DIR/src"/ +# Pretend an older ctxusb is already published. +cp "$DEB_DIR/$CTXUSB_DEB" "$SANDBOX_DIR/repo/ctxusb_26.07.0.100_amd64.deb" + +if out=$(python3 - "$REPO_ROOT" "$SANDBOX_DIR" <<'PYEOF' 2>&1 +import sys, os, shutil, re, subprocess + +repo_root, work = sys.argv[1], sys.argv[2] +sys.path.insert(0, repo_root) +os.chdir(os.environ["PYTHONPYCACHEPREFIX"]) +import update_local_repo as u + +repo_dir = os.path.join(work, "repo") +stage = os.path.join(work, "staging") +src = os.path.join(work, "src") + +# Redirect all module-level paths into the sandbox. +u.LOCAL_REPO_DIR = repo_dir +u.STAGING_DIR = stage +u.DEBIAN_FALLBACK_DIR = src +u.save_last_release = lambda rd: None + +# Never write to /etc and never run apt-get from inside a test. +real_copyfile = shutil.copyfile +def guarded_copyfile(s, d, *a, **k): + if str(d).startswith("/etc/"): + d = os.path.join(work, "etc-redirect", os.path.basename(str(d))) + os.makedirs(os.path.dirname(d), exist_ok=True) + return real_copyfile(s, d, *a, **k) +u.shutil.copyfile = guarded_copyfile + +real_run = subprocess.run +calls = [] +def guarded_run(args, *a, **k): + if isinstance(args, (list, tuple)) and args and args[0] == "apt-get": + calls.append(list(args)) + return subprocess.CompletedProcess(args, 0, b"", b"") + return real_run(args, *a, **k) +u.subprocess.run = guarded_run + +before = sorted(f for f in os.listdir(repo_dir) if f.endswith(".deb")) +release = {"tag_name": "v26.08", "published_at": "2026-01-01T00:00:00Z", "assets": {}} +u.sync_debian(True, release) +after = sorted(f for f in os.listdir(repo_dir) if f.endswith(".deb")) + +pk = open(os.path.join(repo_dir, "Packages")).read() +print("BEFORE:", before) +print("AFTER:", after) +print("APT-GET-CALLS:", calls) +print("STANZAS:", len(re.findall(r"^Package: ", pk, re.M))) +for m in re.finditer(r"^Package: (\S+)$\n(?:.*\n)*?^Version: (\S+)$", pk, re.M): + print("VERSION: %s=%s" % (m.group(1), m.group(2))) +PYEOF +); then + printf '%s\n' "$out" | sed 's/^/ /' + + # Check the AFTER line specifically. Grepping the whole output would match + # the BEFORE line, which deliberately contains the stale filename - the + # test would then fail on its own evidence and could never pass. + after_line=$(printf '%s\n' "$out" | grep '^AFTER:' || true) + if printf '%s' "$after_line" | grep -q 'ctxusb_26\.07\.0\.100'; then + fail "T09 the stale ctxusb_26.07.0.100 was not removed from the repo dir" + else + pass "T09 stale ctxusb_26.07.0.100 was pruned" + fi + + # Stronger than a file count: a prune that deleted everything would also + # give "2 stanzas" if the index were rebuilt from nothing. Assert both + # current packages are actually present after the sync. + for expected_deb in "$ICACLIENT_DEB" "$CTXUSB_DEB"; do + if printf '%s' "$after_line" | grep -qF "$expected_deb"; then + pass "T09 $expected_deb present after sync" + else + fail "T09 $expected_deb missing after sync (prune removed too much)" + fi + done + + if printf '%s\n' "$out" | grep -q 'STANZAS: 2'; then + pass "T09 Packages holds exactly 2 stanzas after sync" + else + fail "T09 Packages does not hold exactly 2 stanzas after sync" + fi + + if printf '%s\n' "$out" | grep -q "VERSION: ctxusb=$EXPECTED_VERSION"; then + pass "T09 ctxusb resolves to $EXPECTED_VERSION" + else + fail "T09 ctxusb does not resolve to $EXPECTED_VERSION" + fi +else + fail "T09 sandboxed sync_debian raised: $(printf '%s' "$out" | tr '\n' ' ')" +fi + +if [ -e "$SANDBOX_DIR/etc-redirect/local-citrix-repo.list" ]; then + pass "T09 sync_debian writes the apt source file (redirected to the sandbox)" +else + fail "T09 sync_debian did not write the apt source file" +fi + +# --------------------------------------------------------------------------- +# T10: trusted=yes still verifies the index (negative control) +# --------------------------------------------------------------------------- +# This is the control for T06/T07: it proves apt would actually fail on a +# corrupt repo, so a PASS above is meaningful rather than vacuous. + +head2 "T10 corrupt index is rejected even with trusted=yes" + +mkdir -p "$TAMPER_DIR" +cp "$REPO_DIR"/*.deb "$TAMPER_DIR"/ +build_repo "$TAMPER_DIR" >/dev/null 2>&1 + +printf 'deb [trusted=yes] file:%s ./\n' "$TAMPER_DIR" > "$LIST_FILE" +purge_apt_lists_for "$TAMPER_DIR" +apt-get update -qq >/dev/null 2>&1 + +# Baseline: the package is visible from the untampered repo. +base=$(apt-cache policy icaclient 2>/dev/null | grep -c "$EXPECTED_VERSION" || true) +if [ "$base" -ge 1 ]; then + pass "T10 baseline: icaclient visible before tampering" +else + fail "T10 baseline: icaclient not visible even before tampering" +fi + +# Now corrupt Packages after Release recorded its hash. +printf '\nPackage: evilpkg\nVersion: 6.6.6\nArchitecture: amd64\nFilename: ./evilpkg.deb\nSize: 1\n' \ + >> "$TAMPER_DIR/Packages" +purge_apt_lists_for "$TAMPER_DIR" +tamper_out=$(apt-get update 2>&1) +tamper_rc=$? + +if [ "$tamper_rc" -ne 0 ]; then + pass "T10 apt-get update FAILS on a hash mismatch (exit $tamper_rc)" +else + fail "T10 apt-get update accepted a corrupted Packages file (exit 0)" +fi + +if printf '%s\n' "$tamper_out" | grep -qi 'hash sum mismatch'; then + pass "T10 failure is reported as a Hash Sum mismatch" +else + fail "T10 no Hash Sum mismatch reported" + printf '%s\n' "$tamper_out" | grep -E '^(E:|W:|Err:)' | head -5 | sed 's/^/ /' +fi + +if [ "$(apt-cache policy evilpkg 2>/dev/null | grep -c '6.6.6' || true)" -eq 0 ]; then + pass "T10 the injected package is not available to apt" +else + fail "T10 the injected package became available - the index was trusted blindly" +fi + +# Restore the good source for the remaining tests. +printf 'deb [trusted=yes] file:%s ./\n' "$REPO_DIR" > "$LIST_FILE" +purge_apt_lists_for "$REPO_DIR" +apt-get update -qq >/dev/null 2>&1 + +# --------------------------------------------------------------------------- +# T11: the production source file matches what the harness verified +# --------------------------------------------------------------------------- + +head2 "T11 production sources.list shape" + +if [ -f "$PROD_SOURCE_FILE" ]; then + active=$(grep -E '^[^#]*deb ' "$PROD_SOURCE_FILE" | head -1) + if printf '%s' "$active" | grep -q 'trusted=yes'; then + pass "T11 production source uses trusted=yes" + else + fail "T11 production source does not use trusted=yes: $active" + fi + if printf '%s' "$active" | grep -q 'file:/var/local/citrix-repo \./$'; then + pass "T11 production source is the flat form (file:/var/local/citrix-repo ./)" + else + fail "T11 production source is not the flat form: $active" + fi + # The harness proved the same shape works, only with a different path. + if printf '%s' "$active" | sed 's|/var/local/citrix-repo|REPO|' \ + | grep -qx 'deb \[trusted=yes\] file:REPO ./'; then + pass "T11 production source has the same shape the harness verified" + else + fail "T11 production source shape differs from what the harness tested" + fi +else + skip "T11 production source file $PROD_SOURCE_FILE does not exist (owned by another change)" +fi + +# --------------------------------------------------------------------------- +# T12: GPG signing path +# --------------------------------------------------------------------------- +# Production deliberately does not sign (trusted=yes). This test exists to +# check the decision, not to gate the port. It needs a signing tool; a stock +# trixie ships only the sqv verifier, so without gpg or sq this SKIPs loudly. + +head2 "T12 GPG signed-by path (design check)" + +SIGNER="" +if command -v gpg >/dev/null 2>&1; then + SIGNER="gpg" +fi + +if [ -z "$SIGNER" ]; then + skip "T12 no gpg on this machine - signing untested here" + if command -v sq >/dev/null 2>&1; then + info "sq is present but this test is written against gpg; not attempted" + fi + info "would have checked: throwaway key -> Release.gpg -> apt with signed-by= ->" + info "positive case succeeds, wrong-key case fails, and whether either is" + info "more fragile than trusted=yes. Measured out-of-band; see README.md." +else + GNUPGHOME_T="$TMP_DIR/gnupg" + mkdir -p "$GNUPGHOME_T"; chmod 700 "$GNUPGHOME_T" + cat > "$TMP_DIR/keyparams" <<'KEYEOF' +%echo generating test key +Key-Type: RSA +Key-Length: 2048 +Name-Real: VDI Test Key +Name-Email: vdi-test@example.invalid +Expire-Date: 1 +%no-protection +%commit +KEYEOF + if gpg --batch --homedir "$GNUPGHOME_T" --gen-key "$TMP_DIR/keyparams" >/dev/null 2>&1; then + gpg --batch --homedir "$GNUPGHOME_T" --armor --export > "$TMP_DIR/signer.asc" 2>/dev/null + gpg --batch --homedir "$GNUPGHOME_T" --armor --detach-sign \ + --output "$REPO_DIR/Release.gpg" "$REPO_DIR/Release" >/dev/null 2>&1 + mkdir -p "$KEYRING_DIR" + cp "$TMP_DIR/signer.asc" "$KEYRING_DIR/zz-vdi-test-signer.asc" + chmod 644 "$KEYRING_DIR/zz-vdi-test-signer.asc" + + printf 'deb [signed-by=%s/zz-vdi-test-signer.asc] file:%s ./\n' \ + "$KEYRING_DIR" "$REPO_DIR" > "$LIST_FILE" + purge_apt_lists_for "$REPO_DIR" + if apt-get update >/dev/null 2>&1; then + pass "T12 signed-by with the correct key verifies" + else + fail "T12 signed-by with the correct key failed" + fi + + # Control: sign with a key apt does not trust. + rm -f "$KEYRING_DIR/zz-vdi-test-signer.asc" + printf 'deb [signed-by=%s/zz-vdi-test-absent.asc] file:%s ./\n' \ + "$KEYRING_DIR" "$REPO_DIR" > "$LIST_FILE" + purge_apt_lists_for "$REPO_DIR" + if apt-get update >/dev/null 2>&1; then + fail "T12 apt accepted the repo without the signing key (signing is ineffective)" + else + pass "T12 apt rejects the repo when the signing key is absent" + fi + + printf 'deb [trusted=yes] file:%s ./\n' "$REPO_DIR" > "$LIST_FILE" + purge_apt_lists_for "$REPO_DIR" + apt-get update -qq >/dev/null 2>&1 + else + skip "T12 throwaway key generation failed (gpg entropy/timeout); signing untested here" + fi +fi + +# --------------------------------------------------------------------------- +# T13: Fedora regression +# --------------------------------------------------------------------------- + +head2 "T13 Fedora path unregressed" + +if ! command -v git >/dev/null 2>&1; then + skip "T13 git is not installed - cannot compare against main" + info "would have checked: ks.cfg absent from main (vacuous), tracked Fedora" + info "files identical to main modulo line endings, parent ks.cfg hash." + info "Install git, or run the checks from a machine that has it." +else + GIT="git -C $REPO_ROOT -c safe.directory=*" + + # The spec'd check. Reported honestly: ks.cfg is not in the repo at all, so + # this diff exits 0 without comparing anything. + ks_diff=$($GIT diff main -- ks.cfg 2>&1) + ks_rc=$? + if [ "$ks_rc" -eq 0 ] && [ -z "$ks_diff" ]; then + if $GIT cat-file -e main:ks.cfg >/dev/null 2>&1; then + pass "T13 git diff main -- ks.cfg is empty and the path exists (real check)" + else + skip "T13 'git diff main -- ks.cfg' is empty but VACUOUS: ks.cfg is not in main" + info "the spec'd assertion cannot fail; ks.cfg lives outside the repo" + fi + else + fail "T13 git diff main -- ks.cfg is not empty (ks.cfg changed or appeared)" + printf '%s\n' "$ks_diff" | head -10 | sed 's/^/ /' + fi + + # The meaningful check: the tracked Fedora-side files must be unchanged. + # Compared modulo CRLF, because this repo is checked out on Windows and the + # blobs in git are LF - a raw diff reports every line as changed. + for f in configure_kde.py local-citrix-repo.repo RPM-GPG-KEY-citrix-local; do + if $GIT show "main:$f" > "$TMP_DIR/main_blob" 2>/dev/null; then + if tr -d '\r' < "$REPO_ROOT/$f" | cmp -s - "$TMP_DIR/main_blob"; then + pass "T13 $f is identical to main (ignoring line endings)" + else + fail "T13 $f differs from main beyond line endings" + fi + else + fail "T13 could not read main:$f" + fi + done + + # update_local_repo.py is expected to differ: the Debian path lives inside + # it behind a family dispatch. Assert the Fedora behaviour instead of the + # file bytes - the .rpm asset names and the exact command sequence. + if out=$(python3 - "$REPO_ROOT" <<'PYEOF' 2>&1 +import ast, subprocess, sys, os +repo = sys.argv[1] + +def load(text): + return ast.parse(text) + +main_src = subprocess.run(["git", "-C", repo, "show", "main:update_local_repo.py"], + capture_output=True, text=True) +if main_src.returncode != 0: + print("CANNOT-READ-MAIN") + raise SystemExit(1) +new_src = open(os.path.join(repo, "update_local_repo.py")).read() + +main = load(main_src.stdout) +new = load(new_src) + +def system_cmds(tree, scope=None): + """Return the os.system() command strings, in source order. + + Both plain strings and f-strings are collected. The Debian port rewrote + "createrepo_c /var/local/citrix-repo" as f"createrepo_c {LOCAL_REPO_DIR}", + and an extractor that only accepted ast.Constant would silently drop those + commands and then report the resulting short list as a regression. That is + a false negative, not evidence. + """ + body = tree.body + if scope: + body = [] + for n in tree.body: + if isinstance(n, ast.FunctionDef) and n.name == scope: + body = n.body + out = [] + for node in ast.walk(ast.Module(body=body, type_ignores=[])): + if not isinstance(node, ast.Call): + continue + f = node.func + if not (isinstance(f, ast.Attribute) and f.attr == "system"): + continue + if not node.args: + continue + arg = node.args[0] + if isinstance(arg, ast.Constant) and isinstance(arg.value, str): + out.append(arg.value) + elif isinstance(arg, ast.JoinedStr): + parts = [] + for v in arg.values: + if isinstance(v, ast.Constant): + parts.append(str(v.value)) + elif isinstance(v, ast.FormattedValue): + inner = v.value + if isinstance(inner, ast.Name): + parts.append("{VAR:%s}" % inner.id) + else: + parts.append("{EXPR}") + out.append("".join(parts)) + return out + +main_cmds = system_cmds(main) +new_cmds = system_cmds(new, "sync_fedora") + +# Normalise BOTH sides: main hard-codes the path, the branch uses a constant. +def norm(cmd): + return (cmd.replace("/var/local/citrix-repo", "") + .replace("{VAR:LOCAL_REPO_DIR}", "")) + +normed_main = [norm(c) for c in main_cmds] +normed_new = [norm(c) for c in new_cmds] +print("MAIN-CMDS:", normed_main) +print("FEDORA-CMDS:", normed_new) +print("SAME-SEQUENCE:", normed_new == normed_main) +if normed_new != normed_main: + print("MISSING-FROM-FEDORA:", [c for c in normed_main if c not in normed_new]) + print("EXTRA-IN-FEDORA:", [c for c in normed_new if c not in normed_main]) + +# The Fedora asset names must still be matched exactly. +names = set() +for node in ast.walk(new): + if isinstance(node, ast.Constant) and isinstance(node.value, str): + if node.value.endswith(".rpm"): + names.add(node.value) +required = {"ctxusb.rpm", "ICAClient.rpm"} +print("RPM-NAMES-PRESENT:", required.issubset(names)) +PYEOF + ); then + printf '%s\n' "$out" | sed 's/^/ /' + if printf '%s\n' "$out" | grep -q 'SAME-SEQUENCE: True'; then + pass "T13 sync_fedora issues the same commands in the same order as main" + else + fail "T13 sync_fedora command sequence differs from main" + fi + if printf '%s\n' "$out" | grep -q 'RPM-NAMES-PRESENT: True'; then + pass "T13 Fedora .rpm asset names still matched exactly" + else + fail "T13 Fedora .rpm asset names changed" + fi + else + fail "T13 could not compare the Fedora code path: $(printf '%s' "$out" | tr '\n' ' ')" + fi + + # ks.cfg lives outside the repo; assert the file the user actually has. + OUTER_KS="$(dirname "$REPO_ROOT")/ks.cfg" + if [ -f "$OUTER_KS" ]; then + if $GIT cat-file -e main:ks.cfg >/dev/null 2>&1; then + if $GIT show main:ks.cfg | tr -d '\r' | cmp -s - <(tr -d '\r' < "$OUTER_KS"); then + pass "T13 out-of-tree ks.cfg matches main:ks.cfg" + else + fail "T13 out-of-tree ks.cfg differs from main:ks.cfg" + fi + else + info "T13 out-of-tree ks.cfg sha256: $(sha256sum "$OUTER_KS" | cut -d' ' -f1)" + info "no main:ks.cfg to compare against; the file was never committed" + fi + else + skip "T13 no ks.cfg found at $OUTER_KS" + fi + + # Record what the branch actually changes, so a future reader can see the + # scope rather than trusting a summary. + info "files changed vs main (ignoring line endings):" + $GIT diff --ignore-cr-at-eol main --stat 2>/dev/null | sed 's/^/ /' +fi + +# --------------------------------------------------------------------------- +# T14: shell lint +# --------------------------------------------------------------------------- + +head2 "T14 bash -n on shell scripts added by the port" + +found_any=0 +while IFS= read -r script; do + # This harness lints itself below; do not lint it twice. + [ "$script" = "$SCRIPT_DIR/run-tests.sh" ] && continue + found_any=1 + if bash -n "$script" 2>"$TMP_DIR/basherr"; then + pass "T14 bash -n $(printf '%s' "$script" | sed "s|$REPO_ROOT/||")" + else + fail "T14 bash -n failed for $script: $(cat "$TMP_DIR/basherr" | tr '\n' ' ')" + fi +done < <(find "$REPO_ROOT" -name '*.sh' -type f 2>/dev/null | grep -v '/\.git/' | sort) + +if [ "$found_any" -eq 0 ]; then + skip "T14 the port adds no other shell scripts (only this harness exists)" + info "when the port adds scripts, this test lints them automatically" +fi + +# Always lint this harness, so the check is not vacuous while the port has no +# scripts of its own. +if bash -n "$SCRIPT_DIR/run-tests.sh" 2>"$TMP_DIR/basherr"; then + pass "T14 bash -n debian/test/run-tests.sh" +else + fail "T14 bash -n failed for run-tests.sh: $(cat "$TMP_DIR/basherr" | tr '\n' ' ')" +fi + +# --------------------------------------------------------------------------- +# T15: the harness left nothing behind +# --------------------------------------------------------------------------- +# The residue half of this test must run AFTER the cleanup, not before it, or it +# can never pass - and a permanently-red test trains people to ignore the +# summary. So: check what can be checked now, then tear down, then verify the +# teardown actually happened, then print the summary. + +head2 "T15 no collateral damage" + +if [ -d "$PROD_REPO_DIR" ]; then + now=$(stat -c '%Y' "$PROD_REPO_DIR" 2>/dev/null || echo "") + if [ "$now" = "$PROD_REPO_MTIME_BEFORE" ]; then + pass "T15 $PROD_REPO_DIR was not modified" + else + fail "T15 $PROD_REPO_DIR changed during the run" + fi +else + pass "T15 $PROD_REPO_DIR does not exist and was not created" +fi + +if [ -d "$REPO_ROOT/__pycache__" ]; then + fail "T15 __pycache__ was written into the repo" +else + pass "T15 the repo working tree was not dirtied by Python" +fi + +# Did anything get installed? Every apt-get install this harness issues is +# --simulate, so the set of installed packages must be unchanged. +if command -v dpkg-query >/dev/null 2>&1; then + installed_now=$(dpkg-query -W -f='${Package}\n' 2>/dev/null | wc -l) + if [ -n "${INSTALLED_BEFORE:-}" ]; then + if [ "$installed_now" = "$INSTALLED_BEFORE" ]; then + pass "T15 installed package count unchanged ($installed_now)" + else + fail "T15 installed package count changed: $INSTALLED_BEFORE -> $installed_now" + fi + fi +fi + +# ---- teardown, then verify the teardown ---- +printf '\n removing temporary source, keyring, caches and %s\n' "$TMP_DIR" +rm -f "$LIST_FILE" 2>/dev/null +rm -f "$KEYRING_DIR"/zz-vdi-test-* 2>/dev/null +rmdir "$KEYRING_DIR" 2>/dev/null +purge_apt_lists_for "$REPO_DIR" +purge_apt_lists_for "$TAMPER_DIR" +purge_apt_lists_for "$SANDBOX_DIR" +rm -rf "$TMP_DIR" 2>/dev/null +apt-get update -qq >/dev/null 2>&1 + +if [ -f "$LIST_FILE" ]; then + fail "T15 the temporary apt source $LIST_FILE survived teardown" +else + pass "T15 temporary apt source removed from /etc/apt/sources.list.d" +fi + +leftover=$(ls /var/lib/apt/lists/*"$(list_fragment "$REPO_DIR")"* 2>/dev/null | wc -l) +if [ "$leftover" -eq 0 ]; then + pass "T15 no cached apt indexes left behind" +else + fail "T15 $leftover cached apt index file(s) left behind" +fi + +if [ -e "$TMP_DIR" ]; then + fail "T15 temporary directory $TMP_DIR survived teardown" +else + pass "T15 temporary directory removed" +fi + +# From here on the EXIT trap has nothing left to do; make sure it cannot undo +# the summary by re-running apt-get update and removing things again. +CLEANED=1 + +# --------------------------------------------------------------------------- +# Summary +# --------------------------------------------------------------------------- + +printf '\n%s\n' "----------------------------------------------------------------" +printf 'SUMMARY: %d passed, %d failed, %d skipped\n' "$PASSED" "$FAILED" "$SKIPPED" + +if [ "$FAILED" -ne 0 ]; then + printf 'FAILED tests:%s\n' "$FAILED_NAMES" + printf 'RESULT: FAIL\n' + exit 1 +fi + +printf 'RESULT: PASS\n' +if [ "$SKIPPED" -ne 0 ]; then + printf '(%d test(s) skipped - read the SKIP lines above; a skip is not a pass)\n' "$SKIPPED" +fi +exit 0 diff --git a/local-citrix-repo.list b/local-citrix-repo.list new file mode 100644 index 0000000..819dfaf --- /dev/null +++ b/local-citrix-repo.list @@ -0,0 +1,21 @@ +# Local Citrix repository for Debian-family clients. +# +# The apt counterpart of local-citrix-repo.repo (which dnf uses on Fedora). +# update_local_repo.py copies this file to +# /etc/apt/sources.list.d/local-citrix-repo.list once the packages are published. +# +# This is a flat ("trivial") apt archive: the packages, Packages, Packages.gz and +# Release all sit directly in /var/local/citrix-repo. That is why the suite is +# "./" - a path ending in a slash with no component - rather than a codename. +# +# trusted=yes is deliberate. The repository is generated on this machine from +# packages update_local_repo.py has just downloaded and verified, so there is no +# third party to authenticate against and signing would only mean generating and +# trusting a key on every fresh install. On Fedora the equivalent file keeps +# gpgcheck=1 because that path already has an RPM signing key. +# +# Unlike dnf, apt has no per-source metadata expiry: the index is re-read only +# when `apt-get update` runs. update_local_repo.py runs it, and the 10-minute +# cron job installed by the preseed runs update_local_repo.py, so the index does +# not go stale in practice. +deb [trusted=yes] file:/var/local/citrix-repo ./ diff --git a/update_local_repo.py b/update_local_repo.py index 1fcb87e..39adebe 100755 --- a/update_local_repo.py +++ b/update_local_repo.py @@ -1,323 +1,749 @@ #!/usr/bin/env python3 +"""Keep the local Citrix package repository in sync with the latest Gitea release. -import urllib.request -import os -import urllib.error -import threading -from concurrent.futures import ThreadPoolExecutor -from tqdm import tqdm -import logging -import time +The script supports both package families that the VDI clients run: + +* **Fedora** (the original target): downloads ``.rpm`` assets and publishes them + through a ``createrepo_c`` repository at ``/var/local/citrix-repo``, read by + dnf through ``/etc/yum.repos.d/local-citrix-repo.repo``. +* **Debian** (this branch): downloads ``.deb`` assets and publishes them through + a flat ``apt-ftparchive`` repository at the same path, read by apt through + ``/etc/apt/sources.list.d/local-citrix-repo.list``. + +The family is detected at runtime from ``/etc/os-release``. The Fedora code +path is unchanged from the version that has been deployed for months: same +asset filenames, same commands, same order. Adding Debian support must not +alter the behaviour of an existing Fedora client, so every Debian-specific +step is behind the family check. + +Both paths share the download, hash-verification and "is there a newer +release?" logic. +""" + +import datetime import hashlib import json -import datetime +import logging +import os import shutil import subprocess +import threading +import time +import urllib.error +import urllib.request +from concurrent.futures import ThreadPoolExecutor + +try: + from tqdm import tqdm +except ImportError: # pragma: no cover - exercised on a bare Debian install + # tqdm is cosmetic: it only draws the download progress bar. A stock Debian + # 13 image has neither pip nor tqdm, and dying at import time here would + # take down the whole provisioning run over a progress bar. Fall back to a + # no-op so the script still works; the preseed installs python3-tqdm from + # apt so the bar is present in practice. + class tqdm: # noqa: N801 (deliberately shadows the real class) + def __init__(self, *args, **kwargs): + pass + + def update(self, _n=1): + pass + + def close(self): + pass logging.basicConfig(level=logging.INFO, format='%(asctime)s - Thread %(threadName)s - %(message)s') -# Update the local repository via git pull before proceeding -try: - subprocess.run(['git', 'pull'], check=True, capture_output=True) - logging.info("Updated repo via git pull") -except subprocess.CalledProcessError as e: - logging.warning(f"Git pull failed (non-zero exit): {e}") -except FileNotFoundError: - logging.warning("Git not found; skipping pull") -except Exception as e: - logging.warning(f"Unexpected error during git pull: {e}") +# --------------------------------------------------------------------------- +# Constants +# --------------------------------------------------------------------------- -# Apply the standard KDE Plasma settings. This also installs the per-session -# helper used by the XDG autostart entry, and updates users who are currently -# logged in. Keep this before the release download so the settings are applied -# even if the Citrix repository update fails. -try: - from configure_kde import apply_all as apply_kde_settings - apply_kde_settings() -except Exception as e: - logging.exception(f"Failed to apply KDE settings: {e}") +REPO_OWNER = "REYHER-Systemtechnik-Public" +REPO_NAME = "VDI-Linux" -api_base = "https://nachos386.lcube-server.de/api/v1" +API_BASE = "https://nachos386.lcube-server.de/api/v1" -import os -import subprocess -import pwd +# Where downloaded packages are staged before being published. +STAGING_DIR = "/temp/packages" +LAST_RELEASE_FILE = os.path.join(STAGING_DIR, "last_release.json") -for user in os.listdir("/home"): - home = f"/home/{user}" +# The published local repository. Same path on both families. +LOCAL_REPO_DIR = "/var/local/citrix-repo" - if not os.path.isdir(home): - continue +# Fedora: local staging path -> exact asset filename in the Gitea release. +# These names are unversioned, which is why they can be matched exactly. Do not +# change this mapping; existing Fedora clients depend on it. +FEDORA_RPM_FILES = { + os.path.join(STAGING_DIR, "ctxusb.rpm"): "ctxusb.rpm", + os.path.join(STAGING_DIR, "ICAClient.rpm"): "ICAClient.rpm", +} +# Debian: the package names as apt knows them. The release assets are named +# with a version (``icaclient_26.08.0.225_amd64.deb``), so they are matched by +# prefix rather than by exact filename. +DEBIAN_PACKAGES = ("icaclient", "ctxusb") + +# If the Gitea release carries no ``.deb`` assets, look here instead. This lets +# an operator drop the two Citrix .deb files onto the machine (or point +# VDI_DEB_DIR at a directory) without having to publish a release first. +DEBIAN_FALLBACK_DIR = os.environ.get("VDI_DEB_DIR", os.path.join(STAGING_DIR, "deb")) + + +# --------------------------------------------------------------------------- +# Distribution detection +# --------------------------------------------------------------------------- + +def detect_package_family(): + """Return ``'debian'`` or ``'fedora'`` for the running machine. + + ``/etc/os-release`` is the authoritative source on every distribution this + script runs on. ``ID`` is checked first and ``ID_LIKE`` second, so Ubuntu, + Mint, Kali and similar are all treated as Debian family. + """ + ident = "" + like = "" try: - desktop = subprocess.check_output( - ["sudo", "-u", user, "xdg-user-dir", "DESKTOP"], - text=True - ).strip() - except Exception: - # Fallback if xdg-user-dir isn't available - desktop = os.path.join(home, "Desktop") + with open("/etc/os-release", encoding="utf-8") as handle: + for line in handle: + line = line.strip() + if line.startswith("ID="): + ident = line[3:].strip().strip('"').lower() + elif line.startswith("ID_LIKE="): + like = line[8:].strip().strip('"').lower() + except OSError: + logging.warning("/etc/os-release is not readable; falling back to release files") - os.makedirs(desktop, exist_ok=True) + tokens = set((ident + " " + like).split()) - script = os.path.join(desktop, "kill_citrix.sh") - with open(script, "w") as f: - f.write("#!/bin/bash\npkill icasessionmgr\npkill wfica\n") + if "debian" in tokens or "ubuntu" in tokens: + return "debian" + if tokens & {"fedora", "rhel", "centos", "rocky", "almalinux"}: + return "fedora" - os.chmod(script, 0o755) + # Fall back to the distribution release files, then to the package manager. + if os.path.exists("/etc/debian_version"): + return "debian" + if os.path.exists("/etc/fedora-release"): + return "fedora" + if shutil.which("apt-get"): + return "debian" + if shutil.which("dnf"): + return "fedora" -def fetch_release_info(repo_owner, repo_name): - """Fetch latest release info including assets from Gitea API.""" - api_url = f"{api_base}/repos/{repo_owner}/{repo_name}/releases/latest" - try: - with urllib.request.urlopen(api_url, timeout=30) as response: - release_info = json.loads(response.read().decode()) + logging.warning("Could not identify the distribution; assuming Fedora") + return "fedora" - tag_name = release_info.get('tag_name', '') - published_at = release_info.get('published_at', '') - if not tag_name or not published_at: - raise ValueError("Invalid release data: missing tag_name or published_at") - - assets = {} - for asset in release_info.get('assets', []): - filename = asset.get('name', '') - if filename.endswith('.rpm'): - download_url = asset.get('browser_download_url', '') - if not download_url: - logging.warning(f"No download URL for {filename}") - continue - # Gitea does not provide digests; hash will be None - hash_value = None - # For compatibility, check if 'digest' exists (unlikely in Gitea) - digest = asset.get('digest', '') - if digest and digest.startswith('sha256:'): - hash_value = digest[7:].lower() - logging.info(f"Fetched hash for {filename}: {hash_value[:16]}...") - else: - logging.info(f"No hash available for {filename}; verification will be skipped") - assets[filename] = {'url': download_url, 'hash': hash_value} - - if not assets: - logging.warning("No RPM assets found in release.") - logging.info(f"Available assets: {[a['name'] for a in release_info.get('assets', [])]}") - else: - logging.info(f"Fetched {len(assets)} assets from API.") - - return { - 'tag_name': tag_name, - 'published_at': published_at, - 'assets': assets - } - except urllib.error.URLError as e: - logging.error(f"Failed to fetch release info from API: {e}") - raise - except json.JSONDecodeError as e: - logging.error(f"Failed to parse API JSON: {e}") - raise - except Exception as e: - logging.error(f"Unexpected error fetching release info: {e}") - raise + +PACKAGE_FAMILY = detect_package_family() + + +def package_extension(): + """The asset extension this family consumes.""" + return ".deb" if PACKAGE_FAMILY == "debian" else ".rpm" + + +def apt_lists_configured(): + """True when the local apt source file is present.""" + return os.path.exists("/etc/apt/sources.list.d/local-citrix-repo.list") + + +def local_repo_is_populated(): + """True when the published repository has an index apt/dnf can read. + + Used by the Debian path to rebuild when the repository directory has been + deleted even though the release has not changed. + """ + if PACKAGE_FAMILY == "debian": + return os.path.exists(os.path.join(LOCAL_REPO_DIR, "Packages")) + return os.path.exists(os.path.join(LOCAL_REPO_DIR, "repodata")) + + +# --------------------------------------------------------------------------- +# Per-user desktop shortcut +# --------------------------------------------------------------------------- + +def install_desktop_shortcuts(): + """Drop ``kill_citrix.sh`` on every user's desktop.""" + for user in os.listdir("/home"): + home = f"/home/{user}" + + if not os.path.isdir(home): + continue + + try: + desktop = subprocess.check_output( + ["sudo", "-u", user, "xdg-user-dir", "DESKTOP"], + text=True + ).strip() + except Exception: + # Fallback if xdg-user-dir isn't available + desktop = os.path.join(home, "Desktop") + + os.makedirs(desktop, exist_ok=True) + + script = os.path.join(desktop, "kill_citrix.sh") + with open(script, "w") as f: + f.write("#!/bin/bash\npkill icasessionmgr\npkill wfica\n") + + os.chmod(script, 0o755) + + +# --------------------------------------------------------------------------- +# Gitea API +# --------------------------------------------------------------------------- + +def fetch_release_info(repo_owner, repo_name, extension=None): + """Fetch latest release info including assets from Gitea API. + + ``extension`` limits the returned assets to one package format. It defaults + to the running machine's family so that a Fedora client never even sees the + ``.deb`` assets, and vice versa. + """ + if extension is None: + extension = package_extension() + + api_url = f"{API_BASE}/repos/{repo_owner}/{repo_name}/releases/latest" + try: + with urllib.request.urlopen(api_url, timeout=30) as response: + release_info = json.loads(response.read().decode()) + + tag_name = release_info.get('tag_name', '') + published_at = release_info.get('published_at', '') + if not tag_name or not published_at: + raise ValueError("Invalid release data: missing tag_name or published_at") + + assets = {} + for asset in release_info.get('assets', []): + filename = asset.get('name', '') + if filename.endswith(extension): + download_url = asset.get('browser_download_url', '') + if not download_url: + logging.warning(f"No download URL for {filename}") + continue + # Gitea does not provide digests; hash will be None + hash_value = None + # For compatibility, check if 'digest' exists (unlikely in Gitea) + digest = asset.get('digest', '') + if digest and digest.startswith('sha256:'): + hash_value = digest[7:].lower() + logging.info(f"Fetched hash for {filename}: {hash_value[:16]}...") + else: + logging.info(f"No hash available for {filename}; verification will be skipped") + assets[filename] = {'url': download_url, 'hash': hash_value} + + if not assets: + logging.warning(f"No {extension} assets found in release.") + logging.info(f"Available assets: {[a['name'] for a in release_info.get('assets', [])]}") + else: + logging.info(f"Fetched {len(assets)} assets from API.") + + return { + 'tag_name': tag_name, + 'published_at': published_at, + 'assets': assets + } + except urllib.error.URLError as e: + logging.error(f"Failed to fetch release info from API: {e}") + raise + except json.JSONDecodeError as e: + logging.error(f"Failed to parse API JSON: {e}") + raise + except Exception as e: + logging.error(f"Unexpected error fetching release info: {e}") + raise + + +# --------------------------------------------------------------------------- +# Download +# --------------------------------------------------------------------------- def compute_sha256(file_path): - """Compute SHA256 hash of a file.""" - sha256 = hashlib.sha256() - try: - with open(file_path, 'rb') as file: - while True: - chunk = file.read(8192) - if not chunk: - break - sha256.update(chunk) - return sha256.hexdigest().lower() - except IOError as e: - logging.error(f"Failed to compute hash for {file_path}: {e}") - return None + """Compute SHA256 hash of a file.""" + sha256 = hashlib.sha256() + try: + with open(file_path, 'rb') as file: + while True: + chunk = file.read(8192) + if not chunk: + break + sha256.update(chunk) + return sha256.hexdigest().lower() + except IOError as e: + logging.error(f"Failed to compute hash for {file_path}: {e}") + return None + def download_chunk(url, save_path, start, end, total_size, progress_bar, lock): - try: - req = urllib.request.Request(url) - req.add_header('Range', f'bytes={start}-{end}') - with urllib.request.urlopen(req, timeout=30) as response: - downloaded = 0 - chunk_size = 8192 - while True: - chunk = response.read(chunk_size) - if not chunk: - break - downloaded += len(chunk) - with lock: - with open(save_path, 'r+b') as file: - file.seek(start + downloaded - len(chunk)) - file.write(chunk) - progress_bar.update(len(chunk)) - logging.info(f"Completed range {start}-{end}") - except urllib.error.HTTPError as http_err: - logging.error(f"HTTP error in range {start}-{end}: {http_err}") - except urllib.error.URLError as url_err: - logging.error(f"URL error in range {start}-{end}: {url_err}") - except IOError as io_err: - logging.error(f"File error in range {start}-{end}: {io_err}") - except Exception as e: - logging.error(f"Unexpected error in range {start}-{end}: {e}") + try: + req = urllib.request.Request(url) + req.add_header('Range', f'bytes={start}-{end}') + with urllib.request.urlopen(req, timeout=30) as response: + downloaded = 0 + chunk_size = 8192 + while True: + chunk = response.read(chunk_size) + if not chunk: + break + downloaded += len(chunk) + with lock: + with open(save_path, 'r+b') as file: + file.seek(start + downloaded - len(chunk)) + file.write(chunk) + progress_bar.update(len(chunk)) + logging.info(f"Completed range {start}-{end}") + except urllib.error.HTTPError as http_err: + logging.error(f"HTTP error in range {start}-{end}: {http_err}") + except urllib.error.URLError as url_err: + logging.error(f"URL error in range {start}-{end}: {url_err}") + except IOError as io_err: + logging.error(f"File error in range {start}-{end}: {io_err}") + except Exception as e: + logging.error(f"Unexpected error in range {start}-{end}: {e}") + def download_file(url, save_path, expected_hash=None, num_threads=4): - try: - with urllib.request.urlopen(url, timeout=30) as response: - total_size = int(response.getheader('Content-Length', 0)) - if total_size == 0: - raise ValueError("Could not determine file size") - logging.info(f"Total file size: {total_size} bytes") + try: + with urllib.request.urlopen(url, timeout=30) as response: + total_size = int(response.getheader('Content-Length', 0)) + if total_size == 0: + raise ValueError("Could not determine file size") + logging.info(f"Total file size: {total_size} bytes") - os.makedirs(os.path.dirname(save_path), exist_ok=True) - with open(save_path, 'wb') as file: - file.truncate(total_size) + os.makedirs(os.path.dirname(save_path), exist_ok=True) + with open(save_path, 'wb') as file: + file.truncate(total_size) - progress_bar = tqdm(total=total_size, unit='B', unit_scale=True, desc=os.path.basename(save_path), mininterval=0.1) - lock = threading.Lock() + progress_bar = tqdm(total=total_size, unit='B', unit_scale=True, desc=os.path.basename(save_path), mininterval=0.1) + lock = threading.Lock() - chunk_size = total_size // num_threads - ranges = [(i * chunk_size, (i + 1) * chunk_size - 1 if i < num_threads - 1 else total_size - 1) - for i in range(num_threads)] + chunk_size = total_size // num_threads + ranges = [(i * chunk_size, (i + 1) * chunk_size - 1 if i < num_threads - 1 else total_size - 1) + for i in range(num_threads)] - start_time = time.time() - with ThreadPoolExecutor(max_workers=num_threads) as executor: - futures = [executor.submit(download_chunk, url, save_path, start, end, total_size, progress_bar, lock) - for start, end in ranges] - for future in futures: - future.result() - end_time = time.time() + start_time = time.time() + with ThreadPoolExecutor(max_workers=num_threads) as executor: + futures = [executor.submit(download_chunk, url, save_path, start, end, total_size, progress_bar, lock) + for start, end in ranges] + for future in futures: + future.result() + end_time = time.time() - progress_bar.close() - print(f"File downloaded successfully and saved to {save_path}") - logging.info(f"Download completed in {end_time - start_time:.2f} seconds") + progress_bar.close() + print(f"File downloaded successfully and saved to {save_path}") + logging.info(f"Download completed in {end_time - start_time:.2f} seconds") - filename = os.path.basename(save_path) - if expected_hash: - computed_hash = compute_sha256(save_path) - if computed_hash is None: - raise Exception(f"Hash computation failed for {save_path}") - if computed_hash != expected_hash: - raise Exception(f"Hash mismatch for {save_path}: expected {expected_hash}, got {computed_hash}") - print(f"Hash verified for {filename}: {computed_hash}") - else: - logging.warning(f"Skipping hash verification for {filename} (no expected hash available)") + filename = os.path.basename(save_path) + if expected_hash: + computed_hash = compute_sha256(save_path) + if computed_hash is None: + raise Exception(f"Hash computation failed for {save_path}") + if computed_hash != expected_hash: + raise Exception(f"Hash mismatch for {save_path}: expected {expected_hash}, got {computed_hash}") + print(f"Hash verified for {filename}: {computed_hash}") + else: + logging.warning(f"Skipping hash verification for {filename} (no expected hash available)") - except urllib.error.HTTPError as http_err: - logging.error(f"HTTP error occurred: {http_err}") - raise - except urllib.error.URLError as url_err: - logging.error(f"URL error occurred: {url_err}") - raise - except IOError as io_err: - logging.error(f"File error occurred while saving: {io_err}") - raise - except Exception as e: - logging.error(f"An unexpected error occurred: {e}") - raise + except urllib.error.HTTPError as http_err: + logging.error(f"HTTP error occurred: {http_err}") + raise + except urllib.error.URLError as url_err: + logging.error(f"URL error occurred: {url_err}") + raise + except IOError as io_err: + logging.error(f"File error occurred while saving: {io_err}") + raise + except Exception as e: + logging.error(f"An unexpected error occurred: {e}") + raise -def is_newer_release(repo_owner, repo_name, last_release_file="/temp/packages/last_release.json"): - """Check if the current release is newer than the last downloaded one.""" - try: - release_data = fetch_release_info(repo_owner, repo_name) - current_tag = release_data['tag_name'] - current_published_at = release_data['published_at'] - - # Handle timezone in published_at (Gitea may include TZ offset, GitHub uses Z) - if 'Z' in current_published_at: - current_published_at = current_published_at.replace('Z', '+00:00') - current_time = datetime.datetime.fromisoformat(current_published_at) - if os.path.exists(last_release_file): - with open(last_release_file, 'r') as f: - last_release = json.load(f) - last_tag = last_release.get('tag_name', '') - last_published_at = last_release.get('published_at', '') - if last_published_at: - if 'Z' in last_published_at: - last_published_at = last_published_at.replace('Z', '+00:00') - last_time = datetime.datetime.fromisoformat(last_published_at) - if current_time <= last_time and last_tag == current_tag: - logging.info(f"No newer release found. Current: {current_tag} ({current_published_at}), Last: {last_tag} ({last_published_at})") - return False, None - - logging.info(f"Newer release detected or no previous download: {current_tag} ({current_published_at})") - return True, { - 'tag_name': current_tag, - 'published_at': current_published_at, - 'assets': release_data['assets'] - } - except urllib.error.URLError as e: - logging.error(f"Failed to fetch release info: {e}") - raise - except json.JSONDecodeError as e: - logging.error(f"Failed to parse API JSON: {e}") - raise - except IOError as e: - logging.error(f"Failed to read/write {last_release_file}: {e}") - raise - except Exception as e: - logging.error(f"Unexpected error checking release: {e}") - raise +def is_newer_release(repo_owner, repo_name, last_release_file=LAST_RELEASE_FILE): + """Check if the current release is newer than the last downloaded one.""" + try: + release_data = fetch_release_info(repo_owner, repo_name) + current_tag = release_data['tag_name'] + current_published_at = release_data['published_at'] -repo_owner = "REYHER-Systemtechnik-Public" -repo_name = "VDI-Linux" -last_release_file = "/temp/packages/last_release.json" + # Handle timezone in published_at (Gitea may include TZ offset, GitHub uses Z) + if 'Z' in current_published_at: + current_published_at = current_published_at.replace('Z', '+00:00') + current_time = datetime.datetime.fromisoformat(current_published_at) -is_newer, release_data = is_newer_release(repo_owner, repo_name, last_release_file) -if is_newer: - assets = release_data['assets'] - if not assets: - raise Exception("No RPM assets available in the latest release. Check logs for details.") + if os.path.exists(last_release_file): + with open(last_release_file, 'r') as f: + last_release = json.load(f) + last_tag = last_release.get('tag_name', '') + last_published_at = last_release.get('published_at', '') + if last_published_at: + if 'Z' in last_published_at: + last_published_at = last_published_at.replace('Z', '+00:00') + last_time = datetime.datetime.fromisoformat(last_published_at) + if current_time <= last_time and last_tag == current_tag: + logging.info(f"No newer release found. Current: {current_tag} ({current_published_at}), Last: {last_tag} ({last_published_at})") + return False, None - rpm_files = { - "/temp/packages/ctxusb.rpm": "ctxusb.rpm", - "/temp/packages/ICAClient.rpm": "ICAClient.rpm" - } + logging.info(f"Newer release detected or no previous download: {current_tag} ({current_published_at})") + return True, { + 'tag_name': current_tag, + 'published_at': current_published_at, + 'assets': release_data['assets'] + } + except urllib.error.URLError as e: + logging.error(f"Failed to fetch release info: {e}") + raise + except json.JSONDecodeError as e: + logging.error(f"Failed to parse API JSON: {e}") + raise + except IOError as e: + logging.error(f"Failed to read/write {last_release_file}: {e}") + raise + except Exception as e: + logging.error(f"Unexpected error checking release: {e}") + raise - for local_path, filename in rpm_files.items(): - if filename not in assets: - raise Exception(f"Required asset {filename} not found in release. Available: {list(assets.keys())}") - - asset_info = assets[filename] - url = asset_info['url'] - expected_hash = asset_info['hash'] - - if os.path.exists(local_path): - os.remove(local_path) - download_file(url, local_path, expected_hash) +def save_last_release(release_data): + os.makedirs(os.path.dirname(LAST_RELEASE_FILE), exist_ok=True) + with open(LAST_RELEASE_FILE, 'w') as f: + # Save only tag and published_at; assets are transient + json.dump({'tag_name': release_data['tag_name'], 'published_at': release_data['published_at']}, f) + logging.info(f"Saved last release info to {LAST_RELEASE_FILE}") - os.makedirs("/var/local/citrix-repo", exist_ok=True) - - logging.info("Copying ctxusb.rpm...") - shutil.copyfile("/temp/packages/ctxusb.rpm", "/var/local/citrix-repo/ctxusb.rpm") - logging.info("Finished copying ctxusb.rpm") - - logging.info("Copying ICAClient.rpm...") - shutil.copyfile("/temp/packages/ICAClient.rpm", "/var/local/citrix-repo/ICAClient.rpm") - logging.info("Finished copying ICAClient.rpm") - - os.system("createrepo_c /var/local/citrix-repo") - - logging.info("Copying local-citrix-repo.repo...") - shutil.copyfile("./local-citrix-repo.repo", "/etc/yum.repos.d/local-citrix-repo.repo") - logging.info("Finished copying local-citrix-repo.repo") - - logging.info("Copying RPM-GPG-KEY-citrix-local...") - shutil.copyfile("./RPM-GPG-KEY-citrix-local", "/var/local/citrix-repo/RPM-GPG-KEY-citrix-local") - logging.info("Finished copying RPM-GPG-KEY-citrix-local") - os.system("rpm --import /var/local/citrix-repo/RPM-GPG-KEY-citrix-local") +# --------------------------------------------------------------------------- +# Fedora publishing path - unchanged from the deployed version +# --------------------------------------------------------------------------- - os.system("dnf makecache") - os.system("pkcon refresh force") +def sync_fedora(is_newer, release_data): + if not is_newer: + logging.info("Skipping download; no new release available.") + return - os.makedirs(os.path.dirname(last_release_file), exist_ok=True) - with open(last_release_file, 'w') as f: - # Save only tag and published_at; assets are transient - json.dump({'tag_name': release_data['tag_name'], 'published_at': release_data['published_at']}, f) - logging.info(f"Saved last release info to {last_release_file}") -else: - logging.info("Skipping download; no new release available.") + assets = release_data['assets'] + if not assets: + raise Exception("No RPM assets available in the latest release. Check logs for details.") -print("All files processed successfully!") + for local_path, filename in FEDORA_RPM_FILES.items(): + if filename not in assets: + raise Exception(f"Required asset {filename} not found in release. Available: {list(assets.keys())}") + + asset_info = assets[filename] + url = asset_info['url'] + expected_hash = asset_info['hash'] + + if os.path.exists(local_path): + os.remove(local_path) + + download_file(url, local_path, expected_hash) + + os.makedirs(LOCAL_REPO_DIR, exist_ok=True) + + logging.info("Copying ctxusb.rpm...") + shutil.copyfile(os.path.join(STAGING_DIR, "ctxusb.rpm"), os.path.join(LOCAL_REPO_DIR, "ctxusb.rpm")) + logging.info("Finished copying ctxusb.rpm") + + logging.info("Copying ICAClient.rpm...") + shutil.copyfile(os.path.join(STAGING_DIR, "ICAClient.rpm"), os.path.join(LOCAL_REPO_DIR, "ICAClient.rpm")) + logging.info("Finished copying ICAClient.rpm") + + os.system(f"createrepo_c {LOCAL_REPO_DIR}") + + logging.info("Copying local-citrix-repo.repo...") + shutil.copyfile("./local-citrix-repo.repo", "/etc/yum.repos.d/local-citrix-repo.repo") + logging.info("Finished copying local-citrix-repo.repo") + + logging.info("Copying RPM-GPG-KEY-citrix-local...") + shutil.copyfile("./RPM-GPG-KEY-citrix-local", os.path.join(LOCAL_REPO_DIR, "RPM-GPG-KEY-citrix-local")) + logging.info("Finished copying RPM-GPG-KEY-citrix-local") + + os.system(f"rpm --import {LOCAL_REPO_DIR}/RPM-GPG-KEY-citrix-local") + + os.system("dnf makecache") + os.system("pkcon refresh force") + + save_last_release(release_data) + + +# --------------------------------------------------------------------------- +# Debian publishing path +# --------------------------------------------------------------------------- + +# The canonical apt source line. local-citrix-repo.list holds the same text and +# is the file a human edits; this constant is the fallback so that a missing or +# unreadable file cannot break the 10-minute cron job. +DEBIAN_SOURCES_LINE = "deb [trusted=yes] file:/var/local/citrix-repo ./" +DEBIAN_SOURCES_PATH = "/etc/apt/sources.list.d/local-citrix-repo.list" + + +def _repo_file(name): + """Locate a file that ships alongside this script. + + The Fedora path uses bare relative paths (``./local-citrix-repo.repo``), + which only works when the working directory happens to be the checkout. + The cron job does ``cd /root/VDI-Linux`` first so that is fine there, but + anything that invokes this script from elsewhere - a test, a manual run, + another wrapper - would fail with a bare FileNotFoundError. Look next to + the script first and fall back to the old relative behaviour so that a + Fedora client whose cwd differs still works exactly as before. + """ + alongside = os.path.join(os.path.dirname(os.path.abspath(__file__)), name) + if os.path.exists(alongside): + return alongside + return "./" + name + + +def _match_debian_asset(assets, package_name): + """Find the release asset for ``package_name``, ignoring its version. + + Citrix names the Debian packages with the version embedded + (``icaclient_26.08.0.225_amd64.deb``), so an exact filename match would + break on every new release. + """ + for filename in sorted(assets): + lowered = filename.lower() + if lowered.startswith(package_name) and lowered.endswith(".deb"): + return filename + return None + + +def resolve_debian_packages(release_data): + """Return [(filename, url_or_local_path, expected_hash)] for both packages. + + Prefers the Gitea release. Falls back to ``DEBIAN_FALLBACK_DIR`` when the + release carries no ``.deb`` assets, so an operator can stage the two files + locally instead of publishing a release. + """ + assets = (release_data or {}).get('assets', {}) or {} + resolved = [] + missing = [] + + for package_name in DEBIAN_PACKAGES: + filename = _match_debian_asset(assets, package_name) + if filename: + info = assets[filename] + resolved.append((filename, info['url'], info['hash'])) + else: + missing.append(package_name) + + if not missing: + return resolved + + # Fall back to a local directory. + logging.warning( + "Release %s has no .deb assets for: %s; looking in %s", + (release_data or {}).get('tag_name', ''), + ", ".join(missing), + DEBIAN_FALLBACK_DIR, + ) + fallback = {} + if os.path.isdir(DEBIAN_FALLBACK_DIR): + for entry in sorted(os.listdir(DEBIAN_FALLBACK_DIR)): + if entry.lower().endswith(".deb"): + fallback[entry] = os.path.join(DEBIAN_FALLBACK_DIR, entry) + + if not fallback: + raise Exception( + "No .deb assets for %s in the latest release, and %s contains no .deb files. " + "Upload icaclient__amd64.deb and ctxusb__amd64.deb to the " + "release, or set VDI_DEB_DIR to a directory holding them." + % (", ".join(missing), DEBIAN_FALLBACK_DIR) + ) + + # Rebuild the list, substituting fallback files for whatever was missing. + resolved = [] + for package_name in DEBIAN_PACKAGES: + filename = _match_debian_asset(assets, package_name) + if filename: + info = assets[filename] + resolved.append((filename, info['url'], info['hash'])) + continue + local_name = _match_debian_asset(fallback, package_name) + if not local_name: + raise Exception( + "No %s*.deb found in the release or in %s. Found there: %s" + % (package_name, DEBIAN_FALLBACK_DIR, sorted(fallback) or "nothing") + ) + logging.info("Using local file %s for %s", fallback[local_name], package_name) + resolved.append((local_name, fallback[local_name], None)) + + return resolved + + +def build_debian_index(repo_dir): + """Generate Packages/Packages.gz/Release for a flat apt repository. + + A flat ("trivial") archive is the apt equivalent of the single-directory + createrepo repository on the Fedora side: no dists/ tree, and apt reads it + with ``deb [trusted=yes] file:/var/local/citrix-repo ./``. + """ + packages_path = os.path.join(repo_dir, "Packages") + release_path = os.path.join(repo_dir, "Release") + + with open(packages_path, "wb") as handle: + result = subprocess.run( + ["apt-ftparchive", "packages", "."], + cwd=repo_dir, stdout=handle, stderr=subprocess.PIPE, + ) + if result.returncode != 0: + raise Exception( + "apt-ftparchive packages failed: %s" + % result.stderr.decode(errors="replace").strip() + ) + + # apt ignores an uncompressed Packages file only for file:// sources; ship + # the gzip as well so the repository also works over http if it is ever + # exported. + with open(packages_path, "rb") as source, open(packages_path + ".gz", "wb") as target: + import gzip + with gzip.GzipFile(fileobj=target, mode="wb", mtime=0) as gz: + shutil.copyfileobj(source, gz) + + with open(release_path, "wb") as handle: + result = subprocess.run( + [ + "apt-ftparchive", + "-o", "APT::FTPArchive::Release::Origin=REYHER", + "-o", "APT::FTPArchive::Release::Label=Citrix Local Repository", + "-o", "APT::FTPArchive::Release::Suite=trixie", + "-o", "APT::FTPArchive::Release::Codename=trixie", + "-o", "APT::FTPArchive::Release::Architectures=amd64", + "-o", "APT::FTPArchive::Release::Description=Locally built Citrix Workspace packages", + "release", ".", + ], + cwd=repo_dir, stdout=handle, stderr=subprocess.PIPE, + ) + if result.returncode != 0: + raise Exception( + "apt-ftparchive release failed: %s" + % result.stderr.decode(errors="replace").strip() + ) + + if os.path.getsize(packages_path) == 0: + raise Exception("apt-ftparchive produced an empty Packages file in %s" % repo_dir) + + +def sync_debian(is_newer, release_data): + repo_ready = local_repo_is_populated() + + if not is_newer and repo_ready: + logging.info("Skipping download; no new release available.") + return + + if not is_newer and not repo_ready: + logging.info("No newer release, but %s has no package index; rebuilding.", LOCAL_REPO_DIR) + + if release_data is None: + # Reached when the release was unchanged but the repository is missing. + release_data = fetch_release_info(REPO_OWNER, REPO_NAME) + + packages = resolve_debian_packages(release_data) + + os.makedirs(STAGING_DIR, exist_ok=True) + os.makedirs(LOCAL_REPO_DIR, exist_ok=True) + + # Remove every .deb from the published directory before repopulating it. + # + # The Fedora path writes fixed filenames (ctxusb.rpm, ICAClient.rpm), so a + # new release overwrites the old files and the repository can never hold two + # versions. Debian package filenames embed the version, so copying alone + # would leave every previous version behind; `apt-ftparchive packages .` + # would then index all of them and apt would always pick the highest, making + # a rollback impossible. Deleting first reproduces the Fedora guarantee. + for entry in os.listdir(LOCAL_REPO_DIR): + if entry.lower().endswith(".deb"): + stale = os.path.join(LOCAL_REPO_DIR, entry) + try: + os.remove(stale) + logging.info("Removed stale package %s", entry) + except OSError as error: + logging.warning("Could not remove stale package %s: %s", stale, error) + + for filename, source, expected_hash in packages: + staged = os.path.join(STAGING_DIR, filename) + + if os.path.exists(source): + # Already a local file (fallback directory). + logging.info("Copying %s into the staging area", source) + if os.path.abspath(source) != os.path.abspath(staged): + shutil.copyfile(source, staged) + else: + if os.path.exists(staged): + os.remove(staged) + download_file(source, staged, expected_hash) + + published = os.path.join(LOCAL_REPO_DIR, filename) + logging.info("Copying %s...", filename) + shutil.copyfile(staged, published) + logging.info("Finished copying %s", filename) + + logging.info("Building the local apt repository index...") + build_debian_index(LOCAL_REPO_DIR) + logging.info("Finished building the local apt repository index") + + # The apt source file. trusted=yes is deliberate: this repository is + # generated on the machine from packages this script just fetched and + # verified, so there is no second party to authenticate against. See + # README.md for the reasoning. + logging.info("Copying local-citrix-repo.list...") + sources_src = _repo_file("local-citrix-repo.list") + try: + shutil.copyfile(sources_src, DEBIAN_SOURCES_PATH) + except OSError as error: + # Never let a missing file stop the repository from being usable: write + # the canonical line instead and say so. + logging.warning("Could not copy %s (%s); writing the source line directly", sources_src, error) + with open(DEBIAN_SOURCES_PATH, "w") as handle: + handle.write("# Local Citrix repository (generated by update_local_repo.py)\n") + handle.write(DEBIAN_SOURCES_LINE + "\n") + logging.info("Finished copying local-citrix-repo.list") + + subprocess.run(["apt-get", "update"], check=False) + + if shutil.which("pkcon"): + subprocess.run(["pkcon", "refresh", "force"], check=False) + + save_last_release(release_data) + + +# --------------------------------------------------------------------------- +# Entry point +# --------------------------------------------------------------------------- + +def main(): + logging.info("Detected package family: %s", PACKAGE_FAMILY) + + # Update the local repository via git pull before proceeding + try: + subprocess.run(['git', 'pull'], check=True, capture_output=True) + logging.info("Updated repo via git pull") + except subprocess.CalledProcessError as e: + logging.warning(f"Git pull failed (non-zero exit): {e}") + except FileNotFoundError: + logging.warning("Git not found; skipping pull") + except Exception as e: + logging.warning(f"Unexpected error during git pull: {e}") + + # Apply the standard KDE Plasma settings. This also installs the per-session + # helper used by the XDG autostart entry, and updates users who are currently + # logged in. Keep this before the release download so the settings are applied + # even if the Citrix repository update fails. + try: + from configure_kde import apply_all as apply_kde_settings + apply_kde_settings() + except Exception as e: + logging.exception(f"Failed to apply KDE settings: {e}") + + install_desktop_shortcuts() + + is_newer, release_data = is_newer_release(REPO_OWNER, REPO_NAME) + + if PACKAGE_FAMILY == "debian": + sync_debian(is_newer, release_data) + else: + sync_fedora(is_newer, release_data) + + print("All files processed successfully!") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())