~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
97 lines · 4.5 KB Raw History
#!/usr/bin/env bash
# Move zds and zat CI from Tangled's hosted spindle to heavypad, give zds its
# Fly deploy token there, and report the hosted spindle's nixery memory cap.
# --dry-run does every read and auth check, then stops before the first write.
set -euo pipefail

dry_run=0
[ "${1:-}" = --dry-run ] && dry_run=1

here=$(cd "$(dirname "$0")" && pwd)
heavypad="did:web:spindle.zzstoatzz.io"
heavypad_url="https://spindle.zzstoatzz.io"
zds="did:plc:6atzzmsifkfmfqmeqrl6jwzo"
zat="did:plc:wkf7mpx6vtimg54hcdl7suww"
owner="did:plc:mkqt76xvfgxuemlwlx6ruc3w"
knot="https://knot1.tangled.sh"

ok() { echo "ok   $*"; }
fail() { echo "FAIL $*"; exit 1; }
claims() { # jwt -> its payload as JSON
	local part
	part=$(printf %s "$1" | cut -d. -f2 | tr '_-' '/+')
	while [ $((${#part} % 4)) -ne 0 ]; do part="$part="; done
	printf %s "$part" | base64 -d
}

# 0. sessions: `tg auth token` hands back the stored access token without
# refreshing it, so a status call refreshes each account first
tg --account zat.dev auth status >/dev/null || fail "tg session for zat.dev"
tg --account zzstoatzz.io auth status >/dev/null || fail "tg session for zzstoatzz.io"
session=$(tg --account zat.dev auth token)
[ "$(claims "$session" | jq -r .sub)" = "$owner" ] || fail "tg's zat.dev token is not for $owner"
ok "zat.dev session"

service_auth() { # audience, method -> a service-auth JWT from zat.dev's PDS
	local token
	token=$(curl -fsS -H "authorization: Bearer $session" \
		"https://pds.zat.dev/xrpc/com.atproto.server.getServiceAuth?aud=$1&lxm=$2" | jq -r .token)
	[ "$(claims "$token" | jq -r '.iss + " " + .aud + " " + .lxm')" = "$owner $1 $2" ] || fail "service auth for $2 at $1"
	printf %s "$token"
}

# 1. each repo's manifest, pointed at heavypad and guarded by its current CID
records=()
for repo in "$zds" "$zat"; do
	current=$(curl -fsS "$knot/xrpc/com.atproto.repo.getRecord?repo=$repo&collection=org.tangled.repo.manifest&rkey=self")
	record=$(printf %s "$current" | jq -c --arg repo "$repo" --arg spindle "$heavypad" \
		'{repo: $repo, collection: "org.tangled.repo.manifest", rkey: "self", swapRecord: .cid, record: (.value + {spindle: $spindle})}')
	service_auth "$repo" com.atproto.repo.putRecord >/dev/null
	ok "manifest for $(printf %s "$current" | jq -r .value.declaration.name): $(printf %s "$current" | jq -r .value.spindle) -> $heavypad"
	records+=("$record")
done

# 2. heavypad takes zat.dev's auth (checked on zat.dev/stream, already there;
# zds only appears once its manifest names heavypad), and fly can mint for zds-pds
heavypad_secrets() { # repo -> its secret keys on heavypad
	curl -fsS -H "authorization: Bearer $(service_auth "$heavypad" org.tangled.secret.listSecrets)" \
		"$heavypad_url/xrpc/org.tangled.secret.listSecrets?repo=$1" | jq -c '[.secrets[].key]'
}
ok "heavypad auth: zat.dev/stream secrets $(heavypad_secrets did:plc:3esqa6ltzsdck6qhsvyusldu)"
fly status -a zds-pds >/dev/null || fail "fly access to zds-pds"
ok "fly access to zds-pds"

if [ "$dry_run" = 1 ]; then
	ok "dry run: stopping before writes"
	exit 0
fi

for record in "${records[@]}"; do
	repo=$(printf %s "$record" | jq -r .repo)
	status=$(printf %s "$record" | curl -sS -o /dev/null -w '%{http_code}' \
		-H "authorization: Bearer $(service_auth "$repo" com.atproto.repo.putRecord)" \
		-H 'content-type: application/json' --data-binary @- "$knot/xrpc/com.atproto.repo.putRecord")
	[ "$status" = 200 ] || fail "spindle for $repo -> heavypad (HTTP $status)"
	ok "spindle for $repo -> heavypad"
done

for _ in $(seq 1 24); do
	heavypad_secrets "$zds" >/dev/null 2>&1 && break
	sleep 5
done
heavypad_secrets "$zds" >/dev/null 2>&1 || fail "heavypad has not picked up zds after 2 minutes"
ok "heavypad knows zds"

# the token is never printed
token=$(fly tokens create deploy -a zds-pds --name zds-ci-heavypad | grep '^FlyV1 ')
status=$(jq -n --arg value "$token" --arg repo "$zds" '{repo: $repo, key: "FLY_API_TOKEN", value: $value}' |
	curl -sS -o /dev/null -w '%{http_code}' -H "authorization: Bearer $(service_auth "$heavypad" org.tangled.secret.addSecret)" \
		-H 'content-type: application/json' --data-binary @- "$heavypad_url/xrpc/org.tangled.secret.addSecret")
unset token
[ "$status" = 200 ] || fail "FLY_API_TOKEN for zds on heavypad (HTTP $status)"
ok "FLY_API_TOKEN for zds on heavypad"

# 3. report the hosted spindle's nixery memory cap to tangled
tg --account zzstoatzz.io issue create "hosted spindle refuses every nixery workflow: 6 GiB default exceeds the 4 GiB repo cap" \
	-R tangled.org/core -F "$here/tangled-nixery-issue.md"
ok "tangled issue filed"