wrolpi/release/inat-build.sh
lrnselfreliance 04ddc7c63c
Species: a monthly build of the packs, signed where the key is (#1036)
* Species: a monthly build of the packs, signed where the key is

The build host builds the packs unsigned (inat-build.sh, a systemd timer), and the machine with the signing key
signs the hashes and the manifest from manifest.json alone (inat-publish.sh), so the packs never leave the build host
and the key never goes to it. build_inat_regions.py --no-sign now still writes each pack's sha256, and --sign signs
such a build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Species: sign only a well-formed build manifest, written again where the key is

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Species: inat-publish.sh passes the bucket to the upload, so a try stays out of the live packs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Species: the monthly build publishes nothing older than what is published

The version is the observations' publication date; a build of the same (or older) observations would replace the
published packs with an older version, and the prune would delete the newer ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 15:05:22 -07:00

55 lines
2.6 KiB
Bash
Executable file

#!/bin/bash
# Build the iNaturalist species packs, unsigned, for inat-publish.sh to sign and publish.
#
# Run monthly by wrolpi-inat-build.timer on the build host. The build (wrolpi/scripts/build_inat_regions.py) runs in a
# one-off container of the api image, so it has the same Python and dependencies as WROLPi. It downloads ~30 GB of
# observations and needs ~50 GB more of work space, kept in $INAT_DIR/work between builds (the Wikidata cache saves
# hours).
#
# The signing key is not on the build host. The finished build is left in $INAT_DIR/out with a READY file holding its
# version; inat-publish.sh, run where the key is, signs the hashes and the manifest, and uploads them with the packs.
#
# Arguments are passed to build_inat_regions.py (e.g. `--regions antarctica --cdn-base URL`, to try a build).
#
# Optional env vars:
# INAT_DIR where the work and output live (default ~/inat-build)
# INAT_NOTIFY a command given one message when the build finishes or fails (e.g. a Pushover script)
# INAT_MANIFEST the published manifest; nothing is built unless the observations are newer than its version
# (default the Spaces origin's, which the CDN does not cache)
set -euo pipefail
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
REPO_DIR=$(dirname "${SCRIPT_DIR}")
INAT_DIR=${INAT_DIR:-${HOME}/inat-build}
INAT_NOTIFY=${INAT_NOTIFY:-}
INAT_MANIFEST=${INAT_MANIFEST:-https://wrolpi.nyc3.digitaloceanspaces.com/maps/inat/manifest.json}
notify() {
echo "$1"
if [ -n "${INAT_NOTIFY}" ]; then
"${INAT_NOTIFY}" "$1" || echo "Notify failed"
fi
}
trap 'notify "The species pack build failed; see journalctl -u wrolpi-inat-build"' ERR
mkdir -p "${INAT_DIR}/work"
# A newer build replaces one that was never published.
rm -rf "${INAT_DIR}/out"
mkdir -p "${INAT_DIR}/out"
published=$(curl -fsS "${INAT_MANIFEST}" | python3 -c 'import json, sys; print(json.load(sys.stdin)["version"])')
echo "Published version: ${published}"
cd "${REPO_DIR}"
docker compose run --rm --no-deps -T -v "${INAT_DIR}:/inat" --entrypoint nice api \
-n 10 python3 wrolpi/scripts/build_inat_regions.py /inat/work /inat/out --no-sign --newer-than "${published}" "$@"
if [ ! -f "${INAT_DIR}/out/manifest.json" ]; then
notify "No species packs were built: iNaturalist has published no observations newer than ${published}"
exit 0
fi
version=$(python3 -c 'import json, sys; print(json.load(open(sys.argv[1]))["version"])' "${INAT_DIR}/out/manifest.json")
echo "${version}" > "${INAT_DIR}/out/READY"
notify "Species packs ${version} are built, and wait to be signed and published (inat-publish.sh)"