@caelo-cms/provisioning 0.11.13 → 0.11.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. package/dist/__tests__/stack-under-mocks.d.ts +2 -0
  2. package/dist/__tests__/stack-under-mocks.d.ts.map +1 -0
  3. package/dist/__tests__/stack-under-mocks.js +59 -0
  4. package/dist/__tests__/stack-under-mocks.js.map +1 -0
  5. package/dist/admin-domain.d.ts +4 -46
  6. package/dist/admin-domain.d.ts.map +1 -1
  7. package/dist/admin-domain.js +4 -101
  8. package/dist/admin-domain.js.map +1 -1
  9. package/dist/agent-db.js +29 -5
  10. package/dist/audit-job-setup.d.ts.map +1 -1
  11. package/dist/audit-job-setup.js +14 -19
  12. package/dist/audit-job-setup.js.map +1 -1
  13. package/dist/caddy.d.ts +2 -1
  14. package/dist/caddy.d.ts.map +1 -1
  15. package/dist/caddy.js +21 -3
  16. package/dist/caddy.js.map +1 -1
  17. package/dist/cli.js +111 -62
  18. package/dist/cli.js.map +1 -1
  19. package/dist/compose.d.ts +22 -2
  20. package/dist/compose.d.ts.map +1 -1
  21. package/dist/compose.js +53 -3
  22. package/dist/compose.js.map +1 -1
  23. package/dist/converge-checkpoint.d.ts +1 -0
  24. package/dist/converge-checkpoint.d.ts.map +1 -1
  25. package/dist/converge-checkpoint.js +1 -0
  26. package/dist/converge-checkpoint.js.map +1 -1
  27. package/dist/deployed-release.d.ts.map +1 -1
  28. package/dist/deployed-release.js +42 -8
  29. package/dist/deployed-release.js.map +1 -1
  30. package/dist/gateway-bootstrap.d.ts +68 -0
  31. package/dist/gateway-bootstrap.d.ts.map +1 -0
  32. package/dist/gateway-bootstrap.js +145 -0
  33. package/dist/gateway-bootstrap.js.map +1 -0
  34. package/dist/gcp-names.d.ts +33 -14
  35. package/dist/gcp-names.d.ts.map +1 -1
  36. package/dist/gcp-names.js +43 -0
  37. package/dist/gcp-names.js.map +1 -1
  38. package/dist/gcp-url-map.d.ts +19 -4
  39. package/dist/gcp-url-map.d.ts.map +1 -1
  40. package/dist/gcp-url-map.js +33 -5
  41. package/dist/gcp-url-map.js.map +1 -1
  42. package/dist/infra-contract.d.ts +1 -1
  43. package/dist/infra-contract.d.ts.map +1 -1
  44. package/dist/infra-contract.js +18 -1
  45. package/dist/infra-contract.js.map +1 -1
  46. package/dist/lifecycle.d.ts.map +1 -1
  47. package/dist/lifecycle.js +5 -39
  48. package/dist/lifecycle.js.map +1 -1
  49. package/dist/mcp-auth-proxy-setup.d.ts +8 -2
  50. package/dist/mcp-auth-proxy-setup.d.ts.map +1 -1
  51. package/dist/mcp-auth-proxy-setup.js +4 -0
  52. package/dist/mcp-auth-proxy-setup.js.map +1 -1
  53. package/dist/release-manifest.d.ts +1 -1
  54. package/dist/release-manifest.d.ts.map +1 -1
  55. package/dist/release-manifest.js +2 -0
  56. package/dist/release-manifest.js.map +1 -1
  57. package/dist/remote-mcp.d.ts.map +1 -1
  58. package/dist/remote-mcp.js +11 -1
  59. package/dist/remote-mcp.js.map +1 -1
  60. package/dist/runtime-secrets.d.ts +10 -0
  61. package/dist/runtime-secrets.d.ts.map +1 -1
  62. package/dist/runtime-secrets.js +10 -1
  63. package/dist/runtime-secrets.js.map +1 -1
  64. package/dist/stack-contract.d.ts +61 -20
  65. package/dist/stack-contract.d.ts.map +1 -1
  66. package/dist/stack-contract.js +173 -35
  67. package/dist/stack-contract.js.map +1 -1
  68. package/dist/stack-converge.d.ts.map +1 -1
  69. package/dist/stack-converge.js +4 -2
  70. package/dist/stack-converge.js.map +1 -1
  71. package/dist/stack-gateways.d.ts +31 -0
  72. package/dist/stack-gateways.d.ts.map +1 -0
  73. package/dist/stack-gateways.js +40 -0
  74. package/dist/stack-gateways.js.map +1 -0
  75. package/dist/staging-admin.d.ts +2 -2
  76. package/dist/staging-admin.d.ts.map +1 -1
  77. package/dist/staging-gateway-setup.d.ts +140 -0
  78. package/dist/staging-gateway-setup.d.ts.map +1 -0
  79. package/dist/staging-gateway-setup.js +848 -0
  80. package/dist/staging-gateway-setup.js.map +1 -0
  81. package/dist/state-command.d.ts.map +1 -1
  82. package/dist/state-command.js +3 -0
  83. package/dist/state-command.js.map +1 -1
  84. package/dist/upgrade/app-roll.d.ts +4 -4
  85. package/dist/upgrade/app-roll.d.ts.map +1 -1
  86. package/dist/upgrade/app-roll.js +7 -5
  87. package/dist/upgrade/app-roll.js.map +1 -1
  88. package/dist/upgrade/converge.d.ts.map +1 -1
  89. package/dist/upgrade/converge.js +36 -27
  90. package/dist/upgrade/converge.js.map +1 -1
  91. package/dist/upgrade/image-forms.d.ts +3 -3
  92. package/dist/upgrade/image-forms.d.ts.map +1 -1
  93. package/dist/upgrade/image-forms.js +2 -2
  94. package/dist/upgrade/image-forms.js.map +1 -1
  95. package/dist/upgrade/release-images.d.ts +20 -0
  96. package/dist/upgrade/release-images.d.ts.map +1 -1
  97. package/dist/upgrade/release-images.js +28 -0
  98. package/dist/upgrade/release-images.js.map +1 -1
  99. package/dist/upgrade/upgrade.d.ts +3 -1
  100. package/dist/upgrade/upgrade.d.ts.map +1 -1
  101. package/dist/upgrade/upgrade.js +22 -1
  102. package/dist/upgrade/upgrade.js.map +1 -1
  103. package/dist/upgrader-job.d.ts.map +1 -1
  104. package/dist/upgrader-job.js +30 -5
  105. package/dist/upgrader-job.js.map +1 -1
  106. package/dist/upgrader-records.d.ts +2 -2
  107. package/dist/upgrader-records.d.ts.map +1 -1
  108. package/dist/upgrader-records.js +2 -2
  109. package/dist/upgrader-records.js.map +1 -1
  110. package/dist/wizard.d.ts.map +1 -1
  111. package/dist/wizard.js +3 -3
  112. package/dist/wizard.js.map +1 -1
  113. package/dist/wizards/gcp-pulumi.d.ts +7 -1
  114. package/dist/wizards/gcp-pulumi.d.ts.map +1 -1
  115. package/dist/wizards/gcp-pulumi.js +2 -0
  116. package/dist/wizards/gcp-pulumi.js.map +1 -1
  117. package/dist/wizards/gcp.d.ts.map +1 -1
  118. package/dist/wizards/gcp.js +168 -22
  119. package/dist/wizards/gcp.js.map +1 -1
  120. package/package.json +2 -2
  121. package/src/__tests__/stack-under-mocks.ts +69 -0
  122. package/src/admin-domain.test.ts +0 -156
  123. package/src/admin-domain.ts +4 -117
  124. package/src/admin-memory.test.ts +2 -0
  125. package/src/audit-job-setup.test.ts +11 -11
  126. package/src/audit-job-setup.ts +14 -19
  127. package/src/caddy-redirects.docker.test.ts +10 -5
  128. package/src/caddy-redirects.test.ts +7 -5
  129. package/src/caddy-validate.test.ts +1 -1
  130. package/src/caddy.test.ts +12 -6
  131. package/src/caddy.ts +23 -4
  132. package/src/cli.ts +138 -77
  133. package/src/compose.ts +67 -2
  134. package/src/converge-checkpoint.ts +1 -0
  135. package/src/database-tls-builders.test.ts +3 -1
  136. package/src/deployed-release.test.ts +37 -0
  137. package/src/deployed-release.ts +44 -7
  138. package/src/gateway-bootstrap.test.ts +381 -0
  139. package/src/gateway-bootstrap.ts +195 -0
  140. package/src/gateway-env-isolation.test.ts +2 -0
  141. package/src/gcp-names.ts +53 -0
  142. package/src/gcp-url-map.test.ts +27 -3
  143. package/src/gcp-url-map.ts +36 -5
  144. package/src/index.test.ts +9 -3
  145. package/src/infra-contract.test.ts +4 -3
  146. package/src/infra-contract.ts +18 -1
  147. package/src/lifecycle.ts +8 -52
  148. package/src/mcp-auth-proxy-setup.test.ts +15 -6
  149. package/src/mcp-auth-proxy-setup.ts +14 -2
  150. package/src/release-manifest.ts +3 -1
  151. package/src/remote-mcp-stack-config.test.ts +3 -1
  152. package/src/remote-mcp.test.ts +5 -1
  153. package/src/remote-mcp.ts +10 -1
  154. package/src/runtime-secrets.test.ts +10 -1
  155. package/src/runtime-secrets.ts +21 -0
  156. package/src/self-hosted-backup.test.ts +6 -4
  157. package/src/self-hosted-compose.test.ts +2 -0
  158. package/src/stack-cloud-run-names.test.ts +49 -0
  159. package/src/stack-contract-parity.test.ts +7 -3
  160. package/src/stack-contract.test.ts +73 -13
  161. package/src/stack-contract.ts +195 -42
  162. package/src/stack-converge-secrets.test.ts +7 -2
  163. package/src/stack-converge.test.ts +37 -9
  164. package/src/stack-converge.ts +4 -1
  165. package/src/stack-gateways.ts +39 -0
  166. package/src/staging-admin.ts +2 -2
  167. package/src/staging-gateway-setup.test.ts +361 -0
  168. package/src/staging-gateway-setup.ts +1083 -0
  169. package/src/state-command.ts +3 -0
  170. package/src/upgrade/app-roll.ts +7 -4
  171. package/src/upgrade/converge.ts +40 -26
  172. package/src/upgrade/image-forms.ts +7 -7
  173. package/src/upgrade/release-images.test.ts +28 -0
  174. package/src/upgrade/release-images.ts +44 -0
  175. package/src/upgrade/upgrade.test.ts +63 -11
  176. package/src/upgrade/upgrade.ts +29 -2
  177. package/src/upgrader-job.test.ts +23 -0
  178. package/src/upgrader-job.ts +45 -11
  179. package/src/upgrader-records.ts +7 -7
  180. package/src/wizard.ts +2 -3
  181. package/src/wizards/gcp-pulumi.ts +9 -0
  182. package/src/wizards/gcp.ts +219 -23
  183. package/stacks/gcp/Pulumi.yaml +4 -0
  184. package/stacks/gcp/index.ts +128 -66
  185. package/stacks/gcp-firebase/index.ts +101 -50
  186. package/stacks/self-hosted/index.ts +9 -2
