Fix todos. Remove TF_WORKSPACE.

This commit is contained in:
Luke Kysow
2018-07-02 14:39:39 +02:00
parent ef728de0f3
commit 85a775b94b
7 changed files with 53 additions and 67 deletions

View File

@@ -3,11 +3,8 @@ package events
import (
"fmt"
"os"
"path/filepath"
"github.com/docker/docker/pkg/fileutils"
"github.com/hashicorp/go-version"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/vcs"
"github.com/runatlantis/atlantis/server/events/yaml"
@@ -101,7 +98,7 @@ func (p *DefaultProjectCommandBuilder) BuildAutoplanCommands(ctx *CommandContext
} else {
// Otherwise, we use the projects that match the WhenModified fields
// in the config file.
matchingProjects, err := p.matchingProjects(ctx.Log, modifiedFiles, config)
matchingProjects, err := p.ProjectFinder.DetermineProjectsViaConfig(ctx.Log, modifiedFiles, config, repoDir)
if err != nil {
return nil, err
}
@@ -227,44 +224,3 @@ func (p *DefaultProjectCommandBuilder) getCfg(projectName string, dir string, wo
}
return &projCfgs[0], &globalCfg, nil
}
// matchingProjects returns the list of projects whose WhenModified fields match
// any of the modifiedFiles.
func (p *DefaultProjectCommandBuilder) matchingProjects(log *logging.SimpleLogger, modifiedFiles []string, config valid.Config) ([]valid.Project, error) {
var projects []valid.Project
for _, project := range config.Projects {
log.Debug("checking if project at dir %q workspace %q was modified", project.Dir, project.Workspace)
if !project.Autoplan.Enabled {
log.Debug("autoplan disabled, ignoring")
continue
}
// Prepend project dir to when modified patterns because the patterns
// are relative to the project dirs but our list of modified files is
// relative to the repo root.
var whenModifiedRelToRepoRoot []string
for _, wm := range project.Autoplan.WhenModified {
whenModifiedRelToRepoRoot = append(whenModifiedRelToRepoRoot, filepath.Join(project.Dir, wm))
}
pm, err := fileutils.NewPatternMatcher(whenModifiedRelToRepoRoot)
if err != nil {
return nil, errors.Wrapf(err, "matching modified files with patterns: %v", project.Autoplan.WhenModified)
}
// If any of the modified files matches the pattern then this project is
// considered modified.
for _, file := range modifiedFiles {
match, err := pm.Matches(file)
if err != nil {
log.Debug("match err for file %q: %s", file, err)
continue
}
if match {
log.Debug("file %q matched pattern", file)
projects = append(projects, project)
break
}
}
}
// todo: check if dir is deleted though
return projects, nil
}

View File

@@ -19,7 +19,10 @@ import (
"path/filepath"
"strings"
"github.com/docker/docker/pkg/fileutils"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/yaml/valid"
"github.com/runatlantis/atlantis/server/logging"
)
@@ -30,6 +33,7 @@ type ProjectFinder interface {
// DetermineProjects returns the list of projects that were modified based on
// the modifiedFiles. The list will be de-duplicated.
DetermineProjects(log *logging.SimpleLogger, modifiedFiles []string, repoFullName string, repoDir string) []models.Project
DetermineProjectsViaConfig(log *logging.SimpleLogger, modifiedFiles []string, config valid.Config, repoDir string) ([]valid.Project, error)
}
// DefaultProjectFinder implements ProjectFinder.
@@ -72,6 +76,45 @@ func (p *DefaultProjectFinder) DetermineProjects(log *logging.SimpleLogger, modi
return projects
}
func (p *DefaultProjectFinder) DetermineProjectsViaConfig(log *logging.SimpleLogger, modifiedFiles []string, config valid.Config, repoDir string) ([]valid.Project, error) {
var projects []valid.Project
for _, project := range config.Projects {
log.Debug("checking if project at dir %q workspace %q was modified", project.Dir, project.Workspace)
if !project.Autoplan.Enabled {
log.Debug("autoplan disabled, ignoring")
continue
}
// Prepend project dir to when modified patterns because the patterns
// are relative to the project dirs but our list of modified files is
// relative to the repo root.
var whenModifiedRelToRepoRoot []string
for _, wm := range project.Autoplan.WhenModified {
whenModifiedRelToRepoRoot = append(whenModifiedRelToRepoRoot, filepath.Join(project.Dir, wm))
}
pm, err := fileutils.NewPatternMatcher(whenModifiedRelToRepoRoot)
if err != nil {
return nil, errors.Wrapf(err, "matching modified files with patterns: %v", project.Autoplan.WhenModified)
}
// If any of the modified files matches the pattern then this project is
// considered modified.
for _, file := range modifiedFiles {
match, err := pm.Matches(file)
if err != nil {
log.Debug("match err for file %q: %s", file, err)
continue
}
if match {
log.Debug("file %q matched pattern", file)
projects = append(projects, project)
break
}
}
}
// todo: check if dir is deleted though
return projects, nil
}
func (p *DefaultProjectFinder) filterToTerraform(files []string) []string {
var filtered []string
for _, fileName := range files {

View File

@@ -88,11 +88,11 @@ func (p *PlanStepRunner) switchWorkspace(ctx models.ProjectCommandContext, path
}
}
// Finally we'll have to select the workspace. Although we end up running
// with TF_WORKSPACE set, we need to figure out if this workspace exists so
// we can create it if it doesn't. To do this we can either select and catch
// the error or use list and then look for the workspace. Both commands take
// the same amount of time so that's why we're running select here.
// Finally we'll have to select the workspace. We need to figure out if this
// workspace exists so we can create it if it doesn't.
// To do this we can either select and catch the error or use list and then
// look for the workspace. Both commands take the same amount of time so
// that's why we're running select here.
_, err := p.TerraformExecutor.RunCommandWithVersion(ctx.Log, path, []string{workspaceCmd, "select", "-no-color", ctx.Workspace}, tfVersion, ctx.Workspace)
if err != nil {
// If terraform workspace select fails we run terraform workspace

View File

@@ -45,16 +45,13 @@ const terraformPluginCacheDirName = "plugin-cache"
var versionRegex = regexp.MustCompile("Terraform v(.*)\n")
func NewClient(dataDir string) (*DefaultClient, error) {
// todo: use exec.LookPath to find out if we even have terraform rather than
// parsing the error looking for a not found error.
_, err := exec.LookPath("terraform")
if err != nil {
return nil, errors.New("terraform not found in $PATH. \n\nDownload terraform from https://www.terraform.io/downloads.html")
}
versionCmdOutput, err := exec.Command("terraform", "version").CombinedOutput() // #nosec
output := string(versionCmdOutput)
if err != nil {
// exec.go line 35, Error() returns
// "exec: " + strconv.Quote(e.Name) + ": " + e.Err.Error()
if err.Error() == fmt.Sprintf("exec: \"terraform\": %s", exec.ErrNotFound.Error()) {
return nil, errors.New("terraform not found in $PATH. \n\nDownload terraform from https://www.terraform.io/downloads.html")
}
return nil, errors.Wrapf(err, "running terraform version: %s", output)
}
match := versionRegex.FindStringSubmatch(output)
@@ -105,13 +102,6 @@ func (c *DefaultClient) RunCommandWithVersion(log *logging.SimpleLogger, path st
"TF_IN_AUTOMATION=true",
// Cache plugins so terraform init runs faster.
fmt.Sprintf("TF_PLUGIN_CACHE_DIR=%s", c.terraformPluginCacheDir),
// Terraform will run all commands in this workspace. We should have
// already selected this workspace but this is a fail-safe to ensure
// we're operating in the right workspace.
fmt.Sprintf("TF_WORKSPACE=%s", workspace),
// We're keeping this variable even though it duplicates TF_WORKSPACE
// because it's probably safer for users to rely on it. Terraform might
// change the way TF_WORKSPACE works in the future.
fmt.Sprintf("WORKSPACE=%s", workspace),
fmt.Sprintf("ATLANTIS_TERRAFORM_VERSION=%s", tfVersionStr),
fmt.Sprintf("DIR=%s", path),

View File

@@ -13,5 +13,4 @@
//
package vcs
// todo: actually test
// purposefully empty to trigger coverage report

View File

@@ -13,5 +13,4 @@
//
package logging_test
// todo: actually test
// purposefully empty to trigger coverage report

View File

@@ -13,5 +13,4 @@
//
package recovery_test
// todo: actually test
// purposefully empty to trigger coverage report