From 85a775b94b098fddc9fe90dcf3fb1ca86ea34038 Mon Sep 17 00:00:00 2001 From: Luke Kysow Date: Mon, 2 Jul 2018 14:39:39 +0200 Subject: [PATCH] Fix todos. Remove TF_WORKSPACE. --- server/events/project_command_builder.go | 46 +-------------------- server/events/project_finder.go | 43 +++++++++++++++++++ server/events/runtime/plan_step_runner.go | 10 ++--- server/events/terraform/terraform_client.go | 18 ++------ server/events/vcs/client_test.go | 1 - server/logging/logging_test.go | 1 - server/recovery/recovery_test.go | 1 - 7 files changed, 53 insertions(+), 67 deletions(-) diff --git a/server/events/project_command_builder.go b/server/events/project_command_builder.go index cff97ed38..9c8947839 100644 --- a/server/events/project_command_builder.go +++ b/server/events/project_command_builder.go @@ -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 -} diff --git a/server/events/project_finder.go b/server/events/project_finder.go index 315f480bd..254e51c3c 100644 --- a/server/events/project_finder.go +++ b/server/events/project_finder.go @@ -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 { diff --git a/server/events/runtime/plan_step_runner.go b/server/events/runtime/plan_step_runner.go index 88f4abc2d..00fe94df7 100644 --- a/server/events/runtime/plan_step_runner.go +++ b/server/events/runtime/plan_step_runner.go @@ -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 diff --git a/server/events/terraform/terraform_client.go b/server/events/terraform/terraform_client.go index bd2e6de70..b34516c7c 100644 --- a/server/events/terraform/terraform_client.go +++ b/server/events/terraform/terraform_client.go @@ -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), diff --git a/server/events/vcs/client_test.go b/server/events/vcs/client_test.go index 61060cd3b..08ab44393 100644 --- a/server/events/vcs/client_test.go +++ b/server/events/vcs/client_test.go @@ -13,5 +13,4 @@ // package vcs -// todo: actually test // purposefully empty to trigger coverage report diff --git a/server/logging/logging_test.go b/server/logging/logging_test.go index f5cf867ba..1eb24dc29 100644 --- a/server/logging/logging_test.go +++ b/server/logging/logging_test.go @@ -13,5 +13,4 @@ // package logging_test -// todo: actually test // purposefully empty to trigger coverage report diff --git a/server/recovery/recovery_test.go b/server/recovery/recovery_test.go index 0d5355315..4e2e24729 100644 --- a/server/recovery/recovery_test.go +++ b/server/recovery/recovery_test.go @@ -13,5 +13,4 @@ // package recovery_test -// todo: actually test // purposefully empty to trigger coverage report