infrastructure/pipeline/flatpak-cache.sh
Archipelago f4978e6bdc Reuse the cached Flatpak export when nothing changed
- prepare-flatpak-seed.sh records the exact ref->commit manifest of the
  installation; an unchanged manifest keeps the previously verified export
  instead of rebuilding create-usb, fsck, 123 GPG checks and 192k hashes.
- pacman -Sy --needed instead of -Syu: the toolroot is a fixed bootstrap.
- flatpak-cache.sh keeps an "unchanged" outcome and verifies each export once
  (marker) instead of re-hashing 190k files on every run.
2026-11-13 15:51:14 +03:00

96 lines
4.7 KiB
Bash

#!/bin/bash
# All execution is local to the build worker. Never use the host Flatpak install.
set -euo pipefail
source_root=$1
cache=$2
bootstrap=$3
packages=$4
refresh_seconds=${5:-1800}
variant=${6:-gnome}
[[ $variant == gnome || $variant == kde ]]
initial_seconds=${7:-7200}
[[ $refresh_seconds =~ ^[0-9]+$ && $initial_seconds =~ ^[0-9]+$ ]]
(( refresh_seconds > 0 && initial_seconds >= refresh_seconds ))
mkdir -p "$cache" "$packages"
exec 9>"$cache/refresh.lock"
flock -n 9
stamp=$(date -u +%Y%m%dT%H%M%SZ)
attempt="$cache/runs/$stamp"
mkdir -p "$attempt" "$cache/installation" "$cache/exports"
policy="$source_root/purposes/proposals/$variant-flatpak-apps.json"
if [[ ! -d $cache/tools/root.x86_64 ]]; then
printf '%s %s\n' 655bc99849b80859173fa39338a6c910185e952173f6a1a61526cfa34ab8339c "$bootstrap" | sha256sum -c -
mkdir -p "$cache/extractor" "$cache/tools"
tar --zstd -xpf "$bootstrap" -C "$cache/extractor"
systemd-nspawn --quiet --register=no --settings=no --private-network \
-D "$cache/extractor/root.x86_64" --bind-ro="$bootstrap:/bootstrap.tar.zst" \
--bind="$cache/tools:/output" bsdtar --xattrs --acls -xpf /bootstrap.tar.zst -C /output
rm -rf "$cache/extractor"
fi
printf 'Server = https://mirror.nw-sys.ru/archlinux/$repo/os/$arch\n' > "$cache/tools/root.x86_64/etc/pacman.d/mirrorlist"
grep -q '^DisableSandbox' "$cache/tools/root.x86_64/etc/pacman.conf" || \
sed -i '/\[options\]/a DisableSandbox' "$cache/tools/root.x86_64/etc/pacman.conf"
touch "$attempt/builder-marker"
cached=()
if [[ -f $cache/current/SHA256SUMS ]]; then
# Full verification of ~190k files costs minutes and the export is immutable
# once published, so verify it once and trust the marker afterwards.
if [[ ! -f $cache/current/.verified ]]; then
(cd "$cache/current"; sha256sum --quiet -c SHA256SUMS)
: > "$cache/current/.verified"
fi
cached=(--bind-ro="$cache/current:/cached-seed")
fi
result=0
timeout_seconds=$refresh_seconds
if [[ ! -f $cache/current/SHA256SUMS ]]; then timeout_seconds=$initial_seconds; fi
printf 'Flatpak %s: bounded %ss %s\n' "$variant" "$timeout_seconds" "$(if [[ -f $cache/current/SHA256SUMS ]]; then echo refresh; else echo initial-fill; fi)"
timeout --signal=TERM --kill-after=60 "$timeout_seconds" \
systemd-nspawn --quiet --ephemeral --register=no --settings=no \
--machine="archipelago-flatpak-$stamp" -D "$cache/tools/root.x86_64" \
--bind-ro="$source_root/builder/adapters/prepare-flatpak-seed.sh:/prepare.sh" \
--bind-ro="$policy:/policy.json" \
--bind-ro="$attempt/builder-marker:/run/archipelago-flatpak-seed-builder" \
--bind="$attempt:/seed" --bind="$cache/installation:/var/lib/flatpak" \
--bind="$packages:/var/cache/pacman/pkg" "${cached[@]}" \
/bin/bash -ec 'pacman-key --init; pacman-key --populate archlinux; pacman -Sy --needed --noconfirm flatpak python diffutils; bash /prepare.sh' \
> "$attempt/refresh.log" 2>&1 || result=$?
if (( result == 0 )) && [[ -f $attempt/no-change ]]; then
# The installed refs and their commits did not move: the previous export is
# still exact, so skip create-usb, fsck, signature checks and hashing.
outcome=unchanged
rm -rf "$attempt/export" "$attempt/no-change"
elif (( result == 0 )); then
(cd "$attempt/export"; sha256sum --quiet -c SHA256SUMS)
: > "$attempt/export/.verified"
mv "$attempt/export" "$cache/exports/$stamp"
ln -s "exports/$stamp" "$cache/current.next"
mv -Tf "$cache/current.next" "$cache/current"
outcome=updated
else
# A failed refresh never replaces a known-good export with partial updates.
test -f "$cache/current/SHA256SUMS" || { cat "$attempt/refresh.log"; exit "$result"; }
cmp "$policy" "$cache/current/policy.json"
(cd "$cache/current"; sha256sum --quiet -c SHA256SUMS)
outcome=cached-after-refresh-failure
rm -rf "$attempt/export"
fi
python3 - "$cache" "$attempt" "$outcome" "$result" <<'PY'
import hashlib,json,shutil,sys
from pathlib import Path
cache,attempt=map(Path,sys.argv[1:3])
status=dict(state=sys.argv[3],refresh_exit_code=int(sys.argv[4]),log=str(attempt/'refresh.log'),
export=str((cache/'current').resolve()),
manifest_sha256=hashlib.sha256((cache/'current/SHA256SUMS').read_bytes()).hexdigest())
(attempt/'status.json').write_text(json.dumps(status,indent=2)+'\n')
temp=cache/'status.tmp'
temp.write_text(json.dumps(status,indent=2)+'\n')
temp.replace(cache/'status.json')
exports=sorted((p for p in (cache/'exports').iterdir() if p.is_dir()),key=lambda p:p.stat().st_mtime,reverse=True)
current=Path(status['export'])
keep={current,*[p for p in exports if p != current][:2]}
for old in exports:
if old not in keep:
shutil.rmtree(old)
print(json.dumps(status))
PY