-
Notifications
You must be signed in to change notification settings - Fork 267
/
actions.ts
333 lines (280 loc) · 10 KB
/
actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright (C) 2018-2023 Garden Technologies, Inc. <info@garden.io>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import chalk from "chalk"
import { merge } from "lodash"
import type { ActionConfig } from "../../actions/types"
import { Action, ActionMode, ExecutedAction, ResolvedAction } from "../../actions/types"
import { Garden } from "../../garden"
import { GardenModule } from "../../types/module"
import { deline } from "../../util/string"
import {
DeepPrimitiveMap,
joi,
joiIdentifier,
joiIdentifierMap,
joiPrimitive,
joiVariables,
PrimitiveMap,
} from "../common"
import { ProviderMap } from "../provider"
import { ConfigContext, ErrorContext, ParentContext, schema, TemplateContext } from "./base"
import { exampleVersion, OutputConfigContext } from "./module"
import { TemplatableConfigContext } from "./project"
import { DOCS_BASE_URL } from "../../constants"
import type { WorkflowConfig } from "../workflow"
function mergeVariables({ garden, variables }: { garden: Garden; variables: DeepPrimitiveMap }): DeepPrimitiveMap {
const mergedVariables: DeepPrimitiveMap = {}
merge(mergedVariables, garden.variables)
merge(mergedVariables, variables)
merge(mergedVariables, garden.variableOverrides)
return mergedVariables
}
type ActionConfigThisContextParams = Pick<ActionReferenceContextParams, "name" | "mode">
const actionNameSchema = joiIdentifier().description(`The name of the action.`)
const actionModeSchema = joi
.string()
.required()
.default("default")
.allow("default", "sync", "local")
.description(
"The mode that the action should be executed in (e.g. 'sync' or 'local' for Deploy actions). Set to 'default' if no special mode is being used."
)
.example("sync")
class ActionConfigThisContext extends ConfigContext {
@schema(actionNameSchema)
public name: string
@schema(actionModeSchema)
public mode: ActionMode
constructor(root: ConfigContext, { name, mode }: ActionConfigThisContextParams) {
super(root)
this.name = name
this.mode = mode
}
}
interface ActionConfigContextParams {
garden: Garden
config: ActionConfig | WorkflowConfig
thisContextParams: ActionConfigThisContextParams
variables: DeepPrimitiveMap
}
/**
* This is available to built-in fields on action configs. See ActionSpecContext below for the context available
* for action spec and variables.
*/
export class ActionConfigContext extends TemplatableConfigContext {
@schema(ActionConfigThisContext.getSchema().description("Information about the action currently being resolved."))
public this: ActionConfigThisContext
constructor({ garden, config, thisContextParams, variables }: ActionConfigContextParams) {
const mergedVariables = mergeVariables({ garden, variables })
super(garden, config)
this.this = new ActionConfigThisContext(this, thisContextParams)
this.variables = this.var = mergedVariables
}
}
interface ActionReferenceContextParams {
root: ConfigContext
name: string
disabled: boolean
buildPath: string
sourcePath: string
mode: ActionMode
variables: DeepPrimitiveMap
}
export class ActionReferenceContext extends ConfigContext {
@schema(actionNameSchema)
public name: string
@schema(joi.boolean().required().description("Whether the action is disabled.").example(true))
public disabled: boolean
@schema(
joi
.string()
.required()
.description("The local path to the action build directory.")
.example("/my/project/.garden/build/my-action")
)
public buildPath: string
@schema(
joi
.string()
.required()
.description("The local path to the action source directory.")
.example("/my/project/my-action")
)
public sourcePath: string
@schema(actionModeSchema)
public mode: ActionMode
@schema(joiVariables().required().description("The variables configured on the action.").example({ foo: "bar" }))
public var: DeepPrimitiveMap
constructor({ root, name, disabled, buildPath, sourcePath, mode, variables }: ActionReferenceContextParams) {
super(root)
this.name = name
this.disabled = disabled
this.buildPath = buildPath
this.sourcePath = sourcePath
this.var = variables
this.mode = mode
}
}
interface ActionResultContextParams extends ActionReferenceContextParams {
outputs: PrimitiveMap
version: string
}
class ActionResultContext extends ActionReferenceContext {
@schema(
joiIdentifierMap(
joiPrimitive().description(
deline`
The action output value. Refer to individual [action/module type references](${DOCS_BASE_URL}/reference) for details.
`
)
)
.required()
.description(
`The outputs defined by the action (see individual action/module type [references](${DOCS_BASE_URL}/reference) for details).`
)
.meta({ keyPlaceholder: "<output-name>" })
)
public outputs: PrimitiveMap
@schema(joi.string().required().description("The current version of the action.").example(exampleVersion))
public version: string
constructor(params: ActionResultContextParams) {
super(params)
this.outputs = params.outputs
this.version = params.version
}
}
const _actionResultContextSchema = joiIdentifierMap(ActionResultContext.getSchema())
.required()
.meta({ keyPlaceholder: "<action-name>" })
const actionResultContextSchema = (kind: string) =>
_actionResultContextSchema.description(`Information about a ${kind} action dependency, including its outputs.`)
class ActionReferencesContext extends ConfigContext {
@schema(actionResultContextSchema("Build"))
public build: Map<string, ActionResultContext>
@schema(actionResultContextSchema("Deploy"))
public deploy: Map<string, ActionResultContext>
@schema(actionResultContextSchema("Run"))
public run: Map<string, ActionResultContext>
@schema(actionResultContextSchema("Test"))
public test: Map<string, ActionResultContext>
@schema(_actionResultContextSchema.description("Alias for `deploy`."))
public services: Map<string, ActionResultContext>
@schema(_actionResultContextSchema.description("Alias for `run`."))
public tasks: Map<string, ActionResultContext>
constructor(root: ConfigContext, allowPartial: boolean, actions: (ResolvedAction | ExecutedAction)[]) {
super(root)
this.build = new Map()
this.deploy = new Map()
this.run = new Map()
this.test = new Map()
this.services = this.deploy
this.tasks = this.run
for (const action of actions) {
this[action.kind.toLowerCase()].set(
action.name,
new ActionResultContext({
root: this,
name: action.name,
outputs: action.getOutputs(),
version: action.versionString(),
disabled: action.isDisabled(),
buildPath: action.getBuildPath(),
sourcePath: action.sourcePath(),
mode: action.mode(),
variables: action.getVariables(),
})
)
}
// This ensures that any template string containing runtime.* references is returned unchanged when
// there is no or limited runtime context available.
this._alwaysAllowPartial = allowPartial
}
}
export interface ActionSpecContextParams {
garden: Garden
resolvedProviders: ProviderMap
modules: GardenModule[]
partialRuntimeResolution: boolean
action: Action
resolvedDependencies: ResolvedAction[]
executedDependencies: ExecutedAction[]
variables: DeepPrimitiveMap
inputs: DeepPrimitiveMap
}
/**
* Used to resolve action spec and variables.
*/
export class ActionSpecContext extends OutputConfigContext {
@schema(
ActionReferencesContext.getSchema().description(
"Runtime outputs and information from other actions (only resolved at runtime when executing actions)."
)
)
public actions: ActionReferencesContext
@schema(ActionReferencesContext.getSchema().description("Alias for `action`."))
public override runtime: ActionReferencesContext
@schema(
joiVariables().description(`The inputs provided to the config through a template, if applicable.`).meta({
keyPlaceholder: "<input-key>",
})
)
public inputs: DeepPrimitiveMap
@schema(
ParentContext.getSchema().description(
`Information about the config parent, if any (usually a template, if applicable).`
)
)
public parent?: ParentContext
@schema(
TemplateContext.getSchema().description(
`Information about the template used when generating the config, if applicable.`
)
)
public template?: TemplateContext
@schema(ActionReferenceContext.getSchema().description("Information about the action currently being resolved."))
public this: ActionReferenceContext
constructor(params: ActionSpecContextParams) {
const { action, garden, partialRuntimeResolution, variables, inputs, resolvedDependencies, executedDependencies } =
params
const internal = action.getInternal()
const mergedVariables = mergeVariables({ garden, variables })
super({
...params,
variables: mergedVariables,
})
const name = action.name
const buildPath = action.getBuildPath()
const sourcePath = action.sourcePath()
const parentName = internal?.parentName
const templateName = internal?.templateName
this.actions = new ActionReferencesContext(this, partialRuntimeResolution, [
...resolvedDependencies,
...executedDependencies,
])
// Throw specific error when attempting to resolve self
this.actions[action.kind.toLowerCase()].set(
name,
new ErrorContext(`Action ${chalk.white.bold(action.key())} cannot reference itself.`)
)
if (parentName && templateName) {
this.parent = new ParentContext(this, parentName)
this.template = new TemplateContext(this, templateName)
}
this.inputs = inputs
this.runtime = this.actions
this.this = new ActionReferenceContext({
root: this,
disabled: action.isDisabled(),
buildPath,
name,
sourcePath,
mode: action.mode(),
variables: mergedVariables,
})
}
}