| 1 | // Wrangler, as the deploy tool uses it: reading which commit each Worker |
| 2 | // runs, D1 migrations, and deploying. Every call runs in the unit's own |
| 3 | // folder, so Wrangler reads that unit's config (and not a .env at the |
| 4 | // repository root, which may hold a token meant for something else). |
| 5 | |
| 6 | import { spawn } from "node:child_process"; |
| 7 | import { join } from "node:path"; |
| 8 | |
| 9 | import { ROOT } from "./stack.mjs"; |
| 10 | |
| 11 | const WRANGLER = join(ROOT, "node_modules/wrangler/bin/wrangler.js"); |
| 12 | export const ACCOUNT_ID = "1e6f2cffa3f445920836e8ebe446bb58"; |
| 13 | |
| 14 | /** What a deploy's version message starts with, followed by the commit. */ |
| 15 | export const MESSAGE_PREFIX = "g1t-deploy"; |
| 16 | |
| 17 | /** |
| 18 | * The environment Wrangler runs with. In CI (CI=true) it is the job's: |
| 19 | * CLOUDFLARE_API_TOKEN from the repository's secret. On a laptop it is |
| 20 | * your `wrangler login`, unless CLOUDFLARE_DEPLOY_TOKEN is set, as |
| 21 | * scripts/deploy.sh always did: a CLOUDFLARE_API_TOKEN or global API key |
| 22 | * in your shell is for other tools. |
| 23 | */ |
| 24 | export function wranglerEnv(base = process.env) { |
| 25 | const env = { ...base, WRANGLER_SEND_METRICS: "false", NO_COLOR: "1", FORCE_COLOR: "0" }; |
| 26 | env.CLOUDFLARE_ACCOUNT_ID ||= ACCOUNT_ID; |
| 27 | if (base.CLOUDFLARE_DEPLOY_TOKEN) { |
| 28 | env.CLOUDFLARE_API_TOKEN = base.CLOUDFLARE_DEPLOY_TOKEN; |
| 29 | } else if (base.CI !== "true") { |
| 30 | env.CLOUDFLARE_API_TOKEN = ""; |
| 31 | delete env.CLOUDFLARE_API_KEY; |
| 32 | delete env.CLOUDFLARE_EMAIL; |
| 33 | } |
| 34 | return env; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Runs a command; resolves with { code, out } (stdout and stderr together, |
| 39 | * in order). `onLine` sees each line as it comes. |
| 40 | */ |
| 41 | export function exec(command, args, { cwd = ROOT, env = process.env, onLine, shell = false } = {}) { |
| 42 | return new Promise((resolve) => { |
| 43 | const child = spawn(command, args, { cwd, env, shell, windowsHide: true }); |
| 44 | let out = ""; |
| 45 | let partial = ""; |
| 46 | const take = (chunk) => { |
| 47 | const text = chunk.toString(); |
| 48 | out += text; |
| 49 | if (!onLine) return; |
| 50 | const lines = (partial + text).split(/\r?\n/); |
| 51 | partial = lines.pop(); |
| 52 | for (const line of lines) onLine(line); |
| 53 | }; |
| 54 | child.stdout.on("data", take); |
| 55 | child.stderr.on("data", take); |
| 56 | child.on("error", (error) => resolve({ code: 127, out: `${out}${error.message}\n` })); |
| 57 | child.on("close", (code) => { |
| 58 | if (onLine && partial) onLine(partial); |
| 59 | resolve({ code: code ?? 1, out }); |
| 60 | }); |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | /** Runs the repository's own Wrangler in `cwd`. */ |
| 65 | export function wrangler(args, { cwd, env = wranglerEnv(), onLine } = {}) { |
| 66 | return exec(process.execPath, [WRANGLER, ...args], { cwd, env, onLine }); |
| 67 | } |
| 68 | |
| 69 | /** The first JSON value in Wrangler's output (it may print notices first). */ |
| 70 | export function jsonFrom(out) { |
| 71 | const start = out.search(/^[[{]/m); |
| 72 | if (start < 0) throw new Error(`no JSON in: ${out.slice(0, 300)}`); |
| 73 | return JSON.parse(out.slice(start)); |
| 74 | } |
| 75 | |
| 76 | /** The message and tag a deploy of `sha` is annotated with. */ |
| 77 | export function annotation(sha, subject = "") { |
| 78 | const message = `${MESSAGE_PREFIX} ${sha} ${subject}`.trim().slice(0, 100); |
| 79 | return { message, tag: `g1t-${sha.slice(0, 12)}` }; |
| 80 | } |
| 81 | |
| 82 | /** The commit a version message names, or null. Dirty deploys name none. */ |
| 83 | export function commitFrom(message) { |
| 84 | const match = new RegExp(`^${MESSAGE_PREFIX} ([0-9a-f]{40})(?:\\s|$)`).exec(message ?? ""); |
| 85 | return match ? match[1] : null; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Which commit a Worker's live version was deployed from, given Wrangler's |
| 90 | * `deployments status --json` and `versions list --json`. A version made by |
| 91 | * `wrangler secret put` keeps the code of the one before it, so those are |
| 92 | * looked through. Anything else without our message (a deploy by hand, a |
| 93 | * dashboard edit) leaves the commit unknown, and the unit is deployed again. |
| 94 | */ |
| 95 | export function liveCommit(status, versions) { |
| 96 | const live = [...(status.versions ?? [])].sort((a, b) => b.percentage - a.percentage); |
| 97 | if (!live.length) return { sha: null, why: "no live version" }; |
| 98 | const split = live.length > 1 && live[1].percentage > 0; |
| 99 | const byNumber = [...versions].sort((a, b) => b.number - a.number); |
| 100 | let index = byNumber.findIndex((v) => v.id === live[0].version_id); |
| 101 | if (index < 0) return { sha: null, why: "its live version is not among the recent ones", version: live[0].version_id }; |
| 102 | const version = byNumber[index]; |
| 103 | while (index < byNumber.length) { |
| 104 | const candidate = byNumber[index]; |
| 105 | const sha = commitFrom(candidate.annotations?.["workers/message"]); |
| 106 | if (sha) { |
| 107 | return { |
| 108 | sha, |
| 109 | version: version.id, |
| 110 | at: candidate.metadata?.created_on ?? null, |
| 111 | by: candidate.metadata?.author_email ?? null, |
| 112 | split, |
| 113 | why: split ? "a gradual deployment is in progress; its main version is used" : null, |
| 114 | }; |
| 115 | } |
| 116 | if (candidate.annotations?.["workers/triggered_by"] !== "secret") break; |
| 117 | index++; |
| 118 | } |
| 119 | return { sha: null, version: version.id, why: "its live version was not deployed by scripts/deploy.mjs" }; |
| 120 | } |
| 121 | |
| 122 | /** Reads the commit a unit's Worker runs. Never throws. */ |
| 123 | export async function readLive(unit) { |
| 124 | const cwd = join(ROOT, unit.path); |
| 125 | const [status, versions] = await Promise.all([ |
| 126 | wrangler(["deployments", "status", "--name", unit.worker, "--json"], { cwd }), |
| 127 | wrangler(["versions", "list", "--name", unit.worker, "--json"], { cwd }), |
| 128 | ]); |
| 129 | if (status.code !== 0) { |
| 130 | if (/not found|does not exist|10007/i.test(status.out)) return { sha: null, missing: true, why: "never deployed" }; |
| 131 | return { sha: null, error: lastLines(status.out) }; |
| 132 | } |
| 133 | try { |
| 134 | return liveCommit(jsonFrom(status.out), versions.code === 0 ? jsonFrom(versions.out) : []); |
| 135 | } catch (error) { |
| 136 | return { sha: null, error: String(error.message ?? error) }; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** Migration files Wrangler lists as not yet applied. */ |
| 141 | export function pendingFrom(out) { |
| 142 | if (/No migrations to apply/i.test(out)) return []; |
| 143 | const names = [...out.matchAll(/([\w.-]+\.sql)\b/g)].map((m) => m[1]); |
| 144 | return [...new Set(names)]; |
| 145 | } |
| 146 | |
| 147 | /** Pending migrations of a unit's database: { pending } or { error }. */ |
| 148 | export async function pendingMigrations(unit) { |
| 149 | const found = await wrangler(["d1", "migrations", "list", unit.d1.database, "--remote"], { cwd: join(ROOT, unit.path) }); |
| 150 | if (found.code !== 0) return { error: lastLines(found.out) }; |
| 151 | return { pending: pendingFrom(found.out) }; |
| 152 | } |
| 153 | |
| 154 | export function applyMigrations(unit, onLine) { |
| 155 | return wrangler(["d1", "migrations", "apply", unit.d1.database, "--remote"], { cwd: join(ROOT, unit.path), onLine }); |
| 156 | } |
| 157 | |
| 158 | /** The version a deploy made, from Wrangler's output. */ |
| 159 | export function versionFrom(out) { |
| 160 | return /Current Version ID:\s*([0-9a-f-]{36})/i.exec(out)?.[1] ?? null; |
| 161 | } |
| 162 | |
| 163 | /** Whether Docker can build here (for a Containers image). */ |
| 164 | export async function dockerAvailable() { |
| 165 | const found = await exec("docker", ["info", "--format", "{{.ServerVersion}}"]); |
| 166 | return found.code === 0; |
| 167 | } |
| 168 | |
| 169 | export function lastLines(text, count = 12) { |
| 170 | return text.trim().split(/\r?\n/).slice(-count).join("\n"); |
| 171 | } |