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
17 changes: 10 additions & 7 deletions server/apply_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ func (a *ApplyExecutor) apply(ctx *CommandContext, repoDir string, plan models.P
}
ctx.Log.Info("acquired lock with id %q", lockAttempt.LockKey)

// check if terraform version is >= 0.9.0
terraformVersion := a.terraform.Version()

// check if config file is found, if not we continue the run
absolutePath := filepath.Dir(plan.LocalPath)
var applyExtraArgs []string
Expand All @@ -120,18 +123,18 @@ func (a *ApplyExecutor) apply(ctx *CommandContext, repoDir string, plan models.P
return ProjectResult{Error: err}
}
ctx.Log.Info("parsed atlantis config file in %q", absolutePath)
applyExtraArgs = config.GetExtraArguments(ctx.Command.Name.String())
// check if there was terraform version specified in the project config
// if so then we override the default terraform version
if config.TerraformVersion != nil {
terraformVersion = config.TerraformVersion
}
applyExtraArgs = populateRuntimeEnvironmentVariables(config.GetExtraArguments(ctx.Command.Name.String()), absolutePath, tfEnv, terraformVersion)
}

// check if terraform version is >= 0.9.0
terraformVersion := a.terraform.Version()
if config.TerraformVersion != nil {
terraformVersion = config.TerraformVersion
}
constraints, _ := version.NewConstraint(">= 0.9.0")
if constraints.Check(terraformVersion) {
ctx.Log.Info("determined that we are running terraform with version >= 0.9.0. Running version %s", terraformVersion)
_, err := a.terraform.RunInitAndEnv(ctx.Log, absolutePath, tfEnv, config.GetExtraArguments("init"), terraformVersion)
_, err := a.terraform.RunInitAndEnv(ctx.Log, absolutePath, tfEnv, populateRuntimeEnvironmentVariables(config.GetExtraArguments("init"), absolutePath, tfEnv, terraformVersion), terraformVersion)
if err != nil {
return ProjectResult{Error: err}
}
Expand Down
17 changes: 10 additions & 7 deletions server/plan_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ func (p *PlanExecutor) plan(ctx *CommandContext, repoDir string, project models.
}
ctx.Log.Info("acquired lock with id %q", lockAttempt.LockKey)

// check if terraform version is >= 0.9.0
terraformVersion := p.terraform.Version()

// check if config file is found, if not we continue the run
var config ProjectConfig
absolutePath := filepath.Join(repoDir, project.Path)
Expand All @@ -126,18 +129,18 @@ func (p *PlanExecutor) plan(ctx *CommandContext, repoDir string, project models.
return ProjectResult{Error: err}
}
ctx.Log.Info("parsed atlantis config file in %q", absolutePath)
planExtraArgs = config.GetExtraArguments(ctx.Command.Name.String())
// check if there was terraform version specified in the project config
// if so then we override the default terraform version
if config.TerraformVersion != nil {
terraformVersion = config.TerraformVersion
}
planExtraArgs = populateRuntimeEnvironmentVariables(config.GetExtraArguments(ctx.Command.Name.String()), absolutePath, tfEnv, terraformVersion)
}

// check if terraform version is >= 0.9.0
terraformVersion := p.terraform.Version()
if config.TerraformVersion != nil {
terraformVersion = config.TerraformVersion
}
constraints, _ := version.NewConstraint(">= 0.9.0")
if constraints.Check(terraformVersion) {
ctx.Log.Info("determined that we are running terraform with version >= 0.9.0. Running version %s", terraformVersion)
_, err := p.terraform.RunInitAndEnv(ctx.Log, absolutePath, tfEnv, config.GetExtraArguments("init"), terraformVersion)
_, err := p.terraform.RunInitAndEnv(ctx.Log, absolutePath, tfEnv, populateRuntimeEnvironmentVariables(config.GetExtraArguments("init"), absolutePath, tfEnv, terraformVersion), terraformVersion)
if err != nil {
return ProjectResult{Error: err}
}
Expand Down
22 changes: 22 additions & 0 deletions server/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package server
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added utils.go to be used for functions like these.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this doesn't belong in a server util. It is used for the execution so can we not put it in a command runner? Util classes should be reserved for the most generic of functions. Otherwise it just becomes a dumping ground. This function doesn't strike me as generic enough.


import (
"strings"

version "github.com/hashicorp/go-version"
)

// populateRuntimeEnvironmentVariables populates the terraform extra vars specified in the project config file
// with atlantis specific environment variables
func populateRuntimeEnvironmentVariables(extraArgs []string, workspaceDir string, tfEnv string, tfVersion *version.Version) []string {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the function that replaces the arguments for both plan_executor.go and apply_executor.go.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We've been using object-oriented style functions throughout the codebase so I think we should stick with that for now. That means associating this method with an actual struct.

var extraArgsFinal []string
for _, v := range extraArgs {
if strings.Contains(v, "${ENVIRONMENT}") || strings.Contains(v, "${ATLANTIS_TERRAFORM_VERSION}") || strings.Contains(v, "${WORKSPACE}") {
v = strings.Replace(v, "${ENVIRONMENT}", tfEnv, -1)
v = strings.Replace(v, "${ATLANTIS_TERRAFORM_VERSION}", tfVersion.String(), -1)
v = strings.Replace(v, "${WORKSPACE}", workspaceDir, -1)
}
extraArgsFinal = append(extraArgsFinal, v)
}
return extraArgsFinal
}
38 changes: 38 additions & 0 deletions server/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package server

import (
"testing"

version "github.com/hashicorp/go-version"
. "github.com/hootsuite/atlantis/testing_util"
)

func TestPopulateRuntimeEnvironmentVariables(t *testing.T) {
// test environment variable ${ENVIRONMENT}
extraArgs := []string{"-backend-config=env/${ENVIRONMENT}.tfvars", "-no-color"}
expectedArgs := []string{"-backend-config=env/testing.tfvars", "-no-color"}
tfVersion, _ := version.NewVersion("0.1.1")
args := populateRuntimeEnvironmentVariables(extraArgs, "./workspace", "testing", tfVersion)
Equals(t, expectedArgs, args)

// test environment variable ${WORKSPACE}
extraArgs = []string{"-from-module=${WORKSPACE}/module", "-no-color"}
expectedArgs = []string{"-from-module=./path/to/workspace/module", "-no-color"}
tfVersion, _ = version.NewVersion("0.1.1")
args = populateRuntimeEnvironmentVariables(extraArgs, "./path/to/workspace", "testing", tfVersion)
Equals(t, expectedArgs, args)

// test environment variable ${ATLANTIS_TERRAFORM_VERSION}
extraArgs = []string{"-backend-config=env/${ATLANTIS_TERRAFORM_VERSION}/testing.tfvars", "-no-color"}
expectedArgs = []string{"-backend-config=env/0.1.1/testing.tfvars", "-no-color"}
tfVersion, _ = version.NewVersion("0.1.1")
args = populateRuntimeEnvironmentVariables(extraArgs, "./path/to/workspace", "testing", tfVersion)
Equals(t, expectedArgs, args)

// test all environment variables together
extraArgs = []string{"-backend-config=${WORKSPACE}/env/${ATLANTIS_TERRAFORM_VERSION}/${ENVIRONMENT}.tfvars", "-no-color"}
expectedArgs = []string{"-backend-config=./path/to/workspace/env/0.1.1/testing.tfvars", "-no-color"}
tfVersion, _ = version.NewVersion("0.1.1")
args = populateRuntimeEnvironmentVariables(extraArgs, "./path/to/workspace", "testing", tfVersion)
Equals(t, expectedArgs, args)
}