@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/execution.ts CHANGED
@@ -2,19 +2,16 @@
2
2
  // runs. Of the authored setup only `build.base`, `build.os` and the SPA's `build.buildCommands` survive; the rest of
3
3
  // the build, the deploy gate and the whole `run` section are generated here. PURE — no network, no filesystem.
4
4
 
5
+ import { type ContractRecipe, contractRecipe } from '@fabrika/app/build'
5
6
  import {
6
7
  type ContractArtifact,
7
- DEV_LAUNCHER_CONFIG_FILE,
8
- DEV_LAUNCHER_MODULE,
9
- DEV_LAUNCHER_STATE_FILE,
10
- type DevLauncherConfig,
11
8
  FABRIKA_CONTRACT_DIGEST,
12
9
  FABRIKA_SOURCE_MAP_UPLOAD,
13
10
  FABRIKA_SOURCE_MAP_UPLOAD_KEY,
14
11
  type FrameworkVariableName,
15
12
  } from '@fabrika/auth-core'
16
13
  import type { OperationsAssetRoot, OperationsSourceMapBuildUpload } from '@fabrika/operations-contract/releases'
17
- import { FRAMEWORK_RELEASE_PATTERN, type JsonValue } from '@fabrika/provider-contract'
14
+ import type { JsonValue } from '@fabrika/provider-contract'
18
15
  import packageManifest from '../package.json'
19
16
  import { renderYaml } from './compile'
20
17
  import { parseZeropsDescriptor, type PreparedZeropsDescriptor, selectZeropsDescriptorSetup } from './descriptor'
@@ -33,8 +30,7 @@ export const ZEROPS_APPLICATION_RUNTIME = 'alpine/bun@1.3'
33
30
  */
34
31
  export const FABRIKA_CHECK_VERSION: string = packageManifest.version
35
32
 
36
- /** The compiler the platform type-checks with. It is fetched by version, never taken from the application's lockfile. */
37
- export const PLATFORM_TYPESCRIPT_VERSION = '5.9.3'
33
+ export { PLATFORM_TYPESCRIPT_VERSION } from '@fabrika/app/build'
38
34
 
39
35
  /** System packages an application may add to its runtime image, installed by the generated `run.prepareCommands`. */
40
36
  export const ZEROPS_RUNTIME_PACKAGES = ['ffmpeg', 'ghostscript', 'imagemagick', 'poppler-utils', 'vips'] as const
@@ -46,12 +42,6 @@ export interface ZeropsContractJob {
46
42
  readonly schedule: string
47
43
  }
48
44
 
49
- /** The framework's file scan job (`@fabrika/app`'s `FILE_SCAN_JOB`), run every minute when a collection scans. */
50
- export const ZEROPS_FILE_SCAN_JOB = 'fabrika-file-scan'
51
- /** The framework's file sweep job (`@fabrika/app`'s `FILE_SWEEP_JOB` and its schedule), run when the contract has files. */
52
- export const ZEROPS_FILE_SWEEP_JOB = 'fabrika-file-sweep'
53
- const FILE_SWEEP_SCHEDULE = '41 * * * *'
54
-
55
45
  /**
56
46
  * The RAM in GB a container of a scanning application starts with: one `clamscan` over a batch peaks at about 1 GB
57
47
  * resident while it loads its signatures (measured with ClamAV 1.4), beside the application's own process.
@@ -111,20 +101,10 @@ export interface ZeropsLockedExecution {
111
101
  readonly runtimePackages: readonly ZeropsRuntimePackage[]
112
102
  /** Where the build uploads its source maps, without the bearer; null when Control minted no upload for the run. */
113
103
  readonly sourceMaps: OperationsSourceMapBuildUpload | null
114
- /**
115
- * Present for an environment of kind `dev` (ADR-0049): `run.start` runs the dev launcher instead of the serving
116
- * entry. The environment's kind selects it; an application never authors it.
117
- */
118
- readonly dev?: ZeropsDevExecution
119
- }
120
-
121
- export interface ZeropsDevExecution {
122
- /** The commit the bootstrap run builds: what a new container holds before the launcher's first sync. */
123
- readonly head: string
124
104
  }
125
105
 
126
106
  /** The application ids the generated scripts may name: the contract's own id rule, so a script needs no quoting. */
