-
Notifications
You must be signed in to change notification settings - Fork 160
/
scp.go
92 lines (76 loc) · 2.07 KB
/
scp.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
package cmd
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
. "github.com/cloudfoundry/bosh-cli/cmd/opts"
boshdir "github.com/cloudfoundry/bosh-cli/director"
boshssh "github.com/cloudfoundry/bosh-cli/ssh"
biui "github.com/cloudfoundry/bosh-cli/ui"
)
type SCPCmd struct {
deployment boshdir.Deployment
uuidGen boshuuid.Generator
scpRunner boshssh.SCPRunner
ui biui.UI
hostBuilder boshssh.HostBuilder
}
func NewSCPCmd(
uuidGen boshuuid.Generator,
scpRunner boshssh.SCPRunner,
ui biui.UI,
hostBuilder boshssh.HostBuilder,
) SCPCmd {
return SCPCmd{
uuidGen: uuidGen,
scpRunner: scpRunner,
ui: ui,
hostBuilder: hostBuilder,
}
}
func (c SCPCmd) Run(opts SCPOpts, deploymentFetcher boshssh.DeploymentFetcher) error {
scpArgs := boshssh.NewSCPArgs(opts.Args.Paths, opts.Recursive)
slug, err := scpArgs.AllOrInstanceGroupOrInstanceSlug()
if err != nil {
return err
}
sshOpts, connOpts, err := opts.GatewayFlags.AsSSHOpts()
if err != nil {
return err
}
var result boshdir.SSHResult
if opts.PrivateKey.Bytes == nil {
c.deployment, err = deploymentFetcher()
if err != nil {
return err
}
// host key will be returned by agent over NATS
connOpts.RawOpts = append(connOpts.RawOpts, "-o", "StrictHostKeyChecking=yes")
result, err = c.deployment.SetUpSSH(slug, sshOpts)
if err != nil {
return err
}
defer func() {
_ = c.deployment.CleanUpSSH(slug, sshOpts)
}()
} else {
// no automatic source of host key
connOpts.RawOpts = append(connOpts.RawOpts, "-o", "StrictHostKeyChecking=no")
connOpts.PrivateKey = string(opts.PrivateKey.Bytes)
host, err := c.hostBuilder.BuildHost(slug, opts.Username, deploymentFetcher)
if err != nil {
return err
}
result = boshdir.SSHResult{
Hosts: []boshdir.Host{
host,
},
GatewayUsername: connOpts.GatewayUsername,
GatewayHost: connOpts.GatewayHost,
}
}
err = c.scpRunner.Run(connOpts, result, scpArgs)
if err != nil {
return bosherr.WrapErrorf(err, "Running SCP")
}
return nil
}