-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconduit.go
324 lines (313 loc) · 9.14 KB
/
conduit.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"html/template"
"io/ioutil"
"net"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/spf13/cobra"
)
const tunnelInfo = `
The following services are ready for you to connect to:
{{range $serviceName, $services := .VcapServices}}
{{range $serviceIndex, $service := $services}}
service: {{$service.Name}} ({{$serviceName}})
host: {{$service.Credentials.Host}}
port: {{$service.Credentials.Port}}
username: {{$service.Credentials.Username}}
password: {{$service.Credentials.Password}}
db: {{$service.Credentials.Name}}
{{end}}
{{end}}
`
func waitForConnection(addr string) chan error {
timeout := 3 * time.Second
connection := make(chan error)
go func() {
defer close(connection)
tries := 0
for {
if tries > 5 {
time.Sleep(2 * time.Second)
} else {
time.Sleep(1 * time.Second)
}
tries++
debug("waiting for", addr, "attempt", tries)
conn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
if tries < 15 {
continue
}
connection <- fmt.Errorf("connection fail after %d attempts: %s", tries, err)
break
}
defer conn.Close()
connection <- nil
break
}
}()
return connection
}
var ConnectService = &cobra.Command{
Use: "conduit [flags] SERVICE_INSTANCE [-- COMMAND]",
Example: ` Create a tunnel between your machine and a remote running service:
cf conduit my-service
Run a local application that uses VCAP_SERVICES environment to connect to remote services:
cf conduit my-service-1 my-service-2 -- /path/to/app
Export a postgres database:
cf conduit postgres-instance -- pg_dump -f backup.sql
Import a postgres script:
cf conduit postgres-instance -- psql < backup.sql
Dump a mysql database:
cf conduit mysql-instance -- mysqldump --all-databases > backup.sql
Import a mysql script:
cf conduit mysql-instance -- mysql < backup.sql
`,
Short: "enables temporarily binding services to local running processes",
Long: "spawns a temporary application, binds your desired service and creates an ssh tunnel from the application to your local machine enabling communication directly with the remote service.",
Args: func(cmd *cobra.Command, args []string) error {
if cmd.ArgsLenAtDash() > -1 {
if cmd.ArgsLenAtDash() < 1 {
return errors.New("requires at least one SERVICE_INSTANCE argument to be specified")
}
} else if len(args) < 1 {
return errors.New("requires at least one SERVICE_INSTANCE argument to be specified")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// create status writer
status := NewStatus(os.Stderr)
defer status.Done()
// parse args
var serviceInstanceNames []string
var runargs []string
if cmd.ArgsLenAtDash() > -1 {
serviceInstanceNames = args[:cmd.ArgsLenAtDash()]
runargs = args[cmd.ArgsLenAtDash():]
} else {
serviceInstanceNames = args
runargs = []string{}
}
// create a client
status.Text("Connecting client")
client, err := NewClient(ApiEndpoint, ApiToken, ApiInsecure)
if err != nil {
return err
}
// get org
status.Text("Targeting org", ConduitOrg)
org, err := client.GetOrgByName(ConduitOrg)
if err != nil {
return err
}
// get space
status.Text("Targeting space", ConduitSpace)
space, err := client.GetSpaceByName(org.Guid, ConduitSpace)
if err != nil {
return err
}
// create tunnel app
status.Text("Deploying", ConduitAppName)
appGuid, err := client.CreateApp(ConduitAppName, space.Guid)
if err != nil {
return err
}
defer func() {
if ConduitReuse {
return
}
debug("destroying", ConduitAppName, appGuid)
if err := client.DestroyApp(appGuid); err != nil {
debug("failed to cleanup", ConduitAppName, "app:", err)
}
}()
// upload bits if not staged
status.Text("Uploading", ConduitAppName, "bits")
err = client.UploadStaticAppBits(appGuid)
if err != nil {
return err
}
// start app
status.Text("Starting", ConduitAppName)
err = client.UpdateApp(appGuid, map[string]interface{}{
"state": "STARTED",
})
if err != nil {
return err
}
// get service instances
status.Text("Fetching service infomation")
serviceInstances, err := client.GetServiceInstances(fmt.Sprintf("space_guid:%s", space.Guid))
if err != nil {
return err
}
// configure tunnel
t := &Tunnel{
AppGuid: appGuid,
TunnelAddr: client.Info.AppSshEndpoint,
TunnelHostKey: client.Info.AppSshHostKey,
ForwardAddrs: []ForwardAddrs{},
PasswordFunc: client.SSHCode,
}
// for each service instance
localPort := ConduitLocalPort
for _, name := range serviceInstanceNames {
bound := false
for serviceInstanceGuid, serviceInstance := range serviceInstances {
if name != serviceInstance.Name {
continue
}
// bind conduit app to service instance
status.Text("Binding", serviceInstance.Name)
debug("binding", serviceInstanceGuid, "to", appGuid)
creds, err := client.BindService(appGuid, serviceInstanceGuid)
if err != nil {
return err
}
// configure the port forwarding
debug("creds", creds)
localPort++
t.ForwardAddrs = append(t.ForwardAddrs, ForwardAddrs{
LocalAddr: fmt.Sprintf("127.0.0.1:%d", localPort),
RemoteAddr: fmt.Sprintf("%s:%d", creds.Host, creds.Port),
Credentials: creds,
})
bound = true
}
if !bound {
return fmt.Errorf("failed to bind service: '%s' was not found in space '%s'", name, space.Name)
}
}
// fetch the full app env
status.Text("Fetching environment")
appEnv, err := client.GetAppEnv(appGuid)
if err != nil {
return err
}
// configure the environment
runenv := map[string]string{}
for serviceName, serviceInstances := range appEnv.SystemEnv.VcapServices {
for _, si := range serviceInstances {
// modify
si.Credentials.Host = "127.0.0.1"
si.Credentials.Port = localPort
switch serviceName {
case "postgres":
runenv["PGDATABASE"] = si.Credentials.Name
runenv["PGHOST"] = si.Credentials.Host
runenv["PGPORT"] = fmt.Sprintf("%d", si.Credentials.Port)
runenv["PGUSER"] = si.Credentials.Username
runenv["PGPASSWORD"] = si.Credentials.Password
case "mysql":
tmpdir, err := ioutil.TempDir("", "conduit")
if err != nil {
return err
}
defer os.RemoveAll(tmpdir)
mycnfPath := filepath.Join(tmpdir, "my.cnf")
mycnf := "[mysql]\n"
mycnf += fmt.Sprintf("user = %s\n", si.Credentials.Username)
mycnf += fmt.Sprintf("password = %s\n", si.Credentials.Password)
mycnf += fmt.Sprintf("host = %s\n", si.Credentials.Host)
mycnf += fmt.Sprintf("port = %d\n", si.Credentials.Port)
mycnf += fmt.Sprintf("database = %s\n", si.Credentials.Name)
mycnf += "[mysqldump]\n"
mycnf += fmt.Sprintf("user = %s\n", si.Credentials.Username)
mycnf += fmt.Sprintf("password = %s\n", si.Credentials.Password)
mycnf += fmt.Sprintf("host = 127.0.0.1\n")
mycnf += fmt.Sprintf("port = %d\n", localPort)
if err := ioutil.WriteFile(mycnfPath, []byte(mycnf), 0644); err != nil {
return fmt.Errorf("failed to create temporary mysql config: %s", err)
}
runenv["MYSQL_HOME"] = tmpdir
}
}
}
debug("runenv", runenv)
// poll for started state
status.Text("Waiting for conduit app to become available")
err = client.PollForAppState(appGuid, "STARTED", 15)
if err != nil {
return err
}
// start the tunnel
status.Text("Starting port forwarding")
err = t.Start()
if err != nil {
return err
}
defer t.Stop()
// wait for port forwarding to become active
status.Text("Waiting for port forwarding")
for _, fwd := range t.ForwardAddrs {
select {
case err := <-waitForConnection(fwd.LocalAddr):
if err != nil {
return err
}
case err := <-t.WaitChan():
if err != nil {
return err
}
}
}
status.Done()
// add modified VCAP_SERVICES to environment
if b, err := json.Marshal(appEnv.SystemEnv.VcapServices); err != nil {
return fmt.Errorf("failed to marshal VCAP_SERVICES: %s", err)
} else {
runenv["VCAP_SERVICES"] = string(b)
debug("VCAP_SERVICES", string(b))
}
// render message about ports
if Verbose || len(runargs) == 0 {
t := template.Must(template.New("tunnelInfo").Parse(tunnelInfo))
var out bytes.Buffer
t.Execute(&out, appEnv.SystemEnv)
fmt.Fprintln(os.Stderr, out.String())
}
// execute CMD with enviornment
runargChan := make(chan struct{})
if len(runargs) > 0 {
status.Text("Preparing command:", runargs)
exe, err := exec.LookPath(runargs[0])
if err != nil {
return fmt.Errorf("cannot find '%s' in PATH", runargs[0])
}
proc := exec.Command(exe, runargs[1:]...)
proc.Env = os.Environ()
for k, v := range runenv {
proc.Env = append(proc.Env, fmt.Sprintf("%s=%s", k, v))
}
proc.Stdout = os.Stdout
proc.Stdin = os.Stdin
proc.Stderr = os.Stderr
status.Done()
debug("running", runargs)
if err := proc.Start(); err != nil {
return fmt.Errorf("%s: %s", exe, err)
}
go func() {
defer close(runargChan)
proc.Wait()
}()
} else {
fmt.Fprintln(os.Stderr, "\n\nPress Ctrl+C to shutdown.")
}
// wait
select {
case <-shutdown:
return nil
case <-runargChan:
return nil
}
},
}