peppa/scripts/runner-image.sh

101 lines
5.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# PEP-17/40/267: the runner image (infra/runner) with the agent CLIs pinned in infra/agent-versions.json.
#
# scripts/runner-image.sh — build peppa/runner:cli-<versions>, smoke-test it, and only then
# make it the working image ($PEPPA_RUNNER_IMAGE, default
# peppa/runner:latest); the old one becomes peppa/runner:previous
# scripts/runner-image.sh --push <host…> — the same, then load it on runner hosts over SSH
# scripts/runner-image.sh --rollback — one command back: the working image ↔ the previous one
# scripts/runner-image.sh --try <cli> <ver> — a new version of one CLI (claude|codex|opencode): build and
# smoke-test a candidate; on success write the version into
# infra/agent-versions.json and both Dockerfiles (then a PR).
# The working image is not touched.
# scripts/runner-image.sh --smoke [image] — only the smoke test of an image
#
# Smoke test: every CLI answers `--version` with exactly the pinned version; peppa-hook and peppa-mcp are there.
# A failed smoke test leaves the working image as it was.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
DOCKER="${PEPPA_DOCKER_BIN:-docker}"
IMAGE="${PEPPA_RUNNER_IMAGE:-peppa/runner:latest}"
REPO="${IMAGE%:*}"
PREVIOUS="$REPO:previous"
VERSIONS="${PEPPA_AGENT_VERSIONS_FILE:-$ROOT/infra/agent-versions.json}"
die() { echo "runner-image: $*" >&2; exit 1; }
ver() { node -e 'const v=JSON.parse(require("fs").readFileSync(process.argv[1],"utf8"))[process.argv[2]]; if(!v) process.exit(1); process.stdout.write(v)' "$VERSIONS" "$1" || die "no version of $1 in $VERSIONS"; }
arg_of() { case "$1" in claude) echo CLAUDE_CODE_VERSION;; codex) echo CODEX_VERSION;; opencode) echo OPENCODE_VERSION;; *) die "unknown CLI: $1 (claude|codex|opencode)";; esac; }
# build <tag> <claude> <codex> <opencode>
build() {
"$DOCKER" build -t "$1" \
--build-arg CLAUDE_CODE_VERSION="$2" --build-arg CODEX_VERSION="$3" --build-arg OPENCODE_VERSION="$4" \
"$ROOT/infra/runner"
}
# smoke <image> <claude> <codex> <opencode>
smoke() {
local image=$1 ok=1 got
for pair in "claude:$2" "codex:$3" "opencode:$4"; do
local cli=${pair%%:*} want=${pair#*:}
got=$("$DOCKER" run --rm --entrypoint "$cli" "$image" --version 2>&1 | head -3) || got="(failed) $got"
if grep -Eq "(^|[^0-9.])${want//./\\.}([^0-9.]|$)" <<<"$got"; then echo "smoke: $cli $want ok"
else echo "smoke: $cli wants $want, got: ${got//$'\n'/ }" >&2; ok=0; fi
done
for b in peppa-hook peppa-mcp; do
if "$DOCKER" run --rm --entrypoint sh "$image" -c "command -v $b" >/dev/null 2>&1; then echo "smoke: $b ok"
else echo "smoke: $b missing" >&2; ok=0; fi
done
[ "$ok" = 1 ]
}
pinned() { echo "$(ver claude) $(ver codex) $(ver opencode)"; }
cand_tag() { echo "$REPO:cli-$(echo "$*" | tr ' ' '\n' | paste -sd_ - | tr -c 'A-Za-z0-9_.\n-' '-')"; }
case "${1:-}" in
--rollback)
"$DOCKER" image inspect "$PREVIOUS" >/dev/null 2>&1 || die "no previous image ($PREVIOUS) — nothing to roll back to"
"$DOCKER" tag "$IMAGE" "$REPO:rollback-tmp"
"$DOCKER" tag "$PREVIOUS" "$IMAGE"
"$DOCKER" tag "$REPO:rollback-tmp" "$PREVIOUS"
"$DOCKER" rmi "$REPO:rollback-tmp" >/dev/null
echo "rolled back: $IMAGE is the previous image again (the replaced one is $PREVIOUS; run --rollback again to undo)"
exit 0 ;;
--smoke)
# shellcheck disable=SC2046
smoke "${2:-$IMAGE}" $(pinned) || die "smoke test failed"
exit 0 ;;
--try)
cli=${2:-}; new=${3:-}
[ -n "$cli" ] && [ -n "$new" ] || die "usage: --try <claude|codex|opencode> <version>"
arg=$(arg_of "$cli")
read -r c x o <<<"$(pinned)"
case "$cli" in claude) c=$new;; codex) x=$new;; opencode) o=$new;; esac
tag=$(cand_tag "$c" "$x" "$o")
build "$tag" "$c" "$x" "$o"
smoke "$tag" "$c" "$x" "$o" || die "smoke test failed — nothing changed (candidate $tag)"
node -e 'const f=process.argv[1],v=JSON.parse(require("fs").readFileSync(f,"utf8"));v[process.argv[2]]=process.argv[3];require("fs").writeFileSync(f,JSON.stringify(v,null,2)+"\n")' "$VERSIONS" "$cli" "$new"
for f in "$ROOT/infra/runner/Dockerfile" "$ROOT/infra/platform/Dockerfile"; do
sed -i.bak -E "s/^ARG $arg=.*/ARG $arg=$new/" "$f" && rm -f "$f.bak"
done
echo "$cli $new passed the smoke test; pinned in infra/agent-versions.json and the Dockerfiles — open a PR."
echo "After the merge: scripts/runner-image.sh (agents' image) and the platform deploy (service CLIs)."
exit 0 ;;
esac
read -r c x o <<<"$(pinned)"
tag=$(cand_tag "$c" "$x" "$o")
build "$tag" "$c" "$x" "$o"
smoke "$tag" "$c" "$x" "$o" || die "smoke test failed — the working image $IMAGE is unchanged (candidate $tag)"
if "$DOCKER" image inspect "$IMAGE" >/dev/null 2>&1; then
if [ "$("$DOCKER" image inspect -f '{{.Id}}' "$IMAGE")" != "$("$DOCKER" image inspect -f '{{.Id}}' "$tag")" ]; then
"$DOCKER" tag "$IMAGE" "$PREVIOUS"
fi
fi
"$DOCKER" tag "$tag" "$IMAGE"
echo "built $IMAGE (claude $c, codex $x, opencode $o); previous: $PREVIOUS; back — scripts/runner-image.sh --rollback"
if [[ "${1:-}" == "--push" ]]; then
shift
for h in "$@"; do "$ROOT/packages/cli/bin/peppa" host push-image "$h"; done
fi