-
Notifications
You must be signed in to change notification settings - Fork 160
/
scp.go
154 lines (125 loc) · 3.57 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
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 cmd
import (
"errors"
bihttpagent "github.com/cloudfoundry/bosh-agent/agentclient/http"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshdir "github.com/cloudfoundry/bosh-cli/v7/director"
boshssh "github.com/cloudfoundry/bosh-cli/v7/ssh"
. "github.com/cloudfoundry/bosh-cli/v7/cmd/opts"
)
type SCPCmd struct {
deployment boshdir.Deployment
scpRunner boshssh.SCPRunner
hostBuilder boshssh.HostBuilder
}
func NewSCPCmd(
scpRunner boshssh.SCPRunner,
hostBuilder boshssh.HostBuilder,
) SCPCmd {
return SCPCmd{
scpRunner: scpRunner,
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
}
type EnvSCPCmd struct {
agentClientFactory bihttpagent.AgentClientFactory
scpRunner boshssh.SCPRunner
}
func NewEnvSCPCmd(
agentClientFactory bihttpagent.AgentClientFactory,
scpRunner boshssh.SCPRunner,
) EnvSCPCmd {
return EnvSCPCmd{
agentClientFactory: agentClientFactory,
scpRunner: scpRunner,
}
}
func (c EnvSCPCmd) Run(opts SCPOpts) error {
if opts.PrivateKey.Bytes != nil {
return errors.New("the --private-key flag is not supported in combination with the --director flag")
}
if opts.Endpoint == "" || opts.Certificate == "" {
return errors.New("the --director flag requires both the --agent-endpoint and --agent-certificate flags to be set")
}
agentClient, err := c.agentClientFactory.NewAgentClient("bosh-cli", opts.Endpoint, opts.Certificate)
if err != nil {
return err
}
scpArgs := boshssh.NewSCPArgs(opts.Args.Paths, opts.Recursive)
sshOpts, connOpts, err := opts.GatewayFlags.AsSSHOpts()
if err != nil {
return err
}
agentResult, err := agentClient.SetUpSSH(sshOpts.Username, sshOpts.PublicKey)
if err != nil {
return err
}
result := boshdir.SSHResult{
Hosts: []boshdir.Host{
{
Username: sshOpts.Username,
Host: agentResult.Ip,
HostPublicKey: agentResult.HostPublicKey,
Job: "create-env-vm",
IndexOrID: "0",
},
},
}
defer func() {
_, _ = agentClient.CleanUpSSH(sshOpts.Username)
}()
// host key will be returned by agent over HTTPS
connOpts.RawOpts = append(connOpts.RawOpts, "-o", "StrictHostKeyChecking=yes")
err = c.scpRunner.Run(connOpts, result, scpArgs)
if err != nil {
return bosherr.WrapErrorf(err, "Running SCP")
}
return nil
}