forked from gruntwork-io/terragrunt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
193 lines (155 loc) · 6.98 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
package config
import (
"github.com/gruntwork-io/terragrunt/errors"
"github.com/gruntwork-io/terragrunt/locks"
"github.com/gruntwork-io/terragrunt/remote"
"github.com/hashicorp/hcl"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/util"
"fmt"
"path/filepath"
)
const DefaultTerragruntConfigPath = ".terragrunt"
// TerragruntConfig represents a parsed and expanded configuration
type TerragruntConfig struct {
Lock locks.Lock
RemoteState *remote.RemoteState
Dependencies *ModuleDependencies
}
// terragruntConfigFile represents the configuration supported in the .terragrunt file
type terragruntConfigFile struct {
Include *IncludeConfig `hcl:"include,omitempty"`
Lock *LockConfig `hcl:"lock,omitempty"`
RemoteState *remote.RemoteState `hcl:"remote_state,omitempty"`
Dependencies *ModuleDependencies `hcl:"dependencies,omitempty"`
}
// IncludeConfig represents the configuration settings for a parent .terragrunt file that you can "include" in a
// child .terragrunt file
type IncludeConfig struct {
Path string `hcl:"path"`
}
// LockConfig represents generic configuration for Lock providers
type LockConfig struct {
Backend string `hcl:"backend"`
Config map[string]string `hcl:"config"`
}
// 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"`
}
// 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) {
configString, err := util.ReadFileAsString(configPath)
if err != nil {
return nil, err
}
config, err := parseConfigString(configString, terragruntOptions, include)
if err != nil {
return nil, errors.WithStackTraceAndPrefix(err, "Error parsing Terragrunt config file %s", configPath)
}
return config, nil
}
// Parse the Terragrunt config contained in the given string.
func parseConfigString(configString string, terragruntOptions *options.TerragruntOptions, include *IncludeConfig) (*TerragruntConfig, error) {
// HCL does not natively process interpolations (${...}), and we don't want to write our own HCL parser, so for
// now, we'll do the parsing in two passes. The first pass reads in the config file without processing any
// interpolations. This is mostly to make the (un-interpolated) variables available programmatically,
// especially the parent path, which we need to process other interpolations.
terragruntConfigFromFileFirstPass := &terragruntConfigFile{}
if err := hcl.Decode(terragruntConfigFromFileFirstPass, configString); err != nil {
return nil, errors.WithStackTrace(err)
}
// Now we process the interpolations in the string
resolvedConfigString, err := ResolveTerragruntConfigString(configString, include, terragruntOptions)
if err != nil {
return nil, err
}
// Now we do a second pass at parsing, but this time on the string with all the interpolations already
// resolved
terragruntConfigFromFileSecondPass := &terragruntConfigFile{}
if err := hcl.Decode(terragruntConfigFromFileSecondPass, resolvedConfigString); err != nil {
return nil, errors.WithStackTrace(err)
}
config, err := convertToTerragruntConfig(terragruntConfigFromFileSecondPass, terragruntOptions)
if err != nil {
return nil, err
}
if include != nil && terragruntConfigFromFileFirstPass.Include != nil {
return nil, errors.WithStackTrace(TooManyLevelsOfInheritance(terragruntOptions.TerragruntConfigPath))
}
includedConfig, err := parseIncludedConfig(terragruntConfigFromFileFirstPass.Include, terragruntOptions)
if err != nil {
return nil, err
}
return mergeConfigWithIncludedConfig(config, includedConfig)
}
// 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) (*TerragruntConfig, error) {
if includedConfig == nil {
return config, nil
}
if config.Lock != nil {
includedConfig.Lock = config.Lock
}
if config.RemoteState != nil {
includedConfig.RemoteState = config.RemoteState
}
if config.Dependencies != nil {
includedConfig.Dependencies = config.Dependencies
}
return includedConfig, nil
}
// 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 = filepath.Join(filepath.Dir(terragruntOptions.TerragruntConfigPath), resolvedIncludePath)
}
return ParseConfigFile(resolvedIncludePath, terragruntOptions, includedConfig)
}
// Convert the contents of a fully resolved .terragrunt file to a TerragruntConfig object
func convertToTerragruntConfig(terragruntConfigFromFile *terragruntConfigFile, terragruntOptions *options.TerragruntOptions) (*TerragruntConfig, error) {
terragruntConfig := &TerragruntConfig{}
if terragruntConfigFromFile.Lock != nil {
lock, err := lookupLock(terragruntConfigFromFile.Lock.Backend, terragruntConfigFromFile.Lock.Config)
if err != nil {
return nil, err
}
terragruntConfig.Lock = lock
}
if terragruntConfigFromFile.RemoteState != nil {
terragruntConfigFromFile.RemoteState.FillDefaults()
if err := terragruntConfigFromFile.RemoteState.Validate(); err != nil {
return nil, err
}
terragruntConfig.RemoteState = terragruntConfigFromFile.RemoteState
}
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 string
func (err TooManyLevelsOfInheritance) Error() string {
return fmt.Sprintf("%s includes %s, which itself includes %s. Only one level of includes is allowed.", string(err), DefaultTerragruntConfigPath, DefaultTerragruntConfigPath)
}