2025-11-10 07:53:20 +01:00
|
|
|
#!/usr/bin/env python3
|
2026-11-04 09:24:53 +01:00
|
|
|
"""Keep the local Citrix package repository in sync with the latest Gitea release.
|
2025-11-10 07:53:20 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
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
|
2025-11-04 15:19:37 +01:00
|
|
|
import hashlib
|
|
|
|
|
import json
|
2026-11-04 09:24:53 +01:00
|
|
|
import logging
|
|
|
|
|
import os
|
2025-11-04 15:19:37 +01:00
|
|
|
import shutil
|
2025-11-10 11:28:22 +01:00
|
|
|
import subprocess
|
2026-11-04 09:24:53 +01:00
|
|
|
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
|
2025-11-04 15:19:37 +01:00
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - Thread %(threadName)s - %(message)s')
|
|
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# Constants
|
|
|
|
|
# ---------------------------------------------------------------------------
|
2025-11-10 11:28:22 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
REPO_OWNER = "REYHER-Systemtechnik-Public"
|
|
|
|
|
REPO_NAME = "VDI-Linux"
|
2026-11-03 11:36:52 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
API_BASE = "https://nachos386.lcube-server.de/api/v1"
|
2025-11-10 10:42:57 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
# Where downloaded packages are staged before being published.
|
|
|
|
|
STAGING_DIR = "/temp/packages"
|
|
|
|
|
LAST_RELEASE_FILE = os.path.join(STAGING_DIR, "last_release.json")
|
2026-07-23 14:58:48 +02:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
# The published local repository. Same path on both families.
|
|
|
|
|
LOCAL_REPO_DIR = "/var/local/citrix-repo"
|
2026-07-23 14:58:48 +02:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
# 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",
|
|
|
|
|
}
|
2026-07-23 14:58:48 +02:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
# 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 = ""
|
2026-07-23 14:58:48 +02:00
|
|
|
try:
|
2026-11-04 09:24:53 +01:00
|
|
|
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")
|
2026-07-23 14:58:48 +02:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
tokens = set((ident + " " + like).split())
|
2026-07-23 14:58:48 +02:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
if "debian" in tokens or "ubuntu" in tokens:
|
|
|
|
|
return "debian"
|
|
|
|
|
if tokens & {"fedora", "rhel", "centos", "rocky", "almalinux"}:
|
|
|
|
|
return "fedora"
|
2026-07-23 14:58:48 +02:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
# 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"
|
2026-07-23 14:58:48 +02:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
logging.warning("Could not identify the distribution; assuming Fedora")
|
|
|
|
|
return "fedora"
|
2026-07-23 14:58:48 +02:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
# ---------------------------------------------------------------------------
|
2025-11-04 15:19:37 +01:00
|
|
|
|
|
|
|
|
def compute_sha256(file_path):
|
2026-11-04 09:24:53 +01:00
|
|
|
"""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
|
|
|
|
|
|
2025-11-04 15:19:37 +01:00
|
|
|
|
|
|
|
|
def download_chunk(url, save_path, start, end, total_size, progress_bar, lock):
|
2026-11-04 09:24:53 +01:00
|
|
|
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}")
|
|
|
|
|
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2025-11-10 10:42:57 +01:00
|
|
|
def download_file(url, save_path, expected_hash=None, num_threads=4):
|
2026-11-04 09:24:53 +01:00
|
|
|
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")
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
|
|
|
|
with open(save_path, 'wb') as file:
|
|
|
|
|
file.truncate(total_size)
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
progress_bar = tqdm(total=total_size, unit='B', unit_scale=True, desc=os.path.basename(save_path), mininterval=0.1)
|
|
|
|
|
lock = threading.Lock()
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
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)]
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
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()
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
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")
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
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)")
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
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
|
2025-11-04 15:19:37 +01:00
|
|
|
|
|
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
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']
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
# 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)
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
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
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
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
|
2025-11-04 15:19:37 +01:00
|
|
|
|
|
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
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}")
|
2025-11-10 10:42:57 +01:00
|
|
|
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# Fedora publishing path - unchanged from the deployed version
|
|
|
|
|
# ---------------------------------------------------------------------------
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
def sync_fedora(is_newer, release_data):
|
|
|
|
|
if not is_newer:
|
|
|
|
|
logging.info("Skipping download; no new release available.")
|
|
|
|
|
return
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
assets = release_data['assets']
|
|
|
|
|
if not assets:
|
|
|
|
|
raise Exception("No RPM assets available in the latest release. Check logs for details.")
|
2025-11-04 15:19:37 +01:00
|
|
|
|
2026-11-04 09:24:53 +01:00
|
|
|
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())
|