From bb20ef049512253ad4cd14c2e2581b25274bd277 Mon Sep 17 00:00:00 2001 From: Maxime Deravet Date: Thu, 13 Jun 2019 10:01:06 -0700 Subject: [PATCH 1/3] Append terraform binary dir to PATH when running custom commands --- server/events/runtime/run_step_runner.go | 4 +++- server/events/runtime/run_step_runner_test.go | 4 ++++ server/events/terraform/terraform_client.go | 10 +++++++--- server/server.go | 1 + 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/server/events/runtime/run_step_runner.go b/server/events/runtime/run_step_runner.go index f7a7cb2fce..6a2f859847 100644 --- a/server/events/runtime/run_step_runner.go +++ b/server/events/runtime/run_step_runner.go @@ -6,13 +6,14 @@ import ( "os/exec" "path/filepath" - version "github.com/hashicorp/go-version" + "github.com/hashicorp/go-version" "github.com/runatlantis/atlantis/server/events/models" ) // RunStepRunner runs custom commands. type RunStepRunner struct { DefaultTFVersion *version.Version + TerraformBinDir string } func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, path string) (string, error) { @@ -32,6 +33,7 @@ func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, pa "HEAD_BRANCH_NAME": ctx.Pull.HeadBranch, "HEAD_REPO_NAME": ctx.HeadRepo.Name, "HEAD_REPO_OWNER": ctx.HeadRepo.Owner, + "PATH": fmt.Sprintf("$PATH:%s", r.TerraformBinDir), "PLANFILE": filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectName)), "PROJECT_NAME": ctx.ProjectName, "PULL_AUTHOR": ctx.Pull.Author, diff --git a/server/events/runtime/run_step_runner_test.go b/server/events/runtime/run_step_runner_test.go index d12dc7cc8e..54b3ef38e2 100644 --- a/server/events/runtime/run_step_runner_test.go +++ b/server/events/runtime/run_step_runner_test.go @@ -62,6 +62,9 @@ func TestRunStepRunner_Run(t *testing.T) { { Command: "echo user_name=$USER_NAME", ExpOut: "user_name=acme-user\n", + },{ + Command: "echo $PATH", + ExpOut: "$PATH:/bin/dir\n", }, } @@ -70,6 +73,7 @@ func TestRunStepRunner_Run(t *testing.T) { defaultVersion, _ := version.NewVersion("0.8") r := runtime.RunStepRunner{ DefaultTFVersion: defaultVersion, + TerraformBinDir: "/bin/dir", } for _, c := range cases { t.Run(c.Command, func(t *testing.T) { diff --git a/server/events/terraform/terraform_client.go b/server/events/terraform/terraform_client.go index a42d7bd825..ba444bc124 100644 --- a/server/events/terraform/terraform_client.go +++ b/server/events/terraform/terraform_client.go @@ -27,9 +27,9 @@ import ( "strings" "sync" - getter "github.com/hashicorp/go-getter" - version "github.com/hashicorp/go-version" - homedir "github.com/mitchellh/go-homedir" + "github.com/hashicorp/go-getter" + "github.com/hashicorp/go-version" + "github.com/mitchellh/go-homedir" "github.com/pkg/errors" "github.com/runatlantis/atlantis/server/logging" ) @@ -177,6 +177,10 @@ func (c *DefaultClient) DefaultVersion() *version.Version { return c.defaultVersion } +func (c *DefaultClient) TerraformBinDir() string { + return c.binDir +} + // See Client.RunCommandWithVersion. func (c *DefaultClient) RunCommandWithVersion(log *logging.SimpleLogger, path string, args []string, v *version.Version, workspace string) (string, error) { tfCmd, cmd, err := c.prepCmd(log, v, workspace, path, args) diff --git a/server/server.go b/server/server.go index 5289c58957..eaa29f13b9 100644 --- a/server/server.go +++ b/server/server.go @@ -283,6 +283,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) { }, RunStepRunner: &runtime.RunStepRunner{ DefaultTFVersion: defaultTfVersion, + TerraformBinDir: terraformClient.TerraformBinDir(), }, PullApprovedChecker: vcsClient, WorkingDir: workingDir, From 5c99d1c2a42d2142522e9833f5c21f1977d369d8 Mon Sep 17 00:00:00 2001 From: Maxime Deravet Date: Thu, 13 Jun 2019 12:54:34 -0700 Subject: [PATCH 2/3] Expand $PATH --- server/events/runtime/run_step_runner.go | 3 ++- server/events/runtime/run_step_runner_test.go | 10 ++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/server/events/runtime/run_step_runner.go b/server/events/runtime/run_step_runner.go index 6a2f859847..b41dc2635f 100644 --- a/server/events/runtime/run_step_runner.go +++ b/server/events/runtime/run_step_runner.go @@ -33,7 +33,7 @@ func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, pa "HEAD_BRANCH_NAME": ctx.Pull.HeadBranch, "HEAD_REPO_NAME": ctx.HeadRepo.Name, "HEAD_REPO_OWNER": ctx.HeadRepo.Owner, - "PATH": fmt.Sprintf("$PATH:%s", r.TerraformBinDir), + "PATH": fmt.Sprintf(os.ExpandEnv("$PATH:%s"), r.TerraformBinDir), "PLANFILE": filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectName)), "PROJECT_NAME": ctx.ProjectName, "PULL_AUTHOR": ctx.Pull.Author, @@ -46,6 +46,7 @@ func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, pa for key, val := range customEnvVars { finalEnvVars = append(finalEnvVars, fmt.Sprintf("%s=%s", key, val)) } + cmd.Env = finalEnvVars out, err := cmd.CombinedOutput() diff --git a/server/events/runtime/run_step_runner_test.go b/server/events/runtime/run_step_runner_test.go index 54b3ef38e2..63fee17c75 100644 --- a/server/events/runtime/run_step_runner_test.go +++ b/server/events/runtime/run_step_runner_test.go @@ -1,10 +1,12 @@ package runtime_test import ( + "fmt" + "os" "strings" "testing" - version "github.com/hashicorp/go-version" + "github.com/hashicorp/go-version" "github.com/runatlantis/atlantis/server/events/models" "github.com/runatlantis/atlantis/server/events/runtime" "github.com/runatlantis/atlantis/server/logging" @@ -62,9 +64,9 @@ func TestRunStepRunner_Run(t *testing.T) { { Command: "echo user_name=$USER_NAME", ExpOut: "user_name=acme-user\n", - },{ + }, { Command: "echo $PATH", - ExpOut: "$PATH:/bin/dir\n", + ExpOut: fmt.Sprintf(os.ExpandEnv("$PATH%s"), ":/bin/dir\n"), }, } @@ -73,7 +75,7 @@ func TestRunStepRunner_Run(t *testing.T) { defaultVersion, _ := version.NewVersion("0.8") r := runtime.RunStepRunner{ DefaultTFVersion: defaultVersion, - TerraformBinDir: "/bin/dir", + TerraformBinDir: "/bin/dir", } for _, c := range cases { t.Run(c.Command, func(t *testing.T) { From 92aaecf5f687625972882dcb82b041b6f3798640 Mon Sep 17 00:00:00 2001 From: Maxime Deravet Date: Fri, 14 Jun 2019 09:55:38 -0700 Subject: [PATCH 3/3] Resolve PR comments Add comments on new fields/methods Use Os.Getenv to build the new PATH --- server/events/runtime/run_step_runner.go | 6 +++--- server/events/runtime/run_step_runner_test.go | 2 +- server/events/terraform/terraform_client.go | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/server/events/runtime/run_step_runner.go b/server/events/runtime/run_step_runner.go index b41dc2635f..1d1f10007a 100644 --- a/server/events/runtime/run_step_runner.go +++ b/server/events/runtime/run_step_runner.go @@ -13,7 +13,8 @@ import ( // RunStepRunner runs custom commands. type RunStepRunner struct { DefaultTFVersion *version.Version - TerraformBinDir string + // TerraformBinDir is the directory where Atlantis downloads Terraform binaries. + TerraformBinDir string } func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, path string) (string, error) { @@ -33,7 +34,7 @@ func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, pa "HEAD_BRANCH_NAME": ctx.Pull.HeadBranch, "HEAD_REPO_NAME": ctx.HeadRepo.Name, "HEAD_REPO_OWNER": ctx.HeadRepo.Owner, - "PATH": fmt.Sprintf(os.ExpandEnv("$PATH:%s"), r.TerraformBinDir), + "PATH": fmt.Sprintf("%s:%s", os.Getenv("PATH"), r.TerraformBinDir), "PLANFILE": filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectName)), "PROJECT_NAME": ctx.ProjectName, "PULL_AUTHOR": ctx.Pull.Author, @@ -46,7 +47,6 @@ func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, pa for key, val := range customEnvVars { finalEnvVars = append(finalEnvVars, fmt.Sprintf("%s=%s", key, val)) } - cmd.Env = finalEnvVars out, err := cmd.CombinedOutput() diff --git a/server/events/runtime/run_step_runner_test.go b/server/events/runtime/run_step_runner_test.go index 63fee17c75..f30f250b60 100644 --- a/server/events/runtime/run_step_runner_test.go +++ b/server/events/runtime/run_step_runner_test.go @@ -66,7 +66,7 @@ func TestRunStepRunner_Run(t *testing.T) { ExpOut: "user_name=acme-user\n", }, { Command: "echo $PATH", - ExpOut: fmt.Sprintf(os.ExpandEnv("$PATH%s"), ":/bin/dir\n"), + ExpOut: fmt.Sprintf("%s:%s\n", os.Getenv("PATH"), "/bin/dir"), }, } diff --git a/server/events/terraform/terraform_client.go b/server/events/terraform/terraform_client.go index ba444bc124..332768e563 100644 --- a/server/events/terraform/terraform_client.go +++ b/server/events/terraform/terraform_client.go @@ -177,6 +177,7 @@ func (c *DefaultClient) DefaultVersion() *version.Version { return c.defaultVersion } +// TerraformBinDir returns the directory where we download Terraform binaries. func (c *DefaultClient) TerraformBinDir() string { return c.binDir }