1 #!/bin/sh2 # Prove the compose control: it starts one sibling mock engine, serves the3 # catalogue over the published port, keeps the token and state across a4 # restart, and has no plugin process in the control.5 #6 # This is a local proof, not a hosted service. It uses the repository's local7 # images and never pulls one. The default runtime root is the rootless Docker8 # user's `/run/user/<uid>/inf3r`; override the socket or root only when the9 # daemon is somewhere else.10 set -eu11 12 root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)13 compose=$root/docker/compose.yaml14 project=${INF3R_COMPOSE_PROJECT:-inf3r-compose-proof}15 port=${INF3R_PORT:-18080}16 uid=$(id -u)17 socket=${INF3R_DOCKER_SOCKET:-/run/user/$uid/docker.sock}18 run_host=${INF3R_RUN_HOST_DIR:-/run/user/$uid/inf3r}19 target=${CARGO_TARGET_DIR:-$root/target}20 case "$target" in21 /*) ;;22 *) target=$root/$target ;;23 esac24 lock=$target/images.lock25 bundle=${INF3R_BUNDLE_IMAGE:-inf3r/mock/x64-cpu:local}26 control=${INF3R_CONTROL_IMAGE:-inf3r:compose}27 export INF3R_CONTROL_IMAGE=$control28 work=$(mktemp -d "${TMPDIR:-/tmp}/inf3r-compose.XXXXXX")29 config=$work/config30 gate=/tmp/ifg-compose-gate-$$31 host_gate=/tmp/ifg-host-gate-$$32 compose_up=33 34 die() {35 printf 'compose-proof: %s\n' "$*" >&236 exit 137 }38 cleanup() {39 if [ -n "${compose_up:-}" ]; then40 INF3R_PORT=$port INF3R_CONFIG_DIR=$config \41 INF3R_DOCKER_SOCKET=$socket INF3R_RUN_HOST_DIR=$run_host \42 docker compose -p "$project" -f "$compose" down -v --remove-orphans >/dev/null 2>&1 || true43 fi44 rm -rf "$work" "$gate" "$host_gate"45 }46 trap cleanup EXIT INT TERM HUP47 48 command -v docker >/dev/null 2>&1 || die 'docker is not on PATH'49 docker compose version >/dev/null 2>&1 || die 'docker compose is not available'50 [ -S "$socket" ] || die "Docker socket $socket is absent"51 mkdir -p "$run_host"52 chmod 700 "$run_host"53 mkdir -p "$config"54 # Two manifest trees prove that a control can mount bundles beyond the baked55 # mock. The tree is manifest-only: no plugin binary or engine library.56 "$root/ops/control-manifests.sh" mock/x64-cpu toy/x64-cpu >/dev/null57 export INF3R_MANIFEST_DIR=$target/control-manifests58 # The deploy file names the runtime host root. Make the mount and the file use59 # the same path on a host whose uid is not 1000.60 sed -e "s#/run/user/1000/inf3r#$run_host#g" \61 -e "s#/run/user/1000#/run/user/$uid#g" \62 "$root/docker/config/deploy.toml" >"$config/deploy.toml"63 64 # The control image is built here; the engine image is the content-tagged image65 # from the image pipeline, with the local alias that the compose deploy names.66 docker image inspect "$bundle" >/dev/null 2>&1 ||67 "$root/ops/images.sh" build mock/x64-cpu68 docker image inspect "$bundle" >/dev/null 2>&1 ||69 die "the bundle image $bundle is absent"70 [ -f "$lock" ] || die "the image lock $lock is absent; build the bundle image first"71 export INF3R_IMAGES_LOCK_FILE=$lock72 [ -n "${INF3R_CONTROL_IMAGE:-}" ] ||73 docker compose -p "$project" -f "$compose" build >/dev/null74 75 export INF3R_PORT=$port INF3R_CONFIG_DIR=$config \76 INF3R_DOCKER_SOCKET=$socket INF3R_RUN_HOST_DIR=$run_host77 # The development entrypoint creates the first token. A release image has no78 # shell, so initialise the same state volume explicitly; the command is79 # idempotent and the entrypoint sees the existing token. `compose create`80 # makes the volume under Compose's ownership.81 docker compose -p "$project" -f "$compose" create inf3r >/dev/null82 docker run --rm --user 0:0 \83 -v "${project}_inf3r-state:/var/lib/inf3r" \84 -v "$config:/etc/inf3r:ro" -v "$run_host:/run/inf3r" \85 --entrypoint /usr/local/bin/inf3r-web "$control" \86 --init-token --deploy /etc/inf3r/deploy.toml87 docker compose -p "$project" -f "$compose" up -d --pull never >/dev/null88 compose_up=189 90 container=$(docker compose -p "$project" -f "$compose" ps -q inf3r)91 [ -n "$container" ] || die 'compose started no control container'92 for _ in $(seq 1 60); do93 status=$(docker inspect -f '{{.State.Health.Status}}' "$container" 2>/dev/null || true)94 [ "$status" = healthy ] && break95 [ "$status" = unhealthy ] && die 'the control became unhealthy'96 sleep 197 done98 [ "$status" = healthy ] || die "the control is $status, not healthy"99 100 state_volume=$(docker inspect -f '{{range .Mounts}}{{if eq .Destination "/var/lib/inf3r"}}{{.Name}}{{end}}{{end}}' "$container")101 [ -n "$state_volume" ] || die 'the control has no state volume'102 state_dir=$(docker volume inspect -f '{{.Mountpoint}}' "$state_volume")103 token=$(cat "$state_dir/token" 2>/dev/null) || die 'the control wrote no readable token'104 case $token in105 [0-9a-f][0-9a-f]*) ;;106 *) die 'the control wrote no token' ;;107 esac108 [ "${#token}" -eq 64 ] || die "the token is ${#token} characters, not 64"109 models=$(curl -fsS --max-time 10 -H "Authorization: Bearer $token" \110 "http://127.0.0.1:$port/models") || die 'the control did not answer /models'111 case $models in112 *'"name":"chat"'*) ;;113 *) die "the control did not serve the mock catalogue: $models" ;;114 esac115 for bundle in mock/x64-cpu toy/x64-cpu; do116 [ -f "$target/control-manifests/$bundle/bundle.toml" ] ||117 die "the mounted $bundle manifest is absent"118 [ ! -e "$target/control-manifests/$bundle/engine" ] ||119 die "the mounted $bundle manifest carries an engine tree"120 done121 printf 'control read two manifest-only bundles\n'122 123 # The engine is a sibling, not a process in the control. Exact process-name124 # matching is what distinguishes the plugin from the `docker run` CLI process125 # whose argv necessarily names the plugin.126 engine=$(docker ps -q --filter "label=inf3r.control=$run_host" \127 --filter label=inf3r.plugin=mock/x64-cpu)128 [ -n "$engine" ] || die 'compose started no sibling mock engine'129 if docker top "$container" -o comm= 2>/dev/null | grep -qx 'inf3r-mock-x64-cpu'; then130 die 'the control has a plugin process'131 fi132 printf 'control %s healthy; engine %s sibling; no plugin process\n' \133 "${container:0:12}" "${engine:0:12}"134 135 # The control must not have a binary, and neither container may mount a host136 # engine tree. The engine's only host mount is its private socket directory.137 probe=$(docker create "$control")138 if docker cp "$probe:/opt/inf3r/bin/inf3r-mock-x64-cpu" "$work/plugin" 2>/dev/null; then139 docker rm "$probe" >/dev/null140 die 'the control image contains a plugin binary'141 fi142 docker rm "$probe" >/dev/null143 for id in "$container" "$engine"; do144 if docker inspect -f '{{range .Mounts}}{{.Source}} {{.Destination}}{{"\n"}}{{end}}' "$id" |145 grep -Ev '/opt/inf3r/plugins-extra' |146 grep -Eq '(plugins|/models|/engine/|\.so[[:space:]])'; then147 die "container $id mounts a host plugin or engine path"148 fi149 done150 151 # The state volume and its token outlive a restart. The engine is replaced, so152 # this also proves the control can start a fresh sibling after one existed.153 before=$token154 docker compose -p "$project" -f "$compose" restart inf3r >/dev/null155 for _ in $(seq 1 60); do156 status=$(docker inspect -f '{{.State.Health.Status}}' "$container" 2>/dev/null || true)157 [ "$status" = healthy ] && break158 sleep 1159 done160 [ "$status" = healthy ] || die "the restarted control is $status, not healthy"161 after=$(cat "$state_dir/token" 2>/dev/null) ||162 die 'the restarted control lost its token'163 [ "$before" = "$after" ] || die 'the token changed across restart'164 [ -d "$state_dir/blobs" ] || die 'the state volume lost its blob directory'165 printf 'restart kept the token and the blob directory\n'166 167 # The container gate starts its own sibling from the content-tagged image and168 # checks the wire, the cold --describe, and the cache suite.169 cli=$target/debug/inf3r170 [ -x "$cli" ] || cli=$target/release/inf3r171 [ -x "$cli" ] || die "no inf3r cli at $target/{debug,release}/inf3r"172 tag=$("$root/ops/images.sh" tag mock/x64-cpu)173 rm -rf "$gate"174 "$cli" verify --container --deploy "$root/docker/config/gate.toml" \175 --image "$tag" --socket-dir "$gate" --budget 2m mock/x64-cpu >"$work/gate.log" 2>&1 ||176 { cat "$work/gate.log" >&2; die 'the mock container gate failed'; }177 for check in describe cache_cold_erase cache_roundtrip descriptor artifact crash restart; do178 grep -q "pass $check" "$work/gate.log" || die "the gate did not pass $check"179 done180 printf 'container gate pass (describe, cache, descriptor, artifact, crash, restart)\n'181 182 # The same dual-root deploy from the host: the deploy names a control root and183 # a host root, but a host-side gate uses the host root for its own listener and184 # mount. The control directory is intentionally different; it is not touched.185 rm -rf "$host_gate"186 mkdir -p "$host_gate"187 printf '%064d\n' 0 >"$host_gate/token"188 mkdir -p "$host_gate/blobs"189 cat >"$host_gate/deploy.toml" <<EOF190 [[node]]191 host = "gate"192 bin_dir = "/opt/inf3r/bin"193 bundle_dirs = ["$target/control-manifests"]194 blob_dir = "$host_gate/blobs"195 socket_dir = "$host_gate/control"196 socket_host_dir = "$host_gate/host"197 probe_image = "$control"198 web = { unix = "$host_gate/web.sock" }199 auth_file = "$host_gate/token"200 201 [[node.bind]]202 bundle = "mock/x64-cpu"203 runtime = "container"204 image = "$tag"205 command = ["/opt/inf3r/bin/inf3r-mock-x64-cpu"]206 models = ["mock"]207 devices = ["cpu:0"]208 EOF209 "$cli" verify --container --deploy "$host_gate/deploy.toml" \210 --image "$tag" --budget 2m mock/x64-cpu >"$work/host-gate.log" 2>&1 ||211 { cat "$work/host-gate.log" >&2; die 'the dual-root host gate failed'; }212 grep -q 'pass restart' "$work/host-gate.log" || die 'the dual-root host gate did not finish'213 printf 'dual-root gate from the host pass\n'214 215 # The same deploy from inside the control: the listener root is /run/inf3r and216 # the engine mount source is /run/user/<uid>/inf3r. The gate uses a private217 # child root so it never collides with the control's warm engine.218 docker compose -p "$project" -f "$compose" exec -T inf3r /usr/local/bin/inf3r verify \219 --container --deploy /etc/inf3r/deploy.toml --image "$tag" \220 --socket-dir /run/inf3r --budget 2m mock/x64-cpu >"$work/control-gate.log" 2>&1 ||221 { cat "$work/control-gate.log" >&2; die 'the dual-root control gate failed'; }222 grep -q 'pass restart' "$work/control-gate.log" || die 'the dual-root control gate did not finish'223 printf 'dual-root gate from the control pass\n'224 225 INF3R_PORT=$port INF3R_CONFIG_DIR=$config INF3R_DOCKER_SOCKET=$socket \226 INF3R_RUN_HOST_DIR=$run_host \227 docker compose -p "$project" -f "$compose" down -v --remove-orphans >/dev/null228 compose_up=229 left=$(docker ps -aq --filter "label=inf3r.control=$run_host" \230 --filter label=inf3r.managed=1 | tr -d ' \n')231 [ -z "$left" ] || die 'a managed engine container was left behind'232 printf 'compose-proof: ok\n'