/
client.go
154 lines (130 loc) · 4.55 KB
/
client.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
package bosh
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os/exec"
"github.com/EngineerBetter/control-tower/bosh/internal/boshcli"
"github.com/EngineerBetter/control-tower/bosh/internal/workingdir"
"github.com/EngineerBetter/control-tower/config"
"github.com/EngineerBetter/control-tower/iaas"
"github.com/EngineerBetter/control-tower/terraform"
"github.com/EngineerBetter/control-tower/util"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
// StateFilename is default name for bosh-init state file
const StateFilename = "director-state.json"
// CredsFilename is default name for bosh-init creds file
const CredsFilename = "director-creds.yml"
//counterfeiter:generate . IClient
// IClient is a client for performing bosh-init commands
type IClient interface {
Deploy([]byte, []byte, bool) ([]byte, []byte, error)
Cleanup() error
Instances() ([]Instance, error)
CreateEnv([]byte, []byte, string) ([]byte, []byte, error)
Recreate() error
Locks() ([]byte, error)
}
// Instance represents a vm deployed by BOSH
type Instance struct {
Name string
IP string
State string
}
// ClientFactory creates a new IClient
type ClientFactory func(config config.ConfigView, outputs terraform.Outputs, stdout, stderr io.Writer, provider iaas.Provider, versionFile []byte) (IClient, error)
//New returns an IAAS specific implementation of BOSH client
func New(config config.ConfigView, outputs terraform.Outputs, stdout, stderr io.Writer, provider iaas.Provider, versionFile []byte) (IClient, error) {
workingdir, err := workingdir.New()
if err != nil {
return nil, err
}
var binaries map[string]util.BinaryPaths
err = json.Unmarshal(versionFile, &binaries)
if err != nil {
return nil, err
}
boshCLIPath, err := util.DownloadBOSHCLI(binaries)
if err != nil {
return nil, fmt.Errorf("failed to determine BOSH CLI path: [%v]", err)
}
boshCLI := boshcli.New(boshCLIPath, exec.Command)
switch provider.IAAS() {
case iaas.AWS:
return NewAWSClient(config, outputs, workingdir, stdout, stderr, provider, boshCLI, versionFile)
case iaas.GCP:
return NewGCPClient(config, outputs, workingdir, stdout, stderr, provider, boshCLI, versionFile)
}
return nil, fmt.Errorf("IAAS not supported: %s", provider.IAAS())
}
func instances(boshCLI boshcli.ICLI, ip, password, ca string) ([]Instance, error) {
output := new(bytes.Buffer)
if err := boshCLI.RunAuthenticatedCommand(
"instances",
ip,
password,
ca,
false,
output,
"--json",
); err != nil {
return nil, fmt.Errorf("Error [%s] running `bosh instances`. stdout: [%s]", err, output.String())
}
jsonOutput := struct {
Tables []struct {
Rows []struct {
Instance string `json:"instance"`
IPs string `json:"ips"`
ProcessState string `json:"process_state"`
} `json:"Rows"`
} `json:"Tables"`
}{}
if err := json.NewDecoder(output).Decode(&jsonOutput); err != nil {
return nil, err
}
instances := []Instance{}
for _, table := range jsonOutput.Tables {
for _, row := range table.Rows {
instances = append(instances, Instance{
Name: row.Instance,
IP: row.IPs,
State: row.ProcessState,
})
}
}
return instances, nil
}
func saveFilesToWorkingDir(workingdir workingdir.IClient, provider iaas.Provider, creds []byte) error {
concourseVersionsContents, _ := provider.Choose(iaas.Choice{
AWS: awsConcourseVersions,
GCP: gcpConcourseVersions,
}).([]byte)
concourseSHAsContents, _ := provider.Choose(iaas.Choice{
AWS: awsConcourseSHAs,
GCP: gcpConcourseSHAs,
}).([]byte)
filesToSave := map[string][]byte{
concourseVersionsFilename: concourseVersionsContents,
concourseSHAsFilename: concourseSHAsContents,
concourseManifestFilename: concourseManifestContents,
concourseGrafanaFilename: concourseGrafana,
concourseBitBucketAuthFilename: concourseBitBucketAuth,
concourseGitHubAuthFilename: concourseGitHubAuth,
concourseGitHubEnterpriseAuthFilename: concourseGithubEnterpriseAuth,
concourseMainGitHubAuthFilename: concourseMainGitHubAuth,
concourseMicrosoftAuthFilename: concourseMicrosoftAuth,
concourseEphemeralWorkersFilename: concourseEphemeralWorkers,
concourseNoMetricsFilename: concourseNoMetrics,
credsFilename: creds,
extraTagsFilename: extraTags,
}
for filename, contents := range filesToSave {
_, err := workingdir.SaveFileToWorkingDir(filename, contents)
if err != nil {
return fmt.Errorf("failed to save %s to working directory: [%v]", filename, err)
}
}
return nil
}