forked from cloudfoundry/bosh-bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.go
409 lines (346 loc) · 11.6 KB
/
executor.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package bosh
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"github.com/cloudfoundry/bosh-bootloader/helpers"
)
const gcpBoshDirectorEphemeralIPOps = `
- type: replace
path: /networks/name=default/subnets/0/cloud_properties/ephemeral_external_ip?
value: true
`
const awsBoshDirectorEphemeralIPOps = `
- type: replace
path: /resource_pools/name=vms/cloud_properties/auto_assign_public_ip?
value: true
`
const awsEncryptDiskOps = `---
- type: replace
path: /disk_pools/name=disks/cloud_properties?
value:
type: gp2
encrypted: true
kms_key_arn: ((kms_key_arn))
`
const azureSSHStaticIP = `
- type: replace
path: /cloud_provider/ssh_tunnel/host
value: ((external_ip))
`
type Executor struct {
command command
tempDir func(string, string) (string, error)
readFile func(string) ([]byte, error)
unmarshalJSON func([]byte, interface{}) error
marshalJSON func(interface{}) ([]byte, error)
writeFile func(string, []byte, os.FileMode) error
}
type InterpolateInput struct {
IAAS string
DirectorDeploymentVars string
JumpboxDeploymentVars string
BOSHState map[string]interface{}
Variables string
OpsFile string
}
type InterpolateOutput struct {
Variables string
Manifest string
}
type JumpboxInterpolateOutput struct {
Variables string
Manifest string
}
type CreateEnvInput struct {
Manifest string
Variables string
State map[string]interface{}
}
type CreateEnvOutput struct {
State map[string]interface{}
}
type DeleteEnvInput struct {
Manifest string
Variables string
State map[string]interface{}
}
type command interface {
Run(stdout io.Writer, workingDirectory string, args []string) error
}
const VERSION_DEV_BUILD = "[DEV BUILD]"
func NewExecutor(cmd command, tempDir func(string, string) (string, error), readFile func(string) ([]byte, error),
unmarshalJSON func([]byte, interface{}) error,
marshalJSON func(interface{}) ([]byte, error), writeFile func(string, []byte, os.FileMode) error) Executor {
return Executor{
command: cmd,
tempDir: tempDir,
readFile: readFile,
unmarshalJSON: unmarshalJSON,
marshalJSON: marshalJSON,
writeFile: writeFile,
}
}
func (e Executor) JumpboxInterpolate(interpolateInput InterpolateInput) (JumpboxInterpolateOutput, error) {
tempDir, err := e.tempDir("", "")
if err != nil {
return JumpboxInterpolateOutput{}, fmt.Errorf("create temp dir: %s", err)
}
var jumpboxSetupFiles = map[string][]byte{
"jumpbox-deployment-vars.yml": []byte(interpolateInput.JumpboxDeploymentVars),
"jumpbox.yml": MustAsset("vendor/github.com/cppforlife/jumpbox-deployment/jumpbox.yml"),
"cpi.yml": MustAsset(filepath.Join("vendor/github.com/cppforlife/jumpbox-deployment", interpolateInput.IAAS, "cpi.yml")),
}
if interpolateInput.Variables != "" {
jumpboxSetupFiles["variables.yml"] = []byte(interpolateInput.Variables)
}
for path, contents := range jumpboxSetupFiles {
err = e.writeFile(filepath.Join(tempDir, path), contents, os.ModePerm)
if err != nil {
//not tested
return JumpboxInterpolateOutput{}, fmt.Errorf("write file: %s", err)
}
}
args := []string{
"interpolate", filepath.Join(tempDir, "jumpbox.yml"),
"--var-errs",
"--vars-store", filepath.Join(tempDir, "variables.yml"),
"--vars-file", filepath.Join(tempDir, "jumpbox-deployment-vars.yml"),
"-o", filepath.Join(tempDir, "cpi.yml"),
}
buffer := bytes.NewBuffer([]byte{})
err = e.command.Run(buffer, tempDir, args)
if err != nil {
return JumpboxInterpolateOutput{}, fmt.Errorf("bosh interpolate: %s: %s", err, buffer)
}
varsStore, err := e.readFile(filepath.Join(tempDir, "variables.yml"))
if err != nil {
return JumpboxInterpolateOutput{}, fmt.Errorf("read file: %s", err)
}
return JumpboxInterpolateOutput{
Variables: string(varsStore),
Manifest: buffer.String(),
}, nil
}
func (e Executor) DirectorInterpolate(interpolateInput InterpolateInput) (InterpolateOutput, error) {
tempDir, err := e.tempDir("", "")
if err != nil {
//not tested
return InterpolateOutput{}, err
}
var directorSetupFiles = map[string][]byte{
"deployment-vars.yml": []byte(interpolateInput.DirectorDeploymentVars),
"user-ops-file.yml": []byte(interpolateInput.OpsFile),
"bosh.yml": MustAsset("vendor/github.com/cloudfoundry/bosh-deployment/bosh.yml"),
"cpi.yml": MustAsset(filepath.Join("vendor/github.com/cloudfoundry/bosh-deployment", interpolateInput.IAAS, "cpi.yml")),
"iam-instance-profile.yml": MustAsset("vendor/github.com/cloudfoundry/bosh-deployment/aws/iam-instance-profile.yml"),
"gcp-bosh-director-ephemeral-ip-ops.yml": []byte(gcpBoshDirectorEphemeralIPOps),
"aws-bosh-director-ephemeral-ip-ops.yml": []byte(awsBoshDirectorEphemeralIPOps),
"aws-bosh-director-encrypt-disk-ops.yml": []byte(awsEncryptDiskOps),
"azure-ssh-static-ip.yml": []byte(azureSSHStaticIP),
"jumpbox-user.yml": MustAsset("vendor/github.com/cloudfoundry/bosh-deployment/jumpbox-user.yml"),
"gcp-external-ip-not-recommended.yml": MustAsset("vendor/github.com/cloudfoundry/bosh-deployment/external-ip-not-recommended.yml"),
"azure-external-ip-not-recommended.yml": MustAsset("vendor/github.com/cloudfoundry/bosh-deployment/external-ip-not-recommended.yml"),
"aws-external-ip-not-recommended.yml": MustAsset("vendor/github.com/cloudfoundry/bosh-deployment/external-ip-with-registry-not-recommended.yml"),
"uaa.yml": MustAsset("vendor/github.com/cloudfoundry/bosh-deployment/uaa.yml"),
"credhub.yml": MustAsset("vendor/github.com/cloudfoundry/bosh-deployment/credhub.yml"),
}
if interpolateInput.Variables != "" {
directorSetupFiles["variables.yml"] = []byte(interpolateInput.Variables)
}
for path, contents := range directorSetupFiles {
err = e.writeFile(filepath.Join(tempDir, path), contents, os.ModePerm)
if err != nil {
//not tested
return InterpolateOutput{}, err
}
}
var args = []string{
"interpolate", filepath.Join(tempDir, "bosh.yml"),
"--var-errs",
"--var-errs-unused",
"--vars-store", filepath.Join(tempDir, "variables.yml"),
"--vars-file", filepath.Join(tempDir, "deployment-vars.yml"),
"-o", filepath.Join(tempDir, "cpi.yml"),
}
switch interpolateInput.IAAS {
case "gcp":
args = append(args,
"-o", filepath.Join(tempDir, "jumpbox-user.yml"),
"-o", filepath.Join(tempDir, "uaa.yml"),
"-o", filepath.Join(tempDir, "credhub.yml"),
"-o", filepath.Join(tempDir, "gcp-bosh-director-ephemeral-ip-ops.yml"),
)
case "aws":
args = append(args,
"-o", filepath.Join(tempDir, "jumpbox-user.yml"),
"-o", filepath.Join(tempDir, "uaa.yml"),
"-o", filepath.Join(tempDir, "credhub.yml"),
"-o", filepath.Join(tempDir, "aws-bosh-director-ephemeral-ip-ops.yml"),
"-o", filepath.Join(tempDir, "iam-instance-profile.yml"),
"-o", filepath.Join(tempDir, "aws-bosh-director-encrypt-disk-ops.yml"),
)
case "azure":
// NOTE: azure does not yet support jumpbox
args = append(args,
"-o", filepath.Join(tempDir, "jumpbox-user.yml"),
"-o", filepath.Join(tempDir, "azure-external-ip-not-recommended.yml"),
"-o", filepath.Join(tempDir, "azure-ssh-static-ip.yml"),
)
}
buffer := bytes.NewBuffer([]byte{})
err = e.command.Run(buffer, tempDir, args)
if err != nil {
return InterpolateOutput{}, err
}
if interpolateInput.OpsFile != "" {
err = e.writeFile(filepath.Join(tempDir, "bosh.yml"), buffer.Bytes(), os.ModePerm)
if err != nil {
//not tested
return InterpolateOutput{}, err
}
args = []string{
"interpolate", filepath.Join(tempDir, "bosh.yml"),
"--var-errs",
"--vars-store", filepath.Join(tempDir, "variables.yml"),
"--vars-file", filepath.Join(tempDir, "deployment-vars.yml"),
"-o", filepath.Join(tempDir, "user-ops-file.yml"),
}
buffer = bytes.NewBuffer([]byte{})
err = e.command.Run(buffer, tempDir, args)
if err != nil {
return InterpolateOutput{}, err
}
}
varsStore, err := e.readFile(filepath.Join(tempDir, "variables.yml"))
if err != nil {
return InterpolateOutput{}, err
}
return InterpolateOutput{
Variables: string(varsStore),
Manifest: buffer.String(),
}, nil
}
func (e Executor) CreateEnv(createEnvInput CreateEnvInput) (CreateEnvOutput, error) {
tempDir, err := e.writePreviousFiles(createEnvInput.State, createEnvInput.Variables, createEnvInput.Manifest)
if err != nil {
return CreateEnvOutput{}, err
}
statePath := filepath.Join(tempDir, "state.json")
variablesPath := filepath.Join(tempDir, "variables.yml")
manifestPath := filepath.Join(tempDir, "manifest.yml")
args := []string{
"create-env", manifestPath,
"--vars-store", variablesPath,
"--state", statePath,
}
err = e.command.Run(os.Stdout, tempDir, args)
if err != nil {
state, readErr := e.readBOSHState(statePath)
if readErr != nil {
errorList := helpers.Errors{}
errorList.Add(err)
errorList.Add(readErr)
return CreateEnvOutput{}, errorList
}
return CreateEnvOutput{}, NewCreateEnvError(state, err)
}
state, err := e.readBOSHState(statePath)
if err != nil {
return CreateEnvOutput{}, err
}
return CreateEnvOutput{
State: state,
}, nil
}
func (e Executor) readBOSHState(statePath string) (map[string]interface{}, error) {
stateContents, err := e.readFile(statePath)
if err != nil {
return map[string]interface{}{}, err
}
var state map[string]interface{}
err = e.unmarshalJSON(stateContents, &state)
if err != nil {
return map[string]interface{}{}, err
}
return state, nil
}
func (e Executor) DeleteEnv(deleteEnvInput DeleteEnvInput) error {
tempDir, err := e.writePreviousFiles(deleteEnvInput.State, deleteEnvInput.Variables, deleteEnvInput.Manifest)
if err != nil {
return err
}
statePath := filepath.Join(tempDir, "state.json")
variablesPath := filepath.Join(tempDir, "variables.yml")
boshManifestPath := filepath.Join(tempDir, "manifest.yml")
args := []string{
"delete-env", boshManifestPath,
"--vars-store", variablesPath,
"--state", statePath,
}
err = e.command.Run(os.Stdout, tempDir, args)
if err != nil {
state, readErr := e.readBOSHState(statePath)
if readErr != nil {
errorList := helpers.Errors{}
errorList.Add(err)
errorList.Add(readErr)
return errorList
}
return NewDeleteEnvError(state, err)
}
return nil
}
func (e Executor) Version() (string, error) {
tempDir, err := e.tempDir("", "")
if err != nil {
return "", err
}
args := []string{"-v"}
buffer := bytes.NewBuffer([]byte{})
err = e.command.Run(buffer, tempDir, args)
if err != nil {
return "", err
}
versionOutput := buffer.String()
regex := regexp.MustCompile(`\d+.\d+.\d+`)
version := regex.FindString(versionOutput)
if version == "" {
return "", NewBOSHVersionError(errors.New("BOSH version could not be parsed"))
}
return version, nil
}
func (e Executor) writePreviousFiles(state map[string]interface{}, variables, manifest string) (string, error) {
tempDir, err := e.tempDir("", "")
if err != nil {
return "", err
}
statePath := filepath.Join(tempDir, "state.json")
variablesPath := filepath.Join(tempDir, "variables.yml")
boshManifestPath := filepath.Join(tempDir, "manifest.yml")
if state != nil {
boshStateContents, err := e.marshalJSON(state)
if err != nil {
return "", err
}
err = e.writeFile(statePath, boshStateContents, os.ModePerm)
if err != nil {
return "", err
}
}
err = e.writeFile(variablesPath, []byte(variables), os.ModePerm)
if err != nil {
// not tested
return "", err
}
err = e.writeFile(boshManifestPath, []byte(manifest), os.ModePerm)
if err != nil {
// not tested
return "", err
}
return tempDir, nil
}