-
Notifications
You must be signed in to change notification settings - Fork 267
/
base.ts
614 lines (544 loc) · 20.2 KB
/
base.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
* Copyright (C) 2018-2024 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 dotenv from "dotenv"
import { sep, resolve, relative, basename, dirname, join } from "path"
import { load } from "js-yaml"
import { lint } from "yaml-lint"
import fsExtra from "fs-extra"
const { pathExists, readFile } = fsExtra
import { omit, isPlainObject, isArray } from "lodash-es"
import type { BuildDependencyConfig, ModuleConfig } from "./module.js"
import { coreModuleSpecSchema, baseModuleSchemaKeys } from "./module.js"
import { ConfigurationError, FilesystemError, ParameterError } from "../exceptions.js"
import { DEFAULT_BUILD_TIMEOUT_SEC, GardenApiVersion } from "../constants.js"
import type { ProjectConfig } from "../config/project.js"
import { validateWithPath } from "./validation.js"
import { defaultDotIgnoreFile, listDirectory } from "../util/fs.js"
import { isConfigFilename } from "../util/fs.js"
import type { ConfigTemplateKind } from "./config-template.js"
import { isNotNull, isTruthy } from "../util/util.js"
import type { DeepPrimitiveMap, PrimitiveMap } from "./common.js"
import { createSchema, joi } from "./common.js"
import { emitNonRepeatableWarning } from "../warnings.js"
import type { ActionKind } from "../actions/types.js"
import { actionKinds } from "../actions/types.js"
import { mayContainTemplateString } from "../template-string/template-string.js"
import type { Log } from "../logger/log-entry.js"
import type { Document, DocumentOptions } from "yaml"
import { parseAllDocuments } from "yaml"
import { dedent, deline } from "../util/string.js"
import { makeDocsLinkStyled } from "../docs/common.js"
export const configTemplateKind = "ConfigTemplate"
export const renderTemplateKind = "RenderTemplate"
export const noTemplateFields = ["apiVersion", "kind", "type", "name", "internal"]
export const varfileDescription = `
The format of the files is determined by the configured file's extension:
* \`.env\` - Standard "dotenv" format, as defined by [dotenv](https://github.com/motdotla/dotenv#rules).
* \`.yaml\`/\`.yml\` - YAML. The file must consist of a YAML document, which must be a map (dictionary). Keys may contain any value type.
* \`.json\` - JSON. Must contain a single JSON _object_ (not an array).
_NOTE: The default varfile format will change to YAML in Garden v0.13, since YAML allows for definition of nested objects and arrays._
`.trim()
export type ObjectPath = (string | number)[]
export interface YamlDocumentWithSource extends Document {
source: string
}
export interface GardenResourceInternalFields {
/**
* The path/working directory where commands and operations relating to the config should be executed. This is
* most commonly the directory containing the config file.
*
* Note: WHen possible, use `action.getSourcePath()` instead, since it factors in remote source paths and source
* overrides (i.e. `BaseActionConfig.source.path`). This is a lower-level field that doesn't contain template strings,
* and can thus be used early in the resolution flow.
*/
basePath: string
/**
* The path to the resource's config file, if any.
*
* Configs that are read from a file should always have this set, but generated configs (e.g. from templates
* or `augmentGraph` handlers) don't necessarily have a path on disk.
*/
configFilePath?: string
// -> set by templates
inputs?: DeepPrimitiveMap
parentName?: string
templateName?: string
// Used to map fields to specific doc and location
yamlDoc?: YamlDocumentWithSource
}
export interface BaseGardenResource {
apiVersion?: GardenApiVersion
kind: string
name: string
internal: GardenResourceInternalFields
}
export const baseInternalFieldsSchema = createSchema({
name: "base-internal-fields",
keys: () => ({
basePath: joi.string().required().meta({ internal: true }),
configFilePath: joi.string().optional().meta({ internal: true }),
inputs: joi.object().optional().meta({ internal: true }),
parentName: joi.string().optional().meta({ internal: true }),
templateName: joi.string().optional().meta({ internal: true }),
yamlDoc: joi.any().optional().meta({ internal: true }),
}),
allowUnknown: true,
meta: { internal: true },
})
// Note: Avoiding making changes to ModuleConfig and ProjectConfig for now, because of
// the blast radius.
export type GardenResource = BaseGardenResource | ModuleConfig | ProjectConfig
export type RenderTemplateKind = typeof renderTemplateKind
export type ConfigKind = "Module" | "Workflow" | "Project" | ConfigTemplateKind | RenderTemplateKind | ActionKind
export const allConfigKinds = ["Module", "Workflow", "Project", configTemplateKind, renderTemplateKind, ...actionKinds]
/**
* Attempts to parse content as YAML, and applies a linter to produce more informative error messages when
* content is not valid YAML.
*
* @param content - The contents of the file as a string.
* @param sourceDescription - A description of the location of the yaml file, e.g. "bar.yaml at directory /foo/".
* @param version - YAML standard version. Defaults to "1.2"
*/
export async function loadAndValidateYaml(
content: string,
sourceDescription: string,
version: DocumentOptions["version"] = "1.2"
): Promise<YamlDocumentWithSource[]> {
try {
return Array.from(parseAllDocuments(content, { merge: true, strict: false, version }) || []).map((doc) => {
if (doc.errors.length > 0) {
throw doc.errors[0]
}
// Workaround: Call toJS might throw an error that is not listed in the errors above.
// See also https://github.com/eemeli/yaml/issues/497
// We call this here to catch this error early and prevent crashes later on.
doc.toJS()
const docWithSource = doc as YamlDocumentWithSource
docWithSource.source = content
return docWithSource
})
} catch (loadErr) {
// We try to find the error using a YAML linter
try {
await lint(content)
} catch (linterErr) {
throw new ConfigurationError({
message: `Could not parse ${sourceDescription} as valid YAML: ${linterErr}`,
})
}
// ... but default to throwing a generic error, in case the error wasn't caught by yaml-lint.
throw new ConfigurationError({
message: dedent`
Failed to load YAML from ${sourceDescription}.
Linting the file did not yield any errors. This is all we know: ${loadErr}
`,
})
}
}
export async function loadConfigResources(
log: Log,
projectRoot: string,
configPath: string,
allowInvalid = false
): Promise<GardenResource[]> {
const fileData = await readConfigFile(configPath, projectRoot)
const resources = await validateRawConfig({
log,
rawConfig: fileData.toString(),
configPath,
projectRoot,
allowInvalid,
})
return resources
}
export async function validateRawConfig({
log,
rawConfig,
configPath,
projectRoot,
allowInvalid = false,
}: {
log: Log
rawConfig: string
configPath: string
projectRoot: string
allowInvalid?: boolean
}) {
let rawSpecs = await loadAndValidateYaml(rawConfig, `${basename(configPath)} in directory ${dirname(configPath)}`)
// Ignore empty resources
rawSpecs = rawSpecs.filter(Boolean)
const resources = rawSpecs
.map((s) => {
const relPath = relative(projectRoot, configPath)
const description = `config at ${relPath}`
return prepareResource({ log, doc: s, configFilePath: configPath, projectRoot, description, allowInvalid })
})
.filter(isNotNull)
return resources
}
export async function readConfigFile(configPath: string, projectRoot: string) {
try {
return await readFile(configPath)
} catch (err) {
throw new FilesystemError({
message: `Could not find configuration file at ${configPath}. Project root directory: ${projectRoot}`,
})
}
}
export function prepareResource({
log,
doc,
configFilePath,
projectRoot,
description,
allowInvalid = false,
}: {
log: Log
doc: YamlDocumentWithSource
configFilePath: string
projectRoot: string
description: string
allowInvalid?: boolean
}): GardenResource | ModuleConfig | null {
const relPath = relative(projectRoot, configFilePath)
const spec = doc.toJS()
if (spec === null) {
return null
}
if (!isPlainObject(spec)) {
throw new ConfigurationError({
message: `Invalid configuration found in ${description}. Expected mapping object but got ${typeof spec}.`,
})
}
let kind = spec.kind
const basePath = dirname(configFilePath)
if (!allowInvalid) {
for (const field of noTemplateFields) {
if (spec[field] && mayContainTemplateString(spec[field])) {
throw new ConfigurationError({
message: `Resource in ${relPath} has a template string in field '${field}', which does not allow templating.`,
})
}
}
if (spec.internal !== undefined) {
throw new ConfigurationError({
message: `Found invalid key "internal" in config at ${relPath}`,
})
}
}
// Allow this for backwards compatibility
if (kind === "ModuleTemplate") {
spec.kind = kind = configTemplateKind
}
if (kind === "Project") {
spec.path = basePath
spec.configPath = configFilePath
spec.internal = {
basePath,
yamlDoc: doc,
}
return prepareProjectResource(log, spec)
} else if (
actionKinds.includes(kind) ||
kind === "Command" ||
kind === "Workflow" ||
kind === configTemplateKind ||
kind === renderTemplateKind
) {
spec.internal = {
basePath,
configFilePath,
yamlDoc: doc,
}
return spec
} else if (kind === "Module") {
spec.path = basePath
spec.configPath = configFilePath
delete spec.internal
return prepareModuleResource(spec, configFilePath, projectRoot)
} else if (allowInvalid) {
return spec
} else if (!kind) {
throw new ConfigurationError({
message: `Missing \`kind\` field in ${description}`,
})
} else {
throw new ConfigurationError({
message: `Unknown kind ${kind} in ${description}`,
})
}
}
// TODO-0.14: remove these deprecation handlers in 0.14
type DeprecatedConfigHandler = (log: Log, spec: ProjectConfig) => ProjectConfig
function handleDotIgnoreFiles(log: Log, projectSpec: ProjectConfig) {
// If the project config has an explicitly defined `dotIgnoreFile` field,
// it means the config has already been updated to 0.13 format.
if (!!projectSpec.dotIgnoreFile) {
return projectSpec
}
const dotIgnoreFiles = projectSpec.dotIgnoreFiles
// If the project config has neither new `dotIgnoreFile` nor old `dotIgnoreFiles` fields
// then there is nothing to do.
if (!dotIgnoreFiles) {
return projectSpec
}
if (dotIgnoreFiles.length === 0) {
return { ...projectSpec, dotIgnoreFile: defaultDotIgnoreFile }
}
if (dotIgnoreFiles.length === 1) {
emitNonRepeatableWarning(
log,
deline`Multi-valued project configuration field \`dotIgnoreFiles\` is deprecated in 0.13 and will be removed in 0.14. Please use single-valued \`dotIgnoreFile\` instead.`
)
return { ...projectSpec, dotIgnoreFile: dotIgnoreFiles[0] }
}
throw new ConfigurationError({
message: `Cannot auto-convert array-field \`dotIgnoreFiles\` to scalar \`dotIgnoreFile\`: multiple values found in the array [${dotIgnoreFiles.join(
", "
)}]`,
})
}
function handleProjectModules(log: Log, projectSpec: ProjectConfig): ProjectConfig {
// Field 'modules' was intentionally removed from the internal interface `ProjectConfig`,
// but it still can be presented in the runtime if the old config format is used.
if (projectSpec["modules"]) {
emitNonRepeatableWarning(
log,
"Project configuration field `modules` is deprecated in 0.13 and will be removed in 0.14. Please use the `scan` field instead."
)
const scanConfig = projectSpec.scan || {}
for (const key of ["include", "exclude"]) {
if (projectSpec["modules"][key]) {
if (!scanConfig[key]) {
scanConfig[key] = projectSpec["modules"][key]
} else {
log.warn(
`Project-level \`${key}\` is set both in \`modules.${key}\` and \`scan.${key}\`. The value from \`scan.${key}\` will be used (and the value from \`modules.${key}\` will not have any effect).`
)
}
}
}
projectSpec.scan = scanConfig
delete projectSpec["modules"]
}
return projectSpec
}
function handleMissingApiVersion(log: Log, projectSpec: ProjectConfig): ProjectConfig {
// We conservatively set the apiVersion to be compatible with 0.12.
if (projectSpec["apiVersion"] === undefined) {
emitNonRepeatableWarning(
log,
`"apiVersion" is missing in the Project config. Assuming "${
GardenApiVersion.v0
}" for backwards compatibility with 0.12. The "apiVersion"-field is mandatory when using the new action Kind-configs. A detailed migration guide is available at ${makeDocsLinkStyled("guides/migrating-to-bonsai")}`
)
return { ...projectSpec, apiVersion: GardenApiVersion.v0 }
} else {
if (projectSpec["apiVersion"] === GardenApiVersion.v0) {
emitNonRepeatableWarning(
log,
`Project is configured with \`apiVersion: ${GardenApiVersion.v0}\`, running with backwards compatibility.`
)
} else if (projectSpec["apiVersion"] !== GardenApiVersion.v1) {
throw new ConfigurationError({
message: `Project configuration with \`apiVersion: ${projectSpec["apiVersion"]}\` is not supported. Valid values are ${GardenApiVersion.v1} or ${GardenApiVersion.v0}.`,
})
}
}
return projectSpec
}
const bonsaiDeprecatedConfigHandlers: DeprecatedConfigHandler[] = [
handleMissingApiVersion,
handleDotIgnoreFiles,
handleProjectModules,
]
export function prepareProjectResource(log: Log, spec: any): ProjectConfig {
let projectSpec = <ProjectConfig>spec
for (const handler of bonsaiDeprecatedConfigHandlers) {
projectSpec = handler(log, projectSpec)
}
return projectSpec
}
export function prepareModuleResource(spec: any, configPath: string, projectRoot: string): ModuleConfig {
// We allow specifying modules by name only as a shorthand:
// dependencies:
// - foo-module
// - name: foo-module // same as the above
// Empty strings and nulls are omitted from the array.
let dependencies: BuildDependencyConfig[] = spec.build?.dependencies || []
if (spec.build && spec.build.dependencies && isArray(spec.build.dependencies)) {
// We call `prepareBuildDependencies` on `spec.build.dependencies` again in `resolveModuleConfig` to ensure that
// any dependency configs whose module names resolved to null get filtered out.
dependencies = prepareBuildDependencies(spec.build.dependencies)
}
const cleanedSpec = {
...omit(spec, baseModuleSchemaKeys()),
build: { ...spec.build, dependencies },
}
// Had a bit of a naming conflict in the terraform module type with the new module variables concept...
if (spec.type === "terraform") {
cleanedSpec["variables"] = spec.variables
}
// Built-in keys are validated here and the rest are put into the `spec` field
const path = dirname(configPath)
const config: ModuleConfig = {
apiVersion: spec.apiVersion || GardenApiVersion.v0,
kind: "Module",
allowPublish: spec.allowPublish,
build: {
dependencies,
timeout: spec.build?.timeout || DEFAULT_BUILD_TIMEOUT_SEC,
},
local: spec.local,
configPath,
description: spec.description,
disabled: spec.disabled,
generateFiles: spec.generateFiles,
include: spec.include,
exclude: spec.exclude,
name: spec.name,
path,
repositoryUrl: spec.repositoryUrl,
serviceConfigs: [],
spec: cleanedSpec,
testConfigs: [],
type: spec.type,
taskConfigs: [],
variables: spec.variables,
varfile: spec.varfile,
}
validateWithPath({
config,
schema: coreModuleSpecSchema(),
path: configPath,
projectRoot,
configType: "module",
ErrorClass: ConfigurationError,
source: undefined,
})
return config
}
/**
* Normalizes build dependencies such that the string / module name shorthand is converted into the map form,
* and removes any null entries (or entries with null names, which can appear after template resolution).
*/
export function prepareBuildDependencies(buildDependencies: any[]): BuildDependencyConfig[] {
return buildDependencies
.map((dep) => {
if (!dep || (dep && dep.name === null)) {
return null
}
return {
name: dep.name ? dep.name : dep,
copy: dep.copy ? dep.copy : [],
}
})
.filter(isTruthy)
}
export async function findProjectConfig({
log,
path,
allowInvalid = false,
scan = true,
}: {
log: Log
path: string
allowInvalid?: boolean
scan?: boolean
}): Promise<ProjectConfig | undefined> {
const sepCount = path.split(sep).length - 1
let allProjectSpecs: GardenResource[] = []
for (let i = 0; i < sepCount; i++) {
const configFiles = (await listDirectory(path, { recursive: false })).filter(isConfigFilename)
for (const configFile of configFiles) {
const resources = await loadConfigResources(log, path, join(path, configFile), allowInvalid)
const projectSpecs = resources.filter((s) => s.kind === "Project")
if (projectSpecs.length > 1 && !allowInvalid) {
throw new ConfigurationError({
message: `Multiple project declarations found in ${path}/${configFile}`,
})
} else if (projectSpecs.length > 0) {
allProjectSpecs = allProjectSpecs.concat(projectSpecs)
}
}
if (allProjectSpecs.length > 1 && !allowInvalid) {
const configPaths = allProjectSpecs.map((c) => `- ${(c as ProjectConfig).configPath}`)
throw new ConfigurationError({
message: `Multiple project declarations found at paths:\n${configPaths.join("\n")}`,
})
} else if (allProjectSpecs.length === 1) {
return <ProjectConfig>allProjectSpecs[0]
}
if (!scan) {
break
}
path = resolve(path, "..")
}
return
}
export async function loadVarfile({
configRoot,
path,
defaultPath,
}: {
// project root (when resolving project config) or module root (when resolving module config)
configRoot: string
path: string | undefined
defaultPath: string | undefined
}): Promise<PrimitiveMap> {
if (!path && !defaultPath) {
throw new ParameterError({
message: `Neither a path nor a defaultPath was provided. Config root: ${configRoot}`,
})
}
const resolvedPath = resolve(configRoot, <string>(path || defaultPath))
const exists = await pathExists(resolvedPath)
if (!exists && path && path !== defaultPath) {
throw new ConfigurationError({
message: `Could not find varfile at path '${path}'. Absolute path: ${resolvedPath}`,
})
}
if (!exists) {
return {}
}
try {
const data = await readFile(resolvedPath)
const relPath = relative(configRoot, resolvedPath)
const filename = basename(resolvedPath.toLowerCase())
if (filename.endsWith(".json")) {
// JSON parser throws a JSON syntax error on completely empty input file,
// and returns an empty object for an empty JSON.
const parsed = JSON.parse(data.toString())
if (!isPlainObject(parsed)) {
throw new ConfigurationError({
message: `Configured variable file ${relPath} must be a valid plain JSON object. Got: ${typeof parsed}`,
})
}
return parsed as PrimitiveMap
} else if (filename.endsWith(".yml") || filename.endsWith(".yaml")) {
// YAML parser returns `undefined` for empty files, we interpret that as an empty object.
const parsed = load(data.toString()) || {}
if (!isPlainObject(parsed)) {
throw new ConfigurationError({
message: `Configured variable file ${relPath} must be a single plain YAML mapping. Got: ${typeof parsed}`,
})
}
return parsed as PrimitiveMap
} else {
// Note: For backwards-compatibility we fall back on using .env as a default format,
// and don't specifically validate the extension for that.
// The dotenv parser returns an empty object for invalid or empty input file.
const parsed = dotenv.parse(data)
return parsed as PrimitiveMap
}
} catch (error) {
throw new ConfigurationError({
message: `Unable to load varfile at '${path}': ${error}`,
})
}
}