Skip to content

Commit 2749877

Browse files
authored
Merge pull request #194 from mook-as/copy-controlpath
copy: use SSHArgs if copying to a single remote host.
2 parents 950be8a + 13d5197 commit 2749877

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

cmd/limactl/copy.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,8 @@ func copyAction(clicontext *cli.Context) error {
3636
return err
3737
}
3838

39-
const useDotSSH = false
40-
args, err := sshutil.CommonArgs(useDotSSH)
41-
if err != nil {
42-
return err
43-
}
44-
args = append(args, "-3", "--")
39+
instDirs := make(map[string]string)
40+
args := []string{"-3", "--"}
4541
for _, arg := range clicontext.Args().Slice() {
4642
path := strings.Split(arg, ":")
4743
switch len(path) {
@@ -60,15 +56,36 @@ func copyAction(clicontext *cli.Context) error {
6056
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
6157
}
6258
args = append(args, fmt.Sprintf("scp://%[email protected]:%d/%s", u.Username, inst.SSHLocalPort, path[1]))
59+
instDirs[instName] = inst.Dir
6360
default:
6461
return fmt.Errorf("Path %q contains multiple colons", arg)
6562
}
6663
}
67-
cmd := exec.Command(arg0, args...)
64+
65+
sshArgs := []string{}
66+
if len(instDirs) == 1 {
67+
// Only one (instance) host is involved; we can use the instance-specific
68+
// arguments such as ControlPath. This is preferred as we can multiplex
69+
// sessions without re-authenticating (MaxSessions permitting).
70+
for _, instDir := range instDirs {
71+
sshArgs, err = sshutil.SSHArgs(instDir, false)
72+
if err != nil {
73+
return err
74+
}
75+
}
76+
} else {
77+
// Copying among multiple hosts; we can't pass in host-specific options.
78+
sshArgs, err = sshutil.CommonArgs(false)
79+
if err != nil {
80+
return err
81+
}
82+
}
83+
84+
cmd := exec.Command(arg0, append(sshArgs, args...)...)
6885
cmd.Stdin = os.Stdin
6986
cmd.Stdout = os.Stdout
7087
cmd.Stderr = os.Stderr
71-
logrus.Debugf("executing scp (may take a long)): %+v", cmd.Args)
88+
logrus.Debugf("executing scp (may take a long time)): %+v", cmd.Args)
7289

7390
// TODO: use syscall.Exec directly (results in losing tty?)
7491
return cmd.Run()

pkg/sshutil/sshutil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func SSHArgs(instDir string, useDotSSH bool) ([]string, error) {
172172
return nil, err
173173
}
174174
args = append(args,
175-
"-l", u.Username, // guest and host have the same username, but we should specify the username explicitly (#85)
175+
"-o", fmt.Sprintf("User=%s", u.Username), // guest and host have the same username, but we should specify the username explicitly (#85)
176176
"-o", "ControlMaster=auto",
177177
"-o", fmt.Sprintf("ControlPath=\"%s\"", controlSock),
178178
"-o", "ControlPersist=5m",

0 commit comments

Comments
 (0)