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

Extra arguments environment variables #150

Merged
merged 11 commits into from
Sep 25, 2017
2 changes: 1 addition & 1 deletion server/apply_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (a *ApplyExecutor) apply(ctx *CommandContext, repoDir string, plan models.P
}

tfApplyCmd := append(append([]string{"apply", "-no-color", plan.LocalPath}, applyExtraArgs...), ctx.Command.Flags...)
output, err := a.terraform.RunCommandWithVersion(ctx.Log, absolutePath, tfApplyCmd, terraformVersion)
output, err := a.terraform.RunCommandWithVersion(ctx.Log, absolutePath, tfApplyCmd, terraformVersion, tfEnv)
if err != nil {
return ProjectResult{Error: fmt.Errorf("%s\n%s", err.Error(), output)}
}
Expand Down
4 changes: 2 additions & 2 deletions server/plan_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (p *PlanExecutor) plan(ctx *CommandContext, repoDir string, project models.
} else {
ctx.Log.Info("determined that we are running terraform with version < 0.9.0. Running version %s", terraformVersion)
terraformGetCmd := append([]string{"get", "-no-color"}, config.GetExtraArguments("get")...)
_, err := p.terraform.RunCommandWithVersion(ctx.Log, absolutePath, terraformGetCmd, terraformVersion)
_, err := p.terraform.RunCommandWithVersion(ctx.Log, absolutePath, terraformGetCmd, terraformVersion, tfEnv)
if err != nil {
return ProjectResult{Error: err}
}
Expand All @@ -168,7 +168,7 @@ func (p *PlanExecutor) plan(ctx *CommandContext, repoDir string, project models.
if _, err := os.Stat(filepath.Join(repoDir, project.Path, tfEnvFileName)); err == nil {
tfPlanCmd = append(tfPlanCmd, "-var-file", tfEnvFileName)
}
output, err := p.terraform.RunCommandWithVersion(ctx.Log, filepath.Join(repoDir, project.Path), tfPlanCmd, terraformVersion)
output, err := p.terraform.RunCommandWithVersion(ctx.Log, filepath.Join(repoDir, project.Path), tfPlanCmd, terraformVersion, tfEnv)
if err != nil {
// plan failed so unlock the state
if _, err := p.lockingClient.Unlock(lockAttempt.LockKey); err != nil {
Expand Down
28 changes: 23 additions & 5 deletions terraform/terraform_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package terraform

import (
"fmt"
"os"
"os/exec"
"regexp"

Expand Down Expand Up @@ -52,14 +53,31 @@ func (c *Client) Version() *version.Version {

// RunCommandWithVersion executes the provided version of terraform with
// the provided args in path.
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should document what v and env do in the docs here

func (c *Client) RunCommandWithVersion(log *logging.SimpleLogger, path string, args []string, v *version.Version) (string, error) {
func (c *Client) RunCommandWithVersion(log *logging.SimpleLogger, path string, args []string, v *version.Version, env string) (string, error) {
tfExecutable := "terraform"
// if version is the same as the default, don't need to prepend the version name to the executable
if !v.Equal(c.defaultVersion) {
tfExecutable = fmt.Sprintf("%s%s", tfExecutable, v.String())
}
terraformCmd := exec.Command(tfExecutable, args...)

// set environment variables
// this is to support scripts to use the ENVIRONMENT, ATLANTIS_TERRAFORM_VERSION
// and WORKSPACE variables in their scripts
// append current process's environment variables
// this is to prevent the $PATH variable being removed from the environment
envVars := []string{
fmt.Sprintf("ENVIRONMENT=%s", env),
fmt.Sprintf("ATLANTIS_TERRAFORM_VERSION=%s", v.String()),
fmt.Sprintf("WORKSPACE=%s", path),
}
envVars = append(envVars, os.Environ()...)

// append terraform executable name with args
tfCmd := fmt.Sprintf("%s %s", tfExecutable, strings.Join(args, " "))

terraformCmd := exec.Command("bash", "-c", tfCmd)
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we use bash or sh?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think sh....

terraformCmd.Dir = path
terraformCmd.Env = envVars
out, err := terraformCmd.CombinedOutput()
commandStr := strings.Join(terraformCmd.Args, " ")
if err != nil {
Expand All @@ -77,19 +95,19 @@ func (c *Client) RunCommandWithVersion(log *logging.SimpleLogger, path string, a
func (c *Client) RunInitAndEnv(log *logging.SimpleLogger, path string, env string, extraInitArgs []string, version *version.Version) ([]string, error) {
var outputs []string
// run terraform init
output, err := c.RunCommandWithVersion(log, path, append([]string{"init", "-no-color"}, extraInitArgs...), version)
output, err := c.RunCommandWithVersion(log, path, append([]string{"init", "-no-color"}, extraInitArgs...), version, env)
outputs = append(outputs, output)
if err != nil {
return outputs, err
}

// run terraform env new and select
output, err = c.RunCommandWithVersion(log, path, []string{"env", "select", "-no-color", env}, version)
output, err = c.RunCommandWithVersion(log, path, []string{"env", "select", "-no-color", env}, version, env)
outputs = append(outputs, output)
if err != nil {
// if terraform env select fails we will run terraform env new
// to create a new environment
output, err = c.RunCommandWithVersion(log, path, []string{"env", "new", "-no-color", env}, version)
output, err = c.RunCommandWithVersion(log, path, []string{"env", "new", "-no-color", env}, version, env)
if err != nil {
return outputs, err
}
Expand Down