127
- export const LOCKED_APP_ID_PATTERN = /^[a-z0-9][a-z0-9-]*$/
107
+ export { CONTRACT_APP_ID_PATTERN as LOCKED_APP_ID_PATTERN } from '@fabrika/app/build'
128
108
 
129
109
  export const CONTRACT_DIGEST_PATTERN = /^sha256:[0-9a-f]{64}$/
130
110
 
@@ -258,32 +238,6 @@ export function lockedAuthoredBuild(entry: { readonly [key: string]: JsonValue }
258
238
 
259
239
  const SOURCE_ROOT = '/build/source'
260
240
  const RUNTIME_ROOT = '/var/www'
261
- const FABRIKA_BIN = './node_modules/.bin/fabrika'
262
- const MIGRATE_SCRIPT = 'fabrika/migrate.generated.sh'
263
- const VERIFY_SCRIPT = 'fabrika/verify.generated.sh'
264
- const START_SCRIPT = 'fabrika/start.generated.sh'
265
- const DEV_INIT_SCRIPT = 'fabrika/dev-init.generated.sh'
266
- const PLATFORM_TSCONFIG = 'fabrika/tsconfig.platform.json'
267
-
268
- /** The compiler options of the platform's type check; the application's own `tsconfig.json` and its `paths` are not read. */
269
- const PLATFORM_TSCONFIG_CONTENTS = JSON.stringify({
270
- compilerOptions: {
271
- target: 'ES2022',
272
- module: 'ESNext',
273
- moduleResolution: 'bundler',
274
- lib: ['ES2022', 'DOM', 'DOM.Iterable'],
275
- types: ['bun'],
276
- strict: true,
277
- noUncheckedIndexedAccess: true,
278
- noImplicitOverride: true,
279
- forceConsistentCasingInFileNames: true,
280
- skipLibCheck: true,
281
- resolveJsonModule: true,
282
- isolatedModules: true,
283
- noEmit: true,
284
- },
285
- include: ['../fabrika.contract.ts', '../src', 'entry.generated.ts'],
286
- })
287
241
 
288
242
  /** One `printf` writing `lines` to `path`. Each line is single-quoted, so it may hold neither `'` nor an expansion. */
289
243
  const writeFileCommand = (path: string, lines: readonly string[]): string => {
@@ -293,111 +247,6 @@ const writeFileCommand = (path: string, lines: readonly string[]): string => {
293
247
  return `printf '%s\\n' ${lines.map((line) => `'${line}'`).join(' ')} > ${path}`
294
248
  }
295
249
 
296
- const migrateScript = (appId: string): readonly string[] => [
297
- '#!/bin/sh',
298
- 'set -eu',
299
- `cd ${RUNTIME_ROOT}`,
300
- `FABRIKA_APP_ID=${appId} exec ${FABRIKA_BIN} db migrate`,
301
- ]
302
-
303
- // The committed schema types must match the migrated schema and every generated isolation test must pass on a throwaway
304
- // schema. The build has no database, so they run at container start, after the migrations, as init commands: a failed
305
- // init command stops the activation, and the readiness window opens only with `run.start`, after the init commands (both
306
- // verified live), so a run as long as the CRM's ~3 minutes of generated cases does not race it.
307
- const verifyScript = (appId: string): readonly string[] => [
308
- '#!/bin/sh',
309
- 'set -eu',
310
- `cd ${RUNTIME_ROOT}`,
311
- `FABRIKA_APP_ID=${appId} ${FABRIKA_BIN} db codegen --verify`,
312
- `FABRIKA_APP_ID=${appId} exec ${FABRIKA_BIN} db test-isolation`,
313
- ]
314
-
315
- // The readiness check is retried no sooner than every 10 s (the platform's floor), and a server is rarely listening at
316
- // the first attempt, one second after `run.start`. So one attempt waits for the health endpoint itself, for less than
317
- // the retry period; it passes only on a 2xx, like the `httpGet` it replaces.
318
- const readinessCommand = (path: string): string => {
319
- if (!READINESS_PATH_PATTERN.test(path)) throw new Error('zerops: a readiness path must be a literal path of unreserved characters')
320
- const url = `http://127.0.0.1:${ZEROPS_APPLICATION_PORT}${path}`
321
- return `bun -e "const end = Date.now() + 9000; while (Date.now() < end) { try { if ((await fetch('${url}', { signal: AbortSignal.timeout(2000) })).ok) process.exit(0) } catch {} await Bun.sleep(200) } process.exit(1)"`
322
- }
323
-
324
- const SERVE_ENTRY = 'fabrika/entry.generated.ts'
325
-
326
- const startScript: readonly string[] = ['#!/bin/sh', 'set -eu', `cd ${RUNTIME_ROOT}`, `exec bun ${SERVE_ENTRY}`]
327
-
328
- // A dev environment starts the launcher of the application's own framework release, which runs the serving entry as its
329
- // child. Double quotes inside: a generated file line may hold no single quote.
330
- const devStartScript: readonly string[] = [
331
- '#!/bin/sh',
332
- 'set -eu',
333
- `cd ${RUNTIME_ROOT}`,
334
- `exec bun -e "await import(\\"${DEV_LAUNCHER_MODULE}\\").then((dev) => dev.runDevLauncher(process.env))"`,
335
- ]
336
-
337
- // The init commands of a dev container migrate and verify only what the bootstrap run built. A container that restarts
338
- // after a stop keeps the files the launcher synced since, and its state file: the launcher reconciles that one itself.
339
- const devInitScript: readonly string[] = [
340
- '#!/bin/sh',
341
- 'set -eu',
342
- `cd ${RUNTIME_ROOT}`,
343
- `if [ -f ${DEV_LAUNCHER_STATE_FILE} ]; then exit 0; fi`,
344
- `sh ${RUNTIME_ROOT}/${MIGRATE_SCRIPT}`,
345
- `sh ${RUNTIME_ROOT}/${VERIFY_SCRIPT}`,
346
- ]
347
-
348
- /** One command writing `content` to `path` through base64: the content may hold quotes and expansions, the command none. */
349
- const writeBase64Command = (path: string, content: string): string => {
350
- const encoded = btoa(String.fromCharCode(...new TextEncoder().encode(content)))
351
- return `printf '%s' '${encoded}' | base64 -d > ${path}`
352
- }
353
-
354
- /** The build's check chain as the dev launcher runs it in the application root, on what the container holds. */
355
- const devChecks = (release: string): DevLauncherConfig['checks'] => [
356
- { name: 'tests', command: 'bun test --isolate --pass-with-no-tests' },
357
- { name: 'fabrika-check', command: `cd /tmp && bun x --bun -p @fabrika/check@${release} fabrika-check ${RUNTIME_ROOT}` },
358
- { name: 'tsc', command: `cd /tmp && bun x --bun -p typescript@${PLATFORM_TYPESCRIPT_VERSION} tsc -p ${RUNTIME_ROOT}/${PLATFORM_TSCONFIG}` },
359
- ]
360
-
361
- /** What the dev launcher reads (`DEV_LAUNCHER_CONFIG_FILE`): the platform's, written after the checks. */
362
- export function devLauncherConfig(execution: ZeropsLockedExecution, authoredSpa: readonly string[]): DevLauncherConfig {
363
- const dev = execution.dev
364
- if (dev === undefined) throw new Error('zerops: a dev launcher configuration needs a dev environment')
365
- if (!/^[0-9a-f]{40}$/.test(dev.head)) throw new Error('zerops: a dev environment builds an exact commit')
366
- return {
367
- format: 1,
368
- appId: execution.appId,
369
- head: dev.head,
370
- port: ZEROPS_APPLICATION_PORT,
371
- healthPath: execution.contract.readinessPath,
372
- serve: ['bun', SERVE_ENTRY],
373
- migrate: [FABRIKA_BIN, 'db', 'migrate'],
374
- spaBuild: authoredSpa,
375
- checks: devChecks(execution.checkRelease),
376
- }
377
- }
378
-
379
- const devLauncherConfigJson = (execution: ZeropsLockedExecution, authoredSpa: readonly string[]): string =>
380
- JSON.stringify(devLauncherConfig(execution, authoredSpa))
381
-
382
- /** The scripts the build writes, and the ones every container's init commands run, in this order. */
383
- const initScripts = (execution: ZeropsLockedExecution): readonly { readonly path: string; readonly lines: readonly string[] }[] =>
384
- execution.databaseUrl === null
385
- ? []
386
- : [
387
- { path: MIGRATE_SCRIPT, lines: migrateScript(execution.appId) },
388
- { path: VERIFY_SCRIPT, lines: verifyScript(execution.appId) },
389
- ...(execution.dev === undefined ? [] : [{ path: DEV_INIT_SCRIPT, lines: devInitScript }]),
390
- ]
391
-
392
- const initCommandScripts = (execution: ZeropsLockedExecution): readonly string[] =>
393
- execution.databaseUrl === null ? [] : execution.dev === undefined ? [MIGRATE_SCRIPT, VERIFY_SCRIPT] : [DEV_INIT_SCRIPT]
394
-
395
- const INSTALL = 'bun install --frozen-lockfile --ignore-scripts'
396
-
397
- // After every check: a map is uploaded only for a build that can deploy. A failed upload never fails the build; the
398
- // provider reads the state from the report line.
399
- const sourceMapUpload = (release: string): string => `cd /tmp && bun x --bun -p @fabrika/check@${release} fabrika-source-maps ${SOURCE_ROOT} || true`
400
-
401
250
  /**
402
251
  * The upload's build variables: the destination and asset roots as a literal, and the bearer lifted from the service
403
252
  * variable the deploy wrote sensitive, so the descriptor never holds it.
@@ -409,28 +258,34 @@ const sourceMapBuildVariables = (upload: OperationsSourceMapBuildUpload): Record
409
258
  return { [FABRIKA_SOURCE_MAP_UPLOAD]: value, [FABRIKA_SOURCE_MAP_UPLOAD_KEY]: `\${RUNTIME_${FABRIKA_SOURCE_MAP_UPLOAD_KEY}}` }
410
259
  }
411
260
 
412
- /**
413
- * The generated build commands, in order; the authored SPA commands run before all of them. The application's tests
414
- * run first: they are authored code, so everything after them is reinstalled, regenerated or checked again. They run
415
- * `--isolate`, as the application skill runs them: a test file's DOM globals otherwise leak into the next file.
416
- */
417
- export function generatedBuildCommands(execution: ZeropsLockedExecution, authoredSpa: readonly string[] = []): readonly string[] {
418
- if (!LOCKED_APP_ID_PATTERN.test(execution.appId)) throw new Error('zerops: a contract application id must match [a-z0-9][a-z0-9-]*')
419
- if (!FRAMEWORK_RELEASE_PATTERN.test(execution.checkRelease)) throw new Error('zerops: the checker release must be a release version')
420
- return [
421
- INSTALL,
422
- 'bun test --isolate --pass-with-no-tests',
423
- `rm -rf node_modules && ${INSTALL}`,
424
- `bun -e "await import('@fabrika/app/build').then((build) => build.writeGeneratedEntry('.', 'bun'))"`,
425
- `cd /tmp && bun x --bun -p @fabrika/check@${execution.checkRelease} fabrika-check ${SOURCE_ROOT}`,
426
- writeFileCommand(PLATFORM_TSCONFIG, [PLATFORM_TSCONFIG_CONTENTS]),
427
- `cd /tmp && bun x --bun -p typescript@${PLATFORM_TYPESCRIPT_VERSION} tsc -p ${SOURCE_ROOT}/${PLATFORM_TSCONFIG}`,
428
- ...initScripts(execution).map((script) => writeFileCommand(script.path, script.lines)),
429
- ...(execution.dev === undefined
430
- ? [writeFileCommand(START_SCRIPT, startScript)]
431
- : [writeFileCommand(START_SCRIPT, devStartScript), writeBase64Command(DEV_LAUNCHER_CONFIG_FILE, devLauncherConfigJson(execution, authoredSpa))]),
432
- ...(execution.sourceMaps === null ? [] : [sourceMapUpload(execution.checkRelease)]),
433
- ]
261
+ // The readiness check is retried no sooner than every 10 s (the platform's floor), and a server is rarely listening at
262
+ // the first attempt, one second after `run.start`. So one attempt waits for the health endpoint itself, for less than
263
+ // the retry period; it passes only on a 2xx, like the `httpGet` it replaces.
264
+ const readinessCommand = (path: string): string => {
265
+ if (!READINESS_PATH_PATTERN.test(path)) throw new Error('zerops: a readiness path must be a literal path of unreserved characters')
266
+ const url = `http://127.0.0.1:${ZEROPS_APPLICATION_PORT}${path}`
267
+ return `bun -e "const end = Date.now() + 9000; while (Date.now() < end) { try { if ((await fetch('${url}', { signal: AbortSignal.timeout(2000) })).ok) process.exit(0) } catch {} await Bun.sleep(200) } process.exit(1)"`
268
+ }
269
+
270
+ /** `@fabrika/app/build`'s recipe at this installation's paths. */
271
+ const recipeOf = (execution: ZeropsLockedExecution): ContractRecipe =>
272
+ contractRecipe({
273
+ appId: execution.appId,
274
+ checkRelease: execution.checkRelease,
275
+ sourceRoot: SOURCE_ROOT,
276
+ runtimeRoot: RUNTIME_ROOT,
277
+ // The build has no database; a container's init commands run the migrations and schema checks, and the readiness
278
+ // window opens only with `run.start`, after them (both verified live), so the CRM's ~3 minutes does not race it.
279
+ database: execution.databaseUrl !== null,
280
+ sourceMaps: execution.sourceMaps !== null,
281
+ jobs: execution.contract.jobs,
282
+ files: execution.contract.files === true,
283
+ scan: execution.contract.scan === true,
284
+ })
285
+
286
+ /** The generated build commands, in order; the authored SPA commands run before all of them. */
287
+ export function generatedBuildCommands(execution: ZeropsLockedExecution): readonly string[] {
288
+ return recipeOf(execution).build.map((step) => 'run' in step ? step.run : writeFileCommand(step.write, step.lines))
434
289
  }
435
290
 
436
291
  const sortedUnique = (values: readonly string[]): string[] => [...new Set(values)].sort()
@@ -446,16 +301,11 @@ const storageVariables = (hostname: string): Record<StorageVariable, string> =>
446
301
  })
447
302
 
448
303
  /** The crontab lines that run a job of the contract or of the framework, each on one container. */
449
- const jobCrontab = (contract: ZeropsContractRun): { readonly timing: string; readonly command: string; readonly allContainers: boolean }[] => [
450
- ...contract.jobs.map((job) => ({ timing: job.schedule, command: `${FABRIKA_BIN} job run ${job.name}`, allContainers: false })),
451
- ...(contract.files === true
452
- ? [{ timing: FILE_SWEEP_SCHEDULE, command: `${FABRIKA_BIN} job run ${ZEROPS_FILE_SWEEP_JOB}`, allContainers: false }]
453
- : []),
454
- ...(contract.scan === true ? [{ timing: '* * * * *', command: `${FABRIKA_BIN} job run ${ZEROPS_FILE_SCAN_JOB}`, allContainers: false }] : []),
455
- ]
304
+ const jobCrontab = (execution: ZeropsLockedExecution): { readonly timing: string; readonly command: string; readonly allContainers: boolean }[] =>
305
+ recipeOf(execution).jobs.map((job) => ({ timing: job.schedule, command: job.command, allContainers: false }))
456
306
 
457
307
  /** How many job processes the crontab may run at once, each opening the application's database. */
458
- export const zeropsJobProcesses = (contract: ZeropsContractRun): number => jobCrontab(contract).length
308
+ export const zeropsJobProcesses = (execution: ZeropsLockedExecution): number => jobCrontab(execution).length
459
309
 
460
310
  /** The commands every container runs before `run.start`, in order; any failure stops the activation. */
461
311
  export function generatedInitCommands(execution: ZeropsLockedExecution): string[] {
@@ -463,7 +313,7 @@ export function generatedInitCommands(execution: ZeropsLockedExecution): string[
463
313
  // A container starts from the prepared image, whose signatures are as old as its build; a failed refresh leaves
464
314
  // them to the daily one, and the scanner refuses signatures older than three days, so the scan fails closed.
465
315
  ...(execution.contract.scan === true ? [`${FRESHCLAM} || echo 'fabrika: freshclam did not refresh the malware signatures'`] : []),
466
- ...initCommandScripts(execution).map((script) => `sh ${RUNTIME_ROOT}/${script}`),
316
+ ...recipeOf(execution).init.map((path) => `sh ${RUNTIME_ROOT}/${path}`),
467
317
  ]
468
318
  }
469
319
 
@@ -488,7 +338,7 @@ export function renderLockedSetup(
488
338
  const scan = contract.scan === true
489
339
  const packages = sortedUnique([...execution.runtimePackages, ...(scan ? [SCANNER_PACKAGE] : [])])
490
340
  // Every container keeps its own signatures; one scan pass at a time holds the job's lease.
491
- const crontab = [...jobCrontab(contract), ...(scan ? [{ timing: FRESHCLAM_SCHEDULE, command: FRESHCLAM, allContainers: true }] : [])]
341
+ const crontab = [...jobCrontab(execution), ...(scan ? [{ timing: FRESHCLAM_SCHEDULE, command: FRESHCLAM, allContainers: true }] : [])]
492
342
  const initCommands = generatedInitCommands(execution)
493
343
  const prepareCommands = packages.length === 0 ? [] : [`sudo apk add --no-cache ${packages.join(' ')}`, ...(scan ? [FRESHCLAM] : [])]
494
344
  return {
@@ -496,25 +346,25 @@ export function renderLockedSetup(
496
346
  build: {
497
347
  base: authored.base ?? [ZEROPS_APPLICATION_RUNTIME],
498
348
  ...(authored.os === null ? {} : { os: authored.os }),
499
- buildCommands: [...authored.commands, ...generatedBuildCommands(execution, authored.commands)],
349
+ buildCommands: [...authored.commands, ...generatedBuildCommands(execution)],
500
350
  ...(execution.sourceMaps === null ? {} : { envVariables: sourceMapBuildVariables(execution.sourceMaps) }),
501
351
  deployFiles: ['./'],
502
352
  cache: ['node_modules'],
503
353
  },
504
354
  deploy: {
505
- readinessCheck: { exec: { command: readinessCommand(contract.readinessPath) }, retryPeriod: '10s', failureTimeout: '180s' },
355
+ readinessCheck: {
356
+ exec: { command: readinessCommand(contract.readinessPath) },
357
+ retryPeriod: '10s',
358
+ failureTimeout: '180s',
359
+ },
506
360
  },
507
361
  run: {
508
362
  base: ZEROPS_APPLICATION_RUNTIME,
509
363
  ...(prepareCommands.length === 0 ? {} : { prepareCommands }),
510
364
  ...(initCommands.length === 0 ? {} : { initCommands }),
511
- start: `sh ${RUNTIME_ROOT}/${START_SCRIPT}`,
365
+ start: `sh ${RUNTIME_ROOT}/${recipeOf(execution).start}`,
512
366
  ports: [{ port: ZEROPS_APPLICATION_PORT, httpSupport: true }],
513
- // A dev container keeps its launcher running while the application it respawns fails: Zerops restarting it
514
- // would only boot the same failing head again.
515
- ...(execution.dev === undefined
516
- ? { healthCheck: { httpGet: probe, execPeriod: '10s', disconnectTimeout: '30s', recoveryTimeout: '10s', failureTimeout: '120s' } }
517
- : {}),
367
+ healthCheck: { httpGet: probe, execPeriod: '10s', disconnectTimeout: '30s', recoveryTimeout: '10s', failureTimeout: '120s' },
518
368
  ...(crontab.length === 0 ? {} : { crontab }),
519
369
  envVariables,
520
370
  },
package/src/index.ts CHANGED
@@ -18,6 +18,8 @@ export type {
18
18
  ZeropsAppVersion,
19
19
  ZeropsAppVersionStatus,
20
20
  ZeropsAppVersionUpload,
21
+ ZeropsBackupConfig,
22
+ ZeropsBackupRetentionPolicy,
21
23
  ZeropsImportedService,
22
24
  ZeropsImportResult,
23
25
  ZeropsIntegrationToken,
@@ -15,10 +15,10 @@ import {
15
15
  ensureFirstAdministrator,
16
16
  type FirstAdministratorApi,
17
17
  type FirstAdministratorResult,
18
+ platformOrigin,
18
19
  } from '@fabrika/installation-init'
19
20
  import type { PlatformAdminInput } from './admin-options'
20
21
  import { REISSUE_FLAG } from './admin-options'
21
- import { platformOrigin } from './hosts'
22
22
  import { MACHINE_KEY_NEXT_STEP_TITLE, machineKeyNextStepLines } from './machine-key'
23
23
 
24
24
  /**
@@ -12,8 +12,7 @@
12
12
  // The command is unattended by construction: nothing here prompts, and a missing required value is an
13
13
  // error naming both the flag and the variable that would supply it.
14
14
 
15
- import { describeArgument, readEnv, requiredSetting, setting } from '@fabrika/installation-init'
16
- import { type McpClient, parseMcpClientsJson } from './mcp'
15
+ import { describeArgument, type McpClient, parseMcpClientsJson, readEnv, requiredSetting, setting } from '@fabrika/installation-init'
17
16
  import { parsePlatformHosts, parsePlatformScheme } from './placement-options'
18
17
  import { readInstallationSourceDir } from './source-options'
19
18