forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scp.go
170 lines (135 loc) · 3.9 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
package commands
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"github.com/docker/machine/libmachine"
"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", "IdentitiesOnly=yes",
"-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
GetIP() (string, 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 cmdScp(c CommandLine, api libmachine.API) error {
args := c.Args()
if len(args) != 2 {
c.ShowHelp()
return errWrongNumberArguments
}
src := args[0]
dest := args[1]
hostInfoLoader := &storeHostInfoLoader{api}
cmd, err := getScpCmd(src, dest, c.Bool("recursive"), hostInfoLoader)
if err != nil {
return err
}
return runCmdWithStdIo(*cmd)
}
func getScpCmd(src, dest string, recursive bool, hostInfoLoader HostInfoLoader) (*exec.Cmd, error) {
cmdPath, err := exec.LookPath("scp")
if err != nil {
return nil, errors.New("Error: You must have a copy of the scp binary locally to use the scp feature.")
}
srcHost, srcPath, srcOpts, err := getInfoForScpArg(src, hostInfoLoader)
if err != nil {
return nil, err
}
destHost, 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
sshArgs = append(sshArgs, "-3")
if recursive {
sshArgs = append(sshArgs, "-r")
}
// 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, srcPath)
if err != nil {
return nil, err
}
sshArgs = append(sshArgs, locationArg)
locationArg, err = generateLocationArg(destHost, destPath)
if err != nil {
return nil, err
}
sshArgs = append(sshArgs, locationArg)
cmd := exec.Command(cmdPath, sshArgs...)
log.Debug(*cmd)
return cmd, nil
}
func getInfoForScpArg(hostAndPath string, hostInfoLoader HostInfoLoader) (HostInfo, string, []string, 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]
path := parts[1]
if hostName == "localhost" {
return nil, path, nil, nil
}
// Remote path
hostInfo, err := hostInfoLoader.load(hostName)
if err != nil {
return nil, "", nil, fmt.Errorf("Error loading host: %s", err)
}
args := []string{}
if hostInfo.GetSSHKeyPath() != "" {
args = append(args, "-i", hostInfo.GetSSHKeyPath())
}
return hostInfo, path, args, nil
}
func generateLocationArg(hostInfo HostInfo, path string) (string, error) {
if hostInfo == nil {
return path, nil
}
ip, err := hostInfo.GetIP()
if err != nil {
return "", err
}
location := fmt.Sprintf("%s@%s:%s", hostInfo.GetSSHUsername(), ip, 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()
}