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

opened this

Merged 22h 37m ago, live in production

sold/agent-stdin → main 0 commits

  • Merged. sold/agent-stdin was merged into main as b1bd486.

Review

Sign in to review this change.
3 files changed +66 −10
src/games/agent.sh +11 −10
@@ -360,7 +360,8 @@ logs() {
360360 NORCON=" "
361361 metrics() {
362362 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
364365 [[ "$name" =~ $SAFE ]] || continue
365366 case "$NORCON" in *" $name "*) continue ;; esac
366367 # `list uuids`: each name with its UUID, so the forge can show who it is (games/people.ts).
@@ -371,7 +372,7 @@ metrics() {
371372 tps="$(capped 5 "${COMPOSE[@]}" exec -T "$name" rcon-cli -- tps 2>/dev/null || true)"
372373 mspt="$(capped 5 "${COMPOSE[@]}" exec -T "$name" rcon-cli -- mspt 2>/dev/null || true)"
373374 out="$(jq -c --arg n "$name" --arg l "${list:0:4000}" --arg t "${tps:0:1000}" --arg m "${mspt:0:2000}" '. + {($n): {list: $l, tps: $t, mspt: $m}}' <<<"$out")"
374 done < <(capped 5 "${COMPOSE[@]}" ps --status running --format '{{.Service}}' 2>/dev/null)
375 done 3< <(capped 5 "${COMPOSE[@]}" ps --status running --format '{{.Service}}' 2>/dev/null)
375376 echo "$out"
376377 }
377378
@@ -396,7 +397,7 @@ people() {
396397 local out='{}' name raw part via="" glist
397398 if ! people_due; then cat "$DIR/.people.json" 2>/dev/null || echo '{}'; return; fi
398399 rm -f "$DIR/.people.due"
399 while IFS= read -r name; do
400 while IFS= read -r name <&3; do
400401 [[ "$name" =~ $SAFE ]] || continue
401402 case "$NORCON" in *" $name "*) continue ;; esac
402403 raw="$(capped 5 "${COMPOSE[@]}" exec -T "$name" sh -c 'for f in whitelist.json ops.json banned-players.json; do echo "=== $f"; cat "$f" 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() {
408409 bans: [file("banned-players.json")[] | {name: (.name // ""), uuid: (.uuid // ""), reason: ((.reason // "") | .[0:200]), created: (.created // "")}][0:500],
409410 whitelistOn: ($raw | test("(?m)^white-list=true"))}' 2>/dev/null)" || continue
410411 out="$(jq -c --arg n "$name" --argjson p "$part" '. + {($n): $p}' <<<"$out")"
411 done < <(capped 5 "${COMPOSE[@]}" ps --status running --format '{{.Service}}' 2>/dev/null)
412 done 3< <(capped 5 "${COMPOSE[@]}" ps --status running --format '{{.Service}}' 2>/dev/null)
412413 glist=""
413414 [ -n "$via" ] && glist="$(proxy_rcon "$via" glist all 2>/dev/null || true)"
414415 jq -c --arg g "${glist:0:20000}" '{servers: ., network: $g}' <<<"$out" >"$DIR/.people.json.next" && mv "$DIR/.people.json.next" "$DIR/.people.json"
@@ -426,7 +427,7 @@ chat() {
426427 cat "$DIR/.chat.out"
427428 return
428429 fi
429 while IFS= read -r name; do
430 while IFS= read -r name <&3; do
430431 [[ "$name" =~ $SAFE ]] || continue
431432 last="$(cat "$DIR/.chat/$name" 2>/dev/null || echo -1)"
432433 # shellcheck disable=SC2016 # the script runs in the container's own shell, $1 its argument
@@ -436,7 +437,7 @@ chat() {
436437 marks+="$name $total"$'\n'
437438 lines="$(tail -n +2 <<<"$got")"
438439 [ -n "$lines" ] && out="$(jq -c --arg n "$name" --arg l "${lines:0:8000}" '. + {($n): ($l | split("\n") | map(select(. != "")))}' <<<"$out")"
439 done < <(capped 5 "${COMPOSE[@]}" ps --status running --format '{{.Service}}' 2>/dev/null)
440 done 3< <(capped 5 "${COMPOSE[@]}" ps --status running --format '{{.Service}}' 2>/dev/null)
440441 # The batch is on disk before its lines count as read.
441442 if [ "$out" != '{}' ]; then
442443 jq -cn --arg id "$(date +%s)-$RANDOM$RANDOM" --argjson lines "$out" '{id: $id, lines: $lines}' >"$DIR/.chat.tmp" && mv "$DIR/.chat.tmp" "$DIR/.chat.out" || return
@@ -666,7 +667,7 @@ apply() {
666667 jq -j '.compose' <<<"$answer" >"$DIR/compose.yml.next" && mv "$DIR/compose.yml.next" "$DIR/compose.yml"
667668 changed=1
668669 fi
669 while IFS= read -r name; do
670 while IFS= read -r name <&3; do
670671 # Only `.env`, compose's own: any other name could land on the agent's own files (agent.env, .auth).
671672 if [ "$name" != ".env" ]; then
672673 echo "forge-game-agent: ignored env file $name: only .env is written" >&2
@@ -675,15 +676,15 @@ apply() {
675676 jq -j --arg n "$name" '.env[$n] // ""' <<<"$answer" >"$DIR/$name.next" || continue
676677 cmp -s "$DIR/$name.next" "$DIR/$name" || { changed=1; changed_env=1; }
677678 mv "$DIR/$name.next" "$DIR/$name"
678 done < <(jq -r '(.env // {}) | keys[]' <<<"$answer" 2>/dev/null)
679 done 3< <(jq -r '(.env // {}) | keys[]' <<<"$answer" 2>/dev/null)
679680 # Queued, never waited on: an up that pulls gigabytes must not stop the box from syncing.
680681 if [ "$changed" = 1 ] && [ -f "$DIR/compose.yml" ]; then
681682 [ "$changed_env" = 1 ] && rm -f "$DIR/.up.failed"
682683 [ "$sha" = "$(cat "$DIR/.up.failed" 2>/dev/null)" ] || queue_job "" "compose up" "$sha" up -d --remove-orphans
683684 fi
684 while IFS= read -r cmd; do
685 while IFS= read -r cmd <&3; do
685686 [ -n "$cmd" ] && run "$cmd"
686 done < <(jq -c '.commands // [] | .[]' <<<"$answer" 2>/dev/null)
687 done 3< <(jq -c '.commands // [] | .[]' <<<"$answer" 2>/dev/null)
687688 }
688689
689690 # 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
177177 await rm(dir, { recursive: true, force: true });
178178 }
179179 }, 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);
180234 });
181235
182236 describe("on a forge", () => {
test/game-people.test.ts +1
@@ -401,5 +401,6 @@ exit 0
401401 await rm(root, { recursive: true, force: true });
402402 }
403403 },
404 60_000,
404405 );
405406 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs