oblaka-iac 0.0.16 → 0.0.18
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 +1 -1
- package/src/api.ts +76 -0
- package/src/commands/deploy.ts +17 -9
- package/src/commands/load-definition.ts +11 -0
- package/src/commands/resource-processor.ts +25 -4
- package/src/commands/run.ts +3 -1
- package/src/index.ts +4 -0
- package/src/resources/container.ts +8 -0
package/package.json
CHANGED
package/src/api.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import * as fs from 'node:fs/promises'
|
|
2
|
+
import * as path from 'node:path'
|
|
3
|
+
import { CloudflareDeployExecutor } from './commands/deploy'
|
|
4
|
+
import type { Input } from './commands/input'
|
|
5
|
+
import type { ConfigWriter, GeneratedConfig } from './commands/resource-processor'
|
|
6
|
+
import type { Worker } from './resources'
|
|
7
|
+
|
|
8
|
+
export type Definition = Worker
|
|
9
|
+
|
|
10
|
+
export interface DeployOptions {
|
|
11
|
+
accountId: string
|
|
12
|
+
apiToken: string
|
|
13
|
+
env?: string
|
|
14
|
+
stateNamespace?: string
|
|
15
|
+
stateNamespaceId?: string
|
|
16
|
+
dryRun?: boolean
|
|
17
|
+
remote?: boolean
|
|
18
|
+
cwd?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface DeployResult {
|
|
22
|
+
wranglerConfigs: GeneratedConfig[]
|
|
23
|
+
wranglerConfig: GeneratedConfig['config'] | undefined
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Programmatically provision the resources of an already-constructed oblaka definition
|
|
28
|
+
* (the result of `define()` / `new Worker(...)`) against Cloudflare.
|
|
29
|
+
*
|
|
30
|
+
* Unlike the CLI, this skips loading the recipe file and works directly on the in-memory
|
|
31
|
+
* definition. It always returns the generated wrangler config(s); `wrangler.jsonc` is written
|
|
32
|
+
* to disk only when `remote` is set and `dryRun` is not (mirroring the CLI deploy behavior).
|
|
33
|
+
*
|
|
34
|
+
* State is stored in the `cf-state` KV namespace under a per-env key (the `env` value).
|
|
35
|
+
*
|
|
36
|
+
* Note: this does not run `wrangler deploy` — it only provisions resources and returns the
|
|
37
|
+
* config the caller needs to feed to `wrangler deploy` themselves.
|
|
38
|
+
*/
|
|
39
|
+
export const deploy = async (definition: Definition | undefined, options: DeployOptions): Promise<DeployResult> => {
|
|
40
|
+
const env = options.env || 'local'
|
|
41
|
+
const cwd = options.cwd ?? process.cwd()
|
|
42
|
+
const remote = options.remote ?? false
|
|
43
|
+
const dryRun = options.dryRun ?? false
|
|
44
|
+
|
|
45
|
+
const input: Input = {
|
|
46
|
+
main: '',
|
|
47
|
+
env,
|
|
48
|
+
stateNamespace: options.stateNamespace || 'cf-state',
|
|
49
|
+
stateNamespaceId: options.stateNamespaceId,
|
|
50
|
+
accountId: options.accountId,
|
|
51
|
+
apiToken: options.apiToken,
|
|
52
|
+
dryRun,
|
|
53
|
+
remote,
|
|
54
|
+
destroy: false,
|
|
55
|
+
validate: false,
|
|
56
|
+
outStatePath: undefined,
|
|
57
|
+
// Programmatic deploy always supplies an explicit apiToken, so wrangler's
|
|
58
|
+
// OAuth token provider (and its persistence) is never exercised.
|
|
59
|
+
persistToken: false,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const shouldWrite = remote && !dryRun
|
|
63
|
+
const configWriter: ConfigWriter = async (configPath, content) => {
|
|
64
|
+
if (!shouldWrite) {
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
await fs.writeFile(path.resolve(cwd, configPath), content)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const wranglerConfigs = await CloudflareDeployExecutor.execute({ input, definition, configWriter })
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
wranglerConfigs,
|
|
74
|
+
wranglerConfig: wranglerConfigs[0]?.config,
|
|
75
|
+
}
|
|
76
|
+
}
|
package/src/commands/deploy.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import * as fs from 'node:fs/promises'
|
|
2
2
|
import { createWranglerTokenProvider } from '../auth'
|
|
3
3
|
import { CloudflareClient } from '../client'
|
|
4
|
-
import { destroyers } from '../resources'
|
|
4
|
+
import { destroyers, type Worker } from '../resources'
|
|
5
5
|
import { KVStateStorage, type State } from '../state'
|
|
6
6
|
import type { Resource } from '../types'
|
|
7
7
|
import type { Input } from './input'
|
|
8
|
-
import type { ResourceApplier } from './resource-processor'
|
|
8
|
+
import type { ConfigWriter, GeneratedConfig, ResourceApplier } from './resource-processor'
|
|
9
9
|
import { ResourceProcessor } from './resource-processor'
|
|
10
10
|
import { atomic, registerShutdownHandler } from './utils/pm'
|
|
11
11
|
|
|
@@ -13,9 +13,11 @@ export class CloudflareDeployExecutor implements ResourceApplier {
|
|
|
13
13
|
private remaining: Set<string>
|
|
14
14
|
private totalChanged = 0
|
|
15
15
|
|
|
16
|
-
public static async execute({ input }: {
|
|
16
|
+
public static async execute({ input, definition, configWriter }: {
|
|
17
17
|
input: Input
|
|
18
|
-
|
|
18
|
+
definition: Worker | undefined
|
|
19
|
+
configWriter?: ConfigWriter
|
|
20
|
+
}): Promise<GeneratedConfig[]> {
|
|
19
21
|
// An explicit API token wins; otherwise fall back to wrangler's stored
|
|
20
22
|
// OAuth credentials (`wrangler login`).
|
|
21
23
|
const getToken = input.apiToken
|
|
@@ -30,7 +32,7 @@ export class CloudflareDeployExecutor implements ResourceApplier {
|
|
|
30
32
|
const stateKey = { env: input.env }
|
|
31
33
|
const state = await stateStore.get(stateKey)
|
|
32
34
|
|
|
33
|
-
const executor = new CloudflareDeployExecutor(input, state, cfClient)
|
|
35
|
+
const executor = new CloudflareDeployExecutor(input, state, cfClient, configWriter)
|
|
34
36
|
|
|
35
37
|
const unregisterShutdown = registerShutdownHandler(async () => {
|
|
36
38
|
if (input.dryRun) {
|
|
@@ -39,8 +41,9 @@ export class CloudflareDeployExecutor implements ResourceApplier {
|
|
|
39
41
|
await stateStore.set(stateKey, state)
|
|
40
42
|
})
|
|
41
43
|
|
|
44
|
+
let generated: GeneratedConfig[]
|
|
42
45
|
try {
|
|
43
|
-
await executor.run()
|
|
46
|
+
generated = await executor.run(definition)
|
|
44
47
|
} finally {
|
|
45
48
|
unregisterShutdown()
|
|
46
49
|
await atomic(async () => {
|
|
@@ -54,19 +57,22 @@ export class CloudflareDeployExecutor implements ResourceApplier {
|
|
|
54
57
|
if (input.outStatePath) {
|
|
55
58
|
await fs.writeFile(input.outStatePath, JSON.stringify(state, null, '\t'))
|
|
56
59
|
}
|
|
60
|
+
|
|
61
|
+
return generated
|
|
57
62
|
}
|
|
58
63
|
|
|
59
64
|
private constructor(
|
|
60
65
|
private readonly input: Input,
|
|
61
66
|
private readonly state: State,
|
|
62
67
|
private readonly client: CloudflareClient,
|
|
68
|
+
private readonly configWriter?: ConfigWriter,
|
|
63
69
|
) {
|
|
64
70
|
this.remaining = new Set(Object.keys(state.resources || {}))
|
|
65
71
|
}
|
|
66
72
|
|
|
67
|
-
private async run() {
|
|
68
|
-
const resourceProcessor = new ResourceProcessor(this)
|
|
69
|
-
await resourceProcessor.
|
|
73
|
+
private async run(definition: Worker | undefined): Promise<GeneratedConfig[]> {
|
|
74
|
+
const resourceProcessor = new ResourceProcessor(this, this.configWriter)
|
|
75
|
+
const generated = await resourceProcessor.process({ definition, env: this.input.env })
|
|
70
76
|
|
|
71
77
|
this.log(`Changed ${this.totalChanged} resources`)
|
|
72
78
|
|
|
@@ -95,6 +101,8 @@ export class CloudflareDeployExecutor implements ResourceApplier {
|
|
|
95
101
|
this.log(`Destroyed ${resource}:${id}`)
|
|
96
102
|
}
|
|
97
103
|
}
|
|
104
|
+
|
|
105
|
+
return generated
|
|
98
106
|
}
|
|
99
107
|
|
|
100
108
|
private log(message: string) {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as path from 'node:path'
|
|
2
|
+
import type { Worker } from '../resources'
|
|
3
|
+
import type { DefineFn } from '../types'
|
|
4
|
+
|
|
5
|
+
export const loadDefinition = async ({ main, env }: {
|
|
6
|
+
main: string
|
|
7
|
+
env: string
|
|
8
|
+
}): Promise<Worker | undefined> => {
|
|
9
|
+
const defineFn = (await import(path.resolve(main))).default as DefineFn
|
|
10
|
+
return defineFn({ env })
|
|
11
|
+
}
|
|
@@ -18,23 +18,36 @@ export class ResourceProcessor {
|
|
|
18
18
|
public async run({ main, env }: {
|
|
19
19
|
main: string
|
|
20
20
|
env: string
|
|
21
|
-
}) {
|
|
21
|
+
}): Promise<GeneratedConfig[]> {
|
|
22
22
|
const defineFn = (await import(path.resolve(main))).default as DefineFn
|
|
23
23
|
const definition = defineFn({ env })
|
|
24
|
+
return this.process({ definition, env })
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public async process({ definition, env }: {
|
|
28
|
+
definition: Worker | undefined
|
|
29
|
+
env: string
|
|
30
|
+
}): Promise<GeneratedConfig[]> {
|
|
31
|
+
const generated: GeneratedConfig[] = []
|
|
24
32
|
|
|
25
33
|
if (definition) {
|
|
26
34
|
this.workers.push(definition)
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
while (this.workers.length) {
|
|
30
|
-
await this.processWorker(env)
|
|
38
|
+
const result = await this.processWorker(env)
|
|
39
|
+
if (result) {
|
|
40
|
+
generated.push(result)
|
|
41
|
+
}
|
|
31
42
|
}
|
|
43
|
+
|
|
44
|
+
return generated
|
|
32
45
|
}
|
|
33
46
|
|
|
34
|
-
private async processWorker(env: string): Promise<
|
|
47
|
+
private async processWorker(env: string): Promise<GeneratedConfig | undefined> {
|
|
35
48
|
const worker = this.workers.shift()
|
|
36
49
|
if (!worker) {
|
|
37
|
-
return
|
|
50
|
+
return undefined
|
|
38
51
|
}
|
|
39
52
|
const configPath = path.join(worker.options.dir, 'wrangler.jsonc')
|
|
40
53
|
const existingConfig = jsoncParser.parse((await tryReadFile(configPath)) || '{}') as Config
|
|
@@ -87,9 +100,17 @@ export class ResourceProcessor {
|
|
|
87
100
|
|
|
88
101
|
const content = `/**\n * File is auto generated DO NOT EDIT\n */\n${JSON.stringify(config, null, '\t')}\n`
|
|
89
102
|
await this.configWriter(configPath, content)
|
|
103
|
+
|
|
104
|
+
return { path: configPath, config, content }
|
|
90
105
|
}
|
|
91
106
|
}
|
|
92
107
|
|
|
108
|
+
export interface GeneratedConfig {
|
|
109
|
+
path: string
|
|
110
|
+
config: Config
|
|
111
|
+
content: string
|
|
112
|
+
}
|
|
113
|
+
|
|
93
114
|
export type ConfigWriter = (path: string, content: string) => Promise<void>
|
|
94
115
|
|
|
95
116
|
const defaultConfigWriter: ConfigWriter = (path, content) => fs.writeFile(path, content)
|
package/src/commands/run.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { hasWranglerCredentials } from '../auth'
|
|
|
3
3
|
import { CloudflareConfigureExecutor } from './configure'
|
|
4
4
|
import { CloudflareDeployExecutor } from './deploy'
|
|
5
5
|
import { input } from './input'
|
|
6
|
+
import { loadDefinition } from './load-definition'
|
|
6
7
|
import { registerSignalHandler } from './utils/pm'
|
|
7
8
|
import { CloudflareValidateExecutor } from './validate'
|
|
8
9
|
|
|
@@ -19,7 +20,8 @@ if (input.validate) {
|
|
|
19
20
|
console.error('Missing credentials: set --api-token / CLOUDFLARE_API_TOKEN, or run `wrangler login` for OAuth')
|
|
20
21
|
process.exit(1)
|
|
21
22
|
}
|
|
22
|
-
await
|
|
23
|
+
const definition = await loadDefinition({ main: input.main, env: input.env })
|
|
24
|
+
await CloudflareDeployExecutor.execute({ input, definition })
|
|
23
25
|
} else {
|
|
24
26
|
await CloudflareConfigureExecutor.execute({ input })
|
|
25
27
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,10 @@ import type { DefineFn } from './types'
|
|
|
2
2
|
|
|
3
3
|
export * from './resources'
|
|
4
4
|
|
|
5
|
+
export { deploy } from './api'
|
|
6
|
+
export type { Definition, DeployOptions, DeployResult } from './api'
|
|
7
|
+
export type { GeneratedConfig } from './commands/resource-processor'
|
|
8
|
+
|
|
5
9
|
export const define = (cb: DefineFn) => {
|
|
6
10
|
return cb
|
|
7
11
|
}
|
|
@@ -11,6 +11,13 @@ export class Container implements BindableResource<ContainerState> {
|
|
|
11
11
|
name: string
|
|
12
12
|
className: string
|
|
13
13
|
image: string
|
|
14
|
+
/**
|
|
15
|
+
* Docker build context for the image, RELATIVE to the wrangler config directory (emitted as
|
|
16
|
+
* `image_build_context`). When unset, wrangler defaults the context to the Dockerfile's own
|
|
17
|
+
* directory. Set it (e.g. `'../..'`) when the Dockerfile must COPY files from OUTSIDE its
|
|
18
|
+
* directory — e.g. a Dockerfile in `packages/runner` that copies sibling packages from a monorepo root.
|
|
19
|
+
*/
|
|
20
|
+
imageBuildContext?: string
|
|
14
21
|
maxInstances: number
|
|
15
22
|
instanceType: 'dev' | 'basic' | 'standard'
|
|
16
23
|
/**
|
|
@@ -63,6 +70,7 @@ export class Container implements BindableResource<ContainerState> {
|
|
|
63
70
|
class_name: this.options.className,
|
|
64
71
|
name: args.state?.name ?? this.options.name,
|
|
65
72
|
image: this.options.image,
|
|
73
|
+
image_build_context: this.options.imageBuildContext,
|
|
66
74
|
instance_type: this.options.instanceType,
|
|
67
75
|
max_instances: this.options.maxInstances,
|
|
68
76
|
rollout_active_grace_period: this.options.rolloutActiveGracePeriod,
|