Files
VDI-Linux/update_local_repo.py
T
Mistercoolertyper 6ddb1d251a 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.
2026-11-04 09:24:57 +01:00

750 lines
30 KiB
Python
Executable File

#!/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
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', '<unknown>'),
", ".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_<version>_amd64.deb and ctxusb_<version>_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())