#!/usr/bin/env bash # Explicit remote-runner opt-in: invokes real models using the dedicated CLI login. # 此脚本会编译测试并实际调用模型;只用于明确授权的专用远程 runner,普通检查不自动运行。 set -euo pipefail umask 077 # 在仓库根目录调用,参数一为已构建 Core/worker 二进制;CLI 安装和专用登录由 runner 运维预置。 # 下方开关必须显式列出已验证图片输出能力的后端名,避免把图片输入能力误当作图片生成能力。 binary=$(realpath "$1") cli=${FOUNDRY_REAL_CODEX_INSTALL:-/opt/endoleap-infra/codex-cli} login=${FOUNDRY_REAL_CODEX_HOME:-/opt/endoleap-infra/foundry/codex/home} : "${FOUNDRY_CANVAS_IMAGE_MODELS:?Set only backend model names verified for image output}" # 检查准备条件,不会自动下载 CLI、创建登录或替换生产服务;真实生成可能消耗模型额度。 test -x "$binary" test -s "$login/auth.json" test -s "$login/models_cache.json" # 随机命名 worker/数据库和临时 socket 目录,退出时一并清理,不复用生产队列。 suffix=$(openssl rand -hex 8) database=foundry-real-db-$suffix worker=foundry-real-worker-$suffix scratch=$(mktemp -d /tmp/foundry-real.XXXXXXXX) cleanup(){ docker rm -f "$worker" "$database" >/dev/null 2>&1 || true; rm -rf "$scratch"; } trap cleanup EXIT install -d -m 700 "$scratch/run" chown 1000:1000 "$scratch/run" # 临时数据库不开放端口,trust 只用于本次隔离命名空间;应用角色/迁移随后按真实迁移器安装。 docker run -d --name "$database" --network none -e POSTGRES_HOST_AUTH_METHOD=trust postgres:17-alpine >/dev/null for attempt in {1..30}; do if docker exec "$database" pg_isready -h 127.0.0.1 -U postgres >/dev/null 2>&1; then break; fi; sleep 1; done # 先安装早期认证基线,创建测试角色,再由迁移器补齐其余 schema 与授权。 docker exec -i "$database" psql -X -U postgres -v ON_ERROR_STOP=1 < db/001-auth.sql docker exec "$database" psql -X -U postgres -v ON_ERROR_STOP=1 -c 'CREATE ROLE foundry_fixture LOGIN' python3 deploy/migrate.py --container "$database" --database postgres --role foundry_fixture # Only the CLI container sees the login. Core/tests receive the private socket, # never the login, runtime configuration or Docker socket. # 只有 CLI 容器只读读取专用登录和模型缓存,测试容器仅挂 socket,不能访问登录或 Docker socket。 # worker 的 bridge 网络用于访问远端模型,数据库仍隔离;CLI 登录只给 worker。 # 根文件系统只读,临时 CODEX_HOME 和任务目录走 tmpfs;脚本退出会删除测试容器与共享 socket 目录。 docker run -d --name "$worker" --init --network bridge --user 1000:1000 \ --cap-drop ALL --security-opt no-new-privileges --read-only --pids-limit 128 --memory 1g \ --tmpfs /tmp:rw,nosuid,nodev,size=128m --tmpfs /work:ro,nosuid,nodev \ --tmpfs /home/node/.codex:rw,nosuid,nodev,size=32m,uid=1000,gid=1000 --workdir /work \ -e "FOUNDRY_CANVAS_IMAGE_MODELS=$FOUNDRY_CANVAS_IMAGE_MODELS" \ --mount "type=bind,source=$binary,target=/opt/foundry,readonly" \ --mount "type=bind,source=$cli,target=/opt/codex,readonly" \ --mount "type=bind,source=$login/auth.json,target=/home/node/.codex/auth.json,readonly" \ --mount "type=bind,source=$login/models_cache.json,target=/home/node/.codex/models_cache.json,readonly" \ --mount "type=bind,source=$scratch/run,target=/run/foundry-codex" \ --mount type=bind,source=/etc/ssl/certs/ca-certificates.crt,target=/etc/ssl/certs/ca-certificates.crt,readonly \ node:24-bookworm-slim /opt/foundry codex-worker >/dev/null for attempt in {1..30}; do if [[ -S "$scratch/run/worker.sock" ]]; then break; fi; sleep 1; done test -S "$scratch/run/worker.sock" # 先等私有 socket 出现,再在 runner 编译并仅运行真实 worker 专项用例;与普通单元测试开关分离。 CGO_ENABLED=0 go test -mod=readonly -c -o bin/platform-real-tests ./internal/platform # 测试可执行文件以普通 uid 运行,只共享数据库网络和 worker socket;没有 Docker socket 和 CLI 登录。 # -test.run 仅选真实 worker 专项,不能把本脚本的成功当作已运行所有平台测试。 docker run --rm --network "container:$database" --user 1000:1000 \ --read-only --cap-drop ALL --security-opt no-new-privileges \ --tmpfs /tmp:rw,nosuid,nodev,noexec,size=16m,mode=1777 \ --mount "type=bind,source=$(pwd)/bin/platform-real-tests,target=/opt/integration-tests,readonly" \ --mount "type=bind,source=$scratch/run,target=/run/canvas-real,readonly" \ -e PLATFORM_TEST_DATABASE_URL=postgres://foundry_fixture@127.0.0.1:5432/postgres?sslmode=disable \ -e FOUNDRY_REAL_CODEX_SOCKET=/run/canvas-real/worker.sock \ --entrypoint /opt/integration-tests postgres:17-alpine -test.v -test.run '^TestCanvasAIRealWorker$' -test.timeout 6m