-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
263 lines (227 loc) · 8.92 KB
/
adapter.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
package cf_cli_adapter
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os/exec"
"strings"
"time"
)
type Adapter struct {
CfCliPath string
}
func (a *Adapter) CreateOrg(name string) error {
fmt.Printf("running: %s create-org %s\n", a.CfCliPath, name)
cmd := exec.Command(a.CfCliPath, "create-org", name)
return runCommandWithTimeout(cmd)
}
func (a *Adapter) DeleteOrg(name string) error {
fmt.Printf("running: %s delete-org -f %s\n", a.CfCliPath, name)
cmd := exec.Command(a.CfCliPath, "delete-org", "-f", name)
return runCommandWithTimeout(cmd)
}
func (a *Adapter) CreateSpace(spaceName, orgName string) error {
fmt.Printf("running: %s create-space %s -o %s\n", a.CfCliPath, spaceName, orgName)
cmd := exec.Command(a.CfCliPath, "create-space", spaceName, "-o", orgName)
return runCommandWithTimeout(cmd)
}
func (a *Adapter) TargetOrg(name string) error {
fmt.Printf("running: %s target -o %s\n", a.CfCliPath, name)
cmd := exec.Command(a.CfCliPath, "target", "-o", name)
return runCommandWithTimeout(cmd)
}
func (a *Adapter) TargetSpace(name string) error {
fmt.Printf("running: %s target -s %s\n", a.CfCliPath, name)
cmd := exec.Command(a.CfCliPath, "target", "-s", name)
return runCommandWithTimeout(cmd)
}
func (a *Adapter) SetApiWithSsl(api string) error {
fmt.Printf("running: %s api %s\n", a.CfCliPath, api)
cmd := exec.Command(a.CfCliPath, "api", api)
return runCommandWithTimeout(cmd)
}
func (a *Adapter) SetApiWithoutSsl(api string) error {
fmt.Printf("running: %s api %s --skip-ssl-validation\n", a.CfCliPath, api)
cmd := exec.Command(a.CfCliPath, "api", api, "--skip-ssl-validation")
return runCommandWithTimeout(cmd)
}
func (a *Adapter) Auth(user, password string) error {
fmt.Printf("running: %s auth <user> <pass> \n", a.CfCliPath)
cmd := exec.Command(a.CfCliPath, "auth", user, password)
return runCommandWithTimeout(cmd)
}
func (a *Adapter) Push(name, directory, manifestFile string) error {
fmt.Printf("running: %s push %s -p %s -f %s\n", a.CfCliPath, name, directory, manifestFile)
bytes, err := exec.Command(a.CfCliPath,
"push", name,
"-p", directory,
"-f", manifestFile).CombinedOutput()
fmt.Printf("output: %s\n", string(bytes))
return err
}
func (a *Adapter) Delete(appName string) error {
fmt.Printf("running: %s delete -f %s\n", a.CfCliPath, appName)
cmd := exec.Command(a.CfCliPath, "delete", "-f", appName)
return runCommandWithTimeout(cmd)
}
func (a *Adapter) Scale(name string, instances int) error {
instancesStr := fmt.Sprintf("%d", instances)
fmt.Printf("running: %s scale %s -i %s\n", a.CfCliPath, name, instancesStr)
cmd := exec.Command(a.CfCliPath, "scale", name, "-i", instancesStr)
return runCommandWithTimeout(cmd)
}
func (a *Adapter) AppGuid(name string) (string, error) {
fmt.Printf("running: %s app %s --guid\n", a.CfCliPath, name)
bytes, err := exec.Command(a.CfCliPath, "app", name, "--guid").CombinedOutput()
return strings.TrimSpace(string(bytes)), err
}
type Apps struct {
TotalResults int `json:"total_results"`
}
func (a *Adapter) OrgGuid(name string) (string, error) {
fmt.Printf("running: %s org %s --guid\n", a.CfCliPath, name)
bytes, err := exec.Command(a.CfCliPath, "org", name, "--guid").CombinedOutput()
return strings.TrimSpace(string(bytes)), err
}
func (a *Adapter) Curl(method, path, inputFile string) ([]byte, error) {
if inputFile != "" {
fmt.Println("running:", a.CfCliPath, "curl", "-X", method, "-d", fmt.Sprintf("@%s", inputFile), path)
return exec.Command(a.CfCliPath, "curl", "-X", method, "-d", fmt.Sprintf("@%s", inputFile), path).CombinedOutput()
}
fmt.Printf("running: %s curl -X %s \"%s\"\n", a.CfCliPath, method, path)
return exec.Command(a.CfCliPath, "curl", "-X", method, path).CombinedOutput()
}
func (a *Adapter) AppCount(orgGuid string) (int, error) {
fmt.Printf("running: %s curl \"/v2/apps?q=organization_guid%%20IN%%20%s\"\n", a.CfCliPath, orgGuid)
bytes, err := exec.Command(a.CfCliPath, "curl", fmt.Sprintf("/v2/apps?q=organization_guid%%20IN%%20%s", orgGuid)).CombinedOutput()
apps := &Apps{}
if err := json.Unmarshal(bytes, apps); err != nil {
return -1, err
}
return apps.TotalResults, err
}
func (a *Adapter) CheckApp(guid string) ([]byte, error) {
fmt.Printf("running: %s curl \"/v2/apps/%s/summary\"\n", a.CfCliPath, guid)
bytes, err := exec.Command(a.CfCliPath, "curl", fmt.Sprintf("/v2/apps/%s/summary", guid)).CombinedOutput()
return bytes, err
}
func (a *Adapter) AllowAccess(sourceApp, destApp string, port int, protocol string) error {
portStr := fmt.Sprintf("%d", port)
fmt.Printf("running: cf allow-access %s %s --port %s --protocol tcp\n", sourceApp, destApp, portStr)
cmd := exec.Command("cf", "allow-access", sourceApp, destApp, "--port", portStr, "--protocol", "tcp")
return runCommandWithTimeout(cmd)
}
func (a *Adapter) RemoveAccess(sourceApp, destApp string, port int, protocol string) error {
portStr := fmt.Sprintf("%d", port)
fmt.Printf("running: cf remove-access %s %s --port %s --protocol tcp\n", sourceApp, destApp, portStr)
cmd := exec.Command("cf", "remove-access", sourceApp, destApp, "--port", portStr, "--protocol", "tcp")
return runCommandWithTimeout(cmd)
}
func (a *Adapter) CreateQuota(name, memory string, instanceMemory, routes, serviceInstances, appInstances, routePorts int) error {
instanceMemoryStr := fmt.Sprintf("%d", instanceMemory)
routesStr := fmt.Sprintf("%d", routes)
serviceInstancesStr := fmt.Sprintf("%d", serviceInstances)
appInstancesStr := fmt.Sprintf("%d", appInstances)
routePortsStr := fmt.Sprintf("%d", routePorts)
fmt.Printf("running cf create-quota %s -m %s -i %s -r %s -s %s -a %s --reserved-route-ports %s\n", name, memory, instanceMemoryStr, routesStr, serviceInstancesStr, appInstancesStr, routePortsStr)
cmd := exec.Command("cf", "create-quota", name, "-m", memory, "-i", instanceMemoryStr, "-r", routesStr, "-s", serviceInstancesStr, "-a", appInstancesStr, "--reserved-route-ports", routePortsStr)
return runCommandWithTimeout(cmd)
}
func (a *Adapter) SetQuota(org, quota string) error {
fmt.Printf("running cf set-quota %s %s\n", org, quota)
cmd := exec.Command("cf", "set-quota", org, quota)
return runCommandWithTimeout(cmd)
}
func (a *Adapter) CreateSecurityGroup(name, filepath string) error {
fmt.Printf("running cf create-security-group %s %s\n", name, filepath)
cmd := exec.Command("cf", "create-security-group", name, filepath)
return runCommandWithTimeout(cmd)
}
type ASG struct {
Resources []struct {
Entity struct {
Rules []struct {
Destination string `json:"destination"`
Ports string `json:"ports"`
Protocol string `json:"protocol"`
} `json:"rules"`
} `json:"entity"`
} `json:"resources"`
}
func (a *Adapter) SecurityGroup(name string) (string, error) {
fmt.Printf("running: %s curl \"/v2/security_groups?q=name%%3A%s\n", a.CfCliPath, name)
bytes, err := exec.Command(a.CfCliPath, "curl", fmt.Sprintf("/v2/security_groups?q=name%%3A%s", name)).CombinedOutput()
asg := &ASG{}
if err := json.Unmarshal(bytes, asg); err != nil {
return "", err
}
if len(asg.Resources) == 0 {
return "", errors.New("no asgs with the name " + name)
}
rules, err := json.Marshal(asg.Resources[0].Entity.Rules)
if err != nil {
return "", err
}
return string(rules), err
}
func (a *Adapter) BindSecurityGroup(name, org, space string) error {
fmt.Printf("running cf bind-security-group %s %s %s\n", name, org, space)
cmd := exec.Command("cf", "bind-security-group", name, org, space)
return runCommandWithTimeout(cmd)
}
func (a *Adapter) UnbindSecurityGroup(name, org, space string) error {
fmt.Printf("running cf unbind-security-group %s %s %s\n", name, org, space)
cmd := exec.Command("cf", "unbind-security-group", name, org, space)
return runCommandWithTimeout(cmd)
}
func (a *Adapter) DeleteSecurityGroup(name string) error {
fmt.Printf("running cf delete-security-group -f %s \n", name)
cmd := exec.Command("cf", "delete-security-group", "-f", name)
return runCommandWithTimeout(cmd)
}
func (a *Adapter) DeleteQuota(quota string) error {
fmt.Printf("running cf delete-quota %s -f\n", quota)
cmd := exec.Command("cf", "delete-quota", quota, "-f")
return runCommandWithTimeout(cmd)
}
type CmdErr struct {
Out string
Err string
Message string
}
func (e *CmdErr) Error() string {
return fmt.Sprintf("%s:\n\nOut:\n%s\n\nErr:%s\n", e.Message, e.Out, e.Err)
}
func runCommandWithTimeout(cmd *exec.Cmd) error {
outBuffer := &bytes.Buffer{}
errBuffer := &bytes.Buffer{}
wrapErr := func(msg string) error {
return &CmdErr{
Out: outBuffer.String(),
Err: errBuffer.String(),
Message: msg,
}
}
cmd.Stdout = outBuffer
cmd.Stderr = errBuffer
if err := cmd.Start(); err != nil {
return err
}
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
select {
case <-time.After(2 * time.Minute):
if err := cmd.Process.Kill(); err != nil {
return wrapErr(fmt.Sprintf("command timed out and could not be killed: %s", err))
}
return wrapErr("command timed out")
case err := <-done:
if err != nil {
return wrapErr(err.Error())
}
}
return nil
}