@fabrika/provider-zerops 0.9.0-dev.1 → 0.9.0-dev.10

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 (174) hide show
  1. package/package.json +17 -14
  2. package/sandbox/Dockerfile +16 -0
  3. package/sandbox/bin/postgres-auth.sh +38 -0
  4. package/sandbox/bin/reset.sh +12 -0
  5. package/sandbox/bin/run-build.sh +35 -0
  6. package/sandbox/bin/run-launcher.sh +18 -0
  7. package/sandbox/bin/start-launcher.sh +25 -0
  8. package/sandbox/bin/stop.sh +47 -0
  9. package/sandbox/init.sh +61 -0
  10. package/src/api/client.ts +15 -1
  11. package/src/api/errors.ts +7 -3
  12. package/src/api/readers.ts +29 -0
  13. package/src/api/types.ts +52 -0
  14. package/src/api.ts +2 -0
  15. package/src/cli.ts +11 -0
  16. package/src/collaborators.ts +0 -14
  17. package/src/compile.ts +1 -1
  18. package/src/control.ts +159 -92
  19. package/src/execution.ts +45 -195
  20. package/src/index.ts +2 -0
  21. package/src/installation/admin.ts +1 -1
  22. package/src/installation/deploy-options.ts +1 -2
  23. package/src/installation/deploy.ts +225 -80
  24. package/src/installation/generated/platform-console-schema.ts +3 -0
  25. package/src/installation/generated/platform-proxy-manifest.ts +1 -1
  26. package/src/installation/guide.md +48 -18
  27. package/src/installation/hosts.ts +1 -7
  28. package/src/installation/index.ts +13 -16
  29. package/src/installation/init-options.ts +2 -1
  30. package/src/installation/install-environment.ts +47 -108
  31. package/src/installation/install-options.ts +3 -1
  32. package/src/installation/install-secrets.ts +24 -68
  33. package/src/installation/install.ts +9 -4
  34. package/src/installation/sidecar.ts +5 -26
  35. package/src/installation/topology/setups.ts +17 -4
  36. package/src/installation/topology/topology.ts +32 -22
  37. package/src/installation/upgrade-options.ts +2 -2
  38. package/src/managed-env.ts +1 -174
  39. package/src/manifest.ts +4 -16
  40. package/src/namespace/claims.ts +60 -1
  41. package/src/namespace/codec.ts +17 -0
  42. package/src/namespace/errors.ts +22 -1
  43. package/src/namespace/manifest.ts +36 -23
  44. package/src/namespace/provisioning.ts +88 -114
  45. package/src/namespace/proxy-attempt.ts +14 -2
  46. package/src/namespace/proxy-reads.ts +28 -23
  47. package/src/namespace/proxy-secrets.ts +18 -5
  48. package/src/namespace/sandbox-host.ts +212 -0
  49. package/src/namespace/subdomain.ts +113 -0
  50. package/src/namespace/topology.ts +1 -1
  51. package/src/namespace/types.ts +13 -0
  52. package/src/namespace/update.ts +1 -0
  53. package/src/namespace-cli.ts +152 -0
  54. package/src/plan.ts +1 -6
  55. package/src/provider.ts +88 -166
  56. package/src/proxy-manifest-carrier.ts +39 -0
  57. package/src/sandbox/base-image.ts +100 -0
  58. package/src/sandbox/constants.ts +44 -0
  59. package/src/sandbox/deploy.ts +471 -0
  60. package/src/sandbox/dev-artifact.ts +206 -0
  61. package/src/sandbox/lifecycle.ts +204 -0
  62. package/src/sandbox/scripts.ts +26 -0
  63. package/src/sandbox-host.ts +28 -0
  64. package/src/source/port.ts +5 -0
  65. package/src/types.ts +0 -2
  66. package/src/zbox/client.ts +259 -0
  67. package/src/zbox/errors.ts +45 -0
  68. package/src/zbox/index.ts +22 -0
  69. package/src/zbox/types.ts +165 -0
  70. package/types/src/api/client.d.ts +5 -0
  71. package/types/src/api/errors.d.ts +26 -0
  72. package/types/src/api/polling.d.ts +41 -0
  73. package/types/src/api/readers.d.ts +1115 -0
  74. package/types/src/api/service-env-limit.d.ts +17 -0
  75. package/types/src/api/service-env-response.d.ts +23 -0
  76. package/types/src/api/types.d.ts +687 -0
  77. package/types/src/api.d.ts +6 -0
  78. package/types/src/authoring.d.ts +9 -0
  79. package/types/src/cli-args.d.ts +19 -0
  80. package/types/src/cli.d.ts +13 -0
  81. package/types/src/collaborators.d.ts +16 -0
  82. package/types/src/compile.d.ts +91 -0
  83. package/types/src/control.d.ts +51 -0
  84. package/types/src/database-budget.d.ts +38 -0
  85. package/types/src/descriptor.d.ts +28 -0
  86. package/types/src/execution.d.ts +114 -0
  87. package/types/src/exposure.d.ts +47 -0
  88. package/types/src/index.d.ts +31 -0
  89. package/types/src/installation/admin-options.d.ts +24 -0
  90. package/types/src/installation/admin.d.ts +35 -0
  91. package/types/src/installation/checker-release.d.ts +12 -0
  92. package/types/src/installation/deploy-options.d.ts +37 -0
  93. package/types/src/installation/deploy.d.ts +136 -0
  94. package/types/src/installation/generated/platform-console-schema.d.ts +2 -0
  95. package/types/src/installation/generated/platform-proxy-manifest.d.ts +2 -0
  96. package/types/src/installation/hosts.d.ts +34 -0
  97. package/types/src/installation/index.d.ts +7 -0
  98. package/types/src/installation/init-options.d.ts +38 -0
  99. package/types/src/installation/init.d.ts +60 -0
  100. package/types/src/installation/install-collaborators.d.ts +24 -0
  101. package/types/src/installation/install-environment.d.ts +63 -0
  102. package/types/src/installation/install-options.d.ts +49 -0
  103. package/types/src/installation/install-project.d.ts +13 -0
  104. package/types/src/installation/install-provisioning.d.ts +58 -0
  105. package/types/src/installation/install-secrets.d.ts +17 -0
  106. package/types/src/installation/install-token.d.ts +10 -0
  107. package/types/src/installation/install.d.ts +23 -0
  108. package/types/src/installation/installation-source.d.ts +8 -0
  109. package/types/src/installation/log.d.ts +44 -0
  110. package/types/src/installation/machine-key.d.ts +19 -0
  111. package/types/src/installation/placement-options.d.ts +11 -0
  112. package/types/src/installation/platform-archive.d.ts +7 -0
  113. package/types/src/installation/platform-source.d.ts +8 -0
  114. package/types/src/installation/platform-upload.d.ts +3 -0
  115. package/types/src/installation/sidecar.d.ts +49 -0
  116. package/types/src/installation/source-options.d.ts +1 -0
  117. package/types/src/installation/topology/artifacts.d.ts +8 -0
  118. package/types/src/installation/topology/index.d.ts +5 -0
  119. package/types/src/installation/topology/installation-schemas.d.ts +2242 -0
  120. package/types/src/installation/topology/invariants.d.ts +23 -0
  121. package/types/src/installation/topology/setups.d.ts +59 -0
  122. package/types/src/installation/topology/topology.d.ts +153 -0
  123. package/types/src/installation/topology/validate.d.ts +11 -0
  124. package/types/src/installation/upgrade-options.d.ts +26 -0
  125. package/types/src/installation/upgrade-output.d.ts +6 -0
  126. package/types/src/installation/upgrade.d.ts +88 -0
  127. package/types/src/managed-env.d.ts +24 -0
  128. package/types/src/manifest.d.ts +199 -0
  129. package/types/src/namespace/claims.d.ts +16 -0
  130. package/types/src/namespace/codec.d.ts +352 -0
  131. package/types/src/namespace/constants.d.ts +25 -0
  132. package/types/src/namespace/errors.d.ts +25 -0
  133. package/types/src/namespace/manifest.d.ts +4 -0
  134. package/types/src/namespace/operator.d.ts +12 -0
  135. package/types/src/namespace/provisioning.d.ts +4 -0
  136. package/types/src/namespace/proxy-attempt.d.ts +39 -0
  137. package/types/src/namespace/proxy-reads.d.ts +27 -0
  138. package/types/src/namespace/proxy-secrets.d.ts +8 -0
  139. package/types/src/namespace/proxy-source.d.ts +74 -0
  140. package/types/src/namespace/sandbox-host.d.ts +23 -0
  141. package/types/src/namespace/subdomain.d.ts +43 -0
  142. package/types/src/namespace/topology.d.ts +9 -0
  143. package/types/src/namespace/types.d.ts +111 -0
  144. package/types/src/namespace/update.d.ts +6 -0
  145. package/types/src/namespace/validation.d.ts +12 -0
  146. package/types/src/namespace-cli.d.ts +2 -0
  147. package/types/src/namespace.d.ts +9 -0
  148. package/types/src/plan.d.ts +11 -0
  149. package/types/src/provider.d.ts +60 -0
  150. package/types/src/proxy-manifest-carrier.d.ts +37 -0
  151. package/types/src/sandbox/base-image.d.ts +28 -0
  152. package/types/src/sandbox/constants.d.ts +30 -0
  153. package/types/src/sandbox/deploy.d.ts +75 -0
  154. package/types/src/sandbox/dev-artifact.d.ts +51 -0
  155. package/types/src/sandbox/lifecycle.d.ts +27 -0
  156. package/types/src/sandbox/scripts.d.ts +11 -0
  157. package/types/src/sandbox-host.d.ts +16 -0
  158. package/types/src/schema.generated.d.ts +376 -0
  159. package/types/src/service-names.d.ts +4 -0
  160. package/types/src/service-references.d.ts +13 -0
  161. package/types/src/source/constants.d.ts +6 -0
  162. package/types/src/source/port.d.ts +65 -0
  163. package/types/src/source/types.d.ts +4 -0
  164. package/types/src/source/validation.d.ts +19 -0
  165. package/types/src/source.d.ts +2 -0
  166. package/types/src/types.d.ts +119 -0
  167. package/types/src/version-attempt.d.ts +8 -0
  168. package/types/src/zbox/client.d.ts +2 -0
  169. package/types/src/zbox/errors.d.ts +25 -0
  170. package/types/src/zbox/index.d.ts +3 -0
  171. package/types/src/zbox/types.d.ts +433 -0
  172. package/src/installation/mcp.ts +0 -102
  173. package/src/installation/proxy-manifest.ts +0 -214
  174. package/src/installation/secrets.ts +0 -78
package/src/provider.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  // The Zerops provider owns its plan and every effect needed to execute it.
2
2
  //
3
3
  // A Zerops deploy is HTTP and nothing else (ADR-0003) — no container, no shell, no filesystem — so this
4
- // driver's whole side-effect surface is one REST client plus the portable schema reconciler
5
- // (`./collaborators.ts`). It never spawns a process and has no `runCommand` to be handed.
4
+ // driver's whole side-effect surface is one REST client (`./collaborators.ts`). It never spawns a process and has
5
+ // no `runCommand` to be handed.
6
6
  //
7
7
  // The interesting step is `await-version`: fabrika does not RUN the build, it WATCHES one the platform
8
8
  // runs. Recovery never resumes a deploy (ADR-0044): a version reserved but never triggered is deleted, and
9
9
  // one whose build may have been requested is settled only by polling it.
10
10
 
11
- import { appEnvironmentId, FABRIKA_SOURCE_MAP_UPLOAD_KEY } from '@fabrika/auth-core'
11
+ import { FABRIKA_SOURCE_MAP_UPLOAD_KEY, managedValue, staleManagedVariables } from '@fabrika/auth-core'
12
12
  import { type OperationsSourceMapUploadState, readSourceMapUploadReport } from '@fabrika/operations-contract/releases'
