forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraformExecute.go
113 lines (85 loc) · 2.96 KB
/
terraformExecute.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
package cmd
import (
"bytes"
"fmt"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/SAP/jenkins-library/pkg/terraform"
)
type terraformExecuteUtils interface {
command.ExecRunner
FileExists(filename string) (bool, error)
}
type terraformExecuteUtilsBundle struct {
*command.Command
*piperutils.Files
}
func newTerraformExecuteUtils() terraformExecuteUtils {
utils := terraformExecuteUtilsBundle{
Command: &command.Command{},
Files: &piperutils.Files{},
}
// Reroute command output to logging framework
utils.Stdout(log.Writer())
utils.Stderr(log.Writer())
return &utils
}
func terraformExecute(config terraformExecuteOptions, telemetryData *telemetry.CustomData, commonPipelineEnvironment *terraformExecuteCommonPipelineEnvironment) {
utils := newTerraformExecuteUtils()
err := runTerraformExecute(&config, telemetryData, utils, commonPipelineEnvironment)
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}
func runTerraformExecute(config *terraformExecuteOptions, telemetryData *telemetry.CustomData, utils terraformExecuteUtils, commonPipelineEnvironment *terraformExecuteCommonPipelineEnvironment) error {
if len(config.CliConfigFile) > 0 {
utils.AppendEnv([]string{fmt.Sprintf("TF_CLI_CONFIG_FILE=%s", config.CliConfigFile)})
}
if len(config.Workspace) > 0 {
utils.AppendEnv([]string{fmt.Sprintf("TF_WORKSPACE=%s", config.Workspace)})
}
args := []string{}
if piperutils.ContainsString([]string{"apply", "destroy"}, config.Command) {
args = append(args, "-auto-approve")
}
if piperutils.ContainsString([]string{"apply", "plan"}, config.Command) && config.TerraformSecrets != "" {
args = append(args, fmt.Sprintf("-var-file=%s", config.TerraformSecrets))
}
if piperutils.ContainsString([]string{"init", "validate", "plan", "apply", "destroy"}, config.Command) {
args = append(args, "-no-color")
}
if config.AdditionalArgs != nil {
args = append(args, config.AdditionalArgs...)
}
if config.Init {
err := runTerraform(utils, "init", []string{"-no-color"}, config.GlobalOptions)
if err != nil {
return err
}
}
err := runTerraform(utils, config.Command, args, config.GlobalOptions)
if err != nil {
return err
}
var outputBuffer bytes.Buffer
utils.Stdout(&outputBuffer)
err = runTerraform(utils, "output", []string{"-json"}, config.GlobalOptions)
if err != nil {
return err
}
commonPipelineEnvironment.custom.terraformOutputs, err = terraform.ReadOutputs(outputBuffer.String())
return err
}
func runTerraform(utils terraformExecuteUtils, command string, additionalArgs []string, globalOptions []string) error {
args := []string{}
if len(globalOptions) > 0 {
args = append(args, globalOptions...)
}
args = append(args, command)
if len(additionalArgs) > 0 {
args = append(args, additionalArgs...)
}
return utils.RunExecutable("terraform", args...)
}