Skip to content

Commit

Permalink
Merge pull request #194 from mook-as/copy-controlpath
Browse files Browse the repository at this point in the history
copy: use SSHArgs if copying to a single remote host.
  • Loading branch information
AkihiroSuda authored Sep 3, 2021
2 parents 950be8a + 13d5197 commit 2749877
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
33 changes: 25 additions & 8 deletions cmd/limactl/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@ func copyAction(clicontext *cli.Context) error {
return err
}

const useDotSSH = false
args, err := sshutil.CommonArgs(useDotSSH)
if err != nil {
return err
}
args = append(args, "-3", "--")
instDirs := make(map[string]string)
args := []string{"-3", "--"}
for _, arg := range clicontext.Args().Slice() {
path := strings.Split(arg, ":")
switch len(path) {
Expand All @@ -60,15 +56,36 @@ func copyAction(clicontext *cli.Context) error {
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
}
args = append(args, fmt.Sprintf("scp://%[email protected]:%d/%s", u.Username, inst.SSHLocalPort, path[1]))
instDirs[instName] = inst.Dir
default:
return fmt.Errorf("Path %q contains multiple colons", arg)
}
}
cmd := exec.Command(arg0, args...)

sshArgs := []string{}
if len(instDirs) == 1 {
// Only one (instance) host is involved; we can use the instance-specific
// arguments such as ControlPath. This is preferred as we can multiplex
// sessions without re-authenticating (MaxSessions permitting).
for _, instDir := range instDirs {
sshArgs, err = sshutil.SSHArgs(instDir, false)
if err != nil {
return err
}
}
} else {
// Copying among multiple hosts; we can't pass in host-specific options.
sshArgs, err = sshutil.CommonArgs(false)
if err != nil {
return err
}
}

cmd := exec.Command(arg0, append(sshArgs, args...)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
logrus.Debugf("executing scp (may take a long)): %+v", cmd.Args)
logrus.Debugf("executing scp (may take a long time)): %+v", cmd.Args)

// TODO: use syscall.Exec directly (results in losing tty?)
return cmd.Run()
Expand Down
2 changes: 1 addition & 1 deletion pkg/sshutil/sshutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func SSHArgs(instDir string, useDotSSH bool) ([]string, error) {
return nil, err
}
args = append(args,
"-l", u.Username, // guest and host have the same username, but we should specify the username explicitly (#85)
"-o", fmt.Sprintf("User=%s", u.Username), // guest and host have the same username, but we should specify the username explicitly (#85)
"-o", "ControlMaster=auto",
"-o", fmt.Sprintf("ControlPath=\"%s\"", controlSock),
"-o", "ControlPersist=5m",
Expand Down

0 comments on commit 2749877

Please sign in to comment.