13
13
  import {
14
14
  type DeployStepResult,
@@ -50,7 +50,6 @@ import {
50
50
  import {
51
51
  PLATFORM_TYPESCRIPT_VERSION,
52
52
  renderLockedExecutionDescriptor,
53
- type ZeropsDevExecution,
54
53
  zeropsJobProcesses,
55
54
  type ZeropsLockedExecution,
56
55
  zeropsStartMinRam,
@@ -66,20 +65,9 @@ import {
66
65
  type StorageProvenanceMarker,
67
66
  unprovenBucketReason,
68
67
  } from './exposure'
69
- import {
70
- assertAuthoredManagedDocument,
71
- assertAuthoredManagedInputs,
72
- isFrameworkFamilyVariable,
73
- isSensitiveFamilyVariable,
74
- type ManagedValues,
75
- overlayRelease,
76
- parseManagedEnvironment,
77
- SENSITIVE_MANAGED_KEYS,
78
- SERVICE_MANAGED_KEYS,
79
- } from './managed-env'
68
+ import { assertAuthoredManagedDocument, assertAuthoredManagedInputs, overlayRelease } from './managed-env'
80
69
  import {
81
70
  applicationDatabaseOf,
82
- approvedSchemaOf,
83
71
  type FabrikaImportDocument,
84
72
  type FabrikaManifest,
85
73
  lockedExecutionFor,
@@ -97,8 +85,6 @@ export const CANCELLED = 'deploy cancelled'
97
85
  export interface ZeropsDeployRun {
98
86
  readonly appId: string
99
87
  readonly env: string
100
- /** Every origin IAM may hand this app a session at; app-wide, because IAM's registry is keyed by app id. */
101
- readonly returnOrigins?: readonly string[]
102
88
  readonly secrets: Readonly<Record<string, string>>
103
89
  readonly vars: Readonly<Record<string, string>>
104
90
  /** Platform-owned runtime configuration, separate from operator-managed application vars. */
@@ -109,8 +95,6 @@ export interface ZeropsDeployRun {
109
95
  readonly access?: ProviderApprovedAccess
110
96
  /** The run-scoped source-map destination Control minted; the build uploads to it. */
111
97
  readonly artifactUpload?: ProviderDeployInput['artifactUpload']
112
- /** Present for an environment of kind `dev`: the launcher starts instead of the serving entry, on one container. */
113
- readonly dev?: ZeropsDevExecution
114
98
  readonly dryRun: boolean
115
99
  readonly signal: AbortSignal
116
100
  readonly events: ProviderRunEvents
@@ -335,9 +319,8 @@ interface StepEnv {
335
319
  signal: AbortSignal
336
320
  dryRun: boolean
337
321
  attempt: Attempt
338
- managedValues: ManagedValues
339
- /** The key of each job and webhook of the approved contract, by its variable (ADR-0022 "Principal kinds"). */
340
- familyVariables: Readonly<Record<string, string>>
322
+ /** What Control handed (`parseManagedEnvironment`). */
323
+ managed: ProviderManagedEnvironment
341
324
  /** `await-version` gives up at this instant. */
342
325
  deadlineMs: number
343
326
  }
@@ -580,9 +563,9 @@ const verifyExposure = async (env: StepEnv): Promise<void> => {
580
563
  }
581
564
  }
582
565
  if (findings.length > 0) {
583
- const dsn = env.managedValues.FABRIKA_OPERATIONS_DSN
566
+ const dsn = managedValue(env.managed, 'FABRIKA_OPERATIONS_DSN')
584
567
  if (dsn !== null) {
585
- const alert = { appId: run.appId, env: run.env, release: env.managedValues.FABRIKA_RELEASE, findings }
568
+ const alert = { appId: run.appId, env: run.env, release: managedValue(env.managed, 'FABRIKA_RELEASE'), findings }
586
569
  await zerops.alertExposure({ dsn, alert, signal }).then(
587
570
  () => log(' raised an Operations alert'),
588
571
  () => log(' [warn] the Operations alert could not be sent'),
@@ -598,86 +581,101 @@ const verifyExposure = async (env: StepEnv): Promise<void> => {
598
581
  )
599
582
  }
600
583
 
601
- /** Write the eleven service-level managed keys; `FABRIKA_RELEASE` is never a service variable (ADR-0044). */
584
+ /** Write the managed variables Control handed; `FABRIKA_RELEASE` is never a service variable (ADR-0044). */
602
585
  const writeServiceEnv = async (env: StepEnv): Promise<void> => {
603
- const { zerops, log, signal, managedValues, familyVariables } = env
586
+ const { zerops, log, signal, managed } = env
604
587
  const serviceId = env.run.target.serviceId
588
+ const written = Object.entries(managed).flatMap(([key, { value, credential }]) =>
589
+ key === 'FABRIKA_RELEASE' || value === null ? [] : [{ key, value, credential }]
590
+ )
591
+ const writtenKeys = new Set(written.map(({ key }) => key))
605
592
  if (env.dryRun) {
606
593
  const upload = env.run.artifactUpload !== undefined
607
- const written = [
608
- ...SERVICE_MANAGED_KEYS.filter((key) => managedValues[key] !== null),
609
- ...Object.keys(familyVariables),
610
- ...(upload ? [FABRIKA_SOURCE_MAP_UPLOAD_KEY] : []),
611
- ]
612
- const removed = [...SERVICE_MANAGED_KEYS.filter((key) => managedValues[key] === null), ...(upload ? [] : [FABRIKA_SOURCE_MAP_UPLOAD_KEY])]
613
- log(` [dry-run] would write service variables on ${serviceId}: ${written.join(', ') || '(none)'}`)
614
- log(` [dry-run] would remove service variables on ${serviceId} if present: ${removed.join(', ') || '(none)'}`)
615
- if (Object.keys(familyVariables).length > 0) {
616
- log(` [dry-run] would remove every other job, webhook, connector and mail variable on ${serviceId}`)
617
- }
594
+ const removed = Object.keys(managed).filter((key) => key !== 'FABRIKA_RELEASE' && !writtenKeys.has(key))
595
+ log(
596
+ ` [dry-run] would write service variables on ${serviceId}: ${
597
+ [...writtenKeys, ...(upload ? [FABRIKA_SOURCE_MAP_UPLOAD_KEY] : [])].join(', ') || '(none)'
598
+ }`,
599
+ )
600
+ log(
601
+ ` [dry-run] would remove service variables on ${serviceId} if present: ${
602
+ [...removed, ...(upload ? [] : [FABRIKA_SOURCE_MAP_UPLOAD_KEY])].join(', ') || '(none)'
603
+ }`,
604
+ )
605
+ log(` [dry-run] would remove every other job, webhook, connector, mail and platform variable on ${serviceId}`)
618
606
  return
619
607
  }
620
608
  assertRunning(signal)
621
609
  // One read: every write below waits for its own user-data sync, so the build request never meets `userDataSyncRunning`.
622
610
  const records = await zerops.api.listServiceEnv({ serviceId, signal })
623
- for (const key of SERVICE_MANAGED_KEYS) {
624
- const value = managedValues[key]
625
- const existing = records.find((record) => record.key === key)
626
- const sensitive = SENSITIVE_MANAGED_KEYS.has(key)
627
- assertRunning(signal)
628
- if (value !== null && (sensitive || readableContent(existing) !== value)) {
629
- await zerops.api.putServiceEnv({ serviceId, key, value, sensitive, signal })
630
- log(` ${key}: updated`)
631
- } else if (value === null && existing !== undefined) {
632
- await zerops.api.deleteServiceEnv({ envId: existing.id, signal })
633
- log(` ${key}: removed`)
634
- } else {
635
- log(` ${key}: unchanged`)
636
- }
611
+ const changes: ServiceEnvChange[] = []
612
+ const put = (key: string, value: string, sensitive: boolean): void => {
613
+ changes.push({ line: `${key}: updated`, apply: () => zerops.api.putServiceEnv({ serviceId, key, value, sensitive, signal }) })
637
614
  }
638
- const provenance = env.attempt.storageProvenance
639
- const marker = records.find((record) => record.key === STORAGE_PROVENANCE_KEY)
640
- if (typeof provenance === 'string' && readableContent(marker) !== provenance) {
641
- assertRunning(signal)
642
- // Not a credential, and the exposure read-back above reads it: it must stay readable.
643
- await zerops.api.putServiceEnv({ serviceId, key: STORAGE_PROVENANCE_KEY, value: provenance, sensitive: false, signal })
644
- log(` ${STORAGE_PROVENANCE_KEY}: updated`)
645
- } else if (provenance === null && marker !== undefined) {
646
- assertRunning(signal)
647
- await zerops.api.deleteServiceEnv({ envId: marker.id, signal })
648
- log(` ${STORAGE_PROVENANCE_KEY}: removed`)
615
+ const remove = (key: string, envId: string): void => {
616
+ changes.push({ line: `${key}: removed`, apply: () => zerops.api.deleteServiceEnv({ envId, signal }) })
649
617
  }
650
- // A workload key is written like a managed key; one whose workload is gone is removed, so no stale key stays readable.
651
- for (const [key, value] of Object.entries(familyVariables)) {
618
+ // A credential is written sensitive and never read back, so it is written every time.
619
+ for (const { key, value, credential } of written) {
652
620
  const existing = records.find((record) => record.key === key)
653
- const sensitive = isSensitiveFamilyVariable(key)
654
- assertRunning(signal)
655
- if (sensitive || readableContent(existing) !== value) {
656
- await zerops.api.putServiceEnv({ serviceId, key, value, sensitive, signal })
657
- log(` ${key}: updated`)
658
- } else {
659
- log(` ${key}: unchanged`)
660
- }
621
+ if (credential || readableContent(existing) !== value) put(key, value, credential)
622
+ else changes.push({ line: `${key}: unchanged`, apply: null })
661
623
  }
624
+ // A variable Control no longer hands is removed, so no stale key stays readable. `/env` also reports the descriptor's
625
+ // `FABRIKA_RELEASE`, which Zerops refuses to delete.
626
+ const stale = new Set(staleManagedVariables(records.map((record) => record.key), new Set([...writtenKeys, 'FABRIKA_RELEASE'])))
662
627
  for (const record of records) {
663
- if (isFrameworkFamilyVariable(record.key) && !Object.hasOwn(familyVariables, record.key)) {
664
- assertRunning(signal)
665
- await zerops.api.deleteServiceEnv({ envId: record.id, signal })
666
- log(` ${record.key}: removed`)
667
- }
628
+ if (stale.has(record.key)) remove(record.key, record.id)
668
629
  }
630
+ const provenance = env.attempt.storageProvenance
631
+ const marker = records.find((record) => record.key === STORAGE_PROVENANCE_KEY)
632
+ // Not a credential, and the exposure read-back above reads it: it must stay readable.
633
+ if (typeof provenance === 'string' && readableContent(marker) !== provenance) put(STORAGE_PROVENANCE_KEY, provenance, false)
634
+ else if (provenance === null && marker !== undefined) remove(STORAGE_PROVENANCE_KEY, marker.id)
669
635
  // The build lifts the upload's bearer from here (`${RUNTIME_…}`), so the descriptor never holds it.
670
636
  const bearer = env.run.artifactUpload?.bearer ?? null
671
637
  const uploadKey = records.find((record) => record.key === FABRIKA_SOURCE_MAP_UPLOAD_KEY)
672
- if (bearer !== null) {
673
- assertRunning(signal)
674
- await zerops.api.putServiceEnv({ serviceId, key: FABRIKA_SOURCE_MAP_UPLOAD_KEY, value: bearer, sensitive: true, signal })
675
- log(` ${FABRIKA_SOURCE_MAP_UPLOAD_KEY}: updated`)
676
- } else if (uploadKey !== undefined) {
677
- assertRunning(signal)
678
- await zerops.api.deleteServiceEnv({ envId: uploadKey.id, signal })
679
- log(` ${FABRIKA_SOURCE_MAP_UPLOAD_KEY}: removed`)
638
+ if (bearer !== null) put(FABRIKA_SOURCE_MAP_UPLOAD_KEY, bearer, true)
639
+ else if (uploadKey !== undefined) remove(FABRIKA_SOURCE_MAP_UPLOAD_KEY, uploadKey.id)
640
+ await applyServiceEnvChanges(changes, log, signal)
641
+ }
642
+
643
+ /** One service variable write or removal `write-service-env` decided on, or a key it leaves as it is (`apply: null`). */
644
+ interface ServiceEnvChange {
645
+ readonly line: string
646
+ readonly apply: (() => Promise<void>) | null
647
+ }
648
+
649
+ /** Each write waits ~3 s for its own user-data sync; concurrent writes to one service do not conflict (zerops-platform.md). */
650
+ const SERVICE_ENV_WRITE_CONCURRENCY = 6
651
+
652
+ /**
653
+ * Applies the changes `SERVICE_ENV_WRITE_CONCURRENCY` at a time and waits for every started one, so no sync still runs
654
+ * when the step ends. A failure or a cancel starts no further write; lines follow the decision order.
655
+ */
656
+ const applyServiceEnvChanges = async (changes: readonly ServiceEnvChange[], log: (line: string) => void, signal: AbortSignal): Promise<void> => {
657
+ const applied = new Set<number>()
658
+ const failures: unknown[] = []
659
+ let next = 0
660
+ const worker = async (): Promise<void> => {
661
+ while (next < changes.length && failures.length === 0 && !signal.aborted) {
662
+ const index = next++
663
+ const apply = changes[index]?.apply ?? null
664
+ if (apply === null) continue
665
+ try {
666
+ await apply()
667
+ applied.add(index)
668
+ } catch (error) {
669
+ failures.push(error)
670
+ }
671
+ }
680
672
  }
673
+ await Promise.all(Array.from({ length: SERVICE_ENV_WRITE_CONCURRENCY }, worker))
674
+ for (const [index, change] of changes.entries()) {
675
+ if (change.apply === null || applied.has(index)) log(` ${change.line}`)
676
+ }
677
+ if (failures.length > 0) throw failures[0]
678
+ assertRunning(signal)
681
679
  }
682
680
 
683
681
  /** The custom autoscaling with its minimum RAM raised to `minRam`; every other field is sent back as it was read. */
@@ -693,7 +691,6 @@ const withMinRam = (custom: ZeropsServiceAutoscaling, minRam: number): ZeropsSer
693
691
  */
694
692
  const raiseStartMemory = async (env: StepEnv): Promise<void> => {
695
693
  const { run, zerops, log, signal, dryRun } = env
696
- if (run.dev !== undefined) await pinOneContainer(env)
697
694
  const locked = lockedExecutionFor(run.artifact, run, dryRun)
698
695
  const minRam = locked === null ? null : zeropsStartMinRam(locked)
699
696
  const hostname = run.artifact.target.deployService
@@ -716,42 +713,6 @@ const raiseStartMemory = async (env: StepEnv): Promise<void> => {
716
713
  log(` minimum RAM of ${hostname}: ${before ?? 'the platform default'} → ${after} GB`)
717
714
  }
718
715
 
719
- /**
720
- * A dev environment runs on exactly one container: the launcher writes the files it syncs into that container, and a
721
- * second one would serve the bootstrap head. Sends the custom autoscaling whole with only the container counts changed,
722
- * and decides by reading it back.
723
- */
724
- const pinOneContainer = async (env: StepEnv): Promise<void> => {
725
- const { run, zerops, log, signal, dryRun } = env
726
- const hostname = run.artifact.target.deployService
727
- if (dryRun) {
728
- log(` [dry-run] would pin ${hostname} to one container`)
729
- return
730
- }
731
- assertRunning(signal)
732
- const service = await zerops.api.getService({ serviceId: run.target.serviceId, signal })
733
- const custom = service.customAutoscaling
734
- const horizontal = service.currentAutoscaling?.horizontal
735
- if (horizontal?.minContainerCount === 1 && horizontal.maxContainerCount === 1) {
736
- log(` ${hostname} runs on one container, unchanged`)
737
- return
738
- }
739
- if (custom === undefined) throw new Error(`zerops: ${hostname} did not report its autoscaling whole, so it cannot be pinned to one container`)
740
- const process = await zerops.api.configureCustomAutoscaling({
741
- serviceId: service.id,
742
- autoscaling: { ...custom, horizontal: { minContainerCount: 1, maxContainerCount: 1 } },
743
- signal,
744
- })
745
- if (process !== null) {
746
- await waitForProcess({ api: zerops.api, processId: process.id, sleep: zerops.sleep, signal, label: `the autoscaling change of ${hostname}` })
747
- }
748
- const after = (await zerops.api.getService({ serviceId: service.id, signal })).currentAutoscaling?.horizontal
749
- if (after?.minContainerCount !== 1 || after.maxContainerCount !== 1) {
750
- throw new Error(`zerops: ${hostname} reads back ${after?.maxContainerCount ?? 'no'} container(s) after it was pinned to one`)
751
- }
752
- log(` ${hostname} pinned to one container`)
753
- }
754
-
755
716
  /** What raising a minimum RAM calls; the deploy's collaborators, and the live platform-facts probe of the same path. */
756
717
  export interface MinRamRaise {
757
718
  zerops: Pick<ZeropsCollaborators, 'api' | 'sleep'>
@@ -817,7 +778,7 @@ const sizeDatabase = async (env: StepEnv): Promise<void> => {
817
778
  const deploy = await zerops.api.getService({ serviceId: run.target.serviceId, signal })
818
779
  const demand: ZeropsDatabaseDemand = {
819
780
  maxContainers: deploy.currentAutoscaling?.horizontal.maxContainerCount ?? ZEROPS_MAX_CONTAINERS,
820
- jobs: zeropsJobProcesses(locked.contract),
781
+ jobs: zeropsJobProcesses(locked),
821
782
  }
822
783
  const service = await zerops.api.findService({ projectId: run.target.projectId, hostname: database.hostname, signal })
823
784
  if (service === null) throw new Error(`zerops: the database ${database.hostname} does not exist, so its connections cannot be sized`)
@@ -979,7 +940,7 @@ const runStep = async (spec: ZeropsStep, env: StepEnv): Promise<void> => {
979
940
  const appVersionId = requireVersion(env, 'trigger-build')
980
941
  await verifyZeropsArtifactSourceDescriptor(artifact.target.sourceDescriptor)
981
942
  const { contents } = artifact.target.sourceDescriptor
982
- const release = env.managedValues.FABRIKA_RELEASE
943
+ const release = managedValue(env.managed, 'FABRIKA_RELEASE')
983
944
  const descriptor = locked === null
984
945
  ? overlayRelease(contents, artifact.target.zeropsSetup, release)
985
946
  : renderLockedExecutionDescriptor(contents, artifact.target.zeropsSetup, release, sizedExecution(locked, attempt))
@@ -1029,35 +990,6 @@ const runStep = async (spec: ZeropsStep, env: StepEnv): Promise<void> => {
1029
990
  await awaitVersion(env, appVersionId, attempt.processId, locked !== null && locked.databaseUrl !== null)
1030
991
  return
1031
992
  }
1032
-
1033
- case 'reconcile-schema': {
1034
- const schema = approvedSchemaOf(run)
1035
- const propustkaUrl = target.propustkaUrl
1036
- if (schema === undefined || propustkaUrl === undefined) {
1037
- return
1038
- }
1039
- // Return origins ride this step because `apps.setReturnOrigins` 404s for an app IAM has never
1040
- // heard of, and the schema reconcile is what registers it. Separate call, same moment.
1041
- const returnOrigins = run.returnOrigins !== undefined && run.returnOrigins.length > 0 ? run.returnOrigins : undefined
1042
- const iamApp = appEnvironmentId(artifact.app.id, artifact.app.env)
1043
- if (dryRun) {
1044
- log(` [dry-run] would reconcile schema for \`${iamApp}\` against ${propustkaUrl}`)
1045
- if (returnOrigins !== undefined) {
1046
- log(` [dry-run] would register return origins for \`${iamApp}\`: ${returnOrigins.join(', ')}`)
1047
- }
1048
- return
1049
- }
1050
- assertRunning(signal)
1051
- await zerops.reconcileSchema({
1052
- url: propustkaUrl,
1053
- app: iamApp,
1054
- schema,
1055
- ...(returnOrigins === undefined ? {} : { returnOrigins }),
1056
- adminKey: target.adminKey,
1057
- signal,
1058
- })
1059
- return
1060
- }
1061
993
  }
1062
994
  }
1063
995
 
@@ -1079,15 +1011,6 @@ async (run: ZeropsDeployRun): Promise<ZeropsDeployResult> => {
1079
1011
  throw new Error(`zerops: artifact environment drift: expected \`${run.env}\`, got \`${run.artifact.app.env}\``)
1080
1012
  }
1081
1013
  assertAuthoredManagedInputs({ variableNames: Object.keys(run.vars), secretNames: Object.keys(run.secrets) })
1082
- // The proxy serves an endpoint table exactly when Control approved a contract, whatever the artifact says.
1083
- const contractProxy = run.access !== undefined
1084
- const { values: handed, familyVariables } = parseManagedEnvironment(run.managedEnvironment)
1085
- // Only a contract application checks the secret; any other would merely hold a credential its own code can print.
1086
- const managedValues: ManagedValues = contractProxy ? handed : { ...handed, FABRIKA_PROXY_SECRET: null }
1087
- if (!run.dryRun && contractProxy && managedValues.FABRIKA_PROXY_SECRET === null) {
1088
- // The application would refuse every request its proxy sends (ADR-0022 rule 5).
1089
- throw new Error('zerops: a contract application needs its proxy secret, and Control supplied none')
1090
- }
1091
1014
  const hostnames = manifestServiceHostnames(run.artifact)
1092
1015
  const document = interpolateManifest(run.artifact.target.importDocument, run.artifact.app.pipeline.vars, run.vars, hostnames)
1093
1016
  const compiled = { yaml: renderYaml(document), hostnames }
@@ -1101,11 +1024,10 @@ async (run: ZeropsDeployRun): Promise<ZeropsDeployResult> => {
1101
1024
  signal: run.signal,
1102
1025
  dryRun: run.dryRun,
1103
1026
  attempt: { triggered: false },
1104
- managedValues,
1105
- familyVariables,
1027
+ managed: run.managedEnvironment,
1106
1028
  deadlineMs: Date.now() + ZEROPS_VERSION_DEADLINE_MS,
1107
1029
  }
1108
- const steps = buildZeropsPlan(run.artifact, run.target)
1030
+ const steps = buildZeropsPlan(run.artifact)
1109
1031
  const options = { appId: run.appId, env: run.env, dryRun: run.dryRun, signal: run.signal, log: run.events.log }
1110
1032
  const result = await runSteps(steps, options, (step) => runStep(step, env))
1111
1033
  const { appVersionId, triggered } = env.attempt
@@ -30,3 +30,42 @@ export const zeropsProxyManifestCarrier = (descriptorContents: string, setup = '
30
30
  }
31
31
  return 'archive'
32
32
  }
33
+
34
+ /** The build variable that marks a proxy source whose manifest parser knows `brand` (ADR-0066). */
35
+ export const FABRIKA_PROXY_MANIFEST_BRAND = 'FABRIKA_PROXY_MANIFEST_BRAND'
36
+
37
+ /** The build variable that marks a proxy source whose manifest parser knows a dev sandbox entry's `audience` and `upstreamHost`. */
38
+ export const FABRIKA_PROXY_MANIFEST_SANDBOXES = 'FABRIKA_PROXY_MANIFEST_SANDBOXES'
39
+
40
+ /**
41
+ * The value a source that serves dev sandboxes declares. `2`: its sandbox hop also drops the `X-Forwarded-For` Caddy adds,
42
+ * which the Box router refuses on a `project` port; a `1` source parses the entries but every request to them fails.
43
+ */
44
+ export const FABRIKA_PROXY_MANIFEST_SANDBOXES_VALUE = '2'
45
+
46
+ /** Whether the build section Zerops applies to the descriptor's proxy setup declares `marker: "<value>"`, as the carrier is decided. */
47
+ const declaresMarker = (descriptorContents: string, marker: string, setup: string, value = '1'): boolean => {
48
+ const parsed = parseZeropsDescriptor(descriptorContents)
49
+ for (const entry of appliedZeropsSetups(parsed, selectZeropsDescriptorSetup(parsed, setup))) {
50
+ const read = buildSectionSchema.safeParse(entry)
51
+ if (!read.success) continue
52
+ return read.data.build.envVariables?.[marker] === value
53
+ }
54
+ return false
55
+ }
56
+
57
+ /**
58
+ * Whether a pinned descriptor's proxy reads a manifest that carries the installation's client logo. An older parser
59
+ * refuses the unknown key and fails the build, so a namespace kept on such a pin gets no logo and keeps the fabrika
60
+ * mark until it is re-pinned (`--proxy-source=current`).
61
+ */
62
+ export const zeropsProxyAcceptsBrand = (descriptorContents: string, setup = 'proxy'): boolean =>
63
+ declaresMarker(descriptorContents, FABRIKA_PROXY_MANIFEST_BRAND, setup)
64
+
65
+ /**
66
+ * Whether a pinned descriptor's proxy serves dev sandbox entries. An older parser refuses their `audience` and
67
+ * `upstreamHost` and fails the build, and a `1` source reaches no sandbox, so a namespace on either pin serves no
68
+ * sandbox until it is re-pinned.
69
+ */
70
+ export const zeropsProxyAcceptsSandboxes = (descriptorContents: string, setup = 'proxy'): boolean =>
71
+ declaresMarker(descriptorContents, FABRIKA_PROXY_MANIFEST_SANDBOXES, setup, FABRIKA_PROXY_MANIFEST_SANDBOXES_VALUE)
@@ -0,0 +1,100 @@
1
+ // The base image of every dev workspace sandbox, built on the sandbox host from the Dockerfile context this package
2
+ // ships (`packages/provider-zerops/sandbox/`). Its name carries the hash of that context, so a release that changes the
3
+ // context builds a new base and every earlier one stays as it was.
4
+
5
+ import type { Sleeper } from '../collaborators'
6
+ import { type ZboxBase, type ZboxClient, ZboxOperationFailed } from '../zbox'
7
+
8
+ /** Every file of the build context; nothing else in the directory is sent. The scripts under `bin/` ship with each deploy. */
9
+ export const SANDBOX_BASE_CONTEXT_FILES = ['Dockerfile', 'init.sh'] as const
10
+
11
+ const CONTEXT_DIRECTORY = new URL('../../sandbox/', import.meta.url)
12
+
13
+ /** The first 12 hex of a sha256 over named files: the same bytes give the same version on every installation. */
14
+ export function contentVersion(files: Readonly<Record<string, Uint8Array>>): string {
15
+ const hash = new Bun.CryptoHasher('sha256')
16
+ for (const name of Object.keys(files).sort()) {
17
+ const content = files[name]
18
+ if (content === undefined) continue
19
+ hash.update(`${name}\u0000${content.byteLength}\u0000`)
20
+ hash.update(content)
21
+ }
22
+ return hash.digest('hex').slice(0, 12)
23
+ }
24
+
25
+ /** Reads `names` from the directory `relative` to the package's `sandbox/`. */
26
+ export async function readSandboxFiles(relative: string, names: readonly string[]): Promise<Record<string, Uint8Array>> {
27
+ const directory = new URL(relative, CONTEXT_DIRECTORY)
28
+ const files: Record<string, Uint8Array> = {}
29
+ for (const name of names) files[name] = await Bun.file(new URL(name, directory)).bytes()
30
+ return files
31
+ }
32
+
33
+ /** How long a base build may take. Measured: the package install and the flatten take well under a minute. */
34
+ const BASE_BUILD_DEADLINE_MS = 15 * 60_000
35
+
36
+ /** How often a base another call is building is read again. */
37
+ const BASE_POLL_MS = 3_000
38
+
39
+ export interface SandboxBaseContext {
40
+ /** `fabrika-dev-<first 12 hex of the context's sha256>`. */
41
+ readonly name: string
42
+ readonly files: Readonly<Record<string, Uint8Array>>
43
+ }
44
+
45
+ /** The name of the base a context builds: the same bytes name the same base on every installation. */
46
+ export function sandboxBaseName(files: Readonly<Record<string, Uint8Array>>): string {
47
+ return `fabrika-dev-${contentVersion(files)}`
48
+ }
49
+
50
+ /** The context this package ships. */
51
+ export async function readSandboxBaseContext(): Promise<SandboxBaseContext> {
52
+ const files = await readSandboxFiles('./', SANDBOX_BASE_CONTEXT_FILES)
53
+ return { name: sandboxBaseName(files), files }
54
+ }
55
+
56
+ /** The context as the plain tar archive `POST /base-build` takes. */
57
+ export async function sandboxBaseArchive(context: SandboxBaseContext): Promise<Uint8Array<ArrayBuffer>> {
58
+ return new Bun.Archive({ ...context.files }).bytes()
59
+ }
60
+
61
+ export interface EnsureSandboxBaseOptions {
62
+ readonly signal: AbortSignal
63
+ readonly sleep: Sleeper
64
+ }
65
+
66
+ const failedBase = (name: string, code: string | null): Error =>
67
+ new Error(
68
+ `zerops: the dev sandbox base \`${name}\` failed to build on the sandbox host${code === null ? '' : ` (${code})`}; `
69
+ + 'delete it on the sandbox host, then create the sandbox again',
70
+ )
71
+
72
+ /**
73
+ * The digest of the context's base on the sandbox host: the one it holds READY, or one this call builds. A base that
74
+ * FAILED is refused rather than rebuilt over, so the failure is seen once instead of retried on every workspace.
75
+ */
76
+ export async function ensureSandboxBase(client: ZboxClient, context: SandboxBaseContext, options: EnsureSandboxBaseOptions): Promise<string> {
77
+ const deadline = Date.now() + BASE_BUILD_DEADLINE_MS
78
+ const named = async (): Promise<ZboxBase | null> =>
79
+ (await client.listBases(options.signal)).find((base) => base.name === context.name && base.status !== 'DELETED') ?? null
80
+ let base = await named()
81
+ if (base === null) {
82
+ const started = await client.buildBase({ context: await sandboxBaseArchive(context), name: context.name }, options.signal)
83
+ try {
84
+ await client.awaitOperation(started, { signal: options.signal, deadlineMs: BASE_BUILD_DEADLINE_MS })
85
+ } catch (error) {
86
+ if (error instanceof ZboxOperationFailed) throw failedBase(context.name, error.code)
87
+ throw error
88
+ }
89
+ base = await named()
90
+ }
91
+ while (base !== null && (base.status === 'PULLING' || base.status === 'FLATTENING')) {
92
+ options.signal.throwIfAborted()
93
+ if (Date.now() > deadline) throw new Error(`zerops: the dev sandbox base \`${context.name}\` did not become ready in time`)
94
+ await options.sleep(BASE_POLL_MS, options.signal)
95
+ base = await named()
96
+ }
97
+ if (base === null) throw new Error(`zerops: the sandbox host does not list the dev sandbox base \`${context.name}\` it built`)
98
+ if (base.status === 'FAILED') throw failedBase(context.name, null)
99
+ return base.digest
100
+ }
@@ -0,0 +1,44 @@
1
+ // The paths a dev workspace sandbox's base image and its deploy agree on. The base image's scripts
2
+ // (`packages/provider-zerops/sandbox/`) spell the same paths; a change here changes them there too.
3
+
4
+ /** The application root inside a sandbox: what a deploy extracts the source into and the launcher runs from. */
5
+ export const SANDBOX_APP_ROOT = '/app'
6
+
7
+ /** State that survives a re-bootstrap, which replaces only the application root. */
8
+ export const SANDBOX_STATE_DIR = '/var/lib/fabrika'
9
+
10
+ /** The launcher's environment, written 0600 through the file API: the one way a credential reaches a sandbox process. */
11
+ export const SANDBOX_ENV_FILE = `${SANDBOX_STATE_DIR}/env`
12
+
13
+ /** The generated build script; it holds no credential, and the build reads none. */
14
+ export const SANDBOX_BUILD_SCRIPT = `${SANDBOX_STATE_DIR}/build.sh`
15
+
16
+ /** Where the source archive lands before it is extracted. */
17
+ export const SANDBOX_SOURCE_ARCHIVE = '/tmp/fabrika-source.tar.gz'
18
+
19
+ /** The base image's PID 1: PostgreSQL, then the launcher when a deploy left one. Every other script is uploaded. */
20
+ export const SANDBOX_INIT_COMMAND: readonly string[] = ['/bin/sh', '/usr/local/lib/fabrika/init.sh']
21
+
22
+ /** Where each deploy uploads the helper scripts, one directory per version; `current` names the one in use. */
23
+ export const SANDBOX_BIN_DIR = `${SANDBOX_STATE_DIR}/bin`
24
+ export const SANDBOX_BIN_POINTER = `${SANDBOX_BIN_DIR}/current`
25
+
26
+ /** The launcher's output, which only root reads. */
27
+ export const SANDBOX_LAUNCHER_LOG = `${SANDBOX_STATE_DIR}/launcher.log`
28
+
29
+ /**
30
+ * The database the launcher provisions with: the sandbox's local superuser. The deploy writes it without a password;
31
+ * `run-launcher.sh` adds the password, which stays in the sandbox in a file only root reads.
32
+ */
33
+ export const SANDBOX_DATABASE_URL = 'postgres://postgres@127.0.0.1:5432/postgres'
34
+
35
+ /** The local superuser's password, which `postgres-auth.sh` generates and `run-launcher.sh` adds to that URL. */
36
+ export const SANDBOX_DATABASE_PASSWORD_FILE = `${SANDBOX_STATE_DIR}/postgres-password`
37
+
38
+ /** The connections each process opens: one launcher, one application and a migration share a local PostgreSQL of 100. */
39
+ export const SANDBOX_DATABASE_CONNECTIONS = 10
40
+
41
+ /** The size of one workspace sandbox. */
42
+ export const SANDBOX_CPUS = 2
43
+ export const SANDBOX_MEMORY_BYTES = 2 * 1024 * 1024 * 1024
44
+ export const SANDBOX_DISK_BYTES = 4 * 1024 * 1024 * 1024