This repository has been archived by the owner on Mar 6, 2022. It is now read-only.
/
terraform.go
193 lines (169 loc) · 4.85 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package stack
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
"github.com/dan-v/rattlesnakeos-stack/templates"
log "github.com/sirupsen/logrus"
)
const terraformVersion = "0.11.8"
var darwinBinaryURL = fmt.Sprintf("https://releases.hashicorp.com/terraform/%s/terraform_%s_darwin_amd64.zip", terraformVersion, terraformVersion)
var linuxBinaryURL = fmt.Sprintf("https://releases.hashicorp.com/terraform/%s/terraform_%s_linux_amd64.zip", terraformVersion, terraformVersion)
var windowsBinaryURL = fmt.Sprintf("https://releases.hashicorp.com/terraform/%s/terraform_%s_windows_amd64.zip", terraformVersion, terraformVersion)
const (
lambdaFunctionFilename = "lambda_spot_function.py"
lambdaZipFilename = "lambda_spot.zip"
buildScriptFilename = "build.sh"
)
type terraformClient struct {
configDir string
tempDir *TempDir
stdout io.Writer
stderr io.Writer
}
func newTerraformClient(config *AWSStack, stdout, stderr io.Writer) (*terraformClient, error) {
tempDir, err := NewTempDir("rattlesnakeos-stack")
if err != nil {
return nil, err
}
if err := setupBinary(tempDir); err != nil {
return nil, err
}
// write out shell script
config.BuildScriptFileLocation = tempDir.Path(buildScriptFilename)
if runtime.GOOS == "windows" {
config.BuildScriptFileLocation = strings.Replace(config.BuildScriptFileLocation, "\\", "/", -1)
}
err = ioutil.WriteFile(config.BuildScriptFileLocation, config.renderedBuildScript, 0644)
if err != nil {
return nil, err
}
// write out lambda function and zip it up
err = ioutil.WriteFile(tempDir.Path(lambdaFunctionFilename), config.renderedLambdaFunction, 0644)
if err != nil {
return nil, err
}
// handle potential issue with non default umask as lambda function must have at least 444 permissions to run
if err = os.Chmod(tempDir.Path(lambdaFunctionFilename), 0644); err != nil {
return nil, err
}
files := []string{tempDir.Path(lambdaFunctionFilename)}
output := tempDir.Path(lambdaZipFilename)
err = zipFiles(output, files)
if err != nil {
return nil, err
}
config.LambdaZipFileLocation = tempDir.Path(lambdaZipFilename)
if runtime.GOOS == "windows" {
config.LambdaZipFileLocation = strings.Replace(config.LambdaZipFileLocation, "\\", "/", -1)
}
// render terraform template
renderedTerraform, err := renderTemplate(templates.TerraformTemplate, config)
if err != nil {
return nil, fmt.Errorf("Failed to render terraform template: %v", err)
}
configDir := tempDir.Path("config")
if err := os.Mkdir(configDir, 0777); err != nil {
return nil, err
}
configPath := tempDir.Path("config/main.tf")
if err := ioutil.WriteFile(configPath, renderedTerraform, 0777); err != nil {
return nil, err
}
// create client and run init
client := &terraformClient{
tempDir: tempDir,
configDir: configDir,
stdout: stdout,
stderr: stderr,
}
devNull := bytes.NewBuffer(nil)
if err := client.terraform([]string{"init"}, devNull); err != nil {
io.Copy(stdout, devNull)
return nil, err
}
return client, nil
}
func (client *terraformClient) Apply() error {
client.terraform([]string{
"plan",
"-input=false",
"-out=tfplan",
}, client.stdout)
return client.terraform([]string{
"apply",
"tfplan",
}, client.stdout)
}
func (client *terraformClient) Destroy() error {
return client.terraform([]string{
"destroy",
"-force",
}, client.stdout)
}
func (client *terraformClient) terraform(args []string, stdout io.Writer) error {
terraformBinary := "terraform"
if runtime.GOOS == "windows" {
terraformBinary = "terraform.exe"
}
cmd := exec.Command(client.tempDir.Path(terraformBinary), args...)
cmd.Dir = client.configDir
cmd.Stdout = stdout
cmd.Stderr = client.stderr
return cmd.Run()
}
func (client *terraformClient) Cleanup() error {
return os.RemoveAll(client.tempDir.path)
}
func getTerraformURL() (string, error) {
os := runtime.GOOS
if os == "darwin" {
return darwinBinaryURL, nil
} else if os == "linux" {
return linuxBinaryURL, nil
} else if os == "windows" {
return windowsBinaryURL, nil
}
return "", fmt.Errorf("unknown os: `%s`", os)
}
func setupBinary(tempDir *TempDir) error {
fileHandler, err := os.Create(tempDir.Path("terraform.zip"))
if err != nil {
return err
}
defer fileHandler.Close()
url, err := getTerraformURL()
if err != nil {
return err
}
log.Infoln("Downloading Terraform binary from URL:", url)
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if _, err := io.Copy(fileHandler, resp.Body); err != nil {
return err
}
if err := fileHandler.Sync(); err != nil {
return err
}
err = unzip(tempDir.Path("terraform.zip"), tempDir.path)
if err != nil {
return err
}
terraformBinary := "terraform"
if runtime.GOOS == "windows" {
terraformBinary = "terraform.exe"
}
if err := os.Chmod(tempDir.Path(terraformBinary), 0700); err != nil {
return err
}
return nil
}