#!/usr/bin/env python3 """Keep the local Citrix package repository in sync with the latest Gitea release. 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 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') # --------------------------------------------------------------------------- # Constants # --------------------------------------------------------------------------- REPO_OWNER = "REYHER-Systemtechnik-Public" REPO_NAME = "VDI-Linux" API_BASE = "https://nachos386.lcube-server.de/api/v1" # Where downloaded packages are staged before being published. STAGING_DIR = "/temp/packages" LAST_RELEASE_FILE = os.path.join(STAGING_DIR, "last_release.json") # The published local repository. Same path on both families. LOCAL_REPO_DIR = "/var/local/citrix-repo" # 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: 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") tokens = set((ident + " " + like).split()) if "debian" in tokens or "ubuntu" in tokens: return "debian" if tokens & {"fedora", "rhel", "centos", "rocky", "almalinux"}: return "fedora" # 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" logging.warning("Could not identify the distribution; assuming Fedora") return "fedora" 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 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}") 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") 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() 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() 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)") 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=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'] # 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 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}") # --------------------------------------------------------------------------- # Fedora publishing path - unchanged from the deployed version # --------------------------------------------------------------------------- def sync_fedora(is_newer, release_data): if not is_newer: logging.info("Skipping download; no new release available.") return assets = release_data['assets'] if not assets: raise Exception("No RPM assets available in the latest release. Check logs for details.") 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 # --------------------------------------------------------------------------- # Debian: no-password updates and the offline update # --------------------------------------------------------------------------- # Lets members of the "sudo" group refresh and install updates from a graphical # session without being asked for a password, which is how the Fedora clients # behave. Debian's own /usr/share/polkit-1/rules.d/org.freedesktop.packagekit.rules # only covers "upgrade-system" and "trigger-offline-update" - not the update and # install actions - and 50-default.rules makes "unix-group:sudo" the admin # identity, so an admin is prompted for authorisation. This rule supplies the # missing cases. # # Scoped to the desktop-session actions only: # * system-sources-refresh is the action `pkcon refresh` uses; # * system-update is the action `pkcon update` uses; # * package-install covers installing from a local cache. # Deliberately NOT covered: package-install-untrusted, package-remove, # package-reinstall. Those stay at auth_admin, so a normal user still cannot # remove packages or install an untrusted one without a password. # # NOTE on subject.local: an earlier version of this rule also required # `subject.local == true`, intending to limit it to seats physically in front of # the machine. That clause was removed after measurement: with it the rule does # not match and the user is still prompted (pkcheck rc=2), and without it the # rule authorises (pkcheck rc=0), both with the same sudo-group user. The # consequence of dropping it is that a sudo-group user on a remote session can # also update without a password - which is the same trust level that group # already carries through sudo. POLKIT_RULE_PATH = "/etc/polkit-1/rules.d/60-vdi-no-password-updates.rules" POLKIT_RULE = """// Installed by VDI-Linux update_local_repo.py. Do not edit; it is rewritten. // // Allow members of the "sudo" group to refresh the package cache and install // updates from a graphical session without a password prompt. This matches the // behaviour of the Fedora clients. Everything else PackageKit can do still // requires authorisation. polkit.addRule(function(action, subject) { if ((action.id == "org.freedesktop.packagekit.system-sources-refresh" || action.id == "org.freedesktop.packagekit.system-update" || action.id == "org.freedesktop.packagekit.package-install") && subject.isInGroup("sudo")) { return polkit.Result.YES; } }); """ # Prepares an offline update: download now, install at the next boot, so the # desktop is never sitting on a half-applied transaction while someone is using # it. Same intent as the Fedora auto_update.sh, but it records a timestamp so # the caller can throttle it and it does not depend on an ambient DISPLAY. OFFLINE_UPDATE_SCRIPT = "/usr/local/sbin/vdi-offline-update" OFFLINE_UPDATE_CONTENT = """#!/bin/bash # Prepare a PackageKit offline update. Installed by VDI-Linux; do not edit. # # Runs as root from cron, so PackageKit authorises it without a password. The # update is downloaded now and applied by packagekit-offline-update.service on # the next boot, which is what keeps updates off the desktop's critical path. set -u STAMP=/var/lib/vdi-offline-update.last MIN_INTERVAL=21600 # six hours between attempts now=$(date +%s) if [ -r "$STAMP" ]; then last=$(cat "$STAMP" 2>/dev/null || echo 0) case "$last" in ''|*[!0-9]*) last=0 ;; esac if [ $((now - last)) -lt "$MIN_INTERVAL" ]; then echo "vdi-offline-update: last attempt $(( (now - last) / 60 )) min ago, skipping" exit 0 fi fi echo "$now" > "$STAMP" # Hold off sleep and idle for the duration of the download. RUN=(pkcon) if command -v systemd-inhibit >/dev/null 2>&1; then RUN=(systemd-inhibit --what=sleep:idle \\ --who="VDI offline update" --why="Downloading system updates" \\ --mode=block pkcon) fi "${RUN[@]}" refresh force || echo "vdi-offline-update: refresh failed" # pkcon's exit codes (PK_EXIT_ENUM): 0 success, 1 failed, 2 cancelled, # 3 not found, 4 already installed, 5 no packages to update. Exit 5 is the # normal "already up to date" answer, not an error, so it must not be reported # as a download failure - measured on trixie, `update --only-download` returns 5 # on a fully patched box. "${RUN[@]}" update --only-download rc=$? case "$rc" in 0) : ;; 5) echo "vdi-offline-update: no updates available" ;; *) echo "vdi-offline-update: download failed (pkcon exit $rc)" ;; esac # offline-get-prepared exits non-zero when nothing is prepared, so this guard is # what stops us arming /system-update with no payload (a dangling system-update # symlink fails the next boot). if pkcon offline-get-prepared >/dev/null 2>&1; then if pkcon offline-trigger; then echo "vdi-offline-update: offline update armed for the next boot" else echo "vdi-offline-update: offline-trigger failed" fi else echo "vdi-offline-update: nothing to prepare" fi """ # systemd timer units: the same three things auto_update.sh does, on a timer. # Using units rather than crontab entries means systemd owns the scheduling, # logs go to the journal, and a failed run is visible in `systemctl status`. OFFLINE_UPDATE_SERVICE = """[Unit] Description=Prepare a PackageKit offline update [Service] Type=oneshot ExecStart=/usr/local/sbin/vdi-offline-update Nice=10 IOSchedulingClass=idle """ OFFLINE_UPDATE_TIMER = """[Unit] Description=Prepare a PackageKit offline update periodically [Timer] OnBootSec=15min OnUnitActiveSec=6h RandomizedDelaySec=15min Persistent=true [Install] WantedBy=timers.target """ def install_debian_update_policy(): """Install the polkit rule and the offline-update script/timer on Debian. Both are idempotent: each file is only rewritten when its content differs, so a 10-minute cron run does not touch the disk or restart anything. """ if PACKAGE_FAMILY != "debian": return for path, content, mode in ( (POLKIT_RULE_PATH, POLKIT_RULE, 0o644), (OFFLINE_UPDATE_SCRIPT, OFFLINE_UPDATE_CONTENT, 0o755), ("/etc/systemd/system/vdi-offline-update.service", OFFLINE_UPDATE_SERVICE, 0o644), ("/etc/systemd/system/vdi-offline-update.timer", OFFLINE_UPDATE_TIMER, 0o644), ): try: os.makedirs(os.path.dirname(path), exist_ok=True) current = None if os.path.exists(path): with open(path, encoding="utf-8") as handle: current = handle.read() if current == content: continue temporary = path + ".tmp" with open(temporary, "w", encoding="utf-8") as handle: handle.write(content) os.chmod(temporary, mode) os.replace(temporary, path) logging.info("Installed %s", path) except OSError as error: logging.warning("Could not install %s: %s", path, error) # Enable the timer. `enable --now` would start it, but in a chroot there is # no running systemd, so tolerate failure and let it come up on first boot. if shutil.which("systemctl"): subprocess.run(["systemctl", "daemon-reload"], check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) subprocess.run(["systemctl", "enable", "vdi-offline-update.timer"], check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 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() # Debian needs a polkit rule before a desktop user can update without a # password, and the offline-update timer. Both are idempotent and are done # here, not in the preseed, so existing machines pick them up on the next # cron run - the same way configure_kde.py back-fills older installs. install_debian_update_policy() 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())