/
utils.go
442 lines (386 loc) 路 15 KB
/
utils.go
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
package config
import (
"errors"
"fmt"
"github.com/bmatcuk/doublestar/v4"
g "github.com/cloudposse/atmos/pkg/globals"
s "github.com/cloudposse/atmos/pkg/stack"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/fatih/color"
"os"
"path/filepath"
"strconv"
"strings"
)
// findAllStackConfigsInPathsForStack finds all stack config files in the paths specified by globs for the provided stack
func findAllStackConfigsInPathsForStack(
stack string,
includeStackPaths []string,
excludeStackPaths []string,
) ([]string, []string, bool, error) {
var absolutePaths []string
var relativePaths []string
var stackIsDir = strings.IndexAny(stack, "/") > 0
for _, p := range includeStackPaths {
pathWithExt := p
ext := filepath.Ext(p)
if ext == "" {
ext = g.DefaultStackConfigFileExtension
pathWithExt = p + ext
}
// Find all matches in the glob
matches, err := s.GetGlobMatches(pathWithExt)
if err != nil {
return nil, nil, false, err
}
// Exclude files that match any of the excludePaths
if matches != nil && len(matches) > 0 {
for _, matchedFileAbsolutePath := range matches {
matchedFileRelativePath := u.TrimBasePathFromPath(ProcessedConfig.StacksBaseAbsolutePath+"/", matchedFileAbsolutePath)
// Check if the provided stack matches a file in the config folders (excluding the files from `excludeStackPaths`)
stackMatch := strings.HasSuffix(matchedFileAbsolutePath, stack+g.DefaultStackConfigFileExtension)
if stackMatch == true {
allExcluded := true
for _, excludePath := range excludeStackPaths {
excludeMatch, err := doublestar.PathMatch(excludePath, matchedFileAbsolutePath)
if err != nil {
color.Red("%s", err)
continue
} else if excludeMatch == true {
allExcluded = false
break
}
}
if allExcluded == true && stackIsDir {
return []string{matchedFileAbsolutePath}, []string{matchedFileRelativePath}, true, nil
}
}
include := true
for _, excludePath := range excludeStackPaths {
excludeMatch, err := doublestar.PathMatch(excludePath, matchedFileAbsolutePath)
if err != nil {
color.Red("%s", err)
include = false
continue
} else if excludeMatch {
include = false
continue
}
}
if include == true {
absolutePaths = append(absolutePaths, matchedFileAbsolutePath)
relativePaths = append(relativePaths, matchedFileRelativePath)
}
}
}
}
return absolutePaths, relativePaths, false, nil
}
// findAllStackConfigsInPaths finds all stack config files in the paths specified by globs
func findAllStackConfigsInPaths(
includeStackPaths []string,
excludeStackPaths []string,
) ([]string, []string, error) {
var absolutePaths []string
var relativePaths []string
for _, p := range includeStackPaths {
pathWithExt := p
ext := filepath.Ext(p)
if ext == "" {
ext = g.DefaultStackConfigFileExtension
pathWithExt = p + ext
}
// Find all matches in the glob
matches, err := s.GetGlobMatches(pathWithExt)
if err != nil {
return nil, nil, err
}
// Exclude files that match any of the excludePaths
if matches != nil && len(matches) > 0 {
for _, matchedFileAbsolutePath := range matches {
matchedFileRelativePath := u.TrimBasePathFromPath(ProcessedConfig.StacksBaseAbsolutePath+"/", matchedFileAbsolutePath)
include := true
for _, excludePath := range excludeStackPaths {
excludeMatch, err := doublestar.PathMatch(excludePath, matchedFileAbsolutePath)
if err != nil {
color.Red("%s", err)
include = false
continue
} else if excludeMatch {
include = false
continue
}
}
if include == true {
absolutePaths = append(absolutePaths, matchedFileAbsolutePath)
relativePaths = append(relativePaths, matchedFileRelativePath)
}
}
}
}
return absolutePaths, relativePaths, nil
}
func processEnvVars() error {
basePath := os.Getenv("ATMOS_BASE_PATH")
if len(basePath) > 0 {
color.Cyan("Found ENV var ATMOS_BASE_PATH=%s", basePath)
Config.BasePath = basePath
}
stacksBasePath := os.Getenv("ATMOS_STACKS_BASE_PATH")
if len(stacksBasePath) > 0 {
color.Cyan("Found ENV var ATMOS_STACKS_BASE_PATH=%s", stacksBasePath)
Config.Stacks.BasePath = stacksBasePath
}
stacksIncludedPaths := os.Getenv("ATMOS_STACKS_INCLUDED_PATHS")
if len(stacksIncludedPaths) > 0 {
color.Cyan("Found ENV var ATMOS_STACKS_INCLUDED_PATHS=%s", stacksIncludedPaths)
Config.Stacks.IncludedPaths = strings.Split(stacksIncludedPaths, ",")
}
stacksExcludedPaths := os.Getenv("ATMOS_STACKS_EXCLUDED_PATHS")
if len(stacksExcludedPaths) > 0 {
color.Cyan("Found ENV var ATMOS_STACKS_EXCLUDED_PATHS=%s", stacksExcludedPaths)
Config.Stacks.ExcludedPaths = strings.Split(stacksExcludedPaths, ",")
}
stacksNamePattern := os.Getenv("ATMOS_STACKS_NAME_PATTERN")
if len(stacksNamePattern) > 0 {
color.Cyan("Found ENV var ATMOS_STACKS_NAME_PATTERN=%s", stacksNamePattern)
Config.Stacks.NamePattern = stacksNamePattern
}
componentsTerraformBasePath := os.Getenv("ATMOS_COMPONENTS_TERRAFORM_BASE_PATH")
if len(componentsTerraformBasePath) > 0 {
color.Cyan("Found ENV var ATMOS_COMPONENTS_TERRAFORM_BASE_PATH=%s", componentsTerraformBasePath)
Config.Components.Terraform.BasePath = componentsTerraformBasePath
}
componentsTerraformApplyAutoApprove := os.Getenv("ATMOS_COMPONENTS_TERRAFORM_APPLY_AUTO_APPROVE")
if len(componentsTerraformApplyAutoApprove) > 0 {
color.Cyan("Found ENV var ATMOS_COMPONENTS_TERRAFORM_APPLY_AUTO_APPROVE=%s", componentsTerraformApplyAutoApprove)
applyAutoApproveBool, err := strconv.ParseBool(componentsTerraformApplyAutoApprove)
if err != nil {
return err
}
Config.Components.Terraform.ApplyAutoApprove = applyAutoApproveBool
}
componentsTerraformDeployRunInit := os.Getenv("ATMOS_COMPONENTS_TERRAFORM_DEPLOY_RUN_INIT")
if len(componentsTerraformDeployRunInit) > 0 {
color.Cyan("Found ENV var ATMOS_COMPONENTS_TERRAFORM_DEPLOY_RUN_INIT=%s", componentsTerraformDeployRunInit)
deployRunInitBool, err := strconv.ParseBool(componentsTerraformDeployRunInit)
if err != nil {
return err
}
Config.Components.Terraform.DeployRunInit = deployRunInitBool
}
componentsInitRunReconfigure := os.Getenv("ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE")
if len(componentsInitRunReconfigure) > 0 {
color.Cyan("Found ENV var ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE=%s", componentsInitRunReconfigure)
initRunReconfigureBool, err := strconv.ParseBool(componentsInitRunReconfigure)
if err != nil {
return err
}
Config.Components.Terraform.InitRunReconfigure = initRunReconfigureBool
}
componentsTerraformAutoGenerateBackendFile := os.Getenv("ATMOS_COMPONENTS_TERRAFORM_AUTO_GENERATE_BACKEND_FILE")
if len(componentsTerraformAutoGenerateBackendFile) > 0 {
color.Cyan("Found ENV var ATMOS_COMPONENTS_TERRAFORM_AUTO_GENERATE_BACKEND_FILE=%s", componentsTerraformAutoGenerateBackendFile)
componentsTerraformAutoGenerateBackendFileBool, err := strconv.ParseBool(componentsTerraformAutoGenerateBackendFile)
if err != nil {
return err
}
Config.Components.Terraform.AutoGenerateBackendFile = componentsTerraformAutoGenerateBackendFileBool
}
componentsHelmfileBasePath := os.Getenv("ATMOS_COMPONENTS_HELMFILE_BASE_PATH")
if len(componentsHelmfileBasePath) > 0 {
color.Cyan("Found ENV var ATMOS_COMPONENTS_HELMFILE_BASE_PATH=%s", componentsHelmfileBasePath)
Config.Components.Helmfile.BasePath = componentsHelmfileBasePath
}
componentsHelmfileKubeconfigPath := os.Getenv("ATMOS_COMPONENTS_HELMFILE_KUBECONFIG_PATH")
if len(componentsHelmfileKubeconfigPath) > 0 {
color.Cyan("Found ENV var ATMOS_COMPONENTS_HELMFILE_KUBECONFIG_PATH=%s", componentsHelmfileKubeconfigPath)
Config.Components.Helmfile.KubeconfigPath = componentsHelmfileKubeconfigPath
}
componentsHelmfileHelmAwsProfilePattern := os.Getenv("ATMOS_COMPONENTS_HELMFILE_HELM_AWS_PROFILE_PATTERN")
if len(componentsHelmfileHelmAwsProfilePattern) > 0 {
color.Cyan("Found ENV var ATMOS_COMPONENTS_HELMFILE_HELM_AWS_PROFILE_PATTERN=%s", componentsHelmfileHelmAwsProfilePattern)
Config.Components.Helmfile.HelmAwsProfilePattern = componentsHelmfileHelmAwsProfilePattern
}
componentsHelmfileClusterNamePattern := os.Getenv("ATMOS_COMPONENTS_HELMFILE_CLUSTER_NAME_PATTERN")
if len(componentsHelmfileClusterNamePattern) > 0 {
color.Cyan("Found ENV var ATMOS_COMPONENTS_HELMFILE_CLUSTER_NAME_PATTERN=%s", componentsHelmfileClusterNamePattern)
Config.Components.Helmfile.ClusterNamePattern = componentsHelmfileClusterNamePattern
}
workflowsBasePath := os.Getenv("ATMOS_WORKFLOWS_BASE_PATH")
if len(workflowsBasePath) > 0 {
color.Cyan("Found ENV var ATMOS_WORKFLOWS_BASE_PATH=%s", workflowsBasePath)
Config.Workflows.BasePath = workflowsBasePath
}
return nil
}
func checkConfig() error {
if len(Config.Stacks.BasePath) < 1 {
return errors.New("stack base path must be provided in 'stacks.base_path' config or ATMOS_STACKS_BASE_PATH' ENV variable")
}
if len(Config.Stacks.IncludedPaths) < 1 {
return errors.New("at least one path must be provided in 'stacks.included_paths' config or ATMOS_STACKS_INCLUDED_PATHS' ENV variable")
}
return nil
}
func processCommandLineArgs(configAndStacksInfo ConfigAndStacksInfo) error {
if len(configAndStacksInfo.BasePath) > 0 {
Config.BasePath = configAndStacksInfo.BasePath
color.Cyan(fmt.Sprintf("Using command line argument '%s' as base path for stacks and components", configAndStacksInfo.BasePath))
}
if len(configAndStacksInfo.TerraformDir) > 0 {
Config.Components.Terraform.BasePath = configAndStacksInfo.TerraformDir
color.Cyan(fmt.Sprintf("Using command line argument '%s' as terraform directory", configAndStacksInfo.TerraformDir))
}
if len(configAndStacksInfo.HelmfileDir) > 0 {
Config.Components.Helmfile.BasePath = configAndStacksInfo.HelmfileDir
color.Cyan(fmt.Sprintf("Using command line argument '%s' as helmfile directory", configAndStacksInfo.HelmfileDir))
}
if len(configAndStacksInfo.ConfigDir) > 0 {
Config.Stacks.BasePath = configAndStacksInfo.ConfigDir
color.Cyan(fmt.Sprintf("Using command line argument '%s' as stacks directory", configAndStacksInfo.ConfigDir))
}
if len(configAndStacksInfo.StacksDir) > 0 {
Config.Stacks.BasePath = configAndStacksInfo.StacksDir
color.Cyan(fmt.Sprintf("Using command line argument '%s' as stacks directory", configAndStacksInfo.StacksDir))
}
if len(configAndStacksInfo.DeployRunInit) > 0 {
deployRunInitBool, err := strconv.ParseBool(configAndStacksInfo.DeployRunInit)
if err != nil {
return err
}
Config.Components.Terraform.DeployRunInit = deployRunInitBool
color.Cyan(fmt.Sprintf("Using command line argument '%s=%s'", g.DeployRunInitFlag, configAndStacksInfo.DeployRunInit))
}
if len(configAndStacksInfo.AutoGenerateBackendFile) > 0 {
autoGenerateBackendFileBool, err := strconv.ParseBool(configAndStacksInfo.AutoGenerateBackendFile)
if err != nil {
return err
}
Config.Components.Terraform.AutoGenerateBackendFile = autoGenerateBackendFileBool
color.Cyan(fmt.Sprintf("Using command line argument '%s=%s'", g.AutoGenerateBackendFileFlag, configAndStacksInfo.AutoGenerateBackendFile))
}
if len(configAndStacksInfo.WorkflowsDir) > 0 {
Config.Workflows.BasePath = configAndStacksInfo.WorkflowsDir
color.Cyan(fmt.Sprintf("Using command line argument '%s' as workflows directory", configAndStacksInfo.WorkflowsDir))
}
if len(configAndStacksInfo.InitRunReconfigure) > 0 {
initRunReconfigureBool, err := strconv.ParseBool(configAndStacksInfo.InitRunReconfigure)
if err != nil {
return err
}
Config.Components.Terraform.InitRunReconfigure = initRunReconfigureBool
color.Cyan(fmt.Sprintf("Using command line argument '%s=%s'", g.InitRunReconfigure, configAndStacksInfo.InitRunReconfigure))
}
return nil
}
func processLogsConfig() error {
logVerbose := os.Getenv("ATMOS_LOGS_VERBOSE")
if len(logVerbose) > 0 {
color.Cyan("Found ENV var ATMOS_LOGS_VERBOSE=%s", logVerbose)
logVerboseBool, err := strconv.ParseBool(logVerbose)
if err != nil {
return err
}
Config.Logs.Verbose = logVerboseBool
g.LogVerbose = logVerboseBool
}
return nil
}
// GetContextFromVars creates a context object from the provided variables
func GetContextFromVars(vars map[interface{}]interface{}) Context {
var context Context
if namespace, ok := vars["namespace"].(string); ok {
context.Namespace = namespace
}
if tenant, ok := vars["tenant"].(string); ok {
context.Tenant = tenant
}
if environment, ok := vars["environment"].(string); ok {
context.Environment = environment
}
if stage, ok := vars["stage"].(string); ok {
context.Stage = stage
}
if region, ok := vars["region"].(string); ok {
context.Region = region
}
return context
}
// GetContextPrefix calculates context prefix from the context
func GetContextPrefix(stack string, context Context, stackNamePattern string) (string, error) {
if len(stackNamePattern) == 0 {
return "",
errors.New(fmt.Sprintf("Stack name pattern must be provided"))
}
contextPrefix := ""
stackNamePatternParts := strings.Split(stackNamePattern, "-")
for _, part := range stackNamePatternParts {
if part == "{namespace}" {
if len(context.Namespace) == 0 {
return "",
errors.New(fmt.Sprintf("The stack name pattern '%s' specifies 'namespace`, but the stack %s does not have a namespace defined",
stackNamePattern,
stack,
))
}
if len(contextPrefix) == 0 {
contextPrefix = context.Namespace
} else {
contextPrefix = contextPrefix + "-" + context.Namespace
}
} else if part == "{tenant}" {
if len(context.Tenant) == 0 {
return "",
errors.New(fmt.Sprintf("The stack name pattern '%s' specifies 'tenant`, but the stack %s does not have a tenant defined",
stackNamePattern,
stack,
))
}
if len(contextPrefix) == 0 {
contextPrefix = context.Tenant
} else {
contextPrefix = contextPrefix + "-" + context.Tenant
}
} else if part == "{environment}" {
if len(context.Environment) == 0 {
return "",
errors.New(fmt.Sprintf("The stack name pattern '%s' specifies 'environment`, but the stack %s does not have an environment defined",
stackNamePattern,
stack,
))
}
if len(contextPrefix) == 0 {
contextPrefix = context.Environment
} else {
contextPrefix = contextPrefix + "-" + context.Environment
}
} else if part == "{stage}" {
if len(context.Stage) == 0 {
return "",
errors.New(fmt.Sprintf("The stack name pattern '%s' specifies 'stage`, but the stack %s does not have a stage defined",
stackNamePattern,
stack,
))
}
if len(contextPrefix) == 0 {
contextPrefix = context.Stage
} else {
contextPrefix = contextPrefix + "-" + context.Stage
}
}
}
return contextPrefix, nil
}
// ReplaceContextTokens replaces context tokens in the provided pattern and returns a string with all the tokens replaced
func ReplaceContextTokens(context Context, pattern string) string {
r := strings.NewReplacer(
"{base-component}", context.BaseComponent,
"{component}", context.Component,
"{namespace}", context.Namespace,
"{environment}", context.Environment,
"{tenant}", context.Tenant,
"{stage}", context.Stage,
)
return r.Replace(pattern)
}