-
Notifications
You must be signed in to change notification settings - Fork 267
/
helpers.ts
278 lines (252 loc) · 8.68 KB
/
helpers.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
/*
* 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 { fromPairs, memoize } from "lodash-es"
import { joi } from "../config/common.js"
import type { Garden } from "../garden.js"
import type { Log } from "../logger/log-entry.js"
import { createActionLog } from "../logger/log-entry.js"
import { renderDivider } from "../logger/util.js"
import { getLinkedSources } from "../util/ext-source-util.js"
import { buildActionConfigSchema, ExecutedBuildAction, isBuildAction, ResolvedBuildAction } from "./build.js"
import { deployActionConfigSchema, ExecutedDeployAction, isDeployAction, ResolvedDeployAction } from "./deploy.js"
import { ExecutedRunAction, isRunAction, ResolvedRunAction, runActionConfigSchema } from "./run.js"
import { ExecutedTestAction, isTestAction, ResolvedTestAction, testActionConfigSchema } from "./test.js"
import type {
Action,
ActionState,
ExecuteActionParams,
Executed,
ResolveActionParams,
ResolvedAction,
} from "./types.js"
import type { ActionRouter } from "../router/router.js"
import type { ResolvedConfigGraph } from "../graph/config-graph.js"
import { relative, sep } from "path"
import { makeActionCompletePayload } from "../events/util.js"
import type { ActionStatusPayload } from "../events/action-status-events.js"
import type { BuildStatusForEventPayload } from "../plugin/handlers/Build/get-status.js"
import type { DeployStatusForEventPayload } from "../types/service.js"
import type { RunStatusForEventPayload } from "../plugin/plugin.js"
import { styles } from "../logger/styles.js"
/**
* Creates a corresponding Resolved version of the given Action, given the additional parameters needed.
*/
export function actionToResolved<T extends Action>(action: T, params: ResolveActionParams<T["_config"]>) {
if (isBuildAction(action)) {
return new ResolvedBuildAction({ ...action["params"], ...params })
} else if (isDeployAction(action)) {
return new ResolvedDeployAction({ ...action["params"], ...params })
} else if (isRunAction(action)) {
return new ResolvedRunAction({ ...action["params"], ...params })
} else if (isTestAction(action)) {
return new ResolvedTestAction({ ...action["params"], ...params })
} else {
return action satisfies never
}
}
/**
* Creates a corresponding Executed version of the given resolved Action, given the additional parameters needed.
*/
export function resolvedActionToExecuted<T extends ResolvedAction>(
action: T,
params: ExecuteActionParams<T["_config"]>
): Executed<T> {
if (isBuildAction(action)) {
return new ExecutedBuildAction({ ...action["params"], ...params }) as Executed<T>
} else if (isDeployAction(action)) {
return new ExecutedDeployAction({ ...action["params"], ...params }) as Executed<T>
} else if (isRunAction(action)) {
return new ExecutedRunAction({ ...action["params"], ...params }) as Executed<T>
} else if (isTestAction(action)) {
return new ExecutedTestAction({ ...action["params"], ...params }) as Executed<T>
} else {
return action satisfies never
}
}
/**
* Use this to validate any kind (Build, Deploy, Test etc.) of action config.
*/
export const actionConfigSchema = memoize(() =>
joi.alternatives(
buildActionConfigSchema(),
deployActionConfigSchema(),
runActionConfigSchema(),
testActionConfigSchema()
)
)
// TODO: maybe do this implicitly
export async function warnOnLinkedActions(garden: Garden, log: Log, actions: Action[]) {
// Let the user know if any actions are linked to a local path
const linkedSources = await getLinkedSources(garden, "project")
const linkedActionsMsg = actions
.filter((a) => a.isLinked(linkedSources))
.map((a) => `${a.longDescription()} linked to path ${styles.accent(a.sourcePath())}`)
.map((msg) => " " + msg) // indent list
if (linkedActionsMsg.length > 0) {
log.info(renderDivider())
log.info(styles.primary(`The following actions are linked to a local path:\n${linkedActionsMsg.join("\n")}`))
log.info(renderDivider())
}
}
/**
* Get the state of an Action
*/
export async function getActionState(
action: Action,
router: ActionRouter,
graph: ResolvedConfigGraph,
log: Log
): Promise<ActionState> {
const actionLog = createActionLog({ log, actionName: action.name, actionKind: action.kind })
switch (action.kind) {
case "Build":
return (await router.build.getStatus({ action: action as ResolvedBuildAction, log: actionLog, graph }))?.result
?.state
case "Deploy":
return (await router.deploy.getStatus({ action: action as ResolvedDeployAction, log: actionLog, graph }))?.result
?.state
case "Run":
return (await router.run.getResult({ action: action as ResolvedRunAction, log: actionLog, graph }))?.result?.state
case "Test":
return (await router.test.getResult({ action: action as ResolvedTestAction, log: actionLog, graph }))?.result
?.state
default:
return action satisfies never
}
}
/**
* Get action's config file path relative to garden project
*/
export function getRelativeActionConfigPath(projectRoot: string, action: Action): string {
const relPath = relative(projectRoot, action.configPath() ?? "")
return relPath.startsWith("..") ? relPath : "." + sep + relPath
}
export async function getDeployStatusPayloads({
router,
graph,
log,
sessionId,
}: {
router: ActionRouter
graph: ResolvedConfigGraph
log: Log
sessionId: string
}) {
const actions = graph.getDeploys()
return fromPairs(
await Promise.all(
actions.map(async (action) => {
const startedAt = new Date().toISOString()
const actionLog = createActionLog({ log, actionName: action.name, actionKind: action.kind })
const { result } = await router.deploy.getStatus({ action, log: actionLog, graph })
const payload = makeActionCompletePayload({
result,
operation: "getStatus",
startedAt,
force: false,
action,
sessionId,
}) as ActionStatusPayload<DeployStatusForEventPayload>
return [action.name, payload]
})
)
)
}
export async function getBuildStatusPayloads({
router,
graph,
log,
sessionId,
}: {
router: ActionRouter
graph: ResolvedConfigGraph
log: Log
sessionId: string
}) {
const actions = graph.getBuilds()
return fromPairs(
await Promise.all(
actions.map(async (action) => {
const startedAt = new Date().toISOString()
const actionLog = createActionLog({ log, actionName: action.name, actionKind: action.kind })
const { result } = await router.build.getStatus({ action, log: actionLog, graph })
const payload = makeActionCompletePayload({
result,
operation: "getStatus",
startedAt,
force: false,
action,
sessionId,
}) as ActionStatusPayload<BuildStatusForEventPayload>
return [action.name, payload]
})
)
)
}
export async function getTestStatusPayloads({
router,
graph,
log,
sessionId,
}: {
router: ActionRouter
graph: ResolvedConfigGraph
log: Log
sessionId: string
}) {
const actions = graph.getTests()
return fromPairs(
await Promise.all(
actions.map(async (action) => {
const actionLog = createActionLog({ log, actionName: action.name, actionKind: action.kind })
const startedAt = new Date().toISOString()
const { result } = await router.test.getResult({ action, log: actionLog, graph })
const payload = makeActionCompletePayload({
result,
operation: "getStatus",
startedAt,
force: false,
action,
sessionId,
}) as ActionStatusPayload<RunStatusForEventPayload>
return [action.name, payload]
})
)
)
}
export async function getRunStatusPayloads({
router,
graph,
log,
sessionId,
}: {
router: ActionRouter
graph: ResolvedConfigGraph
log: Log
sessionId: string
}) {
const actions = graph.getRuns()
return fromPairs(
await Promise.all(
actions.map(async (action) => {
const actionLog = createActionLog({ log, actionName: action.name, actionKind: action.kind })
const startedAt = new Date().toISOString()
const { result } = await router.run.getResult({ action, log: actionLog, graph })
const payload = makeActionCompletePayload({
result,
operation: "getStatus",
startedAt,
force: false,
action,
sessionId,
}) as ActionStatusPayload<RunStatusForEventPayload>
return [action.name, payload]
})
)
)
}