-
Notifications
You must be signed in to change notification settings - Fork 143
/
terraform.go
99 lines (87 loc) · 3.17 KB
/
terraform.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
// Copyright 2020 The PipeCD Authors.
//
// 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.
package terraform
import (
"context"
provider "github.com/pipe-cd/pipecd/pkg/app/piped/cloudprovider/terraform"
"github.com/pipe-cd/pipecd/pkg/app/piped/executor"
"github.com/pipe-cd/pipecd/pkg/app/piped/toolregistry"
"github.com/pipe-cd/pipecd/pkg/config"
"github.com/pipe-cd/pipecd/pkg/model"
)
type registerer interface {
Register(stage model.Stage, f executor.Factory) error
RegisterRollback(kind model.ApplicationKind, f executor.Factory) error
}
func Register(r registerer) {
f := func(in executor.Input) executor.Executor {
return &deployExecutor{
Input: in,
}
}
r.Register(model.StageTerraformSync, f)
r.Register(model.StageTerraformPlan, f)
r.Register(model.StageTerraformApply, f)
r.RegisterRollback(model.ApplicationKind_TERRAFORM, func(in executor.Input) executor.Executor {
return &rollbackExecutor{
Input: in,
}
})
}
func showUsingVersion(ctx context.Context, cmd *provider.Terraform, lp executor.LogPersister) bool {
version, err := cmd.Version(ctx)
if err != nil {
lp.Errorf("Failed to check terraform version (%v)", err)
return false
}
lp.Infof("Using terraform version %q to execute the terraform commands", version)
return true
}
func selectWorkspace(ctx context.Context, cmd *provider.Terraform, workspace string, lp executor.LogPersister) bool {
if workspace == "" {
return true
}
if err := cmd.SelectWorkspace(ctx, workspace); err != nil {
lp.Errorf("Failed to select workspace %q (%v). You might need to create the workspace before using by command %q", workspace, err, "terraform workspace new "+workspace)
return false
}
lp.Infof("Selected workspace %q", workspace)
return true
}
func findTerraform(ctx context.Context, version string, lp executor.LogPersister) (string, bool) {
path, installed, err := toolregistry.DefaultRegistry().Terraform(ctx, version)
if err != nil {
lp.Errorf("Unable to find required terraform %q (%v)", version, err)
return "", false
}
if installed {
lp.Infof("Terraform %q has just been installed to %q because of no pre-installed binary for that version", version, path)
}
return path, true
}
func findCloudProvider(in *executor.Input) (name string, cfg *config.CloudProviderTerraformConfig, found bool) {
name = in.Application.CloudProvider
if name == "" {
in.LogPersister.Error("Missing the CloudProvider name in the application configuration")
return
}
cp, ok := in.PipedConfig.FindCloudProvider(name, model.ApplicationKind_TERRAFORM)
if !ok {
in.LogPersister.Errorf("The specified cloud provider %q was not found in piped configuration", name)
return
}
cfg = cp.TerraformConfig
found = true
return
}