-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
atlantis_generate_repo_config.go
497 lines (427 loc) 路 16.6 KB
/
atlantis_generate_repo_config.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
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
package exec
import (
"fmt"
"path"
"path/filepath"
"reflect"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/spf13/cobra"
cfg "github.com/cloudposse/atmos/pkg/config"
c "github.com/cloudposse/atmos/pkg/convert"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)
// ExecuteAtlantisGenerateRepoConfigCmd executes 'atlantis generate repo-config' command
func ExecuteAtlantisGenerateRepoConfigCmd(cmd *cobra.Command, args []string) error {
info, err := processCommandLineArgs("", cmd, args, nil)
if err != nil {
return err
}
cliConfig, err := cfg.InitCliConfig(info, true)
if err != nil {
return err
}
flags := cmd.Flags()
outputPath, err := flags.GetString("output-path")
if err != nil {
return err
}
configTemplateName, err := flags.GetString("config-template")
if err != nil {
return err
}
projectTemplateName, err := flags.GetString("project-template")
if err != nil {
return err
}
stacksCsv, err := flags.GetString("stacks")
if err != nil {
return err
}
var stacks []string
if stacksCsv != "" {
stacks = strings.Split(stacksCsv, ",")
}
componentsCsv, err := flags.GetString("components")
if err != nil {
return err
}
var components []string
if componentsCsv != "" {
components = strings.Split(componentsCsv, ",")
}
affectedOnly, err := flags.GetBool("affected-only")
if err != nil {
return err
}
ref, err := flags.GetString("ref")
if err != nil {
return err
}
sha, err := flags.GetString("sha")
if err != nil {
return err
}
repoPath, err := flags.GetString("repo-path")
if err != nil {
return err
}
sshKeyPath, err := flags.GetString("ssh-key")
if err != nil {
return err
}
sshKeyPassword, err := flags.GetString("ssh-key-password")
if err != nil {
return err
}
verbose, err := flags.GetBool("verbose")
if err != nil {
return err
}
// If the flag `--affected-only=true` is passed, find the affected components and stacks
if affectedOnly {
cloneTargetRef, err := flags.GetBool("clone-target-ref")
if err != nil {
return err
}
return ExecuteAtlantisGenerateRepoConfigAffectedOnly(
cliConfig,
outputPath,
configTemplateName,
projectTemplateName,
ref,
sha,
repoPath,
sshKeyPath,
sshKeyPassword,
verbose,
cloneTargetRef,
)
}
return ExecuteAtlantisGenerateRepoConfig(
cliConfig,
outputPath,
configTemplateName,
projectTemplateName,
stacks,
components,
)
}
// ExecuteAtlantisGenerateRepoConfigAffectedOnly generates repository configuration for Atlantis only for the affected compoenents and stacks
func ExecuteAtlantisGenerateRepoConfigAffectedOnly(
cliConfig schema.CliConfiguration,
outputPath string,
configTemplateName string,
projectTemplateName string,
ref string,
sha string,
repoPath string,
sshKeyPath string,
sshKeyPassword string,
verbose bool,
cloneTargetRef bool,
) error {
if repoPath != "" && (ref != "" || sha != "" || sshKeyPath != "" || sshKeyPassword != "") {
return errors.New("if the '--repo-path' flag is specified, the '--ref', '--sha', '--ssh-key' and '--ssh-key-password' flags can't be used")
}
var affected []schema.Affected
var err error
if repoPath != "" {
affected, err = ExecuteDescribeAffectedWithTargetRepoPath(cliConfig, repoPath, verbose, false)
} else if cloneTargetRef {
affected, err = ExecuteDescribeAffectedWithTargetRefClone(cliConfig, ref, sha, sshKeyPath, sshKeyPassword, verbose, false)
} else {
affected, err = ExecuteDescribeAffectedWithTargetRefCheckout(cliConfig, ref, sha, verbose, false)
}
if err != nil {
return err
}
if len(affected) == 0 {
return nil
}
affectedComponents := lo.FilterMap[schema.Affected, string](affected, func(x schema.Affected, _ int) (string, bool) {
if x.ComponentType == "terraform" {
return x.Component, true
}
return "", false
})
affectedStacks := lo.FilterMap[schema.Affected, string](affected, func(x schema.Affected, _ int) (string, bool) {
if x.ComponentType == "terraform" {
return x.Stack, true
}
return "", false
})
return ExecuteAtlantisGenerateRepoConfig(
cliConfig,
outputPath,
configTemplateName,
projectTemplateName,
affectedStacks,
affectedComponents,
)
}
// ExecuteAtlantisGenerateRepoConfig generates repository configuration for Atlantis
func ExecuteAtlantisGenerateRepoConfig(
cliConfig schema.CliConfiguration,
outputPath string,
configTemplateNameArg string,
projectTemplateNameArg string,
stacks []string,
components []string,
) error {
stacksMap, _, err := FindStacksMap(cliConfig, false)
if err != nil {
return err
}
var configTemplate schema.AtlantisRepoConfig
var projectTemplate schema.AtlantisProjectConfig
var ok bool
var atlantisProjects []schema.AtlantisProjectConfig
var componentsSection map[string]any
var terraformSection map[string]any
var componentSection map[string]any
var varsSection map[any]any
var settingsSection map[any]any
if projectTemplateNameArg != "" {
if projectTemplate, ok = cliConfig.Integrations.Atlantis.ProjectTemplates[projectTemplateNameArg]; !ok {
return errors.Errorf("atlantis project template '%s' is not defined in 'integrations.atlantis.project_templates' in 'atmos.yaml'", projectTemplateNameArg)
}
}
// Iterate over all components in all stacks and generate atlantis projects
// Iterate not over the map itself, but over the sorted map keys since Go iterates over maps in random order
stacksMapSortedKeys := u.StringKeysFromMap(stacksMap)
for _, stackConfigFileName := range stacksMapSortedKeys {
stackSection := stacksMap[stackConfigFileName]
if componentsSection, ok = stackSection.(map[any]any)["components"].(map[string]any); !ok {
continue
}
if terraformSection, ok = componentsSection["terraform"].(map[string]any); !ok {
continue
}
// Iterate not over the map itself, but over the sorted map keys since Go iterates over maps in random order
componentMapSortedKeys := u.StringKeysFromMap(terraformSection)
for _, componentName := range componentMapSortedKeys {
compSection := terraformSection[componentName]
if componentSection, ok = compSection.(map[string]any); !ok {
continue
}
// Check if 'components' filter is provided
if len(components) == 0 ||
u.SliceContainsString(components, componentName) {
// Component vars
if varsSection, ok = componentSection["vars"].(map[any]any); !ok {
continue
}
// Component metadata
metadataSection := map[any]any{}
if metadataSection, ok = componentSection["metadata"].(map[any]any); ok {
if componentType, ok := metadataSection["type"].(string); ok {
// Don't include abstract components
if componentType == "abstract" {
continue
}
}
}
// If the project template is not passes on the command line, find and process it in the component project template in the 'settings.atlantis' section
if projectTemplateNameArg == "" {
if settingsSection, ok = componentSection["settings"].(map[any]any); ok {
if settingsAtlantisSection, ok := settingsSection["atlantis"].(map[any]any); ok {
// 'settings.atlantis.project_template' has higher priority than 'settings.atlantis.project_template_name'
if settingsAtlantisProjectTemplate, ok := settingsAtlantisSection["project_template"].(map[any]any); ok {
err = mapstructure.Decode(settingsAtlantisProjectTemplate, &projectTemplate)
if err != nil {
return err
}
} else if settingsAtlantisProjectTemplateName, ok := settingsAtlantisSection["project_template_name"].(string); ok && settingsAtlantisProjectTemplateName != "" {
if projectTemplate, ok = cliConfig.Integrations.Atlantis.ProjectTemplates[settingsAtlantisProjectTemplateName]; !ok {
return errors.Errorf(
"the component '%s' in the stack config file '%s' "+
"specifies the atlantis project template name '%s' "+
"in the 'settings.atlantis.project_template_name' section, "+
"but this atlantis project template is not defined in 'integrations.atlantis.project_templates' in 'atmos.yaml'",
componentName, stackConfigFileName, settingsAtlantisProjectTemplateName)
}
}
}
}
}
// https://www.golinuxcloud.com/golang-check-if-struct-is-empty/
if reflect.ValueOf(projectTemplate).IsZero() {
return errors.Errorf(
"atlantis project template is not specified for the component '%s'. "+
"In needs to be defined in one of these places: 'settings.atlantis.project_template_name' stack config section, "+
"'settings.atlantis.project_template' stack config section, "+
"or passed on the command line using the '--project-template' flag to select a project template from the "+
"collection of templates defined in the 'integrations.atlantis.project_templates' section in 'atmos.yaml'",
componentName)
}
// Find the terraform component
// If 'component' attribute is present, it's the terraform component
// Otherwise, the Atmos component name is the terraform component (by default)
terraformComponent := componentName
if componentAttribute, ok := componentSection["component"].(string); ok {
terraformComponent = componentAttribute
}
// Absolute path to the terraform component
terraformComponentPath := path.Join(
cliConfig.BasePath,
cliConfig.Components.Terraform.BasePath,
terraformComponent,
)
// Context
context := cfg.GetContextFromVars(varsSection)
context.Component = strings.Replace(componentName, "/", "-", -1)
context.ComponentPath = terraformComponentPath
contextPrefix, err := cfg.GetContextPrefix(stackConfigFileName, context, GetStackNamePattern(cliConfig), stackConfigFileName)
if err != nil {
return err
}
// Base component is required to calculate terraform workspace for derived components
if terraformComponent != componentName {
context.BaseComponent = terraformComponent
}
configAndStacksInfo := schema.ConfigAndStacksInfo{
ComponentFromArg: componentName,
Stack: stackConfigFileName,
ComponentMetadataSection: metadataSection,
ComponentSettingsSection: settingsSection,
ComponentVarsSection: varsSection,
Context: context,
ComponentSection: map[string]any{
cfg.VarsSectionName: varsSection,
cfg.SettingsSectionName: settingsSection,
cfg.MetadataSectionName: metadataSection,
},
}
// Calculate terraform workspace
workspace, err := BuildTerraformWorkspace(cliConfig, configAndStacksInfo)
if err != nil {
return err
}
context.Workspace = workspace
// Check if 'stacks' filter is provided
if len(stacks) == 0 ||
// 'stacks' filter can contain the names of the top-level stack config files:
// atmos terraform generate varfiles --stacks=orgs/cp/tenant1/staging/us-east-2,orgs/cp/tenant2/dev/us-east-2
u.SliceContainsString(stacks, stackConfigFileName) ||
// 'stacks' filter can also contain the logical stack names (derived from the context vars):
// atmos terraform generate varfiles --stacks=tenant1-ue2-staging,tenant1-ue2-prod
u.SliceContainsString(stacks, contextPrefix) {
// Generate an atlantis project for the component in the stack
// Replace the context tokens
var whenModified []string
for _, item := range projectTemplate.Autoplan.WhenModified {
processedItem := cfg.ReplaceContextTokens(context, item)
whenModified = append(whenModified, processedItem)
}
atlantisProjectAutoplanConfig := schema.AtlantisProjectAutoplanConfig{
Enabled: projectTemplate.Autoplan.Enabled,
WhenModified: whenModified,
}
atlantisProjectName := cfg.ReplaceContextTokens(context, projectTemplate.Name)
atlantisProject := schema.AtlantisProjectConfig{
Name: atlantisProjectName,
Workspace: cfg.ReplaceContextTokens(context, projectTemplate.Workspace),
Dir: cfg.ReplaceContextTokens(context, projectTemplate.Dir),
TerraformVersion: projectTemplate.TerraformVersion,
DeleteSourceBranchOnMerge: projectTemplate.DeleteSourceBranchOnMerge,
Autoplan: atlantisProjectAutoplanConfig,
ApplyRequirements: projectTemplate.ApplyRequirements,
Workflow: projectTemplate.Workflow,
}
atlantisProjects = append(atlantisProjects, atlantisProject)
}
}
}
}
// If the config template is not passes on the command line, find and process it in the component project template in the 'settings.atlantis' section
if configTemplateNameArg == "" {
if settingsSection, ok = componentSection["settings"].(map[any]any); ok {
if settingsAtlantisSection, ok := settingsSection["atlantis"].(map[any]any); ok {
// 'settings.atlantis.config_template' has higher priority than 'settings.atlantis.config_template_name'
if settingsAtlantisConfigTemplate, ok := settingsAtlantisSection["config_template"].(map[any]any); ok {
err = mapstructure.Decode(settingsAtlantisConfigTemplate, &configTemplate)
if err != nil {
return err
}
} else if settingsAtlantisConfigTemplateName, ok := settingsAtlantisSection["config_template_name"].(string); ok && settingsAtlantisConfigTemplateName != "" {
if configTemplate, ok = cliConfig.Integrations.Atlantis.ConfigTemplates[settingsAtlantisConfigTemplateName]; !ok {
return errors.Errorf(
"atlantis config template name '%s' is specified "+
"in the 'settings.atlantis.config_template_name' section, "+
"but this atlantis config template is not defined in 'integrations.atlantis.config_templates' in 'atmos.yaml'",
settingsAtlantisConfigTemplateName)
}
}
}
}
} else {
if configTemplate, ok = cliConfig.Integrations.Atlantis.ConfigTemplates[configTemplateNameArg]; !ok {
return errors.Errorf("atlantis config template '%s' is not defined in 'integrations.atlantis.config_templates' in 'atmos.yaml'", configTemplateNameArg)
}
}
if reflect.ValueOf(configTemplate).IsZero() {
return errors.Errorf(
"atlantis config template is not specified. " +
"In needs to be defined in one of these places: 'settings.atlantis.config_template_name' stack config section, " +
"'settings.atlantis.config_template' stack config section, " +
"or passed on the command line using the '--config-template' flag to select a config template from the " +
"collection of templates defined in the 'integrations.atlantis.config_templates' section in 'atmos.yaml'")
}
// Final atlantis config
atlantisYaml := schema.AtlantisConfigOutput{}
atlantisYaml.Version = configTemplate.Version
atlantisYaml.Automerge = configTemplate.Automerge
atlantisYaml.DeleteSourceBranchOnMerge = configTemplate.DeleteSourceBranchOnMerge
atlantisYaml.ParallelPlan = configTemplate.ParallelPlan
atlantisYaml.ParallelApply = configTemplate.ParallelApply
atlantisYaml.AllowedRegexpPrefixes = configTemplate.AllowedRegexpPrefixes
atlantisYaml.Projects = atlantisProjects
// Workflows
if settingsSection, ok = componentSection["settings"].(map[any]any); ok {
if settingsAtlantisSection, ok := settingsSection["atlantis"].(map[any]any); ok {
if settingsAtlantisWorkflowTemplates, ok := settingsAtlantisSection["workflow_templates"].(map[any]any); ok {
atlantisYaml.Workflows = c.MapsOfInterfacesToMapsOfStrings(settingsAtlantisWorkflowTemplates)
}
}
}
if reflect.ValueOf(atlantisYaml.Workflows).IsZero() && !reflect.ValueOf(cliConfig.Integrations.Atlantis.WorkflowTemplates).IsZero() {
atlantisYaml.Workflows = cliConfig.Integrations.Atlantis.WorkflowTemplates
}
// Write the atlantis config to a file at the specified path
// Check the command line argument '--output-path' first
// Then check the 'atlantis.path' setting in 'atmos.yaml'
fileName := outputPath
if fileName == "" {
fileName = cliConfig.Integrations.Atlantis.Path
u.LogDebug(cliConfig, fmt.Sprintf("Using 'atlantis.path: %s' from 'atmos.yaml'", fileName))
} else {
u.LogDebug(cliConfig, fmt.Sprintf("Using '--output-path %s' command-line argument", fileName))
}
// If the path is empty, dump to 'stdout'
if fileName != "" {
u.LogDebug(cliConfig, fmt.Sprintf("Writing atlantis repo config file to '%s'\n", fileName))
fileAbsolutePath, err := filepath.Abs(fileName)
if err != nil {
return err
}
// Create all the intermediate subdirectories
err = u.EnsureDir(fileAbsolutePath)
if err != nil {
return err
}
err = u.WriteToFileAsYAML(fileAbsolutePath, atlantisYaml, 0644)
if err != nil {
return err
}
} else {
err = u.PrintAsYAML(atlantisYaml)
if err != nil {
return err
}
}
return nil
}