@fabrika/provider-cloudflare 0.0.26 → 0.3.1
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.
- package/package.json +12 -6
- package/src/assets.ts +87 -0
- package/src/cli-args.ts +22 -4
- package/src/cli.ts +7 -8
- package/src/codec.ts +4 -65
- package/src/collaborators.ts +36 -9
- package/src/command.ts +20 -36
- package/src/control.ts +309 -96
- package/src/index.ts +8 -6
- package/src/installation/cloudflare.ts +184 -0
- package/src/installation/envfile.ts +180 -0
- package/src/installation/index.ts +2 -0
- package/src/installation/init.ts +597 -0
- package/src/installation/installation.ts +77 -0
- package/src/installation/scaffold.ts +106 -0
- package/src/installation/templates/README.md +64 -0
- package/src/installation/templates/fabrika.ref +1 -0
- package/src/installation/templates/gitignore +2 -0
- package/src/installation/templates/platform.yml +181 -0
- package/src/plan.ts +231 -29
- package/src/provider.ts +115 -70
- package/src/proxy-worker.ts +1 -1
- package/src/proxy.ts +1 -1
- package/src/runner-protocol.ts +40 -0
- package/src/schema.ts +34 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/provider-cloudflare",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -29,6 +29,11 @@
|
|
|
29
29
|
"bun": "./src/control.ts",
|
|
30
30
|
"types": "./src/control.ts",
|
|
31
31
|
"default": "./src/control.ts"
|
|
32
|
+
},
|
|
33
|
+
"./installation": {
|
|
34
|
+
"bun": "./src/installation/index.ts",
|
|
35
|
+
"types": "./src/installation/index.ts",
|
|
36
|
+
"default": "./src/installation/index.ts"
|
|
32
37
|
}
|
|
33
38
|
},
|
|
34
39
|
"scripts": {
|
|
@@ -36,11 +41,12 @@
|
|
|
36
41
|
"test": "bun test"
|
|
37
42
|
},
|
|
38
43
|
"dependencies": {
|
|
39
|
-
"
|
|
40
|
-
"@fabrika/
|
|
41
|
-
"@fabrika/
|
|
42
|
-
"@fabrika/
|
|
43
|
-
"@fabrika/
|
|
44
|
+
"zod": "^3.25.76",
|
|
45
|
+
"@fabrika/auth": "^0.3.1",
|
|
46
|
+
"@fabrika/auth-core": "^0.3.1",
|
|
47
|
+
"@fabrika/installation-init": "^0.3.1",
|
|
48
|
+
"@fabrika/proxy-core": "^0.3.1",
|
|
49
|
+
"@fabrika/provider-contract": "^0.3.1",
|
|
44
50
|
"oblaka-iac": "^0.0.18"
|
|
45
51
|
}
|
|
46
52
|
}
|
package/src/assets.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { Stats } from 'node:fs'
|
|
2
|
+
import { lstat, readdir } from 'node:fs/promises'
|
|
3
|
+
import { resolve } from 'node:path'
|
|
4
|
+
import { Worker } from 'oblaka-iac'
|
|
5
|
+
|
|
6
|
+
/** A ceiling on the walk, so a checkout cannot turn the check itself into the denial of service. */
|
|
7
|
+
const MAX_ASSET_ENTRIES = 50_000
|
|
8
|
+
|
|
9
|
+
/** A directory wrangler would refuse on its own; its error is better than one invented here. */
|
|
10
|
+
const lstatOrNull = async (path: string): Promise<Stats | null> => {
|
|
11
|
+
try {
|
|
12
|
+
return await lstat(path)
|
|
13
|
+
} catch (err) {
|
|
14
|
+
const code: unknown = err instanceof Error ? Reflect.get(err, 'code') : undefined
|
|
15
|
+
if (code === 'ENOENT' || code === 'ENOTDIR') return null
|
|
16
|
+
throw err
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const graphWorkers = (root: Worker): readonly Worker[] => {
|
|
21
|
+
const workers = new Set<Worker>()
|
|
22
|
+
const visit = (worker: Worker): void => {
|
|
23
|
+
if (workers.has(worker)) return
|
|
24
|
+
workers.add(worker)
|
|
25
|
+
for (const value of Object.values(worker.options.bindings ?? {})) {
|
|
26
|
+
if (value instanceof Worker) visit(value)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
visit(root)
|
|
30
|
+
return [...workers]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** `assertBoundedAssets` has already bounded the value; this only needs to find it. */
|
|
34
|
+
const assetDirectory = (worker: Worker): string | null => {
|
|
35
|
+
const assets: unknown = worker.options.assets
|
|
36
|
+
if (typeof assets !== 'object' || assets === null) return null
|
|
37
|
+
const directory: unknown = Reflect.get(assets, 'directory')
|
|
38
|
+
return typeof directory === 'string' && directory !== '' ? directory : null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface WalkBudget {
|
|
42
|
+
count: number
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
type Refuse = (detail: string) => Error
|
|
46
|
+
|
|
47
|
+
const walk = async (base: string, prefix: string, budget: WalkBudget, refuse: Refuse): Promise<void> => {
|
|
48
|
+
for (const name of await readdir(resolve(base, prefix))) {
|
|
49
|
+
budget.count += 1
|
|
50
|
+
if (budget.count > MAX_ASSET_ENTRIES) {
|
|
51
|
+
throw refuse(`holds more than ${MAX_ASSET_ENTRIES} entries and is refused rather than walked`)
|
|
52
|
+
}
|
|
53
|
+
const path = prefix === '' ? name : `${prefix}/${name}`
|
|
54
|
+
const stats = await lstat(resolve(base, path))
|
|
55
|
+
if (stats.isSymbolicLink()) {
|
|
56
|
+
throw refuse(`contains \`${path}\`, which is a symbolic link; wrangler follows it and would publish its target`)
|
|
57
|
+
}
|
|
58
|
+
if (stats.isDirectory()) {
|
|
59
|
+
await walk(base, path, budget, refuse)
|
|
60
|
+
} else if (!stats.isFile()) {
|
|
61
|
+
throw refuse(`contains \`${path}\`, which is neither a regular file nor a directory`)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* wrangler `stat`s each asset rather than `lstat`ing it, so a committed symlink to `/proc/self/environ`
|
|
68
|
+
* (the deploy's own environment holds the account token) or to `.git/config` uploads publicly. Call this
|
|
69
|
+
* per `wrangler deploy` spawn, for the Worker whose config lives in `configDir`: the checkout's own build
|
|
70
|
+
* step runs between `open()` and the deploy, so that spawn is the only moment the tree is final.
|
|
71
|
+
*/
|
|
72
|
+
export const assertLiteralAssets = async (root: Worker, rootDir: string, configDir: string): Promise<void> => {
|
|
73
|
+
for (const worker of graphWorkers(root)) {
|
|
74
|
+
if (resolve(rootDir, worker.options.dir) !== configDir) continue
|
|
75
|
+
const directory = assetDirectory(worker)
|
|
76
|
+
if (directory === null) continue
|
|
77
|
+
const base = resolve(configDir, directory)
|
|
78
|
+
const refuse = (detail: string): Error => new Error(`cloudflare: Worker \`${worker.options.name}\` assets directory \`${directory}\` ${detail}`)
|
|
79
|
+
const stats = await lstatOrNull(base)
|
|
80
|
+
if (stats === null) continue
|
|
81
|
+
if (stats.isSymbolicLink()) {
|
|
82
|
+
throw refuse('is a symbolic link; wrangler follows it and would publish its target')
|
|
83
|
+
}
|
|
84
|
+
if (!stats.isDirectory()) continue
|
|
85
|
+
await walk(base, '', { count: 0 }, refuse)
|
|
86
|
+
}
|
|
87
|
+
}
|
package/src/cli-args.ts
CHANGED
|
@@ -5,6 +5,8 @@ export interface ParsedCloudflareArgs {
|
|
|
5
5
|
readonly config: string
|
|
6
6
|
readonly runnerConfig: string | undefined
|
|
7
7
|
readonly workerConfig: string | undefined
|
|
8
|
+
/** The single-component form of `platform deploy|plan`: deploy exactly this one platform config. */
|
|
9
|
+
readonly componentConfig: string | undefined
|
|
8
10
|
readonly managedVarNames: readonly string[]
|
|
9
11
|
readonly dryRun: boolean
|
|
10
12
|
readonly help: boolean
|
|
@@ -17,6 +19,7 @@ export const parseCloudflareArgs = (argv: readonly string[]): ParsedCloudflareAr
|
|
|
17
19
|
let config = './fabrika.config.ts'
|
|
18
20
|
let runnerConfig: string | undefined
|
|
19
21
|
let workerConfig: string | undefined
|
|
22
|
+
let componentConfig: string | undefined
|
|
20
23
|
const managedVarNames: string[] = []
|
|
21
24
|
let dryRun = false
|
|
22
25
|
let help = false
|
|
@@ -34,6 +37,8 @@ export const parseCloudflareArgs = (argv: readonly string[]): ParsedCloudflareAr
|
|
|
34
37
|
runnerConfig = arg.slice('--runner-config='.length)
|
|
35
38
|
} else if (arg.startsWith('--worker-config=')) {
|
|
36
39
|
workerConfig = arg.slice('--worker-config='.length)
|
|
40
|
+
} else if (arg.startsWith('--component-config=')) {
|
|
41
|
+
componentConfig = arg.slice('--component-config='.length)
|
|
37
42
|
} else if (arg.startsWith('--managed-var=')) {
|
|
38
43
|
const name = arg.slice('--managed-var='.length)
|
|
39
44
|
if (!/^[A-Z][A-Z0-9_]*$/.test(name)) {
|
|
@@ -49,7 +54,7 @@ export const parseCloudflareArgs = (argv: readonly string[]): ParsedCloudflareAr
|
|
|
49
54
|
}
|
|
50
55
|
}
|
|
51
56
|
|
|
52
|
-
return { command, subcommand, env, config, runnerConfig, workerConfig, managedVarNames, dryRun, help }
|
|
57
|
+
return { command, subcommand, env, config, runnerConfig, workerConfig, componentConfig, managedVarNames, dryRun, help }
|
|
53
58
|
}
|
|
54
59
|
|
|
55
60
|
export interface PlatformComponent {
|
|
@@ -57,10 +62,23 @@ export interface PlatformComponent {
|
|
|
57
62
|
readonly configPath: string
|
|
58
63
|
}
|
|
59
64
|
|
|
60
|
-
/**
|
|
61
|
-
export
|
|
65
|
+
/** What `platformComponents` needs off a parsed command line. */
|
|
66
|
+
export type PlatformComponentSelection = Pick<ParsedCloudflareArgs, 'runnerConfig' | 'workerConfig' | 'componentConfig'>
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* One config (`--component-config`) or the executor-then-control pair, never mixed. Both run with
|
|
70
|
+
* `platformResources: 'allow'`, which is why the single form lives here and not on `app deploy`.
|
|
71
|
+
*/
|
|
72
|
+
export const platformComponents = (selection: PlatformComponentSelection): readonly PlatformComponent[] => {
|
|
73
|
+
const { runnerConfig, workerConfig, componentConfig } = selection
|
|
74
|
+
if (componentConfig !== undefined && componentConfig !== '') {
|
|
75
|
+
if (runnerConfig !== undefined || workerConfig !== undefined) {
|
|
76
|
+
throw new Error('`--component-config` deploys one component and cannot be combined with --runner-config/--worker-config')
|
|
77
|
+
}
|
|
78
|
+
return [{ label: componentConfig, configPath: componentConfig }]
|
|
79
|
+
}
|
|
62
80
|
if (runnerConfig === undefined || runnerConfig === '') {
|
|
63
|
-
throw new Error('Missing --runner-config=<path> for `platform deploy`')
|
|
81
|
+
throw new Error('Missing --runner-config=<path> (or --component-config=<path>) for `platform deploy`')
|
|
64
82
|
}
|
|
65
83
|
if (workerConfig === undefined || workerConfig === '') {
|
|
66
84
|
throw new Error('Missing --worker-config=<path> for `platform deploy`')
|
package/src/cli.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type { DeployStepResult } from '@fabrika/provider-contract'
|
|
4
4
|
import { parseCloudflareArgs } from './cli-args'
|
|
5
5
|
import { deployCloudflareConfig } from './command'
|
|
6
|
+
import type { CloudflareDeployResult } from './provider'
|
|
6
7
|
|
|
7
8
|
const USAGE = `Internal Cloudflare app executor
|
|
8
9
|
|
|
@@ -23,32 +24,30 @@ const die = (message: string): never => {
|
|
|
23
24
|
throw new Error(message)
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
const formatDuration = (step:
|
|
27
|
+
const formatDuration = (step: DeployStepResult): string => {
|
|
27
28
|
if (step.startedAt === undefined || step.finishedAt === undefined) {
|
|
28
29
|
return ''
|
|
29
30
|
}
|
|
30
31
|
return ` (${step.finishedAt - step.startedAt}ms)`
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
const ICON: Readonly<Record<
|
|
34
|
-
pending: '·',
|
|
35
|
-
running: '…',
|
|
34
|
+
const ICON: Readonly<Record<DeployStepResult['status'], string>> = {
|
|
36
35
|
succeeded: '✓',
|
|
37
36
|
failed: '✗',
|
|
38
37
|
skipped: '∅',
|
|
39
38
|
}
|
|
40
39
|
|
|
41
|
-
const printResult = (result:
|
|
40
|
+
const printResult = (result: CloudflareDeployResult): void => {
|
|
42
41
|
console.log(`\n${result.appId} → ${result.env}: ${result.status}`)
|
|
43
42
|
for (const step of result.steps) {
|
|
44
|
-
console.log(` ${ICON[step.status]
|
|
43
|
+
console.log(` ${ICON[step.status]} ${step.id} — ${step.status}${formatDuration(step)}`)
|
|
45
44
|
if (step.error !== undefined) {
|
|
46
45
|
console.log(` ${step.error}`)
|
|
47
46
|
}
|
|
48
47
|
}
|
|
49
48
|
}
|
|
50
49
|
|
|
51
|
-
const requireSuccessful = (result:
|
|
50
|
+
const requireSuccessful = (result: CloudflareDeployResult): void => {
|
|
52
51
|
printResult(result)
|
|
53
52
|
if (result.status === 'failed') {
|
|
54
53
|
throw new Error('Cloudflare deployment failed')
|
package/src/codec.ts
CHANGED
|
@@ -1,78 +1,17 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
/** Ephemeral credentials and handles used by one Cloudflare deploy. */
|
|
4
|
-
export interface CloudflareTarget {
|
|
5
|
-
readonly accountId: string
|
|
6
|
-
readonly apiToken: string
|
|
7
|
-
readonly stateNamespace?: string
|
|
8
|
-
readonly propustkaUrl?: string
|
|
9
|
-
readonly adminKey?: string
|
|
10
|
-
}
|
|
1
|
+
import type { ProviderCodec } from '@fabrika/provider-contract'
|
|
2
|
+
import { z } from 'zod'
|
|
11
3
|
|
|
12
4
|
/** A checkout recipe. The Oblaka graph stays executable code in the referenced config module. */
|
|
13
5
|
export interface CloudflareArtifact {
|
|
14
6
|
readonly configPath: string
|
|
15
7
|
}
|
|
16
8
|
|
|
17
|
-
const
|
|
18
|
-
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
19
|
-
throw new Error('expected an object')
|
|
20
|
-
}
|
|
21
|
-
const propertyValue = value[property]
|
|
22
|
-
if (propertyValue === undefined) {
|
|
23
|
-
throw new Error(`missing ${property}`)
|
|
24
|
-
}
|
|
25
|
-
return propertyValue
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const stringProperty = (value: JsonValue, property: string): string => {
|
|
29
|
-
const propertyValue = objectProperty(value, property)
|
|
30
|
-
if (typeof propertyValue !== 'string' || propertyValue === '') {
|
|
31
|
-
throw new Error(`${property} must be a non-empty string`)
|
|
32
|
-
}
|
|
33
|
-
return propertyValue
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const optionalStringProperty = (value: JsonValue, property: string): string | undefined => {
|
|
37
|
-
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
38
|
-
throw new Error('expected an object')
|
|
39
|
-
}
|
|
40
|
-
const propertyValue = value[property]
|
|
41
|
-
if (propertyValue === undefined) {
|
|
42
|
-
return undefined
|
|
43
|
-
}
|
|
44
|
-
if (typeof propertyValue !== 'string' || propertyValue === '') {
|
|
45
|
-
throw new Error(`${property} must be a non-empty string`)
|
|
46
|
-
}
|
|
47
|
-
return propertyValue
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export const cloudflareTargetCodec: ProviderCodec<CloudflareTarget> = {
|
|
51
|
-
version: 1,
|
|
52
|
-
encode: (target) => ({
|
|
53
|
-
accountId: target.accountId,
|
|
54
|
-
apiToken: target.apiToken,
|
|
55
|
-
...(target.stateNamespace === undefined ? {} : { stateNamespace: target.stateNamespace }),
|
|
56
|
-
...(target.propustkaUrl === undefined ? {} : { propustkaUrl: target.propustkaUrl }),
|
|
57
|
-
...(target.adminKey === undefined ? {} : { adminKey: target.adminKey }),
|
|
58
|
-
}),
|
|
59
|
-
decode: (payload) => ({
|
|
60
|
-
accountId: stringProperty(payload, 'accountId'),
|
|
61
|
-
apiToken: stringProperty(payload, 'apiToken'),
|
|
62
|
-
...(optionalStringProperty(payload, 'stateNamespace') === undefined
|
|
63
|
-
? {}
|
|
64
|
-
: { stateNamespace: optionalStringProperty(payload, 'stateNamespace') }),
|
|
65
|
-
...(optionalStringProperty(payload, 'propustkaUrl') === undefined
|
|
66
|
-
? {}
|
|
67
|
-
: { propustkaUrl: optionalStringProperty(payload, 'propustkaUrl') }),
|
|
68
|
-
...(optionalStringProperty(payload, 'adminKey') === undefined ? {} : { adminKey: optionalStringProperty(payload, 'adminKey') }),
|
|
69
|
-
}),
|
|
70
|
-
}
|
|
9
|
+
const artifactSchema = z.object({ configPath: z.string().min(1) })
|
|
71
10
|
|
|
72
11
|
export const cloudflareArtifactCodec: ProviderCodec<CloudflareArtifact> = {
|
|
73
12
|
version: 1,
|
|
74
13
|
encode: (artifact) => ({ configPath: artifact.configPath }),
|
|
75
|
-
decode: (payload) => (
|
|
14
|
+
decode: (payload) => artifactSchema.parse(payload),
|
|
76
15
|
}
|
|
77
16
|
|
|
78
17
|
/** Build the default checkout recipe used by the Cloudflare runner. */
|
package/src/collaborators.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import type { SchemaReconciler } from '@fabrika/provider-contract'
|
|
2
|
+
import { writeFile } from 'node:fs/promises'
|
|
2
3
|
import { resolve } from 'node:path'
|
|
3
4
|
import { type Definition, deploy as oblakaDeploy, type DeployResult as OblakaDeployResult } from 'oblaka-iac'
|
|
4
5
|
import { type CloudflareAppConfig, isCloudflareAppConfig } from './authoring'
|
|
5
|
-
import {
|
|
6
|
+
import { RUNNER_OUTCOME_FILE_ENV, type RunnerOutcomeFile } from './runner-protocol'
|
|
6
7
|
|
|
7
8
|
export interface CommandSpec {
|
|
8
9
|
readonly command: string
|
|
9
10
|
readonly args: string[]
|
|
10
11
|
readonly cwd: string
|
|
11
12
|
readonly env?: Record<string, string>
|
|
13
|
+
/** Names dropped from the INHERITED environment: a child that runs checkout code gets no platform credential. */
|
|
14
|
+
readonly omitEnv?: readonly string[]
|
|
12
15
|
readonly stdin?: string
|
|
13
16
|
readonly signal?: AbortSignal
|
|
14
17
|
}
|
|
@@ -33,6 +36,9 @@ export interface ProvisionInput {
|
|
|
33
36
|
|
|
34
37
|
export type OblakaProvisioner = (input: ProvisionInput) => Promise<OblakaDeployResult>
|
|
35
38
|
|
|
39
|
+
/** Hand the run's outcome back to the runner, which folds it into the terminal status the control plane reads. */
|
|
40
|
+
export type OutcomeWriter = (outcome: RunnerOutcomeFile) => Promise<void>
|
|
41
|
+
|
|
36
42
|
export interface LoadedCloudflareConfig {
|
|
37
43
|
readonly config: CloudflareAppConfig
|
|
38
44
|
readonly cwd: string
|
|
@@ -44,22 +50,35 @@ export interface CloudflareCollaborators {
|
|
|
44
50
|
readonly loadConfig: CloudflareConfigLoader
|
|
45
51
|
readonly runCommand: CommandRunner
|
|
46
52
|
readonly provision: OblakaProvisioner
|
|
47
|
-
readonly
|
|
53
|
+
readonly writeOutcome: OutcomeWriter
|
|
48
54
|
}
|
|
49
55
|
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
const loaded: unknown = await import(
|
|
56
|
+
export const loadCloudflareConfig = async (cwd: string, configPath: string): Promise<LoadedCloudflareConfig & { readonly absolutePath: string }> => {
|
|
57
|
+
const absolutePath = resolve(cwd, configPath)
|
|
58
|
+
const loaded: unknown = await import(absolutePath)
|
|
53
59
|
if (typeof loaded !== 'object' || loaded === null || !('default' in loaded) || !isCloudflareAppConfig(loaded.default)) {
|
|
54
|
-
throw new Error(`Cloudflare config at ${
|
|
60
|
+
throw new Error(`Cloudflare config at ${absolutePath} must export a valid default defineApp(...) value`)
|
|
55
61
|
}
|
|
56
|
-
return { config: loaded.default, cwd: resolve(
|
|
62
|
+
return { config: loaded.default, absolutePath, cwd: resolve(absolutePath, '..') }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const defaultLoadConfig: CloudflareConfigLoader = async (cwd, configPath) => {
|
|
66
|
+
const loaded = await loadCloudflareConfig(cwd, configPath)
|
|
67
|
+
return { config: loaded.config, cwd: loaded.cwd }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const childEnvironment = (spec: CommandSpec): Record<string, string | undefined> => {
|
|
71
|
+
const inherited: Record<string, string | undefined> = { ...process.env }
|
|
72
|
+
for (const name of spec.omitEnv ?? []) {
|
|
73
|
+
delete inherited[name]
|
|
74
|
+
}
|
|
75
|
+
return { ...inherited, ...spec.env }
|
|
57
76
|
}
|
|
58
77
|
|
|
59
78
|
const defaultRunCommand: CommandRunner = async (spec) => {
|
|
60
79
|
const proc = Bun.spawn([spec.command, ...spec.args], {
|
|
61
80
|
cwd: spec.cwd,
|
|
62
|
-
env:
|
|
81
|
+
env: childEnvironment(spec),
|
|
63
82
|
stdin: spec.stdin === undefined ? 'inherit' : new TextEncoder().encode(spec.stdin),
|
|
64
83
|
stdout: 'pipe',
|
|
65
84
|
stderr: 'pipe',
|
|
@@ -94,11 +113,19 @@ const defaultProvision: OblakaProvisioner = (input) =>
|
|
|
94
113
|
dryRun: input.dryRun,
|
|
95
114
|
})
|
|
96
115
|
|
|
116
|
+
const defaultWriteOutcome: OutcomeWriter = async (outcome) => {
|
|
117
|
+
const path = process.env[RUNNER_OUTCOME_FILE_ENV]
|
|
118
|
+
if (path === undefined || path === '') {
|
|
119
|
+
throw new Error(`${RUNNER_OUTCOME_FILE_ENV} is not set: this run has nowhere to report its outcome`)
|
|
120
|
+
}
|
|
121
|
+
await writeFile(path, JSON.stringify(outcome), 'utf8')
|
|
122
|
+
}
|
|
123
|
+
|
|
97
124
|
export const defaultCloudflareCollaborators: CloudflareCollaborators = {
|
|
98
125
|
loadConfig: defaultLoadConfig,
|
|
99
126
|
runCommand: defaultRunCommand,
|
|
100
127
|
provision: defaultProvision,
|
|
101
|
-
|
|
128
|
+
writeOutcome: defaultWriteOutcome,
|
|
102
129
|
}
|
|
103
130
|
|
|
104
131
|
export type { SchemaReconciler }
|
package/src/command.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import { type CloudflareAppConfig, isCloudflareAppConfig } from './authoring'
|
|
5
|
-
import { cloudflareArtifact } from './codec'
|
|
6
|
-
import { cloudflareProvider } from './provider'
|
|
1
|
+
import type { CloudflareAppConfig } from './authoring'
|
|
2
|
+
import { loadCloudflareConfig } from './collaborators'
|
|
3
|
+
import { type CloudflareDeployResult, createCloudflareDeployer } from './provider'
|
|
7
4
|
|
|
8
5
|
const requiredEnvironmentValue = (name: string): string => {
|
|
9
6
|
const value = environmentValue(name)
|
|
@@ -36,7 +33,7 @@ const requiredValues = (names: readonly string[] | undefined): Record<string, st
|
|
|
36
33
|
|
|
37
34
|
/**
|
|
38
35
|
* Decode `FABRIKA_IAM_RETURN_ORIGINS`, the comma-separated set the control plane projects into the
|
|
39
|
-
* deploy environment (ADR-
|
|
36
|
+
* deploy environment (ADR-0022). Undefined for an absent or empty value, so a deploy with nothing to
|
|
40
37
|
* project leaves IAM's registry alone rather than clearing it. Part of the executor's environment
|
|
41
38
|
* contract, exported for the test that pins it; not part of the package's public surface.
|
|
42
39
|
*/
|
|
@@ -51,14 +48,8 @@ export interface LoadedCloudflareCommandConfig {
|
|
|
51
48
|
readonly cwd: string
|
|
52
49
|
}
|
|
53
50
|
|
|
54
|
-
export const loadCloudflareCommandConfig = async (configPath: string, cwd: string): Promise<LoadedCloudflareCommandConfig> =>
|
|
55
|
-
|
|
56
|
-
const loaded: unknown = await import(absolutePath)
|
|
57
|
-
if (typeof loaded !== 'object' || loaded === null || !('default' in loaded) || !isCloudflareAppConfig(loaded.default)) {
|
|
58
|
-
throw new Error(`Cloudflare config at ${absolutePath} must export a valid default defineApp(...) value`)
|
|
59
|
-
}
|
|
60
|
-
return { config: loaded.default, absolutePath, cwd: resolve(absolutePath, '..') }
|
|
61
|
-
}
|
|
51
|
+
export const loadCloudflareCommandConfig = async (configPath: string, cwd: string): Promise<LoadedCloudflareCommandConfig> =>
|
|
52
|
+
loadCloudflareConfig(cwd, configPath)
|
|
62
53
|
|
|
63
54
|
export interface CloudflareCommandDeployOptions {
|
|
64
55
|
readonly env: string
|
|
@@ -67,43 +58,36 @@ export interface CloudflareCommandDeployOptions {
|
|
|
67
58
|
readonly dryRun?: boolean
|
|
68
59
|
readonly stateNamespace?: string
|
|
69
60
|
readonly managedVarNames?: readonly string[]
|
|
61
|
+
/** Defaults to `refuse`: only fabrika deploying its own components may name a platform resource. */
|
|
62
|
+
readonly platformResources?: 'refuse' | 'allow'
|
|
70
63
|
readonly signal?: AbortSignal
|
|
71
64
|
readonly log?: (line: string) => void
|
|
72
65
|
}
|
|
73
66
|
|
|
74
|
-
/**
|
|
75
|
-
export const deployCloudflareConfig = async (options: CloudflareCommandDeployOptions): Promise<
|
|
67
|
+
/** Deploy one config with the account credentials, declared secrets and vars read from the environment. */
|
|
68
|
+
export const deployCloudflareConfig = async (options: CloudflareCommandDeployOptions): Promise<CloudflareDeployResult> => {
|
|
76
69
|
const cwd = options.cwd ?? process.cwd()
|
|
77
70
|
const loaded = await loadCloudflareCommandConfig(options.configPath ?? './fabrika.config.ts', cwd)
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
71
|
+
const deploy = createCloudflareDeployer({
|
|
72
|
+
accountId: requiredEnvironmentValue('CLOUDFLARE_ACCOUNT_ID'),
|
|
73
|
+
apiToken: requiredEnvironmentValue('CLOUDFLARE_API_TOKEN'),
|
|
74
|
+
platformResources: options.platformResources ?? 'refuse',
|
|
75
|
+
})
|
|
83
76
|
const domain = environmentValue('FABRIKA_CONTROL_DOMAIN')
|
|
84
|
-
const iamUrl = environmentValue('FABRIKA_IAM_ISSUER')
|
|
85
|
-
const iamProvisioningKey = environmentValue('FABRIKA_IAM_PROVISIONING_KEY')
|
|
86
77
|
const returnOrigins = parseReturnOrigins(environmentValue('FABRIKA_IAM_RETURN_ORIGINS'))
|
|
87
|
-
|
|
78
|
+
return deploy({
|
|
88
79
|
appId: loaded.config.id,
|
|
89
80
|
env: options.env,
|
|
90
81
|
...(domain === undefined ? {} : { domain }),
|
|
91
82
|
...(returnOrigins === undefined ? {} : { returnOrigins }),
|
|
92
83
|
cwd: loaded.cwd,
|
|
84
|
+
configPath: loaded.absolutePath,
|
|
85
|
+
...(options.stateNamespace === undefined ? {} : { stateNamespace: options.stateNamespace }),
|
|
93
86
|
secrets: declaredValues(loaded.config.pipeline?.secrets),
|
|
94
87
|
vars: declaredValues(loaded.config.pipeline?.vars),
|
|
95
88
|
managedEnvironment: requiredValues(options.managedVarNames),
|
|
96
89
|
dryRun: options.dryRun ?? false,
|
|
97
90
|
signal: options.signal ?? new AbortController().signal,
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
accountId: requiredEnvironmentValue('CLOUDFLARE_ACCOUNT_ID'),
|
|
101
|
-
apiToken: requiredEnvironmentValue('CLOUDFLARE_API_TOKEN'),
|
|
102
|
-
...(options.stateNamespace === undefined ? {} : { stateNamespace: options.stateNamespace }),
|
|
103
|
-
...(iamUrl === undefined ? {} : { propustkaUrl: iamUrl }),
|
|
104
|
-
...(iamProvisioningKey === undefined ? {} : { adminKey: iamProvisioningKey }),
|
|
105
|
-
}),
|
|
106
|
-
artifact: cloudflareProvider.encodeArtifact(cloudflareArtifact(loaded.absolutePath)),
|
|
107
|
-
}
|
|
108
|
-
return deploy(cloudflareProvider.runtime, run)
|
|
91
|
+
log: options.log ?? ((line) => console.log(line)),
|
|
92
|
+
})
|
|
109
93
|
}
|