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

socialweb.computer/socialweb-computer-kcp

socialweb.computer: a compute market on atproto (kcp)

1 collaborator
0
0
55 lines · 2.7 KB Raw History
#!/usr/bin/env bash
# Install the current socialweb-computer of a release channel (stable or rc) into ${PREFIX:-$HOME/.local}/bin.
# The channel record (software.release.channel socialweb-computer:<channel> of the releases' account) pins the version
# record by CID, that pins the manifest by CID, and the manifest names each binary's URL and its raw CID (sha2-256):
# every record is checked against its pinned CID and the download against the artifact's CID before it is installed,
# the way a bidder's self-update does (internal/releasefollow). Needs curl, jq, sha256sum, base32, od.
#   scripts/install.sh [stable|rc]
set -euo pipefail
channel="${1:-stable}"
repo="${RELEASE_REPO:-did:plc:gmlkrhimoueejcertevr4hl3}"
case "$channel" in stable | rc) ;; *) echo "usage: $0 [stable|rc]" >&2; exit 2 ;; esac
case "$(uname -s)-$(uname -m)" in
  Linux-x86_64) name=socialweb-computer-linux-amd64 ;;
  Linux-aarch64) name=socialweb-computer-linux-arm64 ;;
  *) echo "install.sh: no release build for $(uname -s) $(uname -m)" >&2; exit 1 ;;
esac
pds="$(curl -fsSL "https://plc.directory/$repo" | jq -r '.service[] | select(.id == "#atproto_pds") | .serviceEndpoint')"

record() {
  local rec
  rec="$(curl -fsSL "$pds/xrpc/com.atproto.repo.getRecord?repo=$repo&collection=${1%%/*}&rkey=${1#*/}")"
  if [ -n "${2:-}" ] && [ "$(jq -r .cid <<<"$rec")" != "$2" ]; then
    echo "install.sh: $1 is $(jq -r .cid <<<"$rec"), not the pinned $2" >&2
    exit 1
  fi
  jq .value <<<"$rec"
}

strongref() { jq -r "$1 | \"\\(.uri | sub(\"^at://[^/]*/\"; \"\"))\\t\\(.cid)\""; }

chan="$(record "software.release.channel/socialweb-computer:$channel")"
IFS=$'\t' read -r vref vcid <<<"$(strongref .version <<<"$chan")"
ver="$(record "$vref" "$vcid")"
version="$(jq -r .version <<<"$ver")"
IFS=$'\t' read -r mref mcid <<<"$(strongref .manifest <<<"$ver")"
man="$(record "$mref" "$mcid")"
IFS=$'\t' read -r url acid <<<"$(jq -r --arg n "$name" '.artifacts[] | select(.name == $n) | "\(.url)\t\(.cid)"' <<<"$man")"
[ -n "$url" ] && [ -n "$acid" ] || { echo "install.sh: $channel $version has no $name" >&2; exit 1; }

b32="$(tr 'a-z' 'A-Z' <<<"${acid#b}")"
while [ $(( ${#b32} % 8 )) -ne 0 ]; do b32="$b32="; done
bytes="$(base32 -d <<<"$b32" | od -An -tx1 -v | tr -d ' \n')"
[ "${bytes:0:8}" = 01551220 ] || { echo "install.sh: $acid is not a raw sha2-256 CID" >&2; exit 1; }
sum="${bytes:8}"

bin="${PREFIX:-$HOME/.local}/bin"
mkdir -p "$bin"
part="$bin/.socialweb-computer.$$"
trap 'rm -f "$part"' EXIT
curl -fsSL -o "$part" "$url"
echo "$sum  $part" | sha256sum -c --quiet
chmod 0755 "$part"
mv "$part" "$bin/socialweb-computer"
echo "installed $channel $version ($sum) to $bin/socialweb-computer"
"$bin/socialweb-computer" version | sed -n 1p