interplane.io
Source of interplane.io: a Rust generator that renders the page from the protocol repository's public record, deployed hourly from the Spark.
Brought by drakestapleton
interplane.io / scripts / sync-to-hub.sh
| 1 | #!/bin/sh |
| 2 | # scripts/sync-to-hub.sh: publish ONE approved commit of interplane.io, and hopbot.org when that |
| 3 | # commit carries a Hop release. Runs on the Spark, which is also where hopbot.org is served. |
| 4 | # |
| 5 | # Usage: sh scripts/sync-to-hub.sh [--log-dir DIR] [COMMIT] |
| 6 | # COMMIT full 40-digit id (lower-case hex) of the commit to publish. It must be the signed |
| 7 | # main of the site project in the store. Without it, that signed main is published |
| 8 | # (this is how the service starts the script); either way step 1 decides. |
| 9 | # --log-dir DIR where the log, receipts, pending markers and the hopbot stage live. The LOGDIR |
| 10 | # setting does the same. Without either, the default folder is used and is written |
| 11 | # only when a transfer starts (see "The log" below). |
| 12 | # |
| 13 | # Order of work. Every refusal happens before a single file is transferred. |
| 14 | # 1. Signed head. Runs `IPL verify STORE interplane.io --expect-owner IPL_EXPECT_OWNER`. The |
| 15 | # project's signed chain must verify and its current owner must be the pinned owner (the |
| 16 | # fingerprint in IPL_EXPECT_OWNER, which must also agree with the owner-pins file ipl reads). |
| 17 | # IPL_EXPECT_OWNER unset or empty is a refusal: there is no default owner. verify must answer |
| 18 | # one line, `OK interplane.io ID main HEAD VISIBILITY OWNER SEQ CHAINHEAD`, naming that owner. |
| 19 | # HEAD is the store's refs/heads/main, which moves only through `ipl land` signed by the owner |
| 20 | # key. COMMIT must equal HEAD; without COMMIT, HEAD is the commit. |
| 21 | # 2. Source. The store's own repository for the project (STORE/projects/interplane.io/repo.git), |
| 22 | # read in place and never written: it must hold COMMIT. No clone, fetch or mirror is used. |
| 23 | # 3. Build. Exports exactly that commit (git archive) into a scratch folder and builds there with |
| 24 | # the pinned compiler, then runs `site verify`. Nothing is built in the store. |
| 25 | # 4. Test data. Refuses output that carries the fixture marker: a TEST-DATA file, a manifest line |
| 26 | # naming it, or the banner attribute data-test-data in any HTML file. |
| 27 | # 5. Destinations. Refuses a Hop release when the hopbot.org folder is missing on this machine. |
| 28 | # 6. Transfer, one destination at a time, each with its own success receipt. |
| 29 | # |
| 30 | # Destinations and receipts (LOGDIR): |
| 31 | # interplane.io rsync over ssh to HUB:WEBDIR. infra/spark/edge-mirror.sh in the forge web |
| 32 | # repository then pulls that folder to the Spark front door. |
| 33 | # receipt.interplane.io, pending.interplane.io |
| 34 | # hopbot.org HOPWEBDIR on this machine, root-owned. The output is staged in |
| 35 | # LOGDIR/hopbot.stage and copied by ONE command run as root: |
| 36 | # sudo -n /usr/bin/rsync -rt --delete --chmod=D755,F644 --chown=root:root \ |
| 37 | # --exclude=.well-known LOGDIR/hopbot.stage/ HOPWEBDIR/ |
| 38 | # -rt copies regular files and folders only: links and special files are skipped. |
| 39 | # .well-known is left alone, as for interplane.io: it belongs to the web server. |
| 40 | # receipt.hopbot.org, pending.hopbot.org |
| 41 | # A receipt holds state, destination, content digest, source commit and time. A destination is |
| 42 | # skipped only when its receipt names the same destination, digest and commit and no pending |
| 43 | # marker is left. So a newly named or changed destination is transferred even when the content is |
| 44 | # the same. The old folder of a changed destination is left as it was; the log says so. |
| 45 | # Withdrawal: when the commit carries no Hop release but receipt.hopbot.org says published, the |
| 46 | # same root command runs with an empty stage. That empties HOPWEBDIR (but for .well-known), the web server goes back to |
| 47 | # redirecting, and the receipt becomes a withdrawal record. |
| 48 | # |
| 49 | # A sudoers rule narrow enough for the root step (not installed by this script). USER is the |
| 50 | # account that runs it and LOGDIR is written out in full; in sudoers the characters , : = inside |
| 51 | # arguments take a backslash: |
| 52 | # USER ALL=(root) NOPASSWD: /usr/bin/rsync -rt --delete --chmod\=D755\,F644 --chown\=root\:root --exclude\=.well-known LOGDIR/hopbot.stage/ /var/www/hopbot.org/ |
| 53 | # |
| 54 | # Settings: |
| 55 | # STORE signed Interplane store (default ~/interplane-store-owner) |
| 56 | # IPL the ipl tool that verifies it (default ~/work/hive/agent-door/interplane-native/ipl) |
| 57 | # IPL_EXPECT_OWNER owner key fingerprint, SHA256:... Required; no default. |
| 58 | # HUB ssh host for interplane.io (default hub) |
| 59 | # WEBDIR document root for interplane.io on HUB (default /var/www/interplane.io) |
| 60 | # HOPWEBDIR document root for hopbot.org on this machine (default /var/www/hopbot.org) |
| 61 | # LOGDIR log, receipts, pending markers, hopbot stage (default ~/.local/share/interplane-site) |
| 62 | # |
| 63 | # The log. A run that names its folder (--log-dir or LOGDIR) logs everything there, refusals |
| 64 | # included. A run that names none uses the default folder, which is the live one, and must not |
| 65 | # leave a mark in it unless it publishes: every refusal, the quiet-flag skip and the "unchanged" |
| 66 | # answer are printed and written nowhere, and the folder is first written when a transfer is about |
| 67 | # to start. So a trial run that is refused cannot add a line to the live log. The service that |
| 68 | # starts this script should name its folder if it wants its refusals on record. |
| 69 | # The build happens in a scratch folder under TMPDIR, never in the log folder. |
| 70 | # OMC pinned Omega compiler for AArch64 Linux (default $OMEGA_SRC/seed/out/omc) |
| 71 | # OMEGA_SRC local Omega checkout (default ~/workspace/omega-mv) |
| 72 | # |
| 73 | # How it is started: there is no schedule. The Spark user service aien-run-interplane-sync runs |
| 74 | # aien-onchange on the project's refs/heads folder in the store and starts this script, with no |
| 75 | # commit, when that folder changes (and at least once a day). Each start publishes the verified |
| 76 | # signed main, or finds it unchanged. A person can also run it by hand. |
| 77 | # |
| 78 | # Other rules: never run while the Spark quiet flag is held (chip runs own the machine then). |
| 79 | # No facts refresh and no last-good fallback: content/facts.json is part of the commit. |
| 80 | # Exit: 0 done or nothing to do, 2 usage, 3 compile, 4 build or verify, 5 signed head or |
| 81 | # source, 6 test data, 7 destination, 8 digest or transfer. |
| 82 | set -eu |
| 83 | |
| 84 | PROJECT=interplane.io |
| 85 | |
| 86 | is_commit() { |
| 87 | case "$1" in ''|*[!0-9a-f]*) return 1 ;; esac |
| 88 | [ "${#1}" -eq 40 ] |
| 89 | } |
| 90 | usage() { |
| 91 | echo 'usage: sync-to-hub.sh [--log-dir DIR] [COMMIT] (the full 40-digit id of the signed main; default: the signed main)' >&2 |
| 92 | exit 2 |
| 93 | } |
| 94 | LOGARG= |
| 95 | if [ "${1:-}" = --log-dir ]; then |
| 96 | [ "$#" -ge 2 ] && [ -n "$2" ] || usage |
| 97 | LOGARG=$2 |
| 98 | shift 2 |
| 99 | fi |
| 100 | case $# in |
| 101 | 0) COMMIT= ;; |
| 102 | 1) is_commit "$1" || usage; COMMIT=$1 ;; |
| 103 | *) usage ;; |
| 104 | esac |
| 105 | |
| 106 | STORE=${STORE:-"$HOME/interplane-store-owner"} |
| 107 | IPL=${IPL:-"$HOME/work/hive/agent-door/interplane-native/ipl"} |
| 108 | OWNER=${IPL_EXPECT_OWNER:-} |
| 109 | SRC="$STORE/projects/$PROJECT/repo.git" |
| 110 | HUB=${HUB:-hub} |
| 111 | WEBDIR=${WEBDIR:-/var/www/interplane.io} |
| 112 | HOPWEBDIR=${HOPWEBDIR-/var/www/hopbot.org} |
| 113 | NAMED=1 |
| 114 | if [ -n "$LOGARG" ]; then |
| 115 | LOGDIR=$LOGARG |
| 116 | elif [ -z "${LOGDIR:-}" ]; then |
| 117 | LOGDIR="$HOME/.local/share/interplane-site" |
| 118 | NAMED=0 |
| 119 | fi |
| 120 | LOG="$LOGDIR/sync.log" |
| 121 | WORK=$(mktemp -d "${TMPDIR:-/tmp}/interplane-site.XXXXXXXX") |
| 122 | trap 'rm -rf "$WORK"' 0 |
| 123 | trap 'exit 1' 1 2 15 |
| 124 | # Until the log is open, lines and command output are held in the scratch folder. |
| 125 | DETAIL="$WORK/detail.log" |
| 126 | : > "$DETAIL" |
| 127 | LOGOPEN=0 |
| 128 | open_log() { |
| 129 | [ "$LOGOPEN" -eq 0 ] || return 0 |
| 130 | mkdir -p "$LOGDIR" |
| 131 | cat "$DETAIL" >> "$LOG" |
| 132 | DETAIL=$LOG |
| 133 | LOGOPEN=1 |
| 134 | } |
| 135 | stamp() { date -u +%Y-%m-%dT%H:%M:%SZ; } |
| 136 | say() { printf '%s %s\n' "$(stamp)" "$*" | tee -a "$DETAIL"; } |
| 137 | refuse() { |
| 138 | code=$1; shift |
| 139 | say "refuse: $*" |
| 140 | if [ "$LOGOPEN" -eq 0 ]; then |
| 141 | echo 'not logged: no log folder was named (--log-dir or LOGDIR), so the default log is left untouched' >&2 |
| 142 | tail -n 20 "$DETAIL" >&2 |
| 143 | fi |
| 144 | exit "$code" |
| 145 | } |
| 146 | [ "$NAMED" -eq 0 ] || open_log |
| 147 | |
| 148 | if command -v quietlock >/dev/null 2>&1 && ! quietlock check >/dev/null 2>&1; then |
| 149 | say "skip: Spark quiet flag held" |
| 150 | exit 0 |
| 151 | fi |
| 152 | |
| 153 | # 1. The signed head, from the store, verified for the pinned owner. |
| 154 | [ -n "$OWNER" ] || refuse 5 "no owner is pinned for the publisher: set IPL_EXPECT_OWNER to the owner key fingerprint" |
| 155 | VERIFIED=$("$IPL" verify "$STORE" "$PROJECT" --expect-owner "$OWNER" 2>>"$DETAIL") || |
| 156 | refuse 5 "$PROJECT does not verify for owner $OWNER in $STORE" |
| 157 | set -f |
| 158 | # shellcheck disable=SC2086 # split the OK line into its fields |
| 159 | set -- $VERIFIED |
| 160 | set +f |
| 161 | if [ "$#" -ne 9 ] || [ "$1" != OK ] || [ "$2" != "$PROJECT" ] || [ "$4" != main ] || ! is_commit "$5" || [ "$7" != "$OWNER" ]; then |
| 162 | refuse 5 "ipl verify gave no single OK line for $PROJECT main owned by $OWNER" |
| 163 | fi |
| 164 | if [ -z "$COMMIT" ]; then |
| 165 | COMMIT=$5 |
| 166 | say "commit: the signed main of $PROJECT, $COMMIT" |
| 167 | elif [ "$COMMIT" != "$5" ]; then |
| 168 | refuse 5 "$COMMIT is not the signed main of $PROJECT; the store says $5" |
| 169 | fi |
| 170 | |
| 171 | # 2. The store's repository must hold that commit. It is read in place, never written. |
| 172 | git --git-dir="$SRC" cat-file -e "$COMMIT^{commit}" 2>>"$DETAIL" || refuse 5 "the store repository $SRC cannot supply $COMMIT" |
| 173 | |
| 174 | # 3. Build exactly that commit, away from any checkout. |
| 175 | mkdir "$WORK/src" |
| 176 | git --git-dir="$SRC" archive --format=tar -o "$WORK/src.tar" "$COMMIT" >>"$DETAIL" 2>&1 || refuse 5 "could not export $COMMIT" |
| 177 | tar -xf "$WORK/src.tar" -C "$WORK/src" >>"$DETAIL" 2>&1 || refuse 5 "could not unpack $COMMIT" |
| 178 | cd "$WORK/src" |
| 179 | BIN="$WORK/site-omega" |
| 180 | if ! sh scripts/build-native.sh "$BIN" >>"$DETAIL" 2>&1; then |
| 181 | refuse 3 "pinned native compilation failed for $COMMIT; see $DETAIL" |
| 182 | fi |
| 183 | "$BIN" build "$WORK/dist" "$WORK/dist-hopbot" >>"$DETAIL" 2>&1 || refuse 4 "native build failed for $COMMIT" |
| 184 | "$BIN" verify "$WORK/dist" "$WORK/dist-hopbot" >>"$DETAIL" 2>&1 || refuse 4 "verify failed for $COMMIT" |
| 185 | cd "$WORK" |
| 186 | HOP=0 |
| 187 | [ ! -d "$WORK/dist-hopbot" ] || HOP=1 |
| 188 | |
| 189 | # 4. Fixture output is never published. |
| 190 | for d in dist dist-hopbot; do |
| 191 | [ -d "$WORK/$d" ] || continue |
| 192 | [ ! -e "$WORK/$d/TEST-DATA" ] || refuse 6 "$d carries the TEST-DATA marker file; fixture output is never published" |
| 193 | if [ -f "$WORK/$d/manifest.sha256" ] && grep -q 'TEST-DATA' "$WORK/$d/manifest.sha256"; then |
| 194 | refuse 6 "the manifest of $d names TEST-DATA; fixture output is never published" |
| 195 | fi |
| 196 | find "$WORK/$d" -type f -name '*.html' -exec grep -l 'data-test-data' {} + > "$WORK/banner" 2>>"$DETAIL" || : |
| 197 | [ ! -s "$WORK/banner" ] || refuse 6 "a page in $d carries the TEST DATA banner; fixture output is never published" |
| 198 | done |
| 199 | |
| 200 | # Include every emitted file and path, so CSS, SVG and download changes publish too. |
| 201 | # Keep each pipeline stage separate: a failed hash command must never become a receipt. |
| 202 | tree_digest() { |
| 203 | (cd "$WORK/$1" && find . -type f -print0) > "$WORK/$1.files" || return 1 |
| 204 | LC_ALL=C sort -z "$WORK/$1.files" > "$WORK/$1.sorted" || return 1 |
| 205 | (cd "$WORK/$1" && xargs -0 sha256sum < "$WORK/$1.sorted") > "$WORK/$1.list" || return 1 |
| 206 | [ -s "$WORK/$1.list" ] || return 1 |
| 207 | D=$(sha256sum "$WORK/$1.list") || return 1 |
| 208 | D=${D%% *} |
| 209 | is_sha256 "$D" |
| 210 | } |
| 211 | is_sha256() { |
| 212 | case "$1" in ''|*[!0-9a-f]*) return 1 ;; esac |
| 213 | [ "${#1}" -eq 64 ] |
| 214 | } |
| 215 | tree_digest dist || refuse 8 "could not take the digest of the interplane.io output" |
| 216 | MAIN_DIGEST=$D |
| 217 | HOP_DIGEST=none |
| 218 | if [ "$HOP" -eq 1 ]; then |
| 219 | tree_digest dist-hopbot || refuse 8 "could not take the digest of the hopbot.org output" |
| 220 | HOP_DIGEST=$D |
| 221 | fi |
| 222 | |
| 223 | # 5. Destinations are settled before anything moves. |
| 224 | MAIN_DEST="$HUB:$WEBDIR" |
| 225 | HOP_DEST="local:$HOPWEBDIR" |
| 226 | field() { sed -n "s/^$2 //p" "$1" 2>/dev/null | head -n 1; } |
| 227 | HOP_RECEIPT="$LOGDIR/receipt.hopbot.org" |
| 228 | HOP_PENDING="$LOGDIR/pending.hopbot.org" |
| 229 | WITHDRAW=0 |
| 230 | if [ "$HOP" -eq 0 ]; then |
| 231 | if [ "$(field "$HOP_RECEIPT" state)" = published ] || [ -e "$HOP_PENDING" ]; then WITHDRAW=1; fi |
| 232 | fi |
| 233 | if [ "$HOP" -eq 1 ]; then |
| 234 | if [ -z "$HOPWEBDIR" ] || [ ! -d "$HOPWEBDIR" ]; then |
| 235 | refuse 7 "this commit carries a Hop release and the hopbot.org folder is missing here: ${HOPWEBDIR:-not named}" |
| 236 | fi |
| 237 | fi |
| 238 | if [ "$WITHDRAW" -eq 1 ]; then |
| 239 | WAS=$(field "$HOP_RECEIPT" destination) |
| 240 | if [ -n "$WAS" ] && [ "$WAS" != "$HOP_DEST" ]; then |
| 241 | refuse 7 "the Hop release was published to $WAS; name that folder in HOPWEBDIR to withdraw it" |
| 242 | fi |
| 243 | if [ -z "$HOPWEBDIR" ] || [ ! -d "$HOPWEBDIR" ]; then |
| 244 | refuse 7 "the Hop release is withdrawn and the hopbot.org folder is missing here: ${HOPWEBDIR:-not named}" |
| 245 | fi |
| 246 | fi |
| 247 | |
| 248 | # 6. Transfer. A pending marker outlives failure or interruption: a partial transfer may have |
| 249 | # changed the destination even when the next run brings the previous digest back. |
| 250 | receipt_is() { |
| 251 | # receipt_is FILE STATE DESTINATION DIGEST: same state, destination, digest and commit |
| 252 | [ -f "$1" ] || return 1 |
| 253 | [ "$(field "$1" state)" = "$2" ] && [ "$(field "$1" destination)" = "$3" ] && |
| 254 | [ "$(field "$1" digest)" = "$4" ] && [ "$(field "$1" commit)" = "$COMMIT" ] |
| 255 | } |
| 256 | write_receipt() { |
| 257 | printf 'state %s\ndestination %s\ndigest %s\ncommit %s\nutc %s\n' "$2" "$3" "$4" "$COMMIT" "$(stamp)" > "$1.new" |
| 258 | mv "$1.new" "$1" |
| 259 | } |
| 260 | moved_from() { |
| 261 | WAS=$(field "$1" destination) |
| 262 | if [ -n "$WAS" ] && [ "$WAS" != "$2" ]; then |
| 263 | say "destination changed from $WAS to $2; the old folder is left as it was" |
| 264 | fi |
| 265 | } |
| 266 | hop_root_copy() { |
| 267 | sudo -n /usr/bin/rsync -rt --delete --chmod=D755,F644 --chown=root:root --exclude=.well-known "$LOGDIR/hopbot.stage/" "$HOPWEBDIR/" >>"$DETAIL" 2>&1 |
| 268 | } |
| 269 | |
| 270 | MAIN_RECEIPT="$LOGDIR/receipt.interplane.io" |
| 271 | MAIN_PENDING="$LOGDIR/pending.interplane.io" |
| 272 | if receipt_is "$MAIN_RECEIPT" published "$MAIN_DEST" "$MAIN_DIGEST" && [ ! -e "$MAIN_PENDING" ]; then |
| 273 | say "unchanged: interplane.io at $MAIN_DEST already holds $COMMIT" |
| 274 | else |
| 275 | open_log |
| 276 | moved_from "$MAIN_RECEIPT" "$MAIN_DEST" |
| 277 | printf '%s %s %s\n' "$MAIN_DEST" "$MAIN_DIGEST" "$COMMIT" > "$MAIN_PENDING" |
| 278 | rsync -a --delete --exclude '.well-known' "$WORK/dist/" "$HUB:$WEBDIR/" >>"$DETAIL" 2>&1 || refuse 8 "transfer to $MAIN_DEST failed; the pending marker stays" |
| 279 | write_receipt "$MAIN_RECEIPT" published "$MAIN_DEST" "$MAIN_DIGEST" |
| 280 | rm "$MAIN_PENDING" |
| 281 | say "published interplane.io $COMMIT to $MAIN_DEST" |
| 282 | fi |
| 283 | |
| 284 | if [ "$HOP" -eq 1 ]; then |
| 285 | if receipt_is "$HOP_RECEIPT" published "$HOP_DEST" "$HOP_DIGEST" && [ ! -e "$HOP_PENDING" ]; then |
| 286 | say "unchanged: hopbot.org at $HOP_DEST already holds $COMMIT" |
| 287 | else |
| 288 | open_log |
| 289 | moved_from "$HOP_RECEIPT" "$HOP_DEST" |
| 290 | printf '%s %s %s\n' "$HOP_DEST" "$HOP_DIGEST" "$COMMIT" > "$HOP_PENDING" |
| 291 | rm -rf "$LOGDIR/hopbot.stage" |
| 292 | mkdir "$LOGDIR/hopbot.stage" |
| 293 | cp -R "$WORK/dist-hopbot/." "$LOGDIR/hopbot.stage/" |
| 294 | hop_root_copy || refuse 8 "transfer to $HOP_DEST failed; the pending marker stays" |
| 295 | write_receipt "$HOP_RECEIPT" published "$HOP_DEST" "$HOP_DIGEST" |
| 296 | rm "$HOP_PENDING" |
| 297 | say "published hopbot.org $COMMIT to $HOP_DEST" |
| 298 | fi |
| 299 | elif [ "$WITHDRAW" -eq 1 ]; then |
| 300 | open_log |
| 301 | printf '%s %s %s\n' "$HOP_DEST" withdrawn "$COMMIT" > "$HOP_PENDING" |
| 302 | rm -rf "$LOGDIR/hopbot.stage" |
| 303 | mkdir "$LOGDIR/hopbot.stage" |
| 304 | hop_root_copy || refuse 8 "emptying $HOP_DEST failed; the pending marker stays" |
| 305 | write_receipt "$HOP_RECEIPT" withdrawn "$HOP_DEST" none |
| 306 | rm "$HOP_PENDING" |
| 307 | say "withdrew hopbot.org: $COMMIT carries no Hop release, $HOP_DEST is now empty" |
| 308 | fi |