forked from openshift/installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform.go
160 lines (138 loc) · 4.57 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
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
149
150
151
152
153
154
155
156
157
158
159
160
package terraform
import (
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/openshift/installer/data"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/openshift/installer/pkg/lineprinter"
texec "github.com/openshift/installer/pkg/terraform/exec"
"github.com/openshift/installer/pkg/terraform/exec/plugins"
)
const (
// StateFileName is the default name for Terraform state files.
StateFileName string = "terraform.tfstate"
// VarFileName is the default name for Terraform var file.
VarFileName string = "terraform.tfvars"
)
// Apply unpacks the platform-specific Terraform modules into the
// given directory and then runs 'terraform init' and 'terraform
// apply'. It returns the absolute path of the tfstate file, rooted
// in the specified directory, along with any errors from Terraform.
func Apply(dir string, platform string, extraArgs ...string) (path string, err error) {
err = unpackAndInit(dir, platform)
if err != nil {
return "", err
}
defaultArgs := []string{
"-auto-approve",
"-input=false",
fmt.Sprintf("-state=%s", filepath.Join(dir, StateFileName)),
fmt.Sprintf("-state-out=%s", filepath.Join(dir, StateFileName)),
}
args := append(defaultArgs, extraArgs...)
args = append(args, dir)
sf := filepath.Join(dir, StateFileName)
tDebug := &lineprinter.Trimmer{WrappedPrint: logrus.Debug}
tError := &lineprinter.Trimmer{WrappedPrint: logrus.Error}
lpDebug := &lineprinter.LinePrinter{Print: tDebug.Print}
lpError := &lineprinter.LinePrinter{Print: tError.Print}
defer lpDebug.Close()
defer lpError.Close()
if exitCode := texec.Apply(dir, args, lpDebug, lpError); exitCode != 0 {
return sf, errors.New("failed to apply using Terraform")
}
return sf, nil
}
// Destroy unpacks the platform-specific Terraform modules into the
// given directory and then runs 'terraform init' and 'terraform
// destroy'.
func Destroy(dir string, platform string, extraArgs ...string) (err error) {
err = unpackAndInit(dir, platform)
if err != nil {
return err
}
defaultArgs := []string{
"-auto-approve",
"-input=false",
fmt.Sprintf("-state=%s", filepath.Join(dir, StateFileName)),
fmt.Sprintf("-state-out=%s", filepath.Join(dir, StateFileName)),
}
args := append(defaultArgs, extraArgs...)
args = append(args, dir)
tDebug := &lineprinter.Trimmer{WrappedPrint: logrus.Debug}
tError := &lineprinter.Trimmer{WrappedPrint: logrus.Error}
lpDebug := &lineprinter.LinePrinter{Print: tDebug.Print}
lpError := &lineprinter.LinePrinter{Print: tError.Print}
defer lpDebug.Close()
defer lpError.Close()
if exitCode := texec.Destroy(dir, args, lpDebug, lpError); exitCode != 0 {
return errors.New("failed to destroy using Terraform")
}
return nil
}
// unpack unpacks the platform-specific Terraform modules into the
// given directory.
func unpack(dir string, platform string) (err error) {
err = data.Unpack(dir, platform)
if err != nil {
return err
}
err = data.Unpack(filepath.Join(dir, "config.tf"), "config.tf")
if err != nil {
return err
}
return nil
}
// unpackAndInit unpacks the platform-specific Terraform modules into
// the given directory and then runs 'terraform init'.
func unpackAndInit(dir string, platform string) (err error) {
err = unpack(dir, platform)
if err != nil {
return errors.Wrap(err, "failed to unpack Terraform modules")
}
if err := setupEmbeddedPlugins(dir); err != nil {
return errors.Wrap(err, "failed to setup embedded Terraform plugins")
}
tDebug := &lineprinter.Trimmer{WrappedPrint: logrus.Debug}
tError := &lineprinter.Trimmer{WrappedPrint: logrus.Error}
lpDebug := &lineprinter.LinePrinter{Print: tDebug.Print}
lpError := &lineprinter.LinePrinter{Print: tError.Print}
defer lpDebug.Close()
defer lpError.Close()
args := []string{
"-get-plugins=false",
}
args = append(args, dir)
if exitCode := texec.Init(dir, args, lpDebug, lpError); exitCode != 0 {
return errors.New("failed to initialize Terraform")
}
return nil
}
func setupEmbeddedPlugins(dir string) error {
execPath, err := os.Executable()
if err != nil {
return errors.Wrap(err, "failed to find path for the executable")
}
pdir := filepath.Join(dir, "plugins")
if err := os.MkdirAll(pdir, 0777); err != nil {
return err
}
for name := range plugins.KnownPlugins {
dst := filepath.Join(pdir, name)
if runtime.GOOS == "windows" {
dst = fmt.Sprintf("%s.exe", dst)
}
if _, err := os.Stat(dst); err == nil {
// stat succeeded, the plugin already exists.
continue
}
logrus.Debugf("Symlinking plugin %s src: %q dst: %q", name, execPath, dst)
if err := os.Symlink(execPath, dst); err != nil {
return err
}
}
return nil
}