-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathterraform.go
116 lines (95 loc) · 3.58 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 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"
"fmt"
provider "github.com/pipe-cd/pipe/pkg/app/piped/cloudprovider/terraform"
"github.com/pipe-cd/pipe/pkg/app/piped/executor"
"github.com/pipe-cd/pipe/pkg/app/piped/toolregistry"
"github.com/pipe-cd/pipe/pkg/config"
"github.com/pipe-cd/pipe/pkg/model"
)
type Executor struct {
executor.Input
config *config.TerraformDeploymentSpec
}
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 &Executor{
Input: in,
}
}
r.Register(model.StageTerraformSync, f)
r.Register(model.StageTerraformPlan, f)
r.Register(model.StageTerraformApply, f)
r.RegisterRollback(model.ApplicationKind_TERRAFORM, f)
}
func (e *Executor) Execute(sig executor.StopSignal) model.StageStatus {
e.config = e.DeploymentConfig.TerraformDeploymentSpec
if e.config == nil {
e.LogPersister.Error("Malformed deployment configuration: missing TerraformDeploymentSpec")
return model.StageStatus_STAGE_FAILURE
}
var (
ctx = sig.Context()
originalStatus = e.Stage.Status
status model.StageStatus
)
switch model.Stage(e.Stage.Name) {
case model.StageTerraformSync:
status = e.ensureSync(ctx)
case model.StageTerraformPlan:
status = e.ensurePlan(ctx)
case model.StageTerraformApply:
status = e.ensureApply(ctx)
case model.StageRollback:
status = e.ensureRollback(ctx)
default:
e.LogPersister.Errorf("Unsupported stage %s for cloudrun application", e.Stage.Name)
return model.StageStatus_STAGE_FAILURE
}
return executor.DetermineStageStatus(sig.Signal(), originalStatus, status)
}
func (e *Executor) ensureSync(ctx context.Context) model.StageStatus {
// terraform init -no-color '-var-file=simplegcs/terraform.tfvars' '-lock=false' simplegcs
// terraform workspace select -no-color default simplegcs
// terraform validate -no-color simplegcs
// terraform plan -no-color '-var-file=simplegcs/terraform.tfvars' '-lock=false' simplegcs
// terraform apply -no-color -auto-approve '-input=false' '-var-file=simplegcs/terraform.tfvars' simplegcs
return model.StageStatus_STAGE_SUCCESS
}
func (e *Executor) ensurePlan(ctx context.Context) model.StageStatus {
return model.StageStatus_STAGE_SUCCESS
}
func (e *Executor) ensureApply(ctx context.Context) model.StageStatus {
return model.StageStatus_STAGE_SUCCESS
}
func (e *Executor) ensureRollback(ctx context.Context) model.StageStatus {
return model.StageStatus_STAGE_SUCCESS
}
func (e *Executor) findTerraform(ctx context.Context, version string) (*provider.Terraform, error) {
path, installed, err := toolregistry.DefaultRegistry().Terraform(ctx, version)
if err != nil {
return nil, fmt.Errorf("no terraform %s (%v)", version, err)
}
if installed {
e.LogPersister.Infof("Terraform %s has just been installed because of no pre-installed binary for that version", version)
}
return provider.NewTerraform(version, path), nil
}