-
Notifications
You must be signed in to change notification settings - Fork 2
/
debug.go
110 lines (95 loc) · 4 KB
/
debug.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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/gruntwork-io/go-commons/errors"
"github.com/gruntwork-io/terragrunt/config"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/terraform"
"github.com/gruntwork-io/terragrunt/util"
)
//const TerragruntTFVarsFile = "terragrunt-debug.tfvars.json"
const TerragruntTFVarsFile = "test.auto.tfvars.json"
const defaultPermissions = int(0600)
// WriteTerragruntDebugFile will create a tfvars file that can be used to invoke the terraform module in the same way
// that terragrunt invokes the module, so that you can debug issues with the terragrunt config.
func WriteTerragruntDebugFile(terragruntOptions *options.TerragruntOptions, terragruntConfig *config.TerragruntConfig) error {
terragruntOptions.Logger.Infof(
//"Debug mode requested: generating debug file %s in working dir %s",
"Generating TFVARS file %s in working dir %s",
TerragruntTFVarsFile,
terragruntOptions.WorkingDir,
)
required, optional, err := terraform.ModuleVariables(terragruntOptions.WorkingDir)
if err != nil {
return err
}
variables := append(required, optional...)
terragruntOptions.Logger.Debugf("The following variables were detected in the terraform module:")
terragruntOptions.Logger.Debugf("%v", variables)
fileContents, err := terragruntDebugFileContents(terragruntOptions, terragruntConfig, variables)
if err != nil {
return err
}
//configFolder := filepath.Dir(terragruntOptions.TerragruntConfigPath)
//fileName := filepath.Join(configFolder, TerragruntTFVarsFile)
// Updated Location For File Name.
// Points To Staging Directory / Staging Subdirectory
fileName := filepath.Join(terragruntOptions.WorkingDir, TerragruntTFVarsFile)
if err := os.WriteFile(fileName, fileContents, os.FileMode(defaultPermissions)); err != nil {
return errors.WithStackTrace(err)
}
terragruntOptions.Logger.Infof("Variables passed to terraform are located in \"%s\"", fileName)
terragruntOptions.Logger.Infof("Run this command to replicate how terraform was invoked:")
terragruntOptions.Logger.Infof(
"\tterraform -chdir=\"%s\" %s -var-file=\"%s\" ",
terragruntOptions.WorkingDir,
strings.Join(terragruntOptions.TerraformCliArgs, " "),
fileName,
)
return nil
}
// terragruntDebugFileContents will return a tfvars file in json format of all the terragrunt rendered variables values
// that should be set to invoke the terraform module in the same way as terragrunt. Note that this will only include the
// values of variables that are actually defined in the module.
func terragruntDebugFileContents(
terragruntOptions *options.TerragruntOptions,
terragruntConfig *config.TerragruntConfig,
moduleVariables []string,
) ([]byte, error) {
envVars := map[string]string{}
if terragruntOptions.Env != nil {
envVars = terragruntOptions.Env
}
jsonValuesByKey := make(map[string]interface{})
for varName, varValue := range terragruntConfig.Inputs {
nameAsEnvVar := fmt.Sprintf("%s_%s", terraform.TFVarPrefix, varName)
_, varIsInEnv := envVars[nameAsEnvVar]
varIsDefined := util.ListContainsElement(moduleVariables, varName)
// Only add to the file if the explicit env var does NOT exist and the variable is defined in the module.
// We must do this in order to avoid overriding the env var when the user follows up with a direct invocation to
// terraform using this file (due to the order in which terraform resolves config sources).
switch {
case !varIsInEnv && varIsDefined:
jsonValuesByKey[varName] = varValue
case varIsInEnv:
terragruntOptions.Logger.Debugf(
"WARN: The variable %s was omitted from the debug file because the env var %s is already set.",
varName, nameAsEnvVar,
)
case !varIsDefined:
terragruntOptions.Logger.Debugf(
"WARN: The variable %s was omitted because it is not defined in the terraform module.",
varName,
)
}
}
jsonContent, err := json.MarshalIndent(jsonValuesByKey, "", " ")
if err != nil {
return nil, errors.WithStackTrace(err)
}
return jsonContent, nil
}