| 1 | #!/usr/bin/env node |
| 2 | // Render apps/diffinery/wrangler.deploy.jsonc from wrangler.jsonc plus values |
| 3 | // discovered by scripts/bootstrap.sh (real D1 id, Access team domain and AUD). |
| 4 | // |
| 5 | // node scripts/render-config.mjs --d1 <uuid> [--team <x.cloudflareaccess.com>] [--aud <tag>] |
| 6 | // [--public-url https://git.example.com] [--zone example.com] [--mail-from [email protected]] |
| 7 | // [--admins [email protected],[email protected]] |
| 8 | // |
| 9 | // --public-url also points the Worker's route at that host (in --zone, by default |
| 10 | // the host's last two labels) and sends email from notifications@<host>. |
| 11 | // --admins sets BOOTSTRAP_ADMINS (comma-separated emails; bootstrap.sh passes what |
| 12 | // scripts/bootstrap-admins.sh found). Without it, wrangler.jsonc's value stays. |
| 13 | // From the environment it takes RUNNER_IMAGE (scripts/runner-image.sh), CONTAINER_MAX_INSTANCE_TYPE, |
| 14 | // TURNSTILE_SITE_KEY (scripts/turnstile.sh, through bootstrap) and the GitHub App of GitHub |
| 15 | // runners: GITHUB_APP_ID, GITHUB_APP_SLUG, GITHUB_APP_CLIENT_ID and GITHUB_APP_URL. |
| 16 | import { readFileSync, writeFileSync } from "node:fs"; |
| 17 | import { dirname, join } from "node:path"; |
| 18 | import { fileURLToPath } from "node:url"; |
| 19 | import { parseArgs } from "node:util"; |
| 20 | import { parseJsonc } from "./jsonc.mjs"; |
| 21 | |
| 22 | const { values } = parseArgs({ options: { d1: { type: "string" }, team: { type: "string" }, aud: { type: "string" }, "public-url": { type: "string" }, zone: { type: "string" }, "mail-from": { type: "string" }, admins: { type: "string" } } }); |
| 23 | if (!values.d1) throw new Error("--d1 <database id> is required"); |
| 24 | |
| 25 | const appDir = join(dirname(fileURLToPath(import.meta.url)), "..", "apps", "diffinery"); |
| 26 | const cfg = parseJsonc(readFileSync(join(appDir, "wrangler.jsonc"), "utf8")); |
| 27 | delete cfg.env; |
| 28 | delete cfg.$schema; |
| 29 | cfg.d1_databases[0].database_id = values.d1; |
| 30 | if (values.team) cfg.vars.ACCESS_TEAM_DOMAIN = values.team; |
| 31 | if (values.aud) cfg.vars.ACCESS_AUD = values.aud; |
| 32 | if (values["public-url"]) { |
| 33 | const url = new URL(values["public-url"]); |
| 34 | if (url.protocol !== "https:" || url.pathname !== "/") throw new Error("--public-url must be an https origin, like https://git.example.com"); |
| 35 | cfg.vars.PUBLIC_URL = url.origin; |
| 36 | const zone = values.zone ?? url.hostname.split(".").slice(-2).join("."); |
| 37 | // On another Cloudflare-compatible API (Homeflare), a custom domain: the zone isn't Cloudflare's. |
| 38 | const onCloudflare = (process.env.CLOUDFLARE_API_BASE_URL ?? "https://api.cloudflare.com/").startsWith("https://api.cloudflare.com/"); |
| 39 | cfg.routes = onCloudflare ? [{ pattern: `${url.hostname}/*`, zone_name: zone }] : [{ pattern: url.hostname, custom_domain: true }]; |
| 40 | cfg.vars.MAIL_FROM = `notifications@${url.hostname}`; |
| 41 | } |
| 42 | if (values["mail-from"]) cfg.vars.MAIL_FROM = values["mail-from"]; |
| 43 | if (values.admins !== undefined) { |
| 44 | const admins = values.admins.split(",").map((s) => s.trim()).filter(Boolean); |
| 45 | // Only verified email addresses make site administrators (src/lib/directory.ts); a login would be ignored. |
| 46 | const bad = admins.filter((a) => !/^[^\s@,]+@[^\s@,]+$/.test(a)); |
| 47 | if (bad.length) throw new Error(`--admins takes comma-separated email addresses; not an email address: ${bad.map((b) => JSON.stringify(b)).join(", ")}`); |
| 48 | cfg.vars.BOOTSTRAP_ADMINS = admins.join(","); |
| 49 | } |
| 50 | // A runner image already in the registry (scripts/runner-image.sh): containers use it |
| 51 | // instead of building the Dockerfile on every deploy. |
| 52 | if (process.env.RUNNER_IMAGE) { |
| 53 | // Named as the runner reports it (`runner.image` on a job), for /meta and the Live test. |
| 54 | cfg.vars.RUNNER_IMAGE = process.env.RUNNER_IMAGE.split("/").pop(); |
| 55 | for (const c of cfg.containers) { |
| 56 | c.image = process.env.RUNNER_IMAGE; |
| 57 | delete c.image_build_context; |
| 58 | } |
| 59 | } |
| 60 | // The largest instance type a container may have, for a machine smaller than Cloudflare's fleet |
| 61 | // (Homeflare): a class above it (runners, imports, sandboxes) runs on it instead. celld reserves a running container's whole |
| 62 | // instance type, so four 12 GiB large runners fill a 60 GiB machine. The capped class has the smaller |
| 63 | // type's vCPU and memory, though src/runner/sizes.ts still names the larger. Unset, nothing changes. |
| 64 | const INSTANCE_TYPES = ["lite", "basic", "standard-1", "standard-2", "standard-3", "standard-4"]; |
| 65 | const typeRank = (t) => INSTANCE_TYPES.indexOf({ dev: "lite", standard: "standard-1" }[t] ?? t); |
| 66 | const maxType = process.env.CONTAINER_MAX_INSTANCE_TYPE?.trim(); |
| 67 | if (maxType) { |
| 68 | if (typeRank(maxType) < 0) throw new Error(`CONTAINER_MAX_INSTANCE_TYPE must be one of ${INSTANCE_TYPES.join(", ")}`); |
| 69 | for (const c of cfg.containers) { |
| 70 | // A custom instance type (an object) is the operator's own choice; leave it. |
| 71 | if (typeof c.instance_type === "string" && typeRank(c.instance_type) > typeRank(maxType)) c.instance_type = maxType; |
| 72 | } |
| 73 | } |
| 74 | // The site key of the Turnstile widget scripts/turnstile.sh set up for this hostname (not a |
| 75 | // secret). One set in wrangler.jsonc is the operator's own and wins; turnstile.sh then leaves it alone. |
| 76 | if (process.env.TURNSTILE_SITE_KEY && !cfg.vars.TURNSTILE_SITE_KEY) cfg.vars.TURNSTILE_SITE_KEY = process.env.TURNSTILE_SITE_KEY; |
| 77 | // The GitHub App's IDs (docs/self-hosting/configuration.md#github-runners) aren't secrets, but they are |
| 78 | // the instance's own, so they come from the environment like the hostname rather than from |
| 79 | // wrangler.jsonc; one that is empty there leaves wrangler.jsonc's value. Its secrets are Worker secrets. |
| 80 | const githubApp = [ |
| 81 | ["GITHUB_APP_ID", /^\d+$/, "the App ID, a number"], |
| 82 | ["GITHUB_APP_SLUG", /^[a-z0-9][a-z0-9-]*$/, "the slug, like diffinery-runners"], |
| 83 | ["GITHUB_APP_CLIENT_ID", /^\S+$/, "the Client ID, like Iv23li..."], |
| 84 | ["GITHUB_APP_URL", /^https:\/\/[^/\s]+$/, "an https origin, like https://ghe.example.com"], |
| 85 | ]; |
| 86 | for (const [key, shape, want] of githubApp) { |
| 87 | const value = process.env[key]?.trim(); |
| 88 | if (!value) continue; |
| 89 | // Not the value itself: a secret set in the wrong variable would end up in the deploy log. |
| 90 | if (!shape.test(value)) throw new Error(`${key} must be ${want}`); |
| 91 | cfg.vars[key] = value; |
| 92 | } |
| 93 | writeFileSync(join(appDir, "wrangler.deploy.jsonc"), `// Generated by scripts/render-config.mjs; do not edit.\n${JSON.stringify(cfg, null, 2)}\n`); |
| 94 | console.log(`wrote ${join(appDir, "wrangler.deploy.jsonc")}`); |