/
config.go
399 lines (332 loc) · 15 KB
/
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
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/gruntwork-io/terragrunt/errors"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/remote"
"github.com/gruntwork-io/terragrunt/util"
"github.com/hashicorp/hcl"
)
const DefaultTerragruntConfigPath = "terraform.tfvars"
const OldTerragruntConfigPath = ".terragrunt"
// TerragruntConfig represents a parsed and expanded configuration
type TerragruntConfig struct {
Terraform *TerraformConfig
RemoteState *remote.RemoteState
Dependencies *ModuleDependencies
}
func (conf *TerragruntConfig) String() string {
return fmt.Sprintf("TerragruntConfig{Terraform = %v, RemoteState = %v, Dependencies = %v}", conf.Terraform, conf.RemoteState, conf.Dependencies)
}
// terragruntConfigFile represents the configuration supported in a Terragrunt configuration file (i.e.
// terraform.tfvars or .terragrunt)
type terragruntConfigFile struct {
Terraform *TerraformConfig `hcl:"terraform,omitempty"`
Include *IncludeConfig `hcl:"include,omitempty"`
Lock *LockConfig `hcl:"lock,omitempty"`
RemoteState *remote.RemoteState `hcl:"remote_state,omitempty"`
Dependencies *ModuleDependencies `hcl:"dependencies,omitempty"`
}
// Older versions of Terraform did not support locking, so Terragrunt offered locking as a feature. As of version 0.9.0,
// Terraform supports locking natively, so this feature was removed from Terragrunt. However, we keep around the
// LockConfig so we can log a warning for Terragrunt users who are still trying to use it.
type LockConfig map[interface{}]interface{}
// tfvarsFileWithTerragruntConfig represents a .tfvars file that contains a terragrunt = { ... } block
type tfvarsFileWithTerragruntConfig struct {
Terragrunt *terragruntConfigFile `hcl:"terragrunt,omitempty"`
}
// IncludeConfig represents the configuration settings for a parent Terragrunt configuration file that you can
// "include" in a child Terragrunt configuration file
type IncludeConfig struct {
Path string `hcl:"path"`
}
// ModuleDependencies represents the paths to other Terraform modules that must be applied before the current module
// can be applied
type ModuleDependencies struct {
Paths []string `hcl:"paths"`
}
func (deps *ModuleDependencies) String() string {
return fmt.Sprintf("ModuleDependencies{Paths = %v}", deps.Paths)
}
// TerraformConfig specifies where to find the Terraform configuration files
type TerraformConfig struct {
ExtraArgs []TerraformExtraArguments `hcl:"extra_arguments"`
Source string `hcl:"source"`
}
func (conf *TerraformConfig) String() string {
return fmt.Sprintf("TerraformConfig{Source = %v}", conf.Source)
}
// TerraformExtraArguments sets a list of arguments to pass to Terraform if command fits any in the `Commands` list
type TerraformExtraArguments struct {
Name string `hcl:",key"`
Arguments []string `hcl:"arguments,omitempty"`
RequiredVarFiles []string `hcl:"required_var_files,omitempty"`
OptionalVarFiles []string `hcl:"optional_var_files,omitempty"`
Commands []string `hcl:"commands,omitempty"`
}
func (conf *TerraformExtraArguments) String() string {
return fmt.Sprintf("TerraformArguments{Name = %s, Arguments = %v, Commands = %v}", conf.Name, conf.Arguments, conf.Commands)
}
// Return the default path to use for the Terragrunt configuration file. The reason this is a method rather than a
// constant is that older versions of Terragrunt stored configuration in a different file. This method returns the
// path to the old configuration format if such a file exists and the new format otherwise.
func DefaultConfigPath(workingDir string) string {
path := util.JoinPath(workingDir, OldTerragruntConfigPath)
if util.FileExists(path) {
return path
}
return util.JoinPath(workingDir, DefaultTerragruntConfigPath)
}
// Returns a list of all Terragrunt config files in the given path or any subfolder of the path. A file is a Terragrunt
// config file if it has a name as returned by the DefaultConfigPath method and contains Terragrunt config contents
// as returned by the IsTerragruntConfigFile method.
func FindConfigFilesInPath(rootPath string) ([]string, error) {
configFiles := []string{}
err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
configPath := DefaultConfigPath(path)
isTerragruntConfig, err := IsTerragruntConfigFile(configPath)
if err != nil {
return err
}
if isTerragruntConfig {
configFiles = append(configFiles, configPath)
}
}
return nil
})
return configFiles, err
}
// Returns true if the given path corresponds to file that could be a Terragrunt config file. A file could be a
// Terragrunt config file if:
//
// 1. The file exists
// 2. It is a .terragrunt file, which is the old Terragrunt-specific file format
// 3. The file contains HCL contents with a terragrunt = { ... } block
func IsTerragruntConfigFile(path string) (bool, error) {
if !util.FileExists(path) {
return false, nil
}
if isOldTerragruntConfig(path) {
return true, nil
}
return isNewTerragruntConfig(path)
}
// Returns true if the given path points to an old Terragrunt config file
func isOldTerragruntConfig(path string) bool {
return strings.HasSuffix(path, OldTerragruntConfigPath)
}
// Retrusn true if the given path points to a new (current) Terragrunt config file
func isNewTerragruntConfig(path string) (bool, error) {
configContents, err := util.ReadFileAsString(path)
if err != nil {
return false, err
}
containsBlock, err := containsTerragruntBlock(configContents)
if err != nil {
return false, errors.WithStackTrace(ErrorParsingTerragruntConfig{ConfigPath: path, Underlying: err})
}
return containsBlock, nil
}
// Returns true if the given string contains valid HCL with a terragrunt = { ... } block
func containsTerragruntBlock(configString string) (bool, error) {
terragruntConfig := &tfvarsFileWithTerragruntConfig{}
if err := hcl.Decode(terragruntConfig, configString); err != nil {
return false, errors.WithStackTrace(err)
}
return terragruntConfig.Terragrunt != nil, nil
}
// Read the Terragrunt config file from its default location
func ReadTerragruntConfig(terragruntOptions *options.TerragruntOptions) (*TerragruntConfig, error) {
terragruntOptions.Logger.Printf("Reading Terragrunt config file at %s", terragruntOptions.TerragruntConfigPath)
return ParseConfigFile(terragruntOptions.TerragruntConfigPath, terragruntOptions, nil)
}
// Parse the Terragrunt config file at the given path. If the include parameter is not nil, then treat this as a config
// included in some other config file when resolving relative paths.
func ParseConfigFile(configPath string, terragruntOptions *options.TerragruntOptions, include *IncludeConfig) (*TerragruntConfig, error) {
if isOldTerragruntConfig(configPath) {
terragruntOptions.Logger.Printf("DEPRECATION WARNING: Found deprecated config file format %s. This old config format will not be supported in the future. Please move your config files into a %s file.", configPath, DefaultTerragruntConfigPath)
}
configString, err := util.ReadFileAsString(configPath)
if err != nil {
return nil, err
}
config, err := parseConfigString(configString, terragruntOptions, include, configPath)
if err != nil {
return nil, err
}
return config, nil
}
// Parse the Terragrunt config contained in the given string.
func parseConfigString(configString string, terragruntOptions *options.TerragruntOptions, include *IncludeConfig, configPath string) (*TerragruntConfig, error) {
resolvedConfigString, err := ResolveTerragruntConfigString(configString, include, terragruntOptions)
if err != nil {
return nil, err
}
terragruntConfigFile, err := parseConfigStringAsTerragruntConfigFile(resolvedConfigString, configPath)
if err != nil {
return nil, err
}
if terragruntConfigFile == nil {
return nil, errors.WithStackTrace(CouldNotResolveTerragruntConfigInFile(configPath))
}
config, err := convertToTerragruntConfig(terragruntConfigFile, terragruntOptions)
if err != nil {
return nil, err
}
if include != nil && terragruntConfigFile.Include != nil {
return nil, errors.WithStackTrace(TooManyLevelsOfInheritance{
ConfigPath: terragruntOptions.TerragruntConfigPath,
FirstLevelIncludePath: include.Path,
SecondLevelIncludePath: terragruntConfigFile.Include.Path,
})
}
includedConfig, err := parseIncludedConfig(terragruntConfigFile.Include, terragruntOptions)
if err != nil {
return nil, err
}
return mergeConfigWithIncludedConfig(config, includedConfig, terragruntOptions)
}
// Parse the given config string, read from the given config file, as a terragruntConfigFile struct. This method solely
// converts the HCL syntax in the string to the terragruntConfigFile struct; it does not process any interpolations.
func parseConfigStringAsTerragruntConfigFile(configString string, configPath string) (*terragruntConfigFile, error) {
if isOldTerragruntConfig(configPath) {
terragruntConfig := &terragruntConfigFile{}
if err := hcl.Decode(terragruntConfig, configString); err != nil {
return nil, errors.WithStackTrace(err)
}
return terragruntConfig, nil
} else {
tfvarsConfig := &tfvarsFileWithTerragruntConfig{}
if err := hcl.Decode(tfvarsConfig, configString); err != nil {
return nil, errors.WithStackTrace(err)
}
return tfvarsConfig.Terragrunt, nil
}
}
// Merge the given config with an included config. Anything specified in the current config will override the contents
// of the included config. If the included config is nil, just return the current config.
func mergeConfigWithIncludedConfig(config *TerragruntConfig, includedConfig *TerragruntConfig, terragruntOptions *options.TerragruntOptions) (*TerragruntConfig, error) {
if includedConfig == nil {
return config, nil
}
if config.RemoteState != nil {
includedConfig.RemoteState = config.RemoteState
}
if config.Terraform != nil {
if includedConfig.Terraform == nil {
includedConfig.Terraform = config.Terraform
} else {
if config.Terraform.Source != "" {
includedConfig.Terraform.Source = config.Terraform.Source
}
mergeExtraArgs(terragruntOptions, config.Terraform.ExtraArgs, &includedConfig.Terraform.ExtraArgs)
}
}
if config.Dependencies != nil {
includedConfig.Dependencies = config.Dependencies
}
return includedConfig, nil
}
// Merge the extra arguments.
//
// If a child's extra_arguments has the same name a parent's extra_arguments,
// then the child's extra_arguments will be selected (and the parent's ignored)
// If a child's extra_arguments has a different name from all of the parent's extra_arguments,
// then the child's extra_arguments will be added to the end of the parents.
// Therefore, terragrunt will put the child extra_arguments after the parent's
// extra_arguments on the terraform cli.
// Therefore, if .tfvar files from both the parent and child contain a variable
// with the same name, the value from the child will win.
func mergeExtraArgs(terragruntOptions *options.TerragruntOptions, childExtraArgs []TerraformExtraArguments, parentExtraArgs *[]TerraformExtraArguments) {
result := *parentExtraArgs
for _, child := range childExtraArgs {
parentExtraArgsWithSameName := getIndexOfExtraArgsWithName(result, child.Name)
if parentExtraArgsWithSameName != -1 {
// If the parent contains an extra_arguments with the same name as the child,
// then override the parent's extra_arguments with the child's.
terragruntOptions.Logger.Printf("extra_arguments '%v' from child overriding parent", child.Name)
result[parentExtraArgsWithSameName] = child
} else {
// If the parent does not contain an extra_arguments with the same name as the child
// then add the child to the end.
// This ensures the child extra_arguments are added to the command line after the parent extra_arguments.
result = append(result, child)
}
}
*parentExtraArgs = result
}
// Returns the index of the extraArgs with the given name,
// or -1 if no extraArgs have the given name.
func getIndexOfExtraArgsWithName(extraArgs []TerraformExtraArguments, name string) int {
for i, extra := range extraArgs {
if extra.Name == name {
return i
}
}
return -1
}
// Parse the config of the given include, if one is specified
func parseIncludedConfig(includedConfig *IncludeConfig, terragruntOptions *options.TerragruntOptions) (*TerragruntConfig, error) {
if includedConfig == nil {
return nil, nil
}
if includedConfig.Path == "" {
return nil, errors.WithStackTrace(IncludedConfigMissingPath(terragruntOptions.TerragruntConfigPath))
}
resolvedIncludePath, err := ResolveTerragruntConfigString(includedConfig.Path, nil, terragruntOptions)
if err != nil {
return nil, err
}
if !filepath.IsAbs(resolvedIncludePath) {
resolvedIncludePath = util.JoinPath(filepath.Dir(terragruntOptions.TerragruntConfigPath), resolvedIncludePath)
}
return ParseConfigFile(resolvedIncludePath, terragruntOptions, includedConfig)
}
// Convert the contents of a fully resolved Terragrunt configuration to a TerragruntConfig object
func convertToTerragruntConfig(terragruntConfigFromFile *terragruntConfigFile, terragruntOptions *options.TerragruntOptions) (*TerragruntConfig, error) {
terragruntConfig := &TerragruntConfig{}
if terragruntConfigFromFile.Lock != nil {
terragruntOptions.Logger.Printf("WARNING: Found a lock configuration in the Terraform configuration at %s. Terraform added native support for locking as of version 0.9.0, so this feature has been removed from Terragrunt and will have no effect. See your Terraform backend docs for how to configure locking: https://www.terraform.io/docs/backends/types/index.html.", terragruntOptions.TerragruntConfigPath)
}
if terragruntConfigFromFile.RemoteState != nil {
terragruntConfigFromFile.RemoteState.FillDefaults()
if err := terragruntConfigFromFile.RemoteState.Validate(); err != nil {
return nil, err
}
terragruntConfig.RemoteState = terragruntConfigFromFile.RemoteState
}
terragruntConfig.Terraform = terragruntConfigFromFile.Terraform
terragruntConfig.Dependencies = terragruntConfigFromFile.Dependencies
return terragruntConfig, nil
}
// Custom error types
type IncludedConfigMissingPath string
func (err IncludedConfigMissingPath) Error() string {
return fmt.Sprintf("The include configuration in %s must specify a 'path' parameter", string(err))
}
type TooManyLevelsOfInheritance struct {
ConfigPath string
FirstLevelIncludePath string
SecondLevelIncludePath string
}
func (err TooManyLevelsOfInheritance) Error() string {
return fmt.Sprintf("%s includes %s, which itself includes %s. Only one level of includes is allowed.", err.ConfigPath, err.FirstLevelIncludePath, err.SecondLevelIncludePath)
}
type CouldNotResolveTerragruntConfigInFile string
func (err CouldNotResolveTerragruntConfigInFile) Error() string {
return fmt.Sprintf("Could not find Terragrunt configuration settings in %s", string(err))
}
type ErrorParsingTerragruntConfig struct {
ConfigPath string
Underlying error
}
func (err ErrorParsingTerragruntConfig) Error() string {
return fmt.Sprintf("Error parsing Terragrunt config at %s: %v", err.ConfigPath, err.Underlying)
}