Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2347 - add --terragrunt-global-cache support to run_cmd #2348

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions config/config_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,26 @@ func runCommand(args []string, trackInclude *TrackInclude, terragruntOptions *op
}

suppressOutput := false
if args[0] == "--terragrunt-quiet" {
suppressOutput = true
args = append(args[:0], args[1:]...)
}

currentPath := filepath.Dir(terragruntOptions.TerragruntConfigPath)
cachePath := currentPath

checkOptions := true
for checkOptions && len(args) > 0 {
switch args[0] {
case "--terragrunt-quiet":
suppressOutput = true
args = append(args[:0], args[1:]...)
case "--terragrunt-global-cache":
cachePath = "_global_"
args = append(args[:0], args[1:]...)
default:
checkOptions = false
}
}

// To avoid re-run of the same run_cmd command, is used in memory cache for command results, with caching key path + arguments
// see: https://github.com/gruntwork-io/terragrunt/issues/1427
cacheKey := fmt.Sprintf("%v-%v", currentPath, args)
cacheKey := fmt.Sprintf("%v-%v", cachePath, args)
cachedValue, foundInCache := runCommandCache.Get(cacheKey)
if foundInCache {
if suppressOutput {
Expand Down
18 changes: 18 additions & 0 deletions config/config_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@ func TestRunCommand(t *testing.T) {
"foo",
nil,
},
{
[]string{"--terragrunt-global-cache", "/bin/bash", "-c", "echo foo"},
terragruntOptionsForTest(t, homeDir),
"foo",
nil,
},
{
[]string{"--terragrunt-global-cache", "--terragrunt-quiet", "/bin/bash", "-c", "echo foo"},
terragruntOptionsForTest(t, homeDir),
"foo",
nil,
},
{
[]string{"--terragrunt-quiet", "--terragrunt-global-cache", "/bin/bash", "-c", "echo foo"},
terragruntOptionsForTest(t, homeDir),
"foo",
nil,
},
{
nil,
terragruntOptionsForTest(t, homeDir),
Expand Down
8 changes: 7 additions & 1 deletion docs/_docs/04_reference/built-in-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ remote_state {
}
```

If the command you are running has the potential to output sensitive values, you may wish to redact the output from appearing in the terminal. To do so, use the special `--terragrunt-quiet` argument which must be passed as the first argument to `run_cmd()`:
If the command you are running has the potential to output sensitive values, you may wish to redact the output from appearing in the terminal. To do so, use the special `--terragrunt-quiet` argument which must be passed as one of the first arguments to `run_cmd()`:

``` hcl
super_secret_value = run_cmd("--terragrunt-quiet", "./decrypt_secret.sh", "foo")
Expand Down Expand Up @@ -685,6 +685,12 @@ carrot
* Output contains multiple times `uuid3`, +1 more output comparing to `uuid1` and `uuid2` - because `uuid3` is declared in locals and inputs which add one more evaluation
* Output contains only once `uuid4` since it is declared only once in `inputs`, `inputs` is not evaluated twice

You can modify this caching behavior to ignore the existing directory if you know the command you are running is not dependent on the current directory path. To do so, use the special `--terragrunt-global-cache` argument which must be passed as one of the first arguments to `run_cmd()` (and can be combined with `--terragrunt-quiet` in any order):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Could you let me know how you tested this behavior? Looking at the code above it doesn't look like they can be combined in any order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what the for loop and switch statement does. It consumes the arguments that match in the switch statement. It doesn't matter which order they occur, they just have to be the first entries.

I tested manually to check the performance difference as shown in the linked issue. We could add additional tests here to verify both can be specified in any order if that helps.


``` hcl
value = run_cmd("--terragrunt-global-cache", "--terragrunt-quiet", "/usr/local/bin/get-account-map")
```

## read\_terragrunt\_config

`read_terragrunt_config(config_path, [default_val])` parses the terragrunt config at the given path and serializes the
Expand Down