infrastructure/deploy/mirror-snapshot-mount.sh

133 lines
5.4 KiB
Bash

#!/bin/bash
# Mount each monthly mirror snapshot read-only under the public artifact root,
# prune views whose snapshot was destroyed by retention, and publish the
# resulting list for the control portal.
set -euo pipefail
dataset=tank/archipelago/archlinux
# /opt/archipelago/data is a symlink onto the tank dataset, but the kernel mount
# table always records the resolved path, so compare resolved paths.
view=$(realpath -m /opt/archipelago/data/public/arch-repo-snapshot)
manifest=/opt/archipelago/data/private/release-cycle/snapshots.json
# Host mounts only reach the artifact container when its bind uses slave
# propagation; the container that serves /archipelago/ is verified by that name.
container=${ARCHIPELAGO_ARTIFACT_CONTAINER:-archipelago-web-1}
artifact_path=${ARCHIPELAGO_ARTIFACT_PATH:-/srv/artifacts}
mkdir -p "$view" "$(dirname "$manifest")"
mapfile -t snaps < <(zfs list -H -o name -t snapshot -d 1 "$dataset" | grep -E '@monthly-' | sort)
declare -A keep=()
dates=()
for snap in "${snaps[@]}"; do
date=${snap##*@monthly-}
keep[$date]=1
target="$view/$date"
mkdir -p "$target"
# Mount snapshots by name: `.zfs/snapshot` automount entries can stay dangling
# after a dataset remount, which makes that path unusable from a service.
# An already correct view is left in place: unmounting it would interrupt a
# build that is downloading from that snapshot.
if mountpoint -q "$target" && [ "$(findmnt -n -o SOURCE --target "$target")" = "$snap" ]; then
dates+=("$date")
continue
fi
if mountpoint -q "$target"; then
if ! umount "$target"; then
echo "warning: cannot unmount stale view $target" >&2
unset 'keep[$date]'
continue
fi
fi
if ! mount -t zfs -o ro "$snap" "$target"; then
# Never leave an unmounted directory behind: it would publish a stale
# snapshot date that no longer resolves, and the prune step would then
# break the retention contract in the other direction.
echo "warning: cannot mount $snap" >&2
unset 'keep[$date]'
rmdir "$target" 2>/dev/null || true
continue
fi
dates+=("$date")
echo "mounted $date"
done
for target in "$view"/*; do
[ -d "$target" ] || continue
date=${target##*/}
[[ $date =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] || continue
[ -n "${keep[$date]:-}" ] && continue
if mountpoint -q "$target"; then
# Retention destroyed the snapshot; its view cannot stay mounted.
if ! umount "$target"; then
echo "warning: cannot unmount view $date of a destroyed snapshot" >&2
continue
fi
fi
echo "pruning view $date"
rm -rf "$target"
done
# A container that bind-mounted the artifact root keeps the mount view it had when
# it started, so a snapshot mounted afterwards looks like an empty directory and
# HTTPS answers 404 although the host view is complete. compose.yaml asks for
# slave propagation, but a container created before that change still serves the
# stale tree: verify from inside and restart it once instead of publishing a date
# no client can download.
container_running() {
[ "$(docker inspect -f '{{.State.Running}}' "$container" 2>/dev/null || true)" = true ]
}
container_serves() {
docker exec "$container" test -f "$artifact_path/arch-repo-snapshot/$1/core/os/x86_64/core.db"
}
if ((${#dates[@]})) && command -v docker >/dev/null 2>&1 && container_running; then
stale=()
for date in "${dates[@]}"; do
container_serves "$date" || stale+=("$date")
done
if ((${#stale[@]})); then
echo "artifact container $container does not serve ${stale[*]}; restarting" >&2
if docker restart "$container" >/dev/null; then
for attempt in {1..10}; do
unserved=()
for date in "${stale[@]}"; do
container_serves "$date" || unserved+=("$date")
done
((${#unserved[@]})) || break
stale=("${unserved[@]}")
sleep 1
done
else
echo "warning: cannot restart $container" >&2
fi
if ((${#stale[@]})); then
echo "warning: artifact container still does not serve ${stale[*]}" >&2
fi
fi
# Never advertise a view the artifact container cannot serve: the portal would
# offer a snapshot that fails inside the first pacman -Syu.
verified=()
for date in "${dates[@]}"; do
if container_serves "$date"; then
verified+=("$date")
else
echo "warning: view $date is not served; excluded from the published list" >&2
fi
done
dates=("${verified[@]}")
fi
# The portal must not depend on this mount namespace or on ZFS automounts, so the
# owner of the views publishes the verified list after mounting.
temporary=$(mktemp "$manifest.XXXXXX")
{
printf '{\n "updated": "%s",\n "snapshots": [' "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
separator=''
for date in "${dates[@]}"; do
if [ -f "$view/$date/core/os/x86_64/core.db" ]; then
printf '%s\n {"date": "%s", "url": "/archipelago/arch-repo-snapshot/%s/$repo/os/$arch"}' \
"$separator" "$date" "$date"
separator=','
else
echo "warning: view $date has no Core database" >&2
fi
done
printf '\n ]\n}\n'
} > "$temporary"
mv "$temporary" "$manifest"
chmod 0644 "$manifest"
echo "published $(grep -c '"date"' "$manifest") verified snapshot(s)"