@fabrika/cli 0.0.9 → 0.0.12
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 +10 -7
- package/src/control.ts +465 -0
- package/src/index.ts +14 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -29,11 +29,14 @@
|
|
|
29
29
|
"test": "bun test"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@fabrika/
|
|
33
|
-
"@fabrika/
|
|
34
|
-
"@fabrika/
|
|
35
|
-
"@fabrika/
|
|
36
|
-
"@fabrika/
|
|
37
|
-
"@fabrika/
|
|
32
|
+
"@fabrika/app": "^0.0.12",
|
|
33
|
+
"@fabrika/auth-core": "^0.0.12",
|
|
34
|
+
"@fabrika/control-contract": "^0.0.12",
|
|
35
|
+
"@fabrika/installation-cloudflare": "^0.0.12",
|
|
36
|
+
"@fabrika/installation-contract": "^0.0.12",
|
|
37
|
+
"@fabrika/installation-zerops": "^0.0.12",
|
|
38
|
+
"@fabrika/provider-cloudflare": "^0.0.12",
|
|
39
|
+
"@fabrika/provider-contract": "^0.0.12",
|
|
40
|
+
"@fabrika/provider-zerops": "^0.0.12"
|
|
38
41
|
}
|
|
39
42
|
}
|
package/src/control.ts
ADDED
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
// The `fabrika control` command area — the operator's non-browser client for the Delivery control
|
|
2
|
+
// plane ([ADR-0033](../../../docs/decisions/0033-operate-the-control-plane-from-the-cli.md)).
|
|
3
|
+
//
|
|
4
|
+
// It adds no server surface. Every verb is one procedure of `ControlRpcContract`, reached through the
|
|
5
|
+
// SAME typed client the console uses, so the two cannot drift. It loads no provider or installation
|
|
6
|
+
// package: the control API is provider-neutral, and `--provider` here only labels the envelope a
|
|
7
|
+
// registration carries.
|
|
8
|
+
//
|
|
9
|
+
// Output contract, matching `zops`: stdout carries DATA ONLY, progress and errors go to stderr, and
|
|
10
|
+
// `--json` prints the procedure's result verbatim so a caller never parses a table.
|
|
11
|
+
|
|
12
|
+
import { createRpcClient, RpcError } from '@fabrika/app'
|
|
13
|
+
import type { IssueKeyInput, IssueKeyResult } from '@fabrika/auth-core'
|
|
14
|
+
import type {
|
|
15
|
+
AppDto,
|
|
16
|
+
ControlRpcContract,
|
|
17
|
+
DeploymentNamespaceDto,
|
|
18
|
+
JsonValue,
|
|
19
|
+
PlanDeploymentNamespaceRequest,
|
|
20
|
+
RegisterAppRequest,
|
|
21
|
+
RunDto,
|
|
22
|
+
} from '@fabrika/control-contract'
|
|
23
|
+
|
|
24
|
+
const USAGE = `fabrika control — operate the Delivery control plane
|
|
25
|
+
|
|
26
|
+
Usage:
|
|
27
|
+
fabrika control key issue --label=<name> --permissions=<a,b,c> [--expires-in=<seconds>]
|
|
28
|
+
fabrika control apps list
|
|
29
|
+
fabrika control apps get --app=<id>
|
|
30
|
+
fabrika control register --provider=<name> --app=<id> --repo=<url> --env=<name> --manifest=<path>
|
|
31
|
+
[--domain=<host>] [--public-origin=<url>] [--trigger-ref=<ref>]
|
|
32
|
+
[--namespace=<id>] [--installation-id=<n|none>]
|
|
33
|
+
fabrika control namespaces list
|
|
34
|
+
fabrika control namespaces plan --namespace=<id> --env=<name> --preset=<cheap|mid|full> [placement]
|
|
35
|
+
fabrika control namespaces create --namespace=<id> --env=<name> --preset=<cheap|mid|full> [placement]
|
|
36
|
+
fabrika control namespaces reconcile --namespace=<id>
|
|
37
|
+
fabrika control deploy --app=<id> --env=<name> [--ref=<ref>]
|
|
38
|
+
fabrika control runs list [--app=<id>] [--env=<name>] [--limit=<n>]
|
|
39
|
+
fabrika control runs get --run=<id>
|
|
40
|
+
fabrika control runs log --run=<id>
|
|
41
|
+
|
|
42
|
+
\`--installation-id\` has three states: omitted resolves the installation from the organization's
|
|
43
|
+
connected GitHub App, \`none\` registers an anonymous PUBLIC repository source, and a number names one
|
|
44
|
+
installation outright.
|
|
45
|
+
|
|
46
|
+
Namespace placement options, each defaulting to what the preset would choose:
|
|
47
|
+
|
|
48
|
+
--project=<name> the provider project the namespace owns
|
|
49
|
+
--core-package=<LIGHT|SERIOUS> defaults to SERIOUS for \`prod\`, LIGHT elsewhere
|
|
50
|
+
--public-access=<custom-domain|zerops-subdomain>
|
|
51
|
+
--exclusive-app=<id> required by, and only by, the \`full\` preset
|
|
52
|
+
|
|
53
|
+
\`namespaces plan\` is a preview and changes nothing; \`namespaces create\` plans and then commits
|
|
54
|
+
that exact plan, which is what the console's two-step form does.
|
|
55
|
+
|
|
56
|
+
Every command accepts --json, which prints the procedure's result verbatim.
|
|
57
|
+
|
|
58
|
+
The control origin is not a credential and takes either form:
|
|
59
|
+
|
|
60
|
+
--url=<origin> FABRIKA_CONTROL_URL
|
|
61
|
+
|
|
62
|
+
Credentials are read from the ENVIRONMENT ONLY and have no flag, so they cannot reach a CI log or a
|
|
63
|
+
process listing:
|
|
64
|
+
|
|
65
|
+
FABRIKA_CONTROL_KEY required a machine \`px_\` credential for the control app
|
|
66
|
+
|
|
67
|
+
\`key issue\` mints that credential and therefore authenticates differently — it presents the
|
|
68
|
+
installation's IAM RPC key as transport authentication and its provisioning key as the ISSUING
|
|
69
|
+
credential. Both are read from the environment only:
|
|
70
|
+
|
|
71
|
+
FABRIKA_IAM_RPC_URL required IAM's own origin (its \`/rpc\` surface), or --iam-url
|
|
72
|
+
FABRIKA_IAM_RPC_KEY required the installation's IAM RPC key
|
|
73
|
+
FABRIKA_IAM_PROVISIONING_KEY required the installation's seeded \`px_\` provisioning key
|
|
74
|
+
FABRIKA_CONTROL_APP_ID optional the control app's IAM id (default \`vozka\`)
|
|
75
|
+
|
|
76
|
+
Connecting a GitHub source is NOT here and will not be: App creation is a browser flow whose
|
|
77
|
+
authorization requires a human principal (ADR-0031). Use Settings → Source in the console.
|
|
78
|
+
`
|
|
79
|
+
|
|
80
|
+
const DEFAULT_CONTROL_APP_ID = 'vozka'
|
|
81
|
+
|
|
82
|
+
interface Flags {
|
|
83
|
+
readonly values: ReadonlyMap<string, string>
|
|
84
|
+
readonly json: boolean
|
|
85
|
+
readonly positional: readonly string[]
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Split `--key=value` options from positional words; `--json` is the one flag every verb shares. */
|
|
89
|
+
export const parseControlFlags = (argv: readonly string[]): Flags => {
|
|
90
|
+
const values = new Map<string, string>()
|
|
91
|
+
const positional: string[] = []
|
|
92
|
+
let json = false
|
|
93
|
+
for (const arg of argv) {
|
|
94
|
+
if (arg === '--json') {
|
|
95
|
+
json = true
|
|
96
|
+
} else if (arg.startsWith('--')) {
|
|
97
|
+
const eq = arg.indexOf('=')
|
|
98
|
+
if (eq === -1) {
|
|
99
|
+
throw new Error(`Option ${arg} needs a value: ${arg}=<value>`)
|
|
100
|
+
}
|
|
101
|
+
values.set(arg.slice(2, eq), arg.slice(eq + 1))
|
|
102
|
+
} else {
|
|
103
|
+
positional.push(arg)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return { values, json, positional }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const required = (flags: Flags, name: string): string => {
|
|
110
|
+
const value = flags.values.get(name)
|
|
111
|
+
if (value === undefined || value === '') {
|
|
112
|
+
throw new Error(`--${name} is required`)
|
|
113
|
+
}
|
|
114
|
+
return value
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const optional = (flags: Flags, name: string): string | undefined => {
|
|
118
|
+
const value = flags.values.get(name)
|
|
119
|
+
return value === undefined || value === '' ? undefined : value
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const positiveInteger = (raw: string, name: string): number => {
|
|
123
|
+
const parsed = Number(raw)
|
|
124
|
+
if (!Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
125
|
+
throw new Error(`--${name} must be a positive integer`)
|
|
126
|
+
}
|
|
127
|
+
return parsed
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** An origin from the flag, else the environment. Trailing slashes are dropped so paths concatenate. */
|
|
131
|
+
const origin = (flags: Flags, flag: string, variable: string, env: Readonly<Record<string, string | undefined>>): string => {
|
|
132
|
+
const value = flags.values.get(flag) ?? env[variable]
|
|
133
|
+
if (value === undefined || value.trim() === '') {
|
|
134
|
+
throw new Error(`Control origin is required. Pass --${flag}=<origin> or set ${variable}.`)
|
|
135
|
+
}
|
|
136
|
+
return value.trim().replace(/\/+$/, '')
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const secret = (variable: string, env: Readonly<Record<string, string | undefined>>): string => {
|
|
140
|
+
const value = env[variable]
|
|
141
|
+
if (value === undefined || value.trim() === '') {
|
|
142
|
+
throw new Error(`${variable} is required (environment only — it has no flag).`)
|
|
143
|
+
}
|
|
144
|
+
return value.trim()
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** The console's client with a bearer instead of a cookie, and no SSO bounce: there is no browser to send. */
|
|
148
|
+
const controlClient = (flags: Flags, env: Readonly<Record<string, string | undefined>>) => {
|
|
149
|
+
const base = origin(flags, 'url', 'FABRIKA_CONTROL_URL', env)
|
|
150
|
+
const key = secret('FABRIKA_CONTROL_KEY', env)
|
|
151
|
+
return createRpcClient<ControlRpcContract>({
|
|
152
|
+
baseUrl: `${base}/api/rpc`,
|
|
153
|
+
fetch: (input, init) => {
|
|
154
|
+
const headers = new Headers(init?.headers)
|
|
155
|
+
headers.set('authorization', `Bearer ${key}`)
|
|
156
|
+
return fetch(input, { ...init, headers })
|
|
157
|
+
},
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const readJsonFile = async (path: string): Promise<JsonValue> => {
|
|
162
|
+
const file = Bun.file(path)
|
|
163
|
+
if (!(await file.exists())) {
|
|
164
|
+
throw new Error(`No such file: ${path}`)
|
|
165
|
+
}
|
|
166
|
+
const text = await file.text()
|
|
167
|
+
try {
|
|
168
|
+
const parsed: unknown = JSON.parse(text)
|
|
169
|
+
return asJsonValue(parsed, path)
|
|
170
|
+
} catch (error) {
|
|
171
|
+
throw error instanceof SyntaxError ? new Error(`${path} is not valid JSON`) : error
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** `JSON.parse` returns `unknown`; narrow it to the contract's own JSON type without a cast. */
|
|
176
|
+
const asJsonValue = (value: unknown, path: string): JsonValue => {
|
|
177
|
+
if (value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
178
|
+
return value
|
|
179
|
+
}
|
|
180
|
+
if (Array.isArray(value)) {
|
|
181
|
+
return value.map((entry) => asJsonValue(entry, path))
|
|
182
|
+
}
|
|
183
|
+
if (typeof value === 'object') {
|
|
184
|
+
const out: Record<string, JsonValue> = {}
|
|
185
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
186
|
+
out[key] = asJsonValue(entry, path)
|
|
187
|
+
}
|
|
188
|
+
return out
|
|
189
|
+
}
|
|
190
|
+
throw new Error(`${path} contains a value JSON cannot carry`)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const emit = (flags: Flags, result: unknown, render: () => string): void => {
|
|
194
|
+
console.info(flags.json ? JSON.stringify(result, null, 2) : render())
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** `fabrika app build` writes `manifestVersion`; it is the version the provider's artifact codec accepts. */
|
|
198
|
+
const manifestVersion = (manifest: JsonValue, path: string): number => {
|
|
199
|
+
const version = typeof manifest === 'object' && manifest !== null && !Array.isArray(manifest)
|
|
200
|
+
? Reflect.get(manifest, 'manifestVersion')
|
|
201
|
+
: undefined
|
|
202
|
+
if (typeof version !== 'number' || !Number.isSafeInteger(version) || version <= 0) {
|
|
203
|
+
throw new Error(`${path} declares no manifestVersion — run \`fabrika app build\` to produce one`)
|
|
204
|
+
}
|
|
205
|
+
return version
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const appLine = (app: AppDto): string => `${app.id}\t${app.repoUrl}\t${app.defaultBranch}`
|
|
209
|
+
|
|
210
|
+
const runLine = (run: RunDto): string => `${run.id}\t${run.appId}\t${run.env}\t${run.status}\t${run.ref}`
|
|
211
|
+
|
|
212
|
+
const runKeyIssue = async (flags: Flags, env: Readonly<Record<string, string | undefined>>): Promise<void> => {
|
|
213
|
+
const permissions = required(flags, 'permissions').split(',').map((entry) => entry.trim()).filter((entry) => entry !== '')
|
|
214
|
+
if (permissions.length === 0) {
|
|
215
|
+
throw new Error('--permissions needs at least one action')
|
|
216
|
+
}
|
|
217
|
+
const expiresIn = optional(flags, 'expires-in')
|
|
218
|
+
const body: IssueKeyInput = {
|
|
219
|
+
app: env['FABRIKA_CONTROL_APP_ID']?.trim() || DEFAULT_CONTROL_APP_ID,
|
|
220
|
+
credential: secret('FABRIKA_IAM_PROVISIONING_KEY', env),
|
|
221
|
+
requestId: crypto.randomUUID(),
|
|
222
|
+
service: { label: required(flags, 'label'), permissions },
|
|
223
|
+
label: required(flags, 'label'),
|
|
224
|
+
...(expiresIn === undefined ? {} : { expiresAt: Math.floor(Date.now() / 1000) + positiveInteger(expiresIn, 'expires-in') }),
|
|
225
|
+
}
|
|
226
|
+
const base = origin(flags, 'iam-url', 'FABRIKA_IAM_RPC_URL', env)
|
|
227
|
+
const response = await fetch(`${base}/rpc/issueKey`, {
|
|
228
|
+
method: 'POST',
|
|
229
|
+
headers: { authorization: `Bearer ${secret('FABRIKA_IAM_RPC_KEY', env)}`, 'content-type': 'application/json' },
|
|
230
|
+
body: JSON.stringify(body),
|
|
231
|
+
})
|
|
232
|
+
if (!response.ok) {
|
|
233
|
+
// The body can quote what came off the wire, so report the status and nothing else.
|
|
234
|
+
throw new Error(`IAM refused the issue request: HTTP ${response.status}`)
|
|
235
|
+
}
|
|
236
|
+
const result: unknown = await response.json()
|
|
237
|
+
const outcome = decodeIssueKeyResult(result)
|
|
238
|
+
if (!outcome.ok) {
|
|
239
|
+
throw new Error(`IAM denied the issue request: ${outcome.reason}`)
|
|
240
|
+
}
|
|
241
|
+
// The token is the command's DATA and goes to stdout alone; the handles a caller needs in order to
|
|
242
|
+
// revoke it later go to stderr, so `--quiet`-style capture gets the credential and nothing else.
|
|
243
|
+
console.error(`credential ${outcome.id}${outcome.principalId === undefined ? '' : `, principal ${outcome.principalId}`}`)
|
|
244
|
+
console.info(flags.json ? JSON.stringify(outcome, null, 2) : outcome.token)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const decodeIssueKeyResult = (value: unknown): IssueKeyResult => {
|
|
248
|
+
if (typeof value !== 'object' || value === null) {
|
|
249
|
+
throw new Error('IAM returned an unreadable issueKey result')
|
|
250
|
+
}
|
|
251
|
+
const ok = Reflect.get(value, 'ok')
|
|
252
|
+
if (ok === false) {
|
|
253
|
+
const reason = Reflect.get(value, 'reason')
|
|
254
|
+
return { ok: false, reason: isIssueKeyDenial(reason) ? reason : 'not_allowed' }
|
|
255
|
+
}
|
|
256
|
+
const token = Reflect.get(value, 'token')
|
|
257
|
+
const id = Reflect.get(value, 'id')
|
|
258
|
+
const principalId = Reflect.get(value, 'principalId')
|
|
259
|
+
if (ok !== true || typeof token !== 'string' || typeof id !== 'string') {
|
|
260
|
+
throw new Error('IAM returned an unreadable issueKey result')
|
|
261
|
+
}
|
|
262
|
+
return { ok: true, token, id, ...(typeof principalId === 'string' ? { principalId } : {}) }
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const ISSUE_KEY_DENIALS = ['missing_token', 'invalid_token', 'unknown_principal', 'disabled', 'not_allowed'] as const
|
|
266
|
+
|
|
267
|
+
const isIssueKeyDenial = (value: unknown): value is (typeof ISSUE_KEY_DENIALS)[number] =>
|
|
268
|
+
typeof value === 'string' && ISSUE_KEY_DENIALS.some((denial) => denial === value)
|
|
269
|
+
|
|
270
|
+
const runRegister = async (flags: Flags, provider: string | undefined, env: Readonly<Record<string, string | undefined>>): Promise<void> => {
|
|
271
|
+
if (provider === undefined || provider === '') {
|
|
272
|
+
throw new Error('--provider=<name> is required: it labels the envelope this registration carries.')
|
|
273
|
+
}
|
|
274
|
+
const manifestPath = required(flags, 'manifest')
|
|
275
|
+
const manifest = await readJsonFile(manifestPath)
|
|
276
|
+
// The envelope's version is the manifest's own, never a literal: `fabrika app build` writes the
|
|
277
|
+
// version its provider's codec accepts, and a hardcoded one goes stale the moment that codec moves.
|
|
278
|
+
const version = manifestVersion(manifest, manifestPath)
|
|
279
|
+
const installationId = optional(flags, 'installation-id')
|
|
280
|
+
// Three states, because the registry has three: absent resolves from the connected GitHub App,
|
|
281
|
+
// `none` is the anonymous public-repository path, and a number names one installation outright.
|
|
282
|
+
const anonymousSource = installationId === 'none'
|
|
283
|
+
const domain = optional(flags, 'domain')
|
|
284
|
+
const publicOrigin = optional(flags, 'public-origin')
|
|
285
|
+
const triggerRef = optional(flags, 'trigger-ref')
|
|
286
|
+
const namespaceId = optional(flags, 'namespace')
|
|
287
|
+
const body: RegisterAppRequest = {
|
|
288
|
+
id: required(flags, 'app'),
|
|
289
|
+
repoUrl: required(flags, 'repo'),
|
|
290
|
+
env: required(flags, 'env'),
|
|
291
|
+
// An empty placeholder: registration DISCOVERS the real target from the manifest and replaces this.
|
|
292
|
+
target: { provider, version, payload: {} },
|
|
293
|
+
artifact: { provider, version, payload: manifest },
|
|
294
|
+
...(installationId === undefined
|
|
295
|
+
// Blank → resolve from the installed GitHub App, which is what the console's empty field means.
|
|
296
|
+
? { resolveInstallationId: true }
|
|
297
|
+
: anonymousSource
|
|
298
|
+
? { githubInstallationId: null }
|
|
299
|
+
: { githubInstallationId: positiveInteger(installationId, 'installation-id') }),
|
|
300
|
+
...(domain === undefined ? {} : { domain }),
|
|
301
|
+
...(publicOrigin === undefined ? {} : { publicOrigin }),
|
|
302
|
+
...(triggerRef === undefined ? {} : { triggerRef }),
|
|
303
|
+
...(namespaceId === undefined ? {} : { namespaceId }),
|
|
304
|
+
}
|
|
305
|
+
const result = await controlClient(flags, env).register(body)
|
|
306
|
+
emit(flags, result, () => `${result.app.id}\t${result.env.env}\t${result.env.provider}`)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const runApps = async (verb: string | undefined, flags: Flags, env: Readonly<Record<string, string | undefined>>): Promise<void> => {
|
|
310
|
+
const client = controlClient(flags, env)
|
|
311
|
+
if (verb === 'list') {
|
|
312
|
+
const result = await client.apps.list()
|
|
313
|
+
emit(flags, result, () => result.items.map(appLine).join('\n'))
|
|
314
|
+
return
|
|
315
|
+
}
|
|
316
|
+
if (verb === 'get') {
|
|
317
|
+
const result = await client.apps.get({ appId: required(flags, 'app') })
|
|
318
|
+
emit(flags, result, () => appLine(result))
|
|
319
|
+
return
|
|
320
|
+
}
|
|
321
|
+
throw new Error(`Unknown \`control apps\` verb: ${verb ?? '(missing)'}`)
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const runRuns = async (verb: string | undefined, flags: Flags, env: Readonly<Record<string, string | undefined>>): Promise<void> => {
|
|
325
|
+
const client = controlClient(flags, env)
|
|
326
|
+
if (verb === 'list') {
|
|
327
|
+
const appId = optional(flags, 'app')
|
|
328
|
+
const environment = optional(flags, 'env')
|
|
329
|
+
const limit = optional(flags, 'limit')
|
|
330
|
+
const result = await client.runs.list({
|
|
331
|
+
...(appId === undefined ? {} : { appId }),
|
|
332
|
+
...(environment === undefined ? {} : { env: environment }),
|
|
333
|
+
...(limit === undefined ? {} : { limit: positiveInteger(limit, 'limit') }),
|
|
334
|
+
})
|
|
335
|
+
emit(flags, result, () => result.items.map(runLine).join('\n'))
|
|
336
|
+
return
|
|
337
|
+
}
|
|
338
|
+
if (verb === 'get') {
|
|
339
|
+
const result = await client.runs.get({ runId: required(flags, 'run') })
|
|
340
|
+
emit(flags, result, () => runLine(result))
|
|
341
|
+
return
|
|
342
|
+
}
|
|
343
|
+
if (verb === 'log') {
|
|
344
|
+
const result = await client.runs.log({ runId: required(flags, 'run') })
|
|
345
|
+
emit(flags, result, () => result.lines.map((line) => line.text).join('\n'))
|
|
346
|
+
return
|
|
347
|
+
}
|
|
348
|
+
throw new Error(`Unknown \`control runs\` verb: ${verb ?? '(missing)'}`)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const namespaceLine = (namespace: DeploymentNamespaceDto): string =>
|
|
352
|
+
`${namespace.id}\t${namespace.env}\t${namespace.state}\t${namespace.exclusiveAppId ?? '-'}`
|
|
353
|
+
|
|
354
|
+
/** Placement flags are the preset's overrides, so only the ones actually given are sent. */
|
|
355
|
+
const namespacePlan = (flags: Flags): PlanDeploymentNamespaceRequest => {
|
|
356
|
+
const exclusiveAppId = optional(flags, 'exclusive-app')
|
|
357
|
+
const options: Record<string, JsonValue> = {}
|
|
358
|
+
const project = optional(flags, 'project')
|
|
359
|
+
const corePackage = optional(flags, 'core-package')
|
|
360
|
+
const publicAccess = optional(flags, 'public-access')
|
|
361
|
+
if (project !== undefined) options['projectName'] = project
|
|
362
|
+
if (corePackage !== undefined) options['corePackage'] = corePackage
|
|
363
|
+
if (publicAccess !== undefined) options['publicAccess'] = publicAccess
|
|
364
|
+
return {
|
|
365
|
+
id: required(flags, 'namespace'),
|
|
366
|
+
env: required(flags, 'env'),
|
|
367
|
+
preset: required(flags, 'preset'),
|
|
368
|
+
...(exclusiveAppId === undefined ? {} : { exclusiveAppId }),
|
|
369
|
+
...(Object.keys(options).length === 0 ? {} : { options }),
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const runNamespaces = async (verb: string | undefined, flags: Flags, env: Readonly<Record<string, string | undefined>>): Promise<void> => {
|
|
374
|
+
const client = controlClient(flags, env)
|
|
375
|
+
if (verb === 'list') {
|
|
376
|
+
const result = await client.namespaces.list()
|
|
377
|
+
emit(flags, result, () => result.items.map(namespaceLine).join('\n'))
|
|
378
|
+
return
|
|
379
|
+
}
|
|
380
|
+
if (verb === 'plan') {
|
|
381
|
+
const result = await client.namespaces.plan(namespacePlan(flags))
|
|
382
|
+
emit(flags, result, () => JSON.stringify(result.namespace.target.payload))
|
|
383
|
+
return
|
|
384
|
+
}
|
|
385
|
+
if (verb === 'reconcile') {
|
|
386
|
+
// Provisioning checkpoints its progress, so a namespace left `failed` by a timeout or a denied
|
|
387
|
+
// call is resumed by reconciling it again rather than by creating a second one.
|
|
388
|
+
const result = await client.namespaces.reconcile({ namespaceId: required(flags, 'namespace') })
|
|
389
|
+
emit(flags, result, () => namespaceLine(result))
|
|
390
|
+
return
|
|
391
|
+
}
|
|
392
|
+
if (verb === 'create') {
|
|
393
|
+
const request = namespacePlan(flags)
|
|
394
|
+
// Plan then commit THAT plan, exactly as the console's two-step form does — never a second
|
|
395
|
+
// derivation, which could differ if the provider's defaults moved between the two calls.
|
|
396
|
+
const planned = await client.namespaces.plan(request)
|
|
397
|
+
const result = await client.namespaces.create({
|
|
398
|
+
id: request.id,
|
|
399
|
+
env: request.env,
|
|
400
|
+
target: planned.namespace.target,
|
|
401
|
+
...(request.exclusiveAppId === undefined ? {} : { exclusiveAppId: request.exclusiveAppId }),
|
|
402
|
+
})
|
|
403
|
+
emit(flags, result, () => namespaceLine(result))
|
|
404
|
+
return
|
|
405
|
+
}
|
|
406
|
+
throw new Error(`Unknown \`control namespaces\` verb: ${verb ?? '(missing)'}`)
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const runDeploy = async (flags: Flags, env: Readonly<Record<string, string | undefined>>): Promise<void> => {
|
|
410
|
+
const ref = optional(flags, 'ref')
|
|
411
|
+
const result = await controlClient(flags, env).deploy({
|
|
412
|
+
appId: required(flags, 'app'),
|
|
413
|
+
env: required(flags, 'env'),
|
|
414
|
+
...(ref === undefined ? {} : { ref }),
|
|
415
|
+
})
|
|
416
|
+
emit(flags, result, () => runLine(result))
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* `command` is the group (`key`, `apps`, `runs`, `register`, `deploy`); the verb, where a group has
|
|
421
|
+
* one, is the first positional word after it. `provider` reaches here only to label an envelope.
|
|
422
|
+
*/
|
|
423
|
+
export const runControlCli = async (
|
|
424
|
+
command: string | undefined,
|
|
425
|
+
argv: readonly string[],
|
|
426
|
+
provider?: string,
|
|
427
|
+
env: Readonly<Record<string, string | undefined>> = process.env,
|
|
428
|
+
): Promise<void> => {
|
|
429
|
+
const flags = parseControlFlags(argv)
|
|
430
|
+
const verb = flags.positional[0]
|
|
431
|
+
try {
|
|
432
|
+
switch (command) {
|
|
433
|
+
case undefined:
|
|
434
|
+
console.info(USAGE)
|
|
435
|
+
return
|
|
436
|
+
case 'key':
|
|
437
|
+
if (verb !== 'issue') throw new Error(`Unknown \`control key\` verb: ${verb ?? '(missing)'}`)
|
|
438
|
+
await runKeyIssue(flags, env)
|
|
439
|
+
return
|
|
440
|
+
case 'apps':
|
|
441
|
+
await runApps(verb, flags, env)
|
|
442
|
+
return
|
|
443
|
+
case 'runs':
|
|
444
|
+
await runRuns(verb, flags, env)
|
|
445
|
+
return
|
|
446
|
+
case 'namespaces':
|
|
447
|
+
await runNamespaces(verb, flags, env)
|
|
448
|
+
return
|
|
449
|
+
case 'register':
|
|
450
|
+
await runRegister(flags, provider, env)
|
|
451
|
+
return
|
|
452
|
+
case 'deploy':
|
|
453
|
+
await runDeploy(flags, env)
|
|
454
|
+
return
|
|
455
|
+
default:
|
|
456
|
+
throw new Error(`Unknown control command: ${command}\n\n${USAGE}`)
|
|
457
|
+
}
|
|
458
|
+
} catch (error) {
|
|
459
|
+
// An RPC denial is an ANSWER, not a transport fault; keep its type so a caller can tell "the
|
|
460
|
+
// control plane said no" from "the control plane was unreachable".
|
|
461
|
+
throw error instanceof RpcError ? new Error(`${error.type}: ${error.message}`) : error
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
export { USAGE as CONTROL_USAGE }
|
package/src/index.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { installationCliFromModule, isInstallationCommand, supportsInstallationCommand } from '@fabrika/installation-contract'
|
|
4
4
|
import { authoredAppProvider } from '@fabrika/provider-contract'
|
|
5
5
|
import { resolve } from 'node:path'
|
|
6
|
+
import { CONTROL_USAGE, runControlCli } from './control.js'
|
|
6
7
|
|
|
7
8
|
const USAGE = `fabrika — application platform CLI
|
|
8
9
|
|
|
@@ -14,10 +15,15 @@ Usage:
|
|
|
14
15
|
fabrika app deploy [--provider=cloudflare] --env=<env> [--config=<path>] [--dry-run]
|
|
15
16
|
fabrika app build [--provider=zerops] --env=<env> [--config=<path>] [--output=<path>]
|
|
16
17
|
fabrika namespace <plan|create|adopt|reconcile> --provider=zerops [provider options]
|
|
18
|
+
fabrika control <key|apps|namespaces|register|deploy|runs> [options]
|
|
17
19
|
|
|
18
20
|
For app commands, the provider is read from the config returned by the selected provider's defineApp().
|
|
19
21
|
An explicit --provider is required only when no app config is available.
|
|
20
22
|
|
|
23
|
+
\`fabrika control\` is the operator's non-browser client for the control plane (ADR-0033). It is
|
|
24
|
+
provider-neutral, loads no installation package, and speaks the same typed API the console does. Run
|
|
25
|
+
\`fabrika control --help\` for its surface.
|
|
26
|
+
|
|
21
27
|
Which platform commands exist, and how WIDE each one is, is the provider's own answer — Zerops'
|
|
22
28
|
\`platform deploy\` owns the whole ordered sequence while Cloudflare's is one step of a scaffolded
|
|
23
29
|
workflow (ADR-0027), and only Zerops offers \`platform install\`, the from-scratch bring-up. Run
|
|
@@ -173,6 +179,10 @@ export const runCli = async (argv: readonly string[]): Promise<void> => {
|
|
|
173
179
|
console.info(installationCliFromModule(await installationModule(parsed.provider)).usage)
|
|
174
180
|
return
|
|
175
181
|
}
|
|
182
|
+
if (parsed.help && parsed.area === 'control') {
|
|
183
|
+
console.info(CONTROL_USAGE)
|
|
184
|
+
return
|
|
185
|
+
}
|
|
176
186
|
if (parsed.help || parsed.area === undefined) {
|
|
177
187
|
console.info(USAGE)
|
|
178
188
|
return
|
|
@@ -189,6 +199,10 @@ export const runCli = async (argv: readonly string[]): Promise<void> => {
|
|
|
189
199
|
await runNamespace(parsed)
|
|
190
200
|
return
|
|
191
201
|
}
|
|
202
|
+
if (parsed.area === 'control') {
|
|
203
|
+
await runControlCli(parsed.command, parsed.rest, parsed.provider)
|
|
204
|
+
return
|
|
205
|
+
}
|
|
192
206
|
throw new Error(`Unknown command area: ${parsed.area}\n\n${USAGE}`)
|
|
193
207
|
}
|
|
194
208
|
|