Public: anyone can read this; the crew can change it.
The game agent asks every running server: docker compose exec no longer takes the list of the loop #17611
Merged 22h 37m ago, live in production
sold/agent-stdin → main 0 commits
3 files
changed +66
−10
src/games/agent.sh +11 −10
| @@ -360,7 +360,8 @@ logs() { | ||
| 360 | 360 | NORCON=" " |
| 361 | 361 | metrics() { |
| 362 | 362 | local out='{}' name list tps mspt |
| 363 | while IFS= read -r name; do | |
| 363 | # Every list is read from fd 3: `docker compose exec` takes all of stdin, the rest of the list with it. | |
| 364 | while IFS= read -r name <&3; do | |
| 364 | 365 | [[ "" =~ ]] || continue |
| 365 | 366 | case "" in *" "*) continue ;; esac |
| 366 | 367 | # `list uuids`: each name with its UUID, so the forge can show who it is (games/people.ts). |
| @@ -371,7 +372,7 @@ metrics() { | ||
| 371 | 372 | tps="$(capped 5 "" exec -T "" rcon-cli -- tps 2>/dev/null || true)" |
| 372 | 373 | mspt="$(capped 5 "" exec -T "" rcon-cli -- mspt 2>/dev/null || true)" |
| 373 | 374 | out="$(jq -c --arg n "" --arg l "" --arg t "" --arg m "" '. + {(): {list: , tps: , mspt: }}' <<<"")" |
| 374 | done < <(capped 5 "" ps --status running --format '{{.Service}}' 2>/dev/null) | |
| 375 | done 3< <(capped 5 "" ps --status running --format '{{.Service}}' 2>/dev/null) | |
| 375 | 376 | echo "" |
| 376 | 377 | } |
| 377 | 378 | |
| @@ -396,7 +397,7 @@ people() { | ||
| 396 | 397 | local out='{}' name raw part via="" glist |
| 397 | 398 | if ! people_due; then cat "/.people.json" 2>/dev/null || echo '{}'; return; fi |
| 398 | 399 | rm -f "/.people.due" |
| 399 | while IFS= read -r name; do | |
| 400 | while IFS= read -r name <&3; do | |
| 400 | 401 | [[ "" =~ ]] || continue |
| 401 | 402 | case "" in *" "*) continue ;; esac |
| 402 | 403 | raw="$(capped 5 "" exec -T "" sh -c 'for f in whitelist.json ops.json banned-players.json; do echo "=== "; cat "" 2>/dev/null; echo; done; echo "=== props"; grep -E "^(white-list|enforce-whitelist)=" server.properties 2>/dev/null' 2>/dev/null)" || continue |
| @@ -408,7 +409,7 @@ people() { | ||
| 408 | 409 | bans: [file("banned-players.json")[] | {name: (.name // ""), uuid: (.uuid // ""), reason: ((.reason // "") | .[0:200]), created: (.created // "")}][0:500], |
| 409 | 410 | whitelistOn: ( | test("(?m)^white-list=true"))}' 2>/dev/null)" || continue |
| 410 | 411 | out="$(jq -c --arg n "" --argjson p "" '. + {(): }' <<<"")" |
| 411 | done < <(capped 5 "" ps --status running --format '{{.Service}}' 2>/dev/null) | |
| 412 | done 3< <(capped 5 "" ps --status running --format '{{.Service}}' 2>/dev/null) | |
| 412 | 413 | glist="" |
| 413 | 414 | [ -n "" ] && glist="$(proxy_rcon "" glist all 2>/dev/null || true)" |
| 414 | 415 | jq -c --arg g "" '{servers: ., network: $g}' <<<"" >"/.people.json.next" && mv "/.people.json.next" "/.people.json" |
| @@ -426,7 +427,7 @@ chat() { | ||
| 426 | 427 | cat "/.chat.out" |
| 427 | 428 | return |
| 428 | 429 | fi |
| 429 | while IFS= read -r name; do | |
| 430 | while IFS= read -r name <&3; do | |
| 430 | 431 | [[ "" =~ ]] || continue |
| 431 | 432 | last="$(cat "/.chat/" 2>/dev/null || echo -1)" |
| 432 | 433 | # shellcheck disable=SC2016 # the script runs in the container's own shell, $1 its argument |
| @@ -436,7 +437,7 @@ chat() { | ||
| 436 | 437 | marks+=" "$'\n' |
| 437 | 438 | lines="$(tail -n +2 <<<"")" |
| 438 | 439 | [ -n "" ] && out="$(jq -c --arg n "" --arg l "" '. + {(): ( | split("\n") | map(select(. != "")))}' <<<"")" |
| 439 | done < <(capped 5 "" ps --status running --format '{{.Service}}' 2>/dev/null) | |
| 440 | done 3< <(capped 5 "" ps --status running --format '{{.Service}}' 2>/dev/null) | |
| 440 | 441 | # The batch is on disk before its lines count as read. |
| 441 | 442 | if [ "" != '{}' ]; then |
| 442 | 443 | jq -cn --arg id "$(date +%s)-" --argjson lines "" '{id: $id, lines: $lines}' >"/.chat.tmp" && mv "/.chat.tmp" "/.chat.out" || return |
| @@ -666,7 +667,7 @@ apply() { | ||
| 666 | 667 | jq -j '.compose' <<<"" >"/compose.yml.next" && mv "/compose.yml.next" "/compose.yml" |
| 667 | 668 | changed=1 |
| 668 | 669 | fi |
| 669 | while IFS= read -r name; do | |
| 670 | while IFS= read -r name <&3; do | |
| 670 | 671 | # Only `.env`, compose's own: any other name could land on the agent's own files (agent.env, .auth). |
| 671 | 672 | if [ "" != ".env" ]; then |
| 672 | 673 | echo "forge-game-agent: ignored env file : only .env is written" >&2 |
| @@ -675,15 +676,15 @@ apply() { | ||
| 675 | 676 | jq -j --arg n "" '.env[$n] // ""' <<<"" >"/.next" || continue |
| 676 | 677 | cmp -s "/.next" "/" || { changed=1; changed_env=1; } |
| 677 | 678 | mv "/.next" "/" |
| 678 | done < <(jq -r '(.env // {}) | keys[]' <<<"" 2>/dev/null) | |
| 679 | done 3< <(jq -r '(.env // {}) | keys[]' <<<"" 2>/dev/null) | |
| 679 | 680 | # Queued, never waited on: an up that pulls gigabytes must not stop the box from syncing. |
| 680 | 681 | if [ "" = 1 ] && [ -f "/compose.yml" ]; then |
| 681 | 682 | [ "" = 1 ] && rm -f "/.up.failed" |
| 682 | 683 | [ "" = "$(cat "/.up.failed" 2>/dev/null)" ] || queue_job "" "compose up" "" up -d --remove-orphans |
| 683 | 684 | fi |
| 684 | while IFS= read -r cmd; do | |
| 685 | while IFS= read -r cmd <&3; do | |
| 685 | 686 | [ -n "" ] && run "" |
| 686 | done < <(jq -c '.commands // [] | .[]' <<<"" 2>/dev/null) | |
| 687 | done 3< <(jq -c '.commands // [] | .[]' <<<"" 2>/dev/null) | |
| 687 | 688 | } |
| 688 | 689 | |
| 689 | 690 | # What a person needs to see why a box is not playing, in about 8 KB: the running job and the tail |
test/game-chat.test.ts +54
| @@ -177,6 +177,60 @@ esac | ||
| 177 | 177 | await rm(dir, { recursive: true, force: true }); |
| 178 | 178 | } |
| 179 | 179 | }, 60_000); |
| 180 | ||
| 181 | test.skipIf(lacks.length > 0)(`every running server is asked and every command run, though exec reads all of its stdin${lacks.length > 0 ? ` (skipped: no ${lacks.join(", ")} here)` : ""}`, async () => { | |
| 182 | const dir = mkdtempSync(join(tmpdir(), "forge-game-stdin-agent-")); | |
| 183 | const docker = join(dir, "docker"); | |
| 184 | // As `docker compose exec -T` does: whatever is on stdin is taken. The first service has no shell. | |
| 185 | writeFileSync( | |
| 186 | docker, | |
| 187 | `#!/usr/bin/env bash | |
| 188 | case "$*" in *" exec -T "*) cat >/dev/null ;; esac | |
| 189 | case "$*" in | |
| 190 | *" ps --status running --format "*) printf 'forge-waker\nhub\nmods\n' ;; | |
| 191 | *" exec -T forge-waker "*) exit 127 ;; | |
| 192 | *" exec -T "*latest.log*) printf '5\n%s\n' '${paper("ada", "!forge help")}' ;; | |
| 193 | *" exec -T "*whitelist.json*) printf '=== whitelist.json\n[]\n=== ops.json\n[{"name":"ada","uuid":"u","level":4}]\n=== banned-players.json\n[]\n=== props\n' ;; | |
| 194 | *" exec -T "*" rcon-cli -- list uuids") echo "There are 0 of a max of 20 players online:" ;; | |
| 195 | *" exec -T "*" rcon-cli -- say "*) echo "$*" >>"${dir}/said" ;; | |
| 196 | esac | |
| 197 | `, | |
| 198 | ); | |
| 199 | chmodSync(docker, 0o755); | |
| 200 | const bodies: { chat: object; people: { servers: object }; metrics: object }[] = []; | |
| 201 | const forge = Bun.serve({ | |
| 202 | port: 0, | |
| 203 | hostname: "127.0.0.1", | |
| 204 | async fetch(req) { | |
| 205 | bodies.push((await req.json()) as (typeof bodies)[number]); | |
| 206 | return Response.json({ composeSha: "", commands: ["one", "two"].map((id) => ({ id, service: "hub", kind: "rcon", text: `say ${id}` })) }); | |
| 207 | }, | |
| 208 | }); | |
| 209 | try { | |
| 210 | const agent = Bun.spawn(["bash", join(import.meta.dir, "../src/games/agent.sh")], { | |
| 211 | env: { | |
| 212 | PATH: process.env["PATH"] ?? "", | |
| 213 | FORGE_URL: `http://127.0.0.1:${String(forge.port)}`, | |
| 214 | FORGE_GAME_TOKEN: "fgb_abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG", | |
| 215 | FORGE_GAME_DIR: dir, | |
| 216 | FORGE_GAME_DOCKER: docker, | |
| 217 | FORGE_GAME_ONCE: "1", | |
| 218 | }, | |
| 219 | stdin: "ignore", | |
| 220 | stdout: "ignore", | |
| 221 | stderr: "ignore", | |
| 222 | }); | |
| 223 | await agent.exited; | |
| 224 | const [sent] = bodies; | |
| 225 | expect(Object.keys(sent!.chat)).toEqual(["hub", "mods"]); | |
| 226 | expect(Object.keys(sent!.metrics)).toEqual(["hub", "mods"]); | |
| 227 | expect(Object.keys(sent!.people.servers)).toEqual(["hub", "mods"]); | |
| 228 | expect((await Bun.file(join(dir, "said")).text()).trim().split("\n").map((l) => l.replace(/^.* -- /, ""))).toEqual(["say one", "say two"]); | |
| 229 | } finally { | |
| 230 | forge.stop(true); | |
| 231 | await rm(dir, { recursive: true, force: true }); | |
| 232 | } | |
| 233 | }, 60_000); | |
| 180 | 234 | }); |
| 181 | 235 | |
| 182 | 236 | describe("on a forge", () => { |
new above
offline