#!/usr/bin/env bash# THE ARTIFACT STORE'S CLIENT — ask before you build, give what you built.## scripts/ci/artifact-store.sh get <kind> <key> <dest-dir> exit 0 on a hit, 1 otherwise# scripts/ci/artifact-store.sh put <kind> <key> <src-dir> exit 0 whether or not it landed# scripts/ci/artifact-store.sh where prints the store's URL, or nothing## The store is a server on the Linux CI box, reached over the tailnet. This# file is the whole of what a Mac lane does with it, and it is written to be# SAFE TO BE WRONG:## FAIL-OPEN, ALWAYS. No store configured, a store that does not answer, a# body that does not unpack — every one of those is a MISS (`get` exits 1)# or a shrug (`put` exits 0 with a sentence), never a failed job. The store# is a cache; a cache is never worth failing a build over. The only non-zero# exit that means "you did it wrong" is 2, for a bad argument.## NEVER HALF AN ARTIFACT. `get` unpacks into `<dest>.partial.$$` and renames# it into place, so a killed run leaves nothing under a name the next run# trusts; `put` tars into a temporary file and hands the store a whole body# with a Content-Length, and the store writes beside and renames too.## BOUNDED WAITS. Five seconds to connect, ten minutes for a transfer — a# tailnet that is slow today costs a build a few seconds of asking and then# the build it would have done anyway.## NEVER AN ARTIFACT IT CANNOT VOUCH FOR. The store records a sha256 for# every artifact and answers it in `X-Content-SHA256`; `get` recomputes the# digest of what it downloaded and refuses — a MISS, nothing unpacked — when# the two differ or the store sent none. `put` declares the digest of what it# sends, so the store can refuse a body that arrived changed, and a 409 (the# store holds a DIFFERENT artifact under this key) is printed as a warning:# it means the key did not cover every input, which is the one thing the# cache must never be quiet about.## Configuration is two variables: `COTTON_ARTIFACT_STORE` (the URL; unset means# the store is off and every `get` is a miss) and `COTTON_ARTIFACT_STORE_TOKEN`# (the bearer token the store requires; without it the store answers 401 and# that is a miss too).## A MAC GIVEN THE TOKEN USES THE STORE. A job sets both variables; a lane# session on a Mac sets neither. The token is the vault's secret# COTTON_ARTIFACT_STORE_TOKEN, placed on each Mac at# `~/.cotton-credentials/artifact-store/token` — and that placement is what# switches the store on for every lane there: the address and the token are read from# their placed files (artifact-store/url, artifact-store/token). So a framework one Mac built is# the next Mac's download, outside CI as well as in it. `COTTON_ARTIFACT_STORE`# set (empty included, which is off) wins over the default; a Mac without the# placement stays off, as before.## Guard: the artifact-store test.set -uo pipefailverb="${1-}"case"$verb"in get|put) [ $# -eq 4 ] || { echo "usage: artifact-store.sh $verb <kind> <key> <dir>" >&2; exit2; } ;; where) [ $# -eq 1 ] || { echo "usage: artifact-store.sh where" >&2; exit2; } ;; *) echo "usage: artifact-store.sh <get|put> <kind> <key> <dir> | where" >&2; exit2 ;;esac# The placed token and address, read through the credentials helper where the# checkout has it (a checkout of the public source does not: no store there).placed_token="" placed_url=""CREDENTIALS_LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib/credentials.sh"if [ -r "$CREDENTIALS_LIB" ]; then# shellcheck source=/dev/null . "$CREDENTIALS_LIB" placed_token="$(cotton_credential artifact-store/token)"# The store's address is placed beside its token (artifact-store/url), never# written here: an address on the private network is not for a public tree. placed_url="$(cotton_credential artifact-store/url)"fiif [ -z "${COTTON_ARTIFACT_STORE+set}" ] && [ -r "$placed_token" ] && [ -r "$placed_url" ]; then store="$(tr -d '[:space:]' < "$placed_url")"else store="${COTTON_ARTIFACT_STORE:-}"fistore="${store%/}"if [ -z "${COTTON_ARTIFACT_STORE_TOKEN:-}" ] && [ -r "$placed_token" ]; then COTTON_ARTIFACT_STORE_TOKEN="$(cat "$placed_token")"fiif [ "$verb" = where ]; then [ -n "$store" ] && printf '%s\n'"$store"exit0fikind="$2" key="$3" dir="$4"# A hit REPLACES <dir>, so `get` asks first that it is not a home folder or# anything above one.if [ "$verb" = get ]; then# shellcheck source=/dev/null . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib/owned-root.sh" refuse_wide_root "the directory a hit replaces (get's <dir>)""$dir"ficase"$kind"in''|*[!a-z0-9-]*) echo "artifact-store: '$kind' is not a kind ([a-z0-9-])" >&2; exit2 ;;esaccase"$key"in''|*[!A-Za-z0-9._-]*) echo "artifact-store: '$key' is not a key ([A-Za-z0-9._-])" >&2; exit2 ;;esacif [ -z "$store" ]; thenif [ "$verb" = get ]; then echo "artifact-store: off (COTTON_ARTIFACT_STORE is not set, and no token and address are placed${placed_token:+ at $placed_token and $placed_url}) — miss $kind/$key"exit1fi echo "artifact-store: off (COTTON_ARTIFACT_STORE is not set, and no token and address are placed${placed_token:+ at $placed_token and $placed_url}) — $kind/$key not stored"exit0fiauth=()if [ -n "${COTTON_ARTIFACT_STORE_TOKEN:-}" ]; then auth=(-H "Authorization: Bearer $COTTON_ARTIFACT_STORE_TOKEN")fi# macOS ships no `sha256sum`; `shasum -a 256` prints the same first field.sha256_of() {if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1"; else shasum -a 256"$1"; fi | cut -d' ' -f1 | tr 'A-F''a-f'}# What a refused request means, for the sentence: a 404 is the ordinary miss,# a 401/403 is a token problem, anything else is the store being away.refusal() {if grep -q " 404""$1"2>/dev/null; then echo misselif grep -qE " 40[13]""$1"2>/dev/null; then echo tokenelse echo awayfi}url="$store/$kind/$key"# `--connect-timeout` bounds the ask; `--max-time` bounds the transfer. `-f`# turns a 404 into a non-zero exit with no body written; `-sS` keeps curl quiet# except for the sentence when it fails.curl_common=(curl -fsS --connect-timeout 5 --max-time 600"${auth[@]+"${auth[@]}"}")tmp="$(mktemp -d)"trap'rm -rf "$tmp"' EXITif [ "$verb" = get ]; thenif ! "${curl_common[@]}" -D "$tmp/headers" -o "$tmp/artifact.tar.gz""$url"2>"$tmp/err"; then# Every refusal is a miss here, and the sentence says which kind.case"$(refusal "$tmp/err")"in miss) echo "artifact-store: miss $kind/$key" ;; token) echo "artifact-store: $store refused $kind/$key ($(tr -d '\n' <"$tmp/err")) — COTTON_ARTIFACT_STORE_TOKEN is missing or wrong; building locally" ;; *) echo "artifact-store: $store did not answer for $kind/$key ($(tr -d '\n' <"$tmp/err")) — building locally" ;;esacexit1fi# THE DIGEST, BEFORE A BYTE IS UNPACKED. The store recorded a sha256 when the# artifact landed and answers it in a header; what came down the wire must# hash to exactly that, or it is not the artifact — whatever put it there. recorded="$(tr -d '\r' <"$tmp/headers" | awk 'tolower($1) == "x-content-sha256:" { print tolower($2) }' | tail -1)" actual="$(sha256_of "$tmp/artifact.tar.gz")"if [ -z "$recorded" ]; then echo "artifact-store: $store sent $kind/$key with no recorded digest — refusing an artifact it cannot vouch for; building locally"exit1fiif [ "$recorded" != "$actual" ]; then echo "artifact-store: $kind/$key came back with sha256 $actual, the store recorded $recorded — refusing it; building locally"exit1fi partial="$dir.partial.$$" rm -rf "$partial" mkdir -p "$partial"if ! tar -xzf "$tmp/artifact.tar.gz" -C "$partial"2>"$tmp/err"; then rm -rf "$partial" echo "artifact-store: $kind/$key came back but did not unpack ($(tr -d '\n' <"$tmp/err")) — building locally"exit1fi rm -rf "$dir" mkdir -p "$(dirname "$dir")" mv "$partial""$dir" echo "artifact-store: hit $kind/$key ($(du -sh "$dir" | cut -f1), sha256 $actual verified) from $store"exit0fi# putif [ ! -d "$dir" ]; then echo "artifact-store: nothing to put — $dir is not a directory" >&2exit2fi# `gzip -n`, not `tar -z`: tar's gzip stamps the time it ran into the header,# so the same bytes packed a second apart had two digests and the store# answered 409 — "the cache key did not cover every input" — to an identical# artifact (run 5482).if ! { tar -cf - -C "$dir" . | gzip -n >"$tmp/artifact.tar.gz"; } 2>"$tmp/err"; then echo "artifact-store: could not pack $dir ($(tr -d '\n' <"$tmp/err")) — not stored, the build itself is fine"exit0fidigest="$(sha256_of "$tmp/artifact.tar.gz")"if answer="$("${curl_common[@]}" -X PUT -H "Content-Type: application/octet-stream" -H "X-Content-SHA256: $digest" -T "$tmp/artifact.tar.gz" "$url" 2>"$tmp/err")"; then echo "artifact-store: $answer"elsecase"$(refusal "$tmp/err")"in token) echo "artifact-store: $store refused $kind/$key ($(tr -d '\n' <"$tmp/err")) — COTTON_ARTIFACT_STORE_TOKEN is missing or wrong; the build itself is fine" ;; *)if grep -q " 409""$tmp/err"2>/dev/null; then# Not a cache miss: the store holds a different artifact under the same# key. The key is supposed to cover every input that changes the bytes. echo "::warning::artifact-store: $store already holds $kind/$key with a DIFFERENT digest than this build's ($digest) — the cache key did not cover every input; read the store's log and scripts/build-xcframework.sh's key"else echo "artifact-store: $store did not take $kind/$key ($(tr -d '\n' <"$tmp/err")) — the build itself is fine"fi ;;esacfiexit0