infrastructure/pipeline/flatpak-cache.sh

77 lines
3.6 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}
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/gnome-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
(cd "$cache/current"; sha256sum --quiet -c SHA256SUMS)
cached=(--bind-ro="$cache/current:/cached-seed")
fi
result=0
timeout --signal=TERM --kill-after=60 "$refresh_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 -Syu --needed --noconfirm flatpak python diffutils; bash /prepare.sh' \
> "$attempt/refresh.log" 2>&1 || result=$?
if (( result == 0 )); then
(cd "$attempt/export"; sha256sum --quiet -c SHA256SUMS)
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