@@ -63,14 +63,17 @@ import {
63
63
  type ReleaseManifest,
64
64
  type ReleaseManifestResult,
65
65
  } from "./release-manifest.js";
66
- import { cloudRunServicePrefix } from "./stack-contract.js";
66
+ import {
67
+ cloudRunServicePrefix,
68
+ SERVICE_IMAGE_NAMES,
69
+ type ServiceImageName,
70
+ } from "./stack-contract.js";
67
71
  import { StateLockHeldError } from "./state-store.js";
68
72
  import {
69
73
  type AppRollPlan,
70
74
  executeAppRoll,
71
75
  planAppRoll,
72
76
  type ReleaseImage,
73
- ROLL_ORDER,
74
77
  type RollSlug,
75
78
  } from "./upgrade/app-roll.js";
76
79
  import { restoreCommand, takeSqlBackup } from "./upgrade/backup.js";
@@ -699,7 +702,7 @@ export async function recordUpdateEnd(
699
702
  return false;
700
703
  }
701
704
 
702
- /** Every digest the revisions serving traffic are known by, per service. */
705
+ /** Every digest the revisions serving traffic are known by, per release image. */
703
706
  async function servingDigests(
704
707
  deps: UpgraderJobDeps,
705
708
  provider: InstallMetadata["provider"],
@@ -708,16 +711,18 @@ async function servingDigests(
708
711
  ok: true;
709
712
  digests: ServingDigests;
710
713
  /** The services' deployed names, for the template hints. */
711
- services: Readonly<Record<RollSlug, string>>;
714
+ services: Readonly<Record<ServiceImageName, string>>;
712
715
  }
713
716
  | { ok: false; error: string }
714
717
  > {
715
- const digests: Partial<Record<RollSlug, readonly string[]>> = {};
716
- const services: Partial<Record<RollSlug, string>> = {};
718
+ const digests: Partial<Record<ServiceImageName, readonly string[]>> = {};
719
+ const services: Partial<Record<ServiceImageName, string>> = {};
717
720
  if (provider !== "gcp" && provider !== "gcp-firebase") {
718
721
  return { ok: false, error: `the upgrader runs on Google Cloud installs only, not ${provider}` };
719
722
  }
720
- for (const slug of ROLL_ORDER) {
723
+ // The staging gateway (#1330) runs the gateway's image and rolls with
724
+ // it; the images, one service each, name the release.
725
+ for (const slug of SERVICE_IMAGE_NAMES) {
721
726
  // #1328 — which service plays which role differs per provider.
722
727
  const prefix = cloudRunServicePrefix(provider, deps.env, slug);
723
728
  const service = await resolveGcpResourceName(
@@ -735,10 +740,39 @@ async function servingDigests(
735
740
  digests[slug] = servingImageDigests(image);
736
741
  services[slug] = service.name;
737
742
  }
743
+ // #1330 — the staging gateway must serve the very image the gateway
744
+ // serves: it rolls with it, and an install whose staging gateway runs
745
+ // another release is not on one release (the upgrader runs only on
746
+ // installs converged to contract 20, which have it).
747
+ const stagingPrefix = cloudRunServicePrefix(provider, deps.env, "staging-gateway");
748
+ const staging = await resolveGcpResourceName(
749
+ deps.run,
750
+ "run-service",
751
+ stagingPrefix,
752
+ deps.projectId,
753
+ deps.region,
754
+ );
755
+ if (staging.status !== "found") {
756
+ return {
757
+ ok: false,
758
+ error: lookupFailure(staging, `${stagingPrefix}* Cloud Run service`, "job"),
759
+ };
760
+ }
761
+ const stagingImage = await findServingImage(deps.run, deps.projectId, deps.region, staging.name);
762
+ if (!stagingImage) {
763
+ return { ok: false, error: "could not read which image the staging gateway serves" };
764
+ }
765
+ const stagingDigests = servingImageDigests(stagingImage);
766
+ if (!stagingDigests.some((d) => (digests.gateway ?? []).includes(d))) {
767
+ return {
768
+ ok: false,
769
+ error: `the staging gateway (${staging.name}) serves ${stagingDigests[0] ?? "an unknown image"}, but the gateway serves ${digests.gateway?.[0] ?? "an unknown image"}: the install does not run one release. Nothing was changed; an upgrade from Cloud Shell rolls both onto one release.`,
770
+ };
771
+ }
738
772
  return {
739
773
  ok: true,
740
774
  digests: digests as ServingDigests,
741
- services: services as Record<RollSlug, string>,
775
+ services: services as Record<ServiceImageName, string>,
742
776
  };
743
777
  }
744
778
 
@@ -749,10 +783,10 @@ async function servingDigests(
749
783
  */
750
784
  async function templateDigests(
751
785
  deps: UpgraderJobDeps,
752
- services: Readonly<Record<RollSlug, string>>,
786
+ services: Readonly<Record<ServiceImageName, string>>,
753
787
  ): Promise<ServingDigests> {
754
- const out: Partial<Record<RollSlug, readonly string[]>> = {};
755
- for (const slug of ROLL_ORDER) {
788
+ const out: Partial<Record<ServiceImageName, readonly string[]>> = {};
789
+ for (const slug of SERVICE_IMAGE_NAMES) {
756
790
  const d = await findTemplateImageDigest(deps.run, deps.projectId, deps.region, services[slug]);
757
791
  out[slug] = d ? [d] : [];
758
792
  }
@@ -30,16 +30,16 @@
30
30
  import type { GcloudRunner } from "./gcloud-retry.js";
31
31
  import type { ImageDigests, InstallMetadata } from "./install-state.js";
32
32
  import type { ReleaseManifest, ReleaseManifestResult } from "./release-manifest.js";
33
- import { ROLL_ORDER, type RollSlug } from "./upgrade/app-roll.js";
33
+ import { SERVICE_IMAGE_NAMES, type ServiceImageName } from "./stack-contract.js";
34
34
  import type { ReleaseImageForms } from "./upgrade/image-forms.js";
35
35
  import { releaseVersionForDigest } from "./upgrade/release-images.js";
36
36
 
37
37
  /** Every digest each serving image is known by (gcp-lookup.ts servingImageDigests). */
38
- export type ServingDigests = Readonly<Record<RollSlug, readonly string[]>>;
38
+ export type ServingDigests = Readonly<Record<ServiceImageName, readonly string[]>>;
39
39
 
40
40
  // #1328 — the MCP-Auth-Proxy serves on every install this code runs on (the
41
41
  // upgrader runs only on installs converged to its contract).
42
- const SLUGS: readonly RollSlug[] = ROLL_ORDER;
42
+ const SLUGS: readonly ServiceImageName[] = SERVICE_IMAGE_NAMES;
43
43
 
44
44
  /**
45
45
  * Whether install.json names the images that serve: per service, the
@@ -176,7 +176,7 @@ export async function findServingRelease(
176
176
  type ReleaseJudgement =
177
177
  | {
178
178
  readonly kind: "judged";
179
- readonly matching: readonly RollSlug[];
179
+ readonly matching: readonly ServiceImageName[];
180
180
  readonly platformDigests: ImageDigests | null;
181
181
  }
182
182
  | { readonly kind: "unreadable"; readonly transient: boolean; readonly message: string };
@@ -191,8 +191,8 @@ async function judgeRelease(
191
191
  serving: ServingDigests,
192
192
  imageForms: ReleaseImageForms,
193
193
  ): Promise<ReleaseJudgement> {
194
- const matching: RollSlug[] = [];
195
- const platform: Partial<Record<RollSlug, string>> = {};
194
+ const matching: ServiceImageName[] = [];
195
+ const platform: Partial<Record<ServiceImageName, string>> = {};
196
196
  for (const slug of SLUGS) {
197
197
  const index = m.digests[slug];
198
198
  // #1328 — a release from before the proxy cannot be what serves here.
@@ -257,7 +257,7 @@ function capitalize(s: string): string {
257
257
  }
258
258
 
259
259
  /** `the admin`, `the gateway and the MCP-Auth-Proxy`, … for the refusal text. */
260
- function serviceList(slugs: readonly RollSlug[]): string {
260
+ function serviceList(slugs: readonly ServiceImageName[]): string {
261
261
  const names = slugs.map((s) => (s === "mcp-auth-proxy" ? "the MCP-Auth-Proxy" : `the ${s}`));
262
262
  if (names.length <= 1) return names[0] ?? "";
263
263
  return `${names.slice(0, -1).join(", ")} and ${names[names.length - 1]}`;
package/src/wizard.ts CHANGED
@@ -154,7 +154,7 @@ export async function runWizard(opts: WizardOptions = {}): Promise<void> {
154
154
  } else {
155
155
  provider = jobModeProvider ?? opts.provider ?? (await pickProvider());
156
156
  domain = opts.domain ?? (await promptDomain());
157
- const stagingDomain = await resolveStagingDomain(domain, provider, opts);
157
+ const stagingDomain = await resolveStagingDomain(domain, opts);
158
158
  ownerEmail = opts.ownerEmail ?? (await promptOwnerEmail());
159
159
  projectIdHint = jobModeProjectId ?? opts.projectId ?? null;
160
160
  installId = deriveInstallId(provider, projectIdHint ?? domain);
@@ -389,11 +389,10 @@ async function promptDomain(): Promise<string> {
389
389
  */
390
390
  async function resolveStagingDomain(
391
391
  domain: string,
392
- provider: Provider,
393
392
  opts: Pick<WizardOptions, "stagingDomain" | "nonInteractive">,
394
393
  ): Promise<string> {
395
394
  const reserved = { liveHosts: [domain, `www.${domain}`], adminHosts: [`admin.${domain}`] };
396
- const problemOf = (host: string) => stagingHostProblem(host, reserved, { provider });
395
+ const problemOf = (host: string) => stagingHostProblem(host, reserved);
397
396
  if (opts.stagingDomain) {
398
397
  const problem = problemOf(opts.stagingDomain);
399
398
  if (problem) {
@@ -36,6 +36,7 @@ import {
36
36
  type RemoteMcpIdentity,
37
37
  serializeRemoteMcpIdentity,
38
38
  } from "../stack-contract.js";
39
+ import { DEPLOY_GATEWAYS_CONFIG } from "../stack-gateways.js";
39
40
  import type { ResolvedBackend } from "../state-backend.js";
40
41
 
41
42
  /**
@@ -108,6 +109,12 @@ export interface PulumiUpInputs {
108
109
  * first `up`): the config keeps what an earlier run wrote.
109
110
  */
110
111
  remoteMcpIdentity?: RemoteMcpIdentity | null;
112
+ /**
113
+ * Whether the stack declares the public data gateway (stack-gateways.ts).
114
+ * False only for the first `up` of an install whose gateway has never
115
+ * started (gateway-bootstrap.ts): its migrations have not run yet.
116
+ */
117
+ deployGateways: boolean;
111
118
  }
112
119
 
113
120
  export interface PulumiUpResult {
@@ -469,6 +476,7 @@ export type StackConfigInputs = Pick<
469
476
  | "iapAllowlist"
470
477
  | "imageDigests"
471
478
  | "remoteMcpIdentity"
479
+ | "deployGateways"
472
480
  > & { provider: "gcp" | "gcp-firebase" };
473
481
 
474
482
  /**
@@ -504,6 +512,7 @@ export async function applyStackConfig(
504
512
  ? { [`${ns}:wafAdaptiveProtection`]: { value: String(inputs.wafAdaptiveProtection) } }
505
513
  : {}),
506
514
  [`${ns}:iapAllowlist`]: { value: inputs.iapAllowlist.join(",") },
515
+ [`${ns}:${DEPLOY_GATEWAYS_CONFIG}`]: { value: String(inputs.deployGateways) },
507
516
  // v0.3.2 — anthropicApiKey config dropped. Runtime path is
508
517
  // /settings/ai → ai_providers (KEK-encrypted).
509
518
  ...imageDigestConfig(provider, inputs.imageDigests),
@@ -50,6 +50,12 @@ import {
50
50
  import { formatDnsRecordGroups, groupDnsRecords, type InstallDnsRecord } from "../dns/groups.js";
51
51
  import { pickDnsAdapter } from "../dns/index.js";
52
52
  import type { DnsRecord } from "../dns/types.js";
53
+ import {
54
+ deleteNeverStartedGateways,
55
+ deployOntoMigratedDatabase,
56
+ type GatewayStart,
57
+ readGatewayStart,
58
+ } from "../gateway-bootstrap.js";
53
59
  import {
54
60
  activeAccount,
55
61
  type BillingAccount,
@@ -129,6 +135,10 @@ import {
129
135
  type RemoteMcpIdentity,
130
136
  sameRemoteMcpIdentity,
131
137
  } from "../stack-contract.js";
138
+ import {
139
+ ensureStagingGatewayEdge,
140
+ ensureStagingGatewayFromLiveGateway,
141
+ } from "../staging-gateway-setup.js";
132
142
  import {
133
143
  chooseInstallBackend,
134
144
  describeBackend,
@@ -413,7 +423,7 @@ export async function runGcpConverge(opts: GcpConvergeOpts): Promise<void> {
413
423
  // the live services, so this `up` keeps what they carry. A fresh install
414
424
  // has no services yet: the identity is resolved after the first `up` and
415
425
  // applied by a second one below.
416
- const identityBefore = deployed
426
+ let identityBefore = deployed
417
427
  ? await stepResolveRemoteMcpIdentity(projectId, region, provider, domain, "before")
418
428
  : null;
419
429
 
@@ -422,7 +432,7 @@ export async function runGcpConverge(opts: GcpConvergeOpts): Promise<void> {
422
432
  // (caelo-website/caelo-cms-images by default). Cloud Run reads them
423
433
  // directly with no operator-side IAM binding — anonymous public pull.
424
434
  phase("Provisioning the infrastructure (pulumi up, 8-15 min)");
425
- const upOpts: PulumiUpOpts = {
435
+ const upOpts: Omit<PulumiUpOpts, "deployGateways"> = {
426
436
  projectId,
427
437
  domain,
428
438
  // #650 — the wizard recorded it (or the install predates the setting).
@@ -445,8 +455,43 @@ export async function runGcpConverge(opts: GcpConvergeOpts): Promise<void> {
445
455
  provider,
446
456
  nonInteractive: opts.nonInteractive,
447
457
  };
448
- await stepPulumiUp(installId, { ...upOpts, remoteMcpIdentity: identityBefore?.identity ?? null });
449
- recordImageDigests(installId, imageDigests);
458
+ // An install whose gateway has never started (a fresh one, or one whose
459
+ // first run failed right here) is migrated between an `up` without the
460
+ // gateway and one with it: the gateway refuses to boot until the
461
+ // migrations isolated its database login (gateway-bootstrap.ts).
462
+ const gatewayStart = await stepReadGatewayStart(projectId, region, provider);
463
+ const { databasePrepared } = await deployOntoMigratedDatabase(gatewayStart, {
464
+ up: async (deployGateways) => {
465
+ if (!deployGateways) {
466
+ log.info(
467
+ "First pass without the public gateway: it can only start once the database migrations have run.",
468
+ );
469
+ }
470
+ await stepPulumiUp(installId, {
471
+ ...upOpts,
472
+ deployGateways,
473
+ remoteMcpIdentity: identityBefore?.identity ?? null,
474
+ });
475
+ // Recorded after every pass: a run stopped between them keeps this
476
+ // release on the next run, which then finds no gateway to read it from.
477
+ recordImageDigests(installId, imageDigests);
478
+ },
479
+ prepareDatabase: async () => {
480
+ await stepPublicRoleIsolation(projectId);
481
+ phase("Applying database migrations");
482
+ await stepMigrations(projectId, region);
483
+ // gcp-firebase: the admin and the MCP-Auth-Proxy exist now, so the
484
+ // gateway's `up` declares their identity instead of a third `up`. On
485
+ // gcp the identity lookup needs the gateway service itself; the read
486
+ // after that `up` (10.05) covers it.
487
+ if (provider === "gcp-firebase") {
488
+ identityBefore =
489
+ (await stepResolveRemoteMcpIdentity(projectId, region, provider, domain, "before")) ??
490
+ identityBefore;
491
+ }
492
+ phase("Provisioning the public gateway (pulumi up)");
493
+ },
494
+ });
450
495
 
451
496
  // === 10.05. The identity once the services exist (#1082) ===
452
497
  // Read again after every `up`: on a fresh install (or one whose identity
@@ -459,7 +504,11 @@ export async function runGcpConverge(opts: GcpConvergeOpts): Promise<void> {
459
504
  log.info(
460
505
  "Applying the remote-MCP URLs and IAP audience through the stack (second pulumi up)...",
461
506
  );
462
- await stepPulumiUp(installId, { ...upOpts, remoteMcpIdentity: identity.identity });
507
+ await stepPulumiUp(installId, {
508
+ ...upOpts,
509
+ deployGateways: true,
510
+ remoteMcpIdentity: identity.identity,
511
+ });
463
512
  }
464
513
 
465
514
  // === 10.1. Remove the default network's pre-populated firewall rules ===
@@ -502,25 +551,35 @@ export async function runGcpConverge(opts: GcpConvergeOpts): Promise<void> {
502
551
  // The stack creates public_role through the Cloud SQL Admin API, which
503
552
  // makes it a member of cloudsqlsuperuser. Revoked before migrations: the
504
553
  // migration job's role audit verifies it and fails the install otherwise.
505
- await stepPublicRoleIsolation(projectId);
554
+ // Already done before the gateway's `up` when it had never started.
555
+ if (!databasePrepared) await stepPublicRoleIsolation(projectId);
506
556
 
507
557
  // === 10.95. The admin may write redirects into the URL map (#587) ===
508
558
  // Confirm-publish writes the redirects table into the load balancer's URL
509
559
  // map; the custom role is the CLI's, not the stack's (url-map-redirects-access.ts).
510
560
  if (provider === "gcp") await stepUrlMapRedirectsAccess(projectId);
511
561
 
512
- phase("Applying database migrations");
513
- {
514
- const { runMigrationsViaCloudRunJob } = await import("../migration-runner.js");
515
- const r = await runMigrationsViaCloudRunJob({ projectId, region });
516
- if (!r.ok) {
517
- cancel(`Migrations failed: ${r.error}. Inspect the Cloud Run Job logs and re-run.`);
518
- process.exit(1);
519
- }
562
+ // A gateway that had never started got the migrations before its `up`,
563
+ // with the admin image this run deploys; the `up` since rolled nothing.
564
+ if (!databasePrepared) {
565
+ phase("Applying database migrations");
566
+ await stepMigrations(projectId, region);
520
567
  }
521
568
 
522
569
  phase("Finishing: operator access, welcome page");
523
570
 
571
+ // === 11.2. The staging gateway and its edge (#1330) ===
572
+ // Before the audit job, which reads the staging bucket this step creates
573
+ // on gcp-firebase.
574
+ const stagingReady = await stepStagingGateway(
575
+ projectId,
576
+ region,
577
+ provider,
578
+ domain,
579
+ installStagingHost(meta),
580
+ imageDigests,
581
+ );
582
+
524
583
  // === 11.3. The quality-audit job: parallel Lighthouse runs (#875) ===
525
584
  // Before the upgrader, which gets actAs on the job's service account.
526
585
  await stepAuditJob(projectId, region, provider);
@@ -573,10 +632,10 @@ export async function runGcpConverge(opts: GcpConvergeOpts): Promise<void> {
573
632
  // A fresh install ran every converge step above, so it is at this CLI's
574
633
  // infrastructure contract — recorded only when the upgrader and the
575
634
  // converge job are in place (the steps contract bumps 2 and 9 added) and
576
- // remote MCP's steps (#806, bump 10) and the scheduler tick (#1140, bump
577
- // 18) succeeded. With the release version, the admin can install the next
635
+ // remote MCP's steps (#806, bump 10), the scheduler tick (#1140, bump 18)
636
+ // and the staging gateway (#1330, bump 20) succeeded. With the release version, the admin can install the next
578
637
  // update itself.
579
- if (upgraderReady && convergeReady && remoteMcpReady && schedulerReady) {
638
+ if (upgraderReady && convergeReady && remoteMcpReady && schedulerReady && stagingReady) {
580
639
  const version = releaseVersion;
581
640
  // #1025 — the linux/amd64 forms Cloud Run reports, next to the indexes,
582
641
  // so the upgrader recognises what serves without a registry lookup.
@@ -1024,6 +1083,77 @@ async function stepGrantBaseRoles(
1024
1083
  * (cloudsqlsuperuser, #633). Idempotent, so it runs on every wizard pass;
1025
1084
  * the migration job's role audit verifies the result.
1026
1085
  */
1086
+ /**
1087
+ * Both migration targets through the one-shot Cloud Run job, with the admin
1088
+ * service's image (migration-runner.ts). Idempotent; aborts the run on a
1089
+ * failure rather than continuing on a half-migrated database.
1090
+ */
1091
+ async function stepMigrations(projectId: string, region: string): Promise<void> {
1092
+ const { runMigrationsViaCloudRunJob } = await import("../migration-runner.js");
1093
+ const r = await runMigrationsViaCloudRunJob({ projectId, region });
1094
+ if (!r.ok) {
1095
+ cancel(`Migrations failed: ${r.error}. Inspect the Cloud Run Job logs and re-run.`);
1096
+ process.exit(1);
1097
+ }
1098
+ }
1099
+
1100
+ /**
1101
+ * Whether the install's data gateway has ever started (gateway-bootstrap.ts),
1102
+ * which decides whether the migrations run before its `pulumi up`. Aborts on
1103
+ * a failed read: guessing "started" would repeat the failed deploy, guessing
1104
+ * "never started" would withhold a gateway that serves visitors.
1105
+ */
1106
+ async function stepReadGatewayStart(
1107
+ projectId: string,
1108
+ region: string,
1109
+ provider: "gcp" | "gcp-firebase",
1110
+ ): Promise<GatewayStart> {
1111
+ const r = await readGatewayStart({ provider, projectId, region }, gcloud);
1112
+ if (!r.ok) {
1113
+ cancel(
1114
+ `Could not tell whether the public gateway is deployed: ${r.error}. Re-run the installer.`,
1115
+ );
1116
+ process.exit(1);
1117
+ }
1118
+ return r.start;
1119
+ }
1120
+
1121
+ /**
1122
+ * Under the install's state lock, before an `up` that withholds the
1123
+ * gateway: read its state again — another run may have deployed it since
1124
+ * this run decided, and withholding it now would delete a serving gateway —
1125
+ * and delete the services a failed first run left, which never started
1126
+ * (deleteNeverStartedGateways: Pulumi cannot when `deletionProtection` is
1127
+ * on).
1128
+ *
1129
+ * @returns whether this `up` declares the gateway after all
1130
+ */
1131
+ async function withholdGatewayUnderLock(
1132
+ projectId: string,
1133
+ region: string,
1134
+ provider: "gcp" | "gcp-firebase",
1135
+ ): Promise<{ ok: true; deployGateways: boolean } | { ok: false; error: string }> {
1136
+ const target = { provider, projectId, region };
1137
+ const now = await readGatewayStart(target, gcloud);
1138
+ if (!now.ok) {
1139
+ return {
1140
+ ok: false,
1141
+ error: `Could not tell whether the public gateway is deployed: ${now.error}. Re-run the installer.`,
1142
+ };
1143
+ }
1144
+ if (now.start === "started") {
1145
+ log.info("Another run has deployed the public gateway meanwhile; this pass keeps it.");
1146
+ return { ok: true, deployGateways: true };
1147
+ }
1148
+ if (now.neverStarted.length === 0) return { ok: true, deployGateways: false };
1149
+ const removed = await deleteNeverStartedGateways(target, now.neverStarted, gcloud);
1150
+ if (!removed.ok) return { ok: false, error: `${removed.error}.` };
1151
+ log.info(
1152
+ `Removed the gateway service a failed run left behind, which never started: ${removed.deleted.join(", ")}.`,
1153
+ );
1154
+ return { ok: true, deployGateways: false };
1155
+ }
1156
+
1027
1157
  async function stepPublicRoleIsolation(projectId: string): Promise<void> {
1028
1158
  const s = spinner();
1029
1159
  s.start("Removing the gateway logins from the Cloud SQL superuser group...");
@@ -1465,6 +1595,49 @@ async function stepRemoteMcp(
1465
1595
  return true;
1466
1596
  }
1467
1597
 
1598
+ /**
1599
+ * #1330 — the staging gateway (the live gateway's image, gate on, serving the
1600
+ * staged build from the staging bucket) and the route of the staging host to
1601
+ * it (staging-gateway-setup.ts). The site works without it, so a failure
1602
+ * warns and `upgrade` retries. Prints the DNS records the staging host needs.
1603
+ *
1604
+ * @returns whether both parts are in place
1605
+ */
1606
+ async function stepStagingGateway(
1607
+ projectId: string,
1608
+ region: string,
1609
+ provider: "gcp" | "gcp-firebase",
1610
+ domain: string,
1611
+ stagingHost: string,
1612
+ imageDigests: ImageDigests,
1613
+ ): Promise<boolean> {
1614
+ const s = spinner();
1615
+ s.start(`Setting up the staging gateway behind the staging gate (${stagingHost})...`);
1616
+ const failed = (error: string) => {
1617
+ s.stop(yellow(`Staging gateway not set up: ${error}`));
1618
+ log.warn(
1619
+ `The site works; ${stagingHost} answers nothing until ${bold(selfCommand("upgrade"))} sets it up.`,
1620
+ );
1621
+ return false;
1622
+ };
1623
+ const target = { provider, projectId, region, env: GCP_STACK_ENV, domain, stagingHost };
1624
+ const service = await ensureStagingGatewayFromLiveGateway({
1625
+ ...target,
1626
+ gatewayImageRef: releaseImageRef("gateway", imageDigests.gateway),
1627
+ });
1628
+ const broken = service.find((o) => o.status === "failed");
1629
+ if (broken) return failed(`${broken.id}: ${broken.error ?? ""}`);
1630
+ const edge = await ensureStagingGatewayEdge(target);
1631
+ if (!edge.ok) return failed(edge.error);
1632
+ s.stop(green(`Staging gateway ready: ${stagingHost} is served behind the staging gate`));
1633
+ for (const line of edge.done) log.info(` ${line}`);
1634
+ if (edge.dnsRecords.length > 0) {
1635
+ log.info(` DNS for ${stagingHost} (Settings → Updates shows the same):`);
1636
+ for (const d of edge.dnsRecords) log.info(` ${d.name} ${d.type} ${d.value}`);
1637
+ }
1638
+ return true;
1639
+ }
1640
+
1468
1641
  /**
1469
1642
  * Delete the firewall rules GCP pre-populates on the `default` network
1470
1643
  * (SSH, RDP, ICMP, internal; default-firewall.ts). A customised rule of the
@@ -1721,6 +1894,8 @@ interface PulumiUpOpts {
1721
1894
  nonInteractive: boolean;
1722
1895
  /** #1082 — the remote-MCP identity the stack declares (gcp-pulumi.ts PulumiUpInputs). */
1723
1896
  remoteMcpIdentity?: RemoteMcpIdentity | null;
1897
+ /** Whether the stack declares the public gateway (gateway-bootstrap.ts). */
1898
+ deployGateways: boolean;
1724
1899
  }
1725
1900
 
1726
1901
  /**
@@ -1817,6 +1992,21 @@ async function stepPulumiUp(installId: string, opts: PulumiUpOpts): Promise<void
1817
1992
  );
1818
1993
  process.exit(1);
1819
1994
  }
1995
+ // Decided before the lock: under it, look again (gateway-bootstrap.ts).
1996
+ let deployGateways = opts.deployGateways;
1997
+ if (!deployGateways) {
1998
+ const withheld = await withholdGatewayUnderLock(
1999
+ opts.projectId,
2000
+ opts.region,
2001
+ opts.provider ?? "gcp",
2002
+ );
2003
+ if (!withheld.ok) {
2004
+ releaseLock();
2005
+ cancel(withheld.error);
2006
+ process.exit(1);
2007
+ }
2008
+ deployGateways = withheld.deployGateways;
2009
+ }
1820
2010
  log.info(`Pulumi up — wall-clock 8–15 min (Cloud SQL is the long pole). Streaming progress...`);
1821
2011
 
1822
2012
  let resourceCount = 0;
@@ -1829,6 +2019,7 @@ async function stepPulumiUp(installId: string, opts: PulumiUpOpts): Promise<void
1829
2019
  installRoot: root,
1830
2020
  secretsDir,
1831
2021
  ...opts,
2022
+ deployGateways,
1832
2023
  // A deployed install's stack must exist: a missing one means the
1833
2024
  // state is elsewhere, and a fresh stack would try to recreate it all.
1834
2025
  stackMustExist: deployed,
@@ -2436,14 +2627,19 @@ async function stepFinalize(installId: string, setupLink: "print" | "secret"): P
2436
2627
  ` To bind ${cyan(`admin.${meta.domain}`)}, run`,
2437
2628
  ` ${dim(selfCommand("admin-domain", "enable"))}`,
2438
2629
  ` (it walks you through the one-time Search Console verification, prints the DNS record and waits for the certificate).`,
2439
- "",
2440
- bold("Protected staging site:"),
2441
- ` Stage builds render ${cyan(installStagingHost(meta))}; to serve it, run`,
2442
- ` ${dim(selfCommand("staging-domain", "enable"))}`,
2443
- ` It maps the host, turns on password protection and prints the staging login once;`,
2444
- ` add the DNS record it prints. The host serves pages after the first Stage.`,
2445
2630
  ]
2446
2631
  : [];
2632
+ // #1330 — staging is served by its own gateway on every GCP install; the
2633
+ // records it needs were printed by the staging gateway step above.
2634
+ adminNote.push(
2635
+ "",
2636
+ bold("Protected staging site:"),
2637
+ ` ${cyan(installStagingHost(meta))} is served by the staging gateway behind the staging gate,`,
2638
+ " once the DNS records printed above (also under Settings → Updates) point at it.",
2639
+ ` To turn on password protection and print the staging login once, run`,
2640
+ ` ${dim(selfCommand("staging-domain", "enable"))}`,
2641
+ " The host serves pages after the first Stage.",
2642
+ );
2447
2643
 
2448
2644
  note(
2449
2645
  [
@@ -100,3 +100,7 @@ config:
100
100
  # Optional config NOT declared above:
101
101
  # caelo-gcp:image-admin / image-gateway — image tags for Cloud Run services.
102
102
  # Default `<region>-docker.pkg.dev/<project>/caelo/<service>:latest`.
103
+ # caelo-gcp:deployGateways — "false" only on the installer's first `up` of an
104
+ # install whose gateway has never started: the stack then declares no
105
+ # gateway, URL map or HTTPS proxy until the migrations have isolated the
106
+ # gateway's database login (src/gateway-bootstrap.ts). Absent = "true".