Skip to content

Commit cf9359b

Browse files
committed
copy: use SSHArgs if copying to a single remote host.
This lets us use ControlPath with the connection, so that if a small number of copies run in parallel we can reuse the connection, rather than failing when too many connections are in flight. Signed-off-by: Mark Yen <[email protected]>
1 parent de25ccc commit cf9359b

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

cmd/limactl/copy.go

Lines changed: 22 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,33 @@ 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+
for _, instDir := range instDirs {
68+
sshArgs, err = sshutil.SSHArgs(instDir, false)
69+
if err != nil {
70+
return err
71+
}
72+
}
73+
} else {
74+
// Copying among multiple hosts; we can't pass in host-specific options.
75+
sshArgs, err = sshutil.CommonArgs(false)
76+
if err != nil {
77+
return err
78+
}
79+
}
80+
81+
cmd := exec.Command(arg0, append(sshArgs, args...)...)
6882
cmd.Stdin = os.Stdin
6983
cmd.Stdout = os.Stdout
7084
cmd.Stderr = os.Stderr
71-
logrus.Debugf("executing scp (may take a long)): %+v", cmd.Args)
85+
logrus.Debugf("executing scp (may take a long time)): %+v", cmd.Args)
7286

7387
// TODO: use syscall.Exec directly (results in losing tty?)
7488
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)