diff --git a/gates/_lib.ts b/gates/_lib.ts index 90ed6e5bd2..aac4b17186 100644 --- a/gates/_lib.ts +++ b/gates/_lib.ts @@ -65,6 +65,9 @@ let held = ""; const staged = new Map>(); +// a hoc script longer than this (quoted) goes up as a staged file +const SCRIPT_MAX = 4096; + const opened = new Map>(); // Exec @@ -150,7 +153,10 @@ export async function ssh(node: number, script: string, } // The hoc way sends each input up to the bastion once, and every session -// reads it there: 48 shards of one pack cost one upload, not 48. +// reads it there: 48 shards of one pack cost one upload, not 48. A long +// script goes up the same way: the mux client hands its command to the +// master in one message, and macOS refuses one near 8 KB ("mm_send_fd: +// sendmsg: Message too long"), so that session never started. async function hoc_ssh(node: number, script: string, input?: Buffer | string, timeout?: number): Promise { const open = await hoc_open(node); @@ -159,24 +165,42 @@ async function hoc_ssh(node: number, script: string, } let from = ""; if (input !== undefined) { - const sum = crypto.createHash("sha1").update(input).digest("hex"); - const up = staged.get(sum) ?? exec("ssh", [...hoc_mux(node), ...HOC_HUB, - "d=$HOME/.bend-gate; mkdir -p $d && { find $d -type f -mmin +60" - + " -delete 2>/dev/null; cat > $d/" + sum + ".$$; } && mv $d/" + sum - + ".$$ $d/" + sum], input); - staged.set(sum, up); - const got = await up; + const got = await hoc_stage(node, input); + if (got.code !== 0) { + return got; + } + from = " < " + got.out; + } + let arg = "'" + script.replaceAll("'", "'\\''") + "'"; + if (arg.length > SCRIPT_MAX) { + const got = await hoc_stage(node, script); if (got.code !== 0) { - if (staged.get(sum) === up) { - staged.delete(sum); - } return got; } - from = " < $HOME/.bend-gate/" + sum; + arg = "\"$(cat " + got.out + ")\""; } return node_exec([...hoc_mux(node), ...HOC_HUB, "sudo -n" - + " /usr/local/bin/hoc-ssh " + String(node) + " '" - + script.replaceAll("'", "'\\''") + "'" + from], undefined, timeout); + + " /usr/local/bin/hoc-ssh " + String(node) + " " + arg + from], + undefined, timeout); +} + +// Puts data on the bastion under its sha1 (once per run) and answers its +// path there in out. +async function hoc_stage(node: number, data: Buffer | string): Promise { + const sum = crypto.createHash("sha1").update(data).digest("hex"); + const up = staged.get(sum) ?? exec("ssh", [...hoc_mux(node), ...HOC_HUB, + "d=$HOME/.bend-gate; mkdir -p $d && { find $d -type f -mmin +60" + + " -delete 2>/dev/null; cat > $d/" + sum + ".$$; } && mv $d/" + sum + + ".$$ $d/" + sum], data); + staged.set(sum, up); + const got = await up; + if (got.code !== 0) { + if (staged.get(sum) === up) { + staged.delete(sum); + } + return got; + } + return { out: "$HOME/.bend-gate/" + sum, err: "", code: 0 }; } function node_exec(args: string[], input?: Buffer | string,