Skip to content

Commit

Permalink
Merge pull request #122 from gruntwork-io/tfvar-config
Browse files Browse the repository at this point in the history
Read configuration from terraform.tfvars
  • Loading branch information
brikis98 authored Feb 9, 2017
2 parents e095252 + e6f5569 commit 0bd7827
Show file tree
Hide file tree
Showing 118 changed files with 1,687 additions and 831 deletions.
687 changes: 396 additions & 291 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cli/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func parseTerragruntOptionsFromArgs(args []string) (*options.TerragruntOptions,
return nil, err
}
if terragruntConfigPath == "" {
terragruntConfigPath = util.JoinPath(workingDir, config.DefaultTerragruntConfigPath)
terragruntConfigPath = config.DefaultConfigPath(workingDir)
}

terraformPath, err := parseStringArg(args, OPT_TERRAGRUNT_TFPATH, os.Getenv("TERRAGRUNT_TFPATH"))
Expand Down
17 changes: 9 additions & 8 deletions cli/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"github.com/gruntwork-io/terragrunt/util"
"fmt"
)

func TestParseTerragruntOptionsFromArgs(t *testing.T) {
Expand Down Expand Up @@ -57,8 +58,8 @@ func TestParseTerragruntOptionsFromArgs(t *testing.T) {
},

{
[]string{"--terragrunt-config", "/some/path/.terragrunt"},
mockOptions("/some/path/.terragrunt", workingDir, []string{}, false, ""),
[]string{"--terragrunt-config", fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath)},
mockOptions(fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath), workingDir, []string{}, false, ""),
nil,
},

Expand All @@ -75,14 +76,14 @@ func TestParseTerragruntOptionsFromArgs(t *testing.T) {
},

{
[]string{"--terragrunt-config", "/some/path/.terragrunt", "--terragrunt-non-interactive"},
mockOptions("/some/path/.terragrunt", workingDir, []string{}, true, ""),
[]string{"--terragrunt-config", fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath), "--terragrunt-non-interactive"},
mockOptions(fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath), workingDir, []string{}, true, ""),
nil,
},

{
[]string{"--foo", "--terragrunt-config", "/some/path/.terragrunt", "bar", "--terragrunt-non-interactive", "--baz", "--terragrunt-working-dir", "/some/path", "--terragrunt-source", "github.com/foo/bar//baz?ref=1.0.3"},
mockOptions("/some/path/.terragrunt", "/some/path", []string{"--foo", "bar", "--baz"}, true, "github.com/foo/bar//baz?ref=1.0.3"),
[]string{"--foo", "--terragrunt-config", fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath), "bar", "--terragrunt-non-interactive", "--baz", "--terragrunt-working-dir", "/some/path", "--terragrunt-source", "github.com/foo/bar//baz?ref=1.0.3"},
mockOptions(fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath), "/some/path", []string{"--foo", "bar", "--baz"}, true, "github.com/foo/bar//baz?ref=1.0.3"),
nil,
},

Expand Down Expand Up @@ -149,9 +150,9 @@ func TestFilterTerragruntArgs(t *testing.T) {
}{
{[]string{}, []string{}},
{[]string{"foo", "--bar"}, []string{"foo", "--bar"}},
{[]string{"foo", "--terragrunt-config", "/some/path/.terragrunt"}, []string{"foo"}},
{[]string{"foo", "--terragrunt-config", fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath)}, []string{"foo"}},
{[]string{"foo", "--terragrunt-non-interactive"}, []string{"foo"}},
{[]string{"foo", "--terragrunt-non-interactive", "--bar", "--terragrunt-working-dir", "/some/path", "--baz", "--terragrunt-config", "/some/path/.terragrunt"}, []string{"foo", "--bar", "--baz"}},
{[]string{"foo", "--terragrunt-non-interactive", "--bar", "--terragrunt-working-dir", "/some/path", "--baz", "--terragrunt-config", fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath)}, []string{"foo", "--bar", "--baz"}},
{[]string{"spin-up", "foo", "bar"}, []string{"foo", "bar"}},
{[]string{"foo", "tear-down", "--foo", "--bar"}, []string{"foo", "--foo", "--bar"}},
}
Expand Down
20 changes: 12 additions & 8 deletions cli/cli_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ var MULTI_MODULE_COMMANDS = []string{CMD_SPIN_UP, CMD_TEAR_DOWN}
// Since Terragrunt is just a thin wrapper for Terraform, and we don't want to repeat every single Terraform command
// in its definition, we don't quite fit into the model of any Go CLI library. Fortunately, urfave/cli allows us to
// override the whole template used for the Usage Text.
//
// TODO: this description text has copy/pasted versions of many Terragrunt constants, such as command names and file
// names. It would be easy to make this code DRY using fmt.Sprintf(), but then it's hard to make the text align nicely.
// Write some code to take generate this help text automatically, possibly leveraging code that's part of urfave/cli.
var CUSTOM_USAGE_TEXT = `DESCRIPTION:
{{.Name}} - {{.UsageText}}
Expand All @@ -52,7 +56,7 @@ COMMANDS:
* Terragrunt forwards all other commands directly to Terraform
GLOBAL OPTIONS:
terragrunt-config Path to the Terragrunt config file. Default is .terragrunt.
terragrunt-config Path to the Terragrunt config file. Default is terraform.tfvars.
terragrunt-tfpath Path to the Terraform binary. Default is terraform (on PATH).
terragrunt-non-interactive Assume "yes" for all prompts.
terragrunt-working-dir The path to the Terraform templates. Default is current directory.
Expand Down Expand Up @@ -87,12 +91,12 @@ func CreateTerragruntCli(version string) *cli.App {
app.Version = version
app.Action = runApp
app.Usage = "terragrunt <COMMAND>"
app.UsageText = `Terragrunt is a thin wrapper for [Terraform](https://www.terraform.io/) that supports locking
app.UsageText = fmt.Sprintf(`Terragrunt is a thin wrapper for [Terraform](https://www.terraform.io/) that supports locking
via Amazon's DynamoDB and enforces best practices. Terragrunt forwards almost all commands, arguments, and options
directly to Terraform, using whatever version of Terraform you already have installed. However, before running
Terraform, Terragrunt will ensure your remote state is configured according to the settings in the .terragrunt file.
Terraform, Terragrunt will ensure your remote state is configured according to the settings in %s.
Moreover, for the apply and destroy commands, Terragrunt will first try to acquire a lock using DynamoDB. For
documentation, see https://github.com/gruntwork-io/terragrunt/.`
documentation, see https://github.com/gruntwork-io/terragrunt/.`, config.DefaultTerragruntConfigPath)

return app
}
Expand Down Expand Up @@ -145,7 +149,7 @@ func runTerragrunt(terragruntOptions *options.TerragruntOptions) error {
}

if conf.Lock == nil {
terragruntOptions.Logger.Printf("WARNING: you have not configured locking in your .terragrunt file. Concurrent changes to your .tfstate files may cause conflicts!")
terragruntOptions.Logger.Printf("WARNING: you have not configured locking in your Terragrunt configuration. Concurrent changes to your .tfstate files may cause conflicts!")
return runTerraformCommand(terragruntOptions)
}

Expand Down Expand Up @@ -208,8 +212,8 @@ func configureRemoteState(remoteState *remote.RemoteState, terragruntOptions *op
return remoteState.ConfigureRemoteState(terragruntOptions)
case "remote":
if secondArg(terragruntOptions.TerraformCliArgs) == "config" {
// Encourage the user to configure remote state by defining it in .terragrunt and letting
// Terragrunt handle it for them
// Encourage the user to configure remote state by defining it in a Terragrunt configuration
// file and letting Terragrunt handle remote state for them
return errors.WithStackTrace(DontManuallyConfigureRemoteState)
} else {
// The other "terraform remote" commands explicitly push or pull state, so we shouldn't mess
Expand Down Expand Up @@ -321,7 +325,7 @@ func runTerraformCommand(terragruntOptions *options.TerragruntOptions) error {

// Custom error types

var DontManuallyConfigureRemoteState = fmt.Errorf("Instead of manually using the 'remote config' command, define your remote state settings in .terragrunt and Terragrunt will automatically configure it for you (and all your team members) next time you run it.")
var DontManuallyConfigureRemoteState = fmt.Errorf("Instead of manually using the 'remote config' command, define your remote state settings in %s and Terragrunt will automatically configure it for you (and all your team members) next time you run it.", config.DefaultTerragruntConfigPath)

type UnrecognizedCommand string
func (commandName UnrecognizedCommand) Error() string {
Expand Down
11 changes: 5 additions & 6 deletions cli/download_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/gruntwork-io/terragrunt/config"
"os"
"github.com/gruntwork-io/terragrunt/errors"
"path/filepath"
"github.com/hashicorp/go-getter"
urlhelper "github.com/hashicorp/go-getter/helper/url"
"io/ioutil"
Expand Down Expand Up @@ -196,9 +195,9 @@ func processTerraformSource(source string, terragruntOptions *options.Terragrunt
}

encodedWorkingDir := util.EncodeBase64Sha1(canonicalWorkingDir)
downloadDir := filepath.Join(os.TempDir(), "terragrunt-download", encodedWorkingDir, rootPath)
workingDir := filepath.Join(downloadDir, modulePath)
versionFile := filepath.Join(downloadDir, ".terragrunt-source-version")
downloadDir := util.JoinPath(os.TempDir(), "terragrunt-download", encodedWorkingDir, rootPath)
workingDir := util.JoinPath(downloadDir, modulePath)
versionFile := util.JoinPath(downloadDir, ".terragrunt-source-version")

return &TerraformSource{
CanonicalSourceURL: rootSourceUrl,
Expand Down Expand Up @@ -315,15 +314,15 @@ func cleanupTerraformFiles(path string, terragruntOptions *options.TerragruntOpt

terragruntOptions.Logger.Printf("Cleaning up existing *.tf files in %s", path)

files, err := zglob.Glob(filepath.Join(path, "**/*.tf"))
files, err := zglob.Glob(util.JoinPath(path, "**/*.tf"))
if err != nil {
return errors.WithStackTrace(err)
}
return util.DeleteFiles(files)
}

// There are two ways a user can tell Terragrunt that it needs to download Terraform configurations from a specific
// URL: via a command-line option or via an entry in the .terragrunt config file. If the user used one of these, this
// URL: via a command-line option or via an entry in the Terragrunt configuratino. If the user used one of these, this
// method returns the source URL and the boolean true; if not, this method returns an empty string and false.
func getTerraformSourceUrl(terragruntOptions *options.TerragruntOptions, terragruntConfig *config.TerragruntConfig) (string, bool) {
if terragruntOptions.Source != "" {
Expand Down
6 changes: 3 additions & 3 deletions cli/download_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func testDownloadTerraformSourceIfNecessary(t *testing.T, canonicalUrl string, d
CanonicalSourceURL: parseUrl(t, canonicalUrl),
DownloadDir: downloadDir,
WorkingDir: downloadDir,
VersionFile: filepath.Join(downloadDir, "version-file.txt"),
VersionFile: util.JoinPath(downloadDir, "version-file.txt"),
}

terragruntOptions := options.NewTerragruntOptionsForTest("./should-not-be-used")
Expand All @@ -160,7 +160,7 @@ func testDownloadTerraformSourceIfNecessary(t *testing.T, canonicalUrl string, d
err := downloadTerraformSourceIfNecessary(terraformSource, terragruntOptions)
assert.Nil(t, err, "For terraform source %v: %v", terraformSource, err)

expectedFilePath := filepath.Join(downloadDir, "main.tf")
expectedFilePath := util.JoinPath(downloadDir, "main.tf")
if assert.True(t, util.FileExists(expectedFilePath), "For terraform source %v", terraformSource) {
actualFileContents := readFile(t, expectedFilePath)
assert.Equal(t, expectedFileContents, actualFileContents, "For terraform source %v", terraformSource)
Expand All @@ -172,7 +172,7 @@ func testAlreadyHaveLatestCode(t *testing.T, canonicalUrl string, downloadDir st
CanonicalSourceURL: parseUrl(t, canonicalUrl),
DownloadDir: downloadDir,
WorkingDir: downloadDir,
VersionFile: filepath.Join(downloadDir, "version-file.txt"),
VersionFile: util.JoinPath(downloadDir, "version-file.txt"),
}

actual, err := alreadyHaveLatestCode(terraformSource)
Expand Down
Loading

0 comments on commit 0bd7827

Please sign in to comment.