forked from runatlantis/atlantis
/
plan_executor.go
148 lines (132 loc) · 5.47 KB
/
plan_executor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2017 HootSuite Media Inc.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an AS IS BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Modified hereafter by contributors to runatlantis/atlantis.
//
package events
import (
"fmt"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/locking"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/run"
"github.com/runatlantis/atlantis/server/events/terraform"
"github.com/runatlantis/atlantis/server/events/vcs"
)
//go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_lock_url_generator.go LockURLGenerator
// LockURLGenerator consumes lock URLs.
type LockURLGenerator interface {
// SetLockURL takes a function that given a lock id, will return a url
// to view that lock.
SetLockURL(func(id string) (url string))
}
// atlantisUserTFVar is the name of the variable we execute terraform
// with, containing the vcs username of who is running the command
const atlantisUserTFVar = "atlantis_user"
// PlanExecutor handles everything related to running terraform plan.
type PlanExecutor struct {
VCSClient vcs.ClientProxy
Terraform terraform.Client
Locker locking.Locker
LockURL func(id string) (url string)
Run run.Runner
Workspace AtlantisWorkspace
ProjectPreExecute ProjectPreExecutor
ProjectFinder ProjectFinder
}
// PlanSuccess is the result of a successful plan.
type PlanSuccess struct {
TerraformOutput string
LockURL string
}
// SetLockURL takes a function that given a lock id, will return a url
// to view that lock.
func (p *PlanExecutor) SetLockURL(f func(id string) (url string)) {
p.LockURL = f
}
// Execute executes terraform plan for the ctx.
func (p *PlanExecutor) Execute(ctx *CommandContext) CommandResponse {
cloneDir, err := p.Workspace.Clone(ctx.Log, ctx.BaseRepo, ctx.HeadRepo, ctx.Pull, ctx.Command.Workspace)
if err != nil {
return CommandResponse{Error: err}
}
var projects []models.Project
if ctx.Command.Dir == "" {
// If they didn't specify a directory to plan in, figure out what
// projects have been modified so we know where to run plan.
modifiedFiles, err := p.VCSClient.GetModifiedFiles(ctx.BaseRepo, ctx.Pull)
if err != nil {
return CommandResponse{Error: errors.Wrap(err, "getting modified files")}
}
ctx.Log.Info("found %d files modified in this pull request", len(modifiedFiles))
projects = p.ProjectFinder.DetermineProjects(ctx.Log, modifiedFiles, ctx.BaseRepo.FullName, cloneDir)
if len(projects) == 0 {
return CommandResponse{Failure: "No Terraform files were modified."}
}
} else {
projects = []models.Project{{
Path: ctx.Command.Dir,
RepoFullName: ctx.BaseRepo.FullName,
}}
}
var results []ProjectResult
for _, project := range projects {
ctx.Log.Info("running plan for project at path %q", project.Path)
result := p.plan(ctx, cloneDir, project)
result.Path = project.Path
results = append(results, result)
}
return CommandResponse{ProjectResults: results}
}
func (p *PlanExecutor) plan(ctx *CommandContext, repoDir string, project models.Project) ProjectResult {
preExecute := p.ProjectPreExecute.Execute(ctx, repoDir, project)
if preExecute.ProjectResult != (ProjectResult{}) {
return preExecute.ProjectResult
}
config := preExecute.ProjectConfig
terraformVersion := preExecute.TerraformVersion
workspace := ctx.Command.Workspace
// Run terraform plan.
planFile := filepath.Join(repoDir, project.Path, fmt.Sprintf("%s.tfplan", workspace))
userVar := fmt.Sprintf("%s=%s", atlantisUserTFVar, ctx.User.Username)
planExtraArgs := config.GetExtraArguments(ctx.Command.Name.String())
tfPlanCmd := append(append([]string{"plan", "-refresh", "-no-color", "-out", planFile, "-var", userVar}, planExtraArgs...), ctx.Command.Flags...)
// Check if env/{workspace}.tfvars exist.
envFileName := filepath.Join("env", workspace+".tfvars")
if _, err := os.Stat(filepath.Join(repoDir, project.Path, envFileName)); err == nil {
tfPlanCmd = append(tfPlanCmd, "-var-file", envFileName)
}
output, err := p.Terraform.RunCommandWithVersion(ctx.Log, filepath.Join(repoDir, project.Path), tfPlanCmd, terraformVersion, workspace)
if err != nil {
// Plan failed so unlock the state.
if _, unlockErr := p.Locker.Unlock(preExecute.LockResponse.LockKey); unlockErr != nil {
ctx.Log.Err("error unlocking state after plan error: %v", unlockErr)
}
return ProjectResult{Error: fmt.Errorf("%s\n%s", err.Error(), output)}
}
ctx.Log.Info("plan succeeded")
// If there are post plan commands then run them.
if len(config.PostPlan) > 0 {
absolutePath := filepath.Join(repoDir, project.Path)
_, err := p.Run.Execute(ctx.Log, config.PostPlan, absolutePath, workspace, terraformVersion, "post_plan")
if err != nil {
return ProjectResult{Error: errors.Wrap(err, "running post plan commands")}
}
}
return ProjectResult{
PlanSuccess: &PlanSuccess{
TerraformOutput: output,
LockURL: p.LockURL(preExecute.LockResponse.LockKey),
},
}
}