wrolpi/release/inat-publish.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

67 lines
3.5 KiB
Bash
Executable file

#!/bin/bash
# Sign and publish the species packs inat-build.sh built on the build host.
#
# Run daily (e.g. by launchd) on the machine with the WROLPi signing key; it does nothing until a build is READY.
# Only manifest.json comes here. Its hashes are signed (a meta4 per pack), and the manifest is written again from what
# was checked and signed. Those go back to the build host, which uploads everything (inat-upload.sh). Then the CDN's
# cached manifest is flushed, and the older versions are deleted.
#
# Required env vars:
# INAT_HOST ssh destination of the build host
# INAT_CDN_ID the DigitalOcean CDN endpoint id (for `doctl compute cdn flush`)
# Optional env vars:
# INAT_REMOTE_REPO the WROLPi checkout on the build host (default projects/wrolpi, from its home)
# INAT_REMOTE_DIR inat-build.sh's INAT_DIR on the build host (default inat-build, from its home)
# INAT_CDN_PATH the manifest's directory on the CDN (default maps/inat)
# INAT_CDN_BASE the packs' URL base, which the signed manifest must use (default build_inat_regions.py's)
# INAT_BUCKET inat-upload.sh's INAT_BUCKET, given to it on the build host (to try a build elsewhere)
# INAT_NOTIFY a command, on the build host, given one message when publishing finishes or fails
# PYTHON a Python with WROLPi's requirements (default the checkout's .venv)
set -euo pipefail
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
REPO_DIR=$(dirname "${SCRIPT_DIR}")
INAT_HOST=${INAT_HOST:?Set INAT_HOST to the build host}
INAT_CDN_ID=${INAT_CDN_ID:?Set INAT_CDN_ID to the CDN endpoint id}
INAT_REMOTE_REPO=${INAT_REMOTE_REPO:-projects/wrolpi}
INAT_REMOTE_DIR=${INAT_REMOTE_DIR:-inat-build}
INAT_CDN_PATH=${INAT_CDN_PATH:-maps/inat}
INAT_NOTIFY=${INAT_NOTIFY:-}
INAT_CDN_BASE=${INAT_CDN_BASE:-}
INAT_BUCKET=${INAT_BUCKET:-}
PYTHON=${PYTHON:-${REPO_DIR}/.venv/bin/python}
notify() {
echo "$1"
if [ -n "${INAT_NOTIFY}" ]; then
ssh "${INAT_HOST}" "${INAT_NOTIFY}" "$(printf '%q' "$1")" || echo "Notify failed"
fi
}
version=$(ssh "${INAT_HOST}" "cat ${INAT_REMOTE_DIR}/out/READY 2>/dev/null" || true)
if [ -z "${version}" ]; then
echo "No species build is waiting to be published"
exit 0
fi
trap 'notify "Publishing species packs ${version} failed; see inat-publish.sh log"' ERR
echo "Publishing species packs ${version}"
work=$(mktemp -d)
trap 'rm -rf "${work}"' EXIT
scp -q "${INAT_HOST}:${INAT_REMOTE_DIR}/out/manifest.json" "${work}/"
# The build host is not trusted with the key: only a well-formed manifest is signed, and it is written again here.
"${PYTHON}" "${REPO_DIR}/wrolpi/scripts/build_inat_regions.py" --sign "${work}" ${INAT_CDN_BASE:+--cdn-base "${INAT_CDN_BASE}"}
signed=$("${PYTHON}" -c 'import json, sys; print(json.load(open(sys.argv[1]))["version"])' "${work}/manifest.json")
if [ "${signed}" != "${version}" ]; then
echo "The build host's READY (${version}) is not its manifest's version (${signed})" >&2
exit 1
fi
scp -q "${work}/${version}/"*.meta4 "${INAT_HOST}:${INAT_REMOTE_DIR}/out/${version}/"
scp -q "${work}/manifest.json" "${work}/manifest.json.sig" "${INAT_HOST}:${INAT_REMOTE_DIR}/out/"
remote_upload="INAT_DIR=${INAT_REMOTE_DIR} ${INAT_BUCKET:+INAT_BUCKET=${INAT_BUCKET} }${INAT_REMOTE_REPO}/release/inat-upload.sh"
ssh "${INAT_HOST}" "${remote_upload} upload ${version}"
doctl compute cdn flush "${INAT_CDN_ID}" --files "${INAT_CDN_PATH}/manifest.json,${INAT_CDN_PATH}/manifest.json.sig"
ssh "${INAT_HOST}" "${remote_upload} prune ${version}"
notify "Species packs ${version} are published"