#!/bin/sh
# deploy-bootstrap — deploy a built bootstrap tarball into the daemon's
# served bootstrap dir, and GC stale backups.
#
# WHY THIS EXISTS: the served bootstrap dir (<storage>/bootstrap, read by
# the daemon's bootstrap handler) accumulated 500M+ of manual
# `<tarball>.bak-<date>` copies because redeploys were done by hand
# (`cp ... .bak-<date>`) and nothing ever pruned them. The existing GC
# (scripts/keep-latest-only.js) only prunes the TREE STORE, not this dir.
#
# This scripts the redeploy AND self-GCs. Bootstrap tarballs are
# rebuildable (build-bootstrap-bare.js) and git-versioned, so dated
# backups are low value — we keep at most ONE rolling `.bak.prev` of the
# replaced file and prune every other `*.bak-*`.
#
# Usage:
# deploy-bootstrap.sh <tarball.tar.gz> [--dir <bootstrap-dir>] [--no-backup]
# deploy-bootstrap.sh --prune-only [--dir <bootstrap-dir>]
set -eu
DIR="${HOME}/pgv3-storage/bootstrap"
TARBALL=""
PRUNE_ONLY=0
KEEP_PREV=1
while [ $# -gt 0 ]; do
case "$1" in
--dir) DIR="$2"; shift 2 ;;
--prune-only) PRUNE_ONLY=1; shift ;;
--no-backup) KEEP_PREV=0; shift ;;
*.tar.gz) TARBALL="$1"; shift ;;
*) echo "deploy-bootstrap: unknown arg: $1" >&2; exit 2 ;;
esac
done
# Remove accumulated dated backups (and, when full-GC, the rolling prev).
# Reports MB freed. Safe on an empty match (the -e guard).
prune () {
full="$1"
freed_kb=0
set +e
for f in "$DIR"/*.bak-* $( [ "$full" = 1 ] && echo "$DIR"/*.bak.prev "$DIR"/*.sha256.bak.prev ); do
[ -e "$f" ] || continue
freed_kb=$((freed_kb + $(du -k "$f" | cut -f1)))
rm -f "$f" && echo " pruned $(basename "$f")"
done
set -e
echo " freed: $((freed_kb / 1024)) MB"
}
if [ "$PRUNE_ONLY" = 1 ]; then
echo "GC stale bootstrap backups in $DIR:"
prune 1
exit 0
fi
[ -n "$TARBALL" ] || { echo "usage: deploy-bootstrap.sh <tarball.tar.gz> [--dir DIR] [--no-backup]" >&2; exit 2; }
[ -f "$TARBALL" ] || { echo "deploy-bootstrap: tarball not found: $TARBALL" >&2; exit 1; }
name=$(basename "$TARBALL")
mkdir -p "$DIR"
# Roll the current live file to a single .bak.prev (overwriting any older
# prev), unless --no-backup. Then deploy the new tarball + sha256.
if [ "$KEEP_PREV" = 1 ] && [ -f "$DIR/$name" ]; then
mv -f "$DIR/$name" "$DIR/$name.bak.prev"
[ -f "$DIR/$name.sha256" ] && mv -f "$DIR/$name.sha256" "$DIR/$name.sha256.bak.prev" || true
fi
cp -f "$TARBALL" "$DIR/$name"
( cd "$DIR" && sha256sum "$name" > "$name.sha256" )
echo "deployed $name -> $DIR ($(du -h "$DIR/$name" | cut -f1))"
# GC every dated backup left from the old manual-cp era (keep only .prev).
prune 0
echo "done."