-
Notifications
You must be signed in to change notification settings - Fork 162
/
ssh.go
88 lines (72 loc) · 2.03 KB
/
ssh.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
package cmd
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
boshdir "github.com/cloudfoundry/bosh-cli/director"
boshssh "github.com/cloudfoundry/bosh-cli/ssh"
boshui "github.com/cloudfoundry/bosh-cli/ui"
)
type SSHCmd struct {
deployment boshdir.Deployment
uuidGen boshuuid.Generator
intSSHRunner boshssh.Runner
nonIntSSHRunner boshssh.Runner
resultsSSHRunner boshssh.Runner
ui boshui.UI
}
func NewSSHCmd(
deployment boshdir.Deployment,
uuidGen boshuuid.Generator,
intSSHRunner boshssh.Runner,
nonIntSSHRunner boshssh.Runner,
resultsSSHRunner boshssh.Runner,
ui boshui.UI,
) SSHCmd {
return SSHCmd{
deployment: deployment,
uuidGen: uuidGen,
intSSHRunner: intSSHRunner,
nonIntSSHRunner: nonIntSSHRunner,
resultsSSHRunner: resultsSSHRunner,
ui: ui,
}
}
func (c SSHCmd) Run(opts SSHOpts) error {
if opts.Results || !c.ui.IsInteractive() {
if len(opts.Command) == 0 {
return bosherr.Errorf("Non-interactive SSH requires non-empty command")
}
}
sshOpts, privKey, err := boshdir.NewSSHOpts(c.uuidGen)
if err != nil {
return bosherr.WrapErrorf(err, "Generating SSH options")
}
connOpts := boshssh.ConnectionOpts{
PrivateKey: privKey,
GatewayDisable: opts.GatewayFlags.Disable,
GatewayUsername: opts.GatewayFlags.Username,
GatewayHost: opts.GatewayFlags.Host,
GatewayPrivateKeyPath: opts.GatewayFlags.PrivateKeyPath,
RawOpts: opts.RawOpts.AsStrings(),
}
result, err := c.deployment.SetUpSSH(opts.Args.Slug, sshOpts)
if err != nil {
return err
}
defer func() {
_ = c.deployment.CleanUpSSH(opts.Args.Slug, sshOpts)
}()
var runner boshssh.Runner
if opts.Results {
runner = c.resultsSSHRunner
} else if !c.ui.IsInteractive() || len(opts.Command) > 0 {
runner = c.nonIntSSHRunner
} else {
runner = c.intSSHRunner
}
err = runner.Run(connOpts, result, opts.Command)
if err != nil {
return bosherr.WrapErrorf(err, "Running SSH")
}
return nil
}