-
Notifications
You must be signed in to change notification settings - Fork 0
/
scp.go
196 lines (161 loc) · 4.83 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
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
package commands
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/persist"
)
var (
errWrongNumberArguments = errors.New("Improper number of arguments")
// TODO: possibly move this to ssh package
baseSSHArgs = []string{
"-o", "StrictHostKeyChecking=no",
"-o", "UserKnownHostsFile=/dev/null",
"-o", "LogLevel=quiet", // suppress "Warning: Permanently added '[localhost]:2022' (ECDSA) to the list of known hosts."
}
)
// HostInfo gives the mandatory information to connect to a host.
type HostInfo interface {
GetMachineName() string
GetSSHHostname() (string, error)
GetSSHPort() (int, error)
GetSSHUsername() string
GetSSHKeyPath() string
}
// HostInfoLoader loads host information.
type HostInfoLoader interface {
load(name string) (HostInfo, error)
}
type storeHostInfoLoader struct {
store persist.Store
}
func (s *storeHostInfoLoader) load(name string) (HostInfo, error) {
host, err := s.store.Load(name)
if err != nil {
return nil, fmt.Errorf("Error loading host: %s", err)
}
return host.Driver, nil
}
func getScpCmd(src, dest string, recursive bool, delta bool, quiet bool, hostInfoLoader HostInfoLoader) (*exec.Cmd, error) {
var cmdPath string
var err error
if !delta {
cmdPath, err = exec.LookPath("scp")
if err != nil {
return nil, errors.New("You must have a copy of the scp binary locally to use the scp feature")
}
} else {
cmdPath, err = exec.LookPath("rsync")
if err != nil {
return nil, errors.New("You must have a copy of the rsync binary locally to use the --delta option")
}
}
srcHost, srcUser, srcPath, srcOpts, err := getInfoForScpArg(src, hostInfoLoader)
if err != nil {
return nil, err
}
destHost, destUser, destPath, destOpts, err := getInfoForScpArg(dest, hostInfoLoader)
if err != nil {
return nil, err
}
// TODO: Check that "-3" flag is available in user's version of scp.
// It is on every system I've checked, but the manual mentioned it's "newer"
sshArgs := baseSSHArgs
if !delta {
sshArgs = append(sshArgs, "-3")
if recursive {
sshArgs = append(sshArgs, "-r")
}
if quiet {
sshArgs = append(sshArgs, "-q")
}
}
// Don't use ssh-agent if both hosts have explicit ssh keys
if !missesExplicitSSHKey(srcHost) && !missesExplicitSSHKey(destHost) {
sshArgs = append(sshArgs, "-o", "IdentitiesOnly=yes")
}
// Append needed -i / private key flags to command.
sshArgs = append(sshArgs, srcOpts...)
sshArgs = append(sshArgs, destOpts...)
// Append actual arguments for the scp command (i.e. docker@<ip>:/path)
locationArg, err := generateLocationArg(srcHost, srcUser, srcPath)
if err != nil {
return nil, err
}
// TODO: Check that "--progress" flag is available in user's version of rsync.
// Use quiet mode as a workaround, if it should happen to not be supported...
if delta {
sshArgs = append([]string{"-e"}, "ssh "+strings.Join(sshArgs, " "))
if !quiet {
sshArgs = append([]string{"--progress"}, sshArgs...)
}
if recursive {
sshArgs = append(sshArgs, "-r")
}
}
sshArgs = append(sshArgs, locationArg)
locationArg, err = generateLocationArg(destHost, destUser, destPath)
if err != nil {
return nil, err
}
sshArgs = append(sshArgs, locationArg)
cmd := exec.Command(cmdPath, sshArgs...)
log.Debug(*cmd)
return cmd, nil
}
func missesExplicitSSHKey(hostInfo HostInfo) bool {
return hostInfo != nil && hostInfo.GetSSHKeyPath() == ""
}
func getInfoForScpArg(hostAndPath string, hostInfoLoader HostInfoLoader) (h HostInfo, user string, path string, args []string, err error) {
// Local path. e.g. "/tmp/foo"
if !strings.Contains(hostAndPath, ":") {
return nil, "", hostAndPath, nil, nil
}
// Path with hostname. e.g. "hostname:/usr/bin/cmatrix"
parts := strings.SplitN(hostAndPath, ":", 2)
hostName := parts[0]
if hParts := strings.SplitN(hostName, "@", 2); len(hParts) == 2 {
user, hostName = hParts[0], hParts[1]
}
path = parts[1]
if hostName == "localhost" {
return nil, "", path, nil, nil
}
// Remote path
h, err = hostInfoLoader.load(hostName)
if err != nil {
return nil, "", "", nil, fmt.Errorf("Error loading host: %s", err)
}
args = []string{}
port, err := h.GetSSHPort()
if err == nil && port > 0 {
args = append(args, "-o", fmt.Sprintf("Port=%v", port))
}
if h.GetSSHKeyPath() != "" {
args = append(args, "-o", fmt.Sprintf("IdentityFile=%q", h.GetSSHKeyPath()))
}
return
}
func generateLocationArg(hostInfo HostInfo, user, path string) (string, error) {
if hostInfo == nil {
return path, nil
}
hostname, err := hostInfo.GetSSHHostname()
if err != nil {
return "", err
}
if user == "" {
user = hostInfo.GetSSHUsername()
}
location := fmt.Sprintf("%s@%s:%s", user, hostname, path)
return location, nil
}
func runCmdWithStdIo(cmd exec.Cmd) error {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}