|
3 | 3 | // |
4 | 4 | // cf workers versions create upload a version stamped with the git |
5 | 5 | // SHA (--tag git-<sha12>), its secrets |
6 | | -// read from stdin; nothing goes live yet |
| 6 | +// through a pipe; nothing goes live yet |
7 | 7 | // cf workers deployments create put that version at 100% |
8 | 8 | // cf workers triggers deploy apply the cron schedule from the config |
9 | 9 | // |
|
33 | 33 | // the previous version's value. No secret is printed or written to disk. |
34 | 34 | import { execFileSync } from "node:child_process"; |
35 | 35 | import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync } from "node:fs"; |
| 36 | +import { setDefaultAutoSelectFamilyAttemptTimeout } from "node:net"; |
36 | 37 | import { homedir, tmpdir } from "node:os"; |
37 | 38 | import { dirname, join } from "node:path"; |
38 | 39 | import { fileURLToPath } from "node:url"; |
@@ -95,13 +96,25 @@ for (const [name, variable] of [["cf", "QTA_CF_MODULE"], ["wrangler", "QTA_WRANG |
95 | 96 | symlinkSync(target, join(stage, "node_modules", name)); |
96 | 97 | } |
97 | 98 |
|
98 | | -function runCf(args, { capture = false, input } = {}) { |
| 99 | +function runCf(args, { capture = false } = {}) { |
99 | 100 | return execFileSync("cf", ["-q", ...args], { |
100 | 101 | cwd: stage, |
101 | 102 | env, |
102 | 103 | encoding: "utf8", |
103 | | - input, |
104 | | - stdio: [input === undefined ? "inherit" : "pipe", capture ? "pipe" : "inherit", "inherit"], |
| 104 | + stdio: ["inherit", capture ? "pipe" : "inherit", "inherit"], |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +// cf reads --secrets-file with a plain file read, which fails on this |
| 109 | +// process's stdin pipe. Hand it a fresh pipe instead: bash's process |
| 110 | +// substitution copies our stdin (the secrets JSON) into /dev/fd/N, and |
| 111 | +// cf itself gets /dev/null, so the values never touch disk or argv. |
| 112 | +function runCfWithSecrets(args, secrets) { |
| 113 | + execFileSync("bash", ["-c", 'cf -q "$@" --secrets-file <(cat) </dev/null', "cf", ...args], { |
| 114 | + cwd: stage, |
| 115 | + env, |
| 116 | + input: JSON.stringify(secrets), |
| 117 | + stdio: ["pipe", "inherit", "inherit"], |
105 | 118 | }); |
106 | 119 | } |
107 | 120 |
|
@@ -121,9 +134,23 @@ async function verify(versionId) { |
121 | 134 | throw new Error(`post-deploy check failed: expected version ${versionId} at 100%, latest deployment serves ${serving.serving}`); |
122 | 135 | } |
123 | 136 | const url = `${WORKER_URL}/status.json`; |
124 | | - const response = await fetch(url, { headers: { "user-agent": BROWSER_UA }, signal: AbortSignal.timeout(15_000) }); |
125 | | - if (!response.ok) throw new Error(`post-deploy check failed: ${url} answered HTTP ${response.status}`); |
126 | | - const status = await response.json(); |
| 137 | + // Node tries each address for only 250 ms by default; on the laptop's |
| 138 | + // network that timed out every address while curl got through. |
| 139 | + setDefaultAutoSelectFamilyAttemptTimeout(5_000); |
| 140 | + let status; |
| 141 | + for (let attempt = 1; ; attempt += 1) { |
| 142 | + try { |
| 143 | + const response = await fetch(url, { headers: { "user-agent": BROWSER_UA }, signal: AbortSignal.timeout(20_000) }); |
| 144 | + if (!response.ok) throw new Error(`HTTP ${response.status}`); |
| 145 | + status = await response.json(); |
| 146 | + break; |
| 147 | + } catch (error) { |
| 148 | + if (attempt >= 3) { |
| 149 | + throw new Error(`version ${versionId} is deployed, but ${url} did not answer: ${error.cause?.message ?? error.message}`); |
| 150 | + } |
| 151 | + await new Promise((resolve) => setTimeout(resolve, 3_000)); |
| 152 | + } |
| 153 | + } |
127 | 154 | console.log(`verified: version ${versionId} serves 100%; ${url} reports the broker ${status.status ?? "unknown"}`); |
128 | 155 | } |
129 | 156 |
|
@@ -163,10 +190,7 @@ if (brokerToken) { |
163 | 190 | } |
164 | 191 |
|
165 | 192 | console.log(`uploading version (tag ${tag}; secrets ${Object.keys(workerSecrets).join(", ")})`); |
166 | | -runCf( |
167 | | - ["workers", "versions", "create", "--worker", WORKER, "--tag", tag, "--message", message, "--secrets-file", "/dev/stdin"], |
168 | | - { input: JSON.stringify(workerSecrets) }, |
169 | | -); |
| 193 | +runCfWithSecrets(["workers", "versions", "create", "--worker", WORKER, "--tag", tag, "--message", message], workerSecrets); |
170 | 194 | const versions = listItems( |
171 | 195 | parseJsonOutput(runCf(["workers", "versions", "list", "--worker-id", WORKER], { capture: true })), |
172 | 196 | "versions", |
|