#!/usr/bin/env bash # Build, smoke-test and publish the vivarium container image. # # scripts/docker.sh build build for this machine's platform, load into docker # scripts/docker.sh test build (PLATFORM, default native), boot it, probe it # scripts/docker.sh publish build linux/amd64 + linux/arm64, push a manifest list # # Environment (all optional): # IMAGE repository (default atcr.io/pardy.at/vivarium) # VERSION version label/tag (default packages/core version) # TAGS tags to publish (default "$VERSION latest") # PLATFORMS publish platforms (default linux/amd64,linux/arm64) # PLATFORM test/build platform (default: docker's native platform) # ATCR_USER registry username — your atproto handle (default pardy.at) # ATCR_APP_PASSWORD atproto app password; when set, publish logs in first # (otherwise it relies on an existing `docker login` / # the docker-credential-atcr helper) # # Needs docker with the buildx plugin. Cross-arch builds need no QEMU: see Dockerfile. set -euo pipefail cd "$(dirname "$0")/.." IMAGE="${IMAGE:-atcr.io/pardy.at/vivarium}" VERSION="${VERSION:-$(sed -n 's/^ "version": "\(.*\)",$/\1/p' packages/core/package.json)}" TAGS="${TAGS:-$VERSION latest}" PLATFORMS="${PLATFORMS:-linux/amd64,linux/arm64}" REVISION="$(git rev-parse HEAD 2>/dev/null || echo unknown)" REGISTRY="${IMAGE%%/*}" BUILDER=vivarium-multiarch log() { printf '\033[1m==> %s\033[0m\n' "$*" >&2; } die() { printf 'error: %s\n' "$*" >&2; exit 1; } command -v docker >/dev/null || die "docker not found" docker buildx version >/dev/null 2>&1 || die "docker buildx plugin not found" build_args=(--build-arg "VERSION=$VERSION" --build-arg "REVISION=$REVISION") # Build one platform and load it into the local image store as $IMAGE:dev. build_local() { local platform_args=() [[ -n "${PLATFORM:-}" ]] && platform_args=(--platform "$PLATFORM") log "building $IMAGE:dev ${PLATFORM:-(native)}" docker buildx build "${platform_args[@]}" "${build_args[@]}" --load -t "$IMAGE:dev" . } cmd_build() { build_local; } cmd_test() { build_local local platform_args=() name="vivarium-smoke-$$" [[ -n "${PLATFORM:-}" ]] && platform_args=(--platform "$PLATFORM") # Sealed, and persisting to /data on an anonymous volume: proves the # unprivileged user can write the mount point. log "starting $name" docker run -d --rm --name "$name" "${platform_args[@]}" \ -p 127.0.0.1::2580 -v /data "$IMAGE:dev" --no-upstream --data /data >/dev/null trap 'docker rm -f "$name" >/dev/null 2>&1 || true' EXIT local port port="$(docker port "$name" 2580/tcp | head -n1 | sed 's/.*://')" local base="http://127.0.0.1:$port" log "waiting for $base" for _ in $(seq 1 60); do curl -fsS "$base/xrpc/localhost.vivarium.getInfo" >/dev/null 2>&1 && break docker inspect "$name" >/dev/null 2>&1 || { docker logs "$name" 2>&1 || true; die "container exited"; } sleep 0.5 done local info did info="$(curl -fsS "$base/xrpc/localhost.vivarium.getInfo")" || { docker logs "$name"; die "getInfo failed"; } grep -q '"pds"' <<<"$info" || die "getInfo has no pds service: $info" curl -fsS "$base/xrpc/com.atproto.server.describeServer" >/dev/null || die "describeServer failed" did="$(curl -fsS -X POST "$base/xrpc/com.atproto.server.createAccount" \ -H 'content-type: application/json' -d '{"handle":"smoke.vivarium.test"}' | sed -n 's/.*"did":"\([^"]*\)".*/\1/p')" [[ "$did" == did:plc:* ]] || die "createAccount returned no did" curl -fsS "$base/xrpc/com.atproto.identity.resolveHandle?handle=smoke.vivarium.test" | grep -q "$did" || die "resolveHandle did not return $did" log "ok: $IMAGE:dev ${PLATFORM:-(native)} serves getInfo, describeServer, createAccount, resolveHandle" } cmd_publish() { if [[ -n "${ATCR_APP_PASSWORD:-}" ]]; then log "logging in to $REGISTRY as ${ATCR_USER:-pardy.at}" printf '%s' "$ATCR_APP_PASSWORD" | docker login "$REGISTRY" -u "${ATCR_USER:-pardy.at}" --password-stdin fi # The default `docker` buildx driver can't push multi-platform manifest lists; # a docker-container builder can. docker buildx inspect "$BUILDER" >/dev/null 2>&1 || docker buildx create --name "$BUILDER" --driver docker-container >/dev/null local tag_args=() for t in $TAGS; do tag_args+=(-t "$IMAGE:$t"); done log "publishing $IMAGE [$TAGS] for $PLATFORMS" docker buildx build --builder "$BUILDER" --platform "$PLATFORMS" \ "${build_args[@]}" "${tag_args[@]}" --push . docker buildx imagetools inspect "$IMAGE:${TAGS%% *}" } case "${1:-}" in build) cmd_build ;; test) cmd_test ;; publish) cmd_publish ;; *) sed -n '2,20p' "$0" | sed 's/^# \{0,1\}//'; exit 2 ;; esac