Files
atlantis/server/events/runtime/apply_step_runner.go
Luke Kysow e98caf3094 Quote args and plan path that may contain spaces.
Bitbucket repo owner names can contain spaces. Need to quote everywhere
these might be used because we're executing with sh -c.
2018-10-01 19:04:48 -05:00

42 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package runtime
import (
"fmt"
"os"
"path/filepath"
"github.com/hashicorp/go-version"
"github.com/runatlantis/atlantis/server/events/models"
)
// ApplyStepRunner runs `terraform apply`.
type ApplyStepRunner struct {
TerraformExecutor TerraformExec
}
func (a *ApplyStepRunner) Run(ctx models.ProjectCommandContext, extraArgs []string, path string) (string, error) {
planPath := filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectConfig))
stat, err := os.Stat(planPath)
if err != nil || stat.IsDir() {
return "", fmt.Errorf("no plan found at path %q and workspace %qdid you run plan?", ctx.RepoRelDir, ctx.Workspace)
}
// NOTE: we need to quote the plan path because Bitbucket Server can
// have spaces in its repo owner names which is part of the path.
tfApplyCmd := append(append(append([]string{"apply", "-input=false", "-no-color"}, extraArgs...), ctx.CommentArgs...), fmt.Sprintf("%q", planPath))
var tfVersion *version.Version
if ctx.ProjectConfig != nil && ctx.ProjectConfig.TerraformVersion != nil {
tfVersion = ctx.ProjectConfig.TerraformVersion
}
out, tfErr := a.TerraformExecutor.RunCommandWithVersion(ctx.Log, path, tfApplyCmd, tfVersion, ctx.Workspace)
// If the apply was successful, delete the plan.
if tfErr == nil {
ctx.Log.Info("apply successful, deleting planfile")
if err := os.Remove(planPath); err != nil {
ctx.Log.Warn("failed to delete planfile after successful apply: %s", err)
}
}
return out, tfErr
}