Skip to content

Commit

Permalink
Support passphrase
Browse files Browse the repository at this point in the history
Signed-off-by: lucklove <[email protected]>
  • Loading branch information
lucklove committed Jul 22, 2020
1 parent e0bdf4d commit f5a8c62
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions pkg/cluster/ansible/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func parseDirs(user string, ins spec.InstanceSpec, sshTimeout int64) (spec.Insta
return ins, nil
}

func parseTiflashConfig(e *executor.SSHExecutor, spec *spec.TiFlashSpec, fname string) error {
func parseTiflashConfig(e executor.Executor, spec *spec.TiFlashSpec, fname string) error {
data, err := readFile(e, fname)
if err != nil {
return errors.AddStack(err)
Expand Down Expand Up @@ -275,7 +275,7 @@ func parseTiflashConfigFromFileData(spec *spec.TiFlashSpec, data []byte) error {
return nil
}

func readFile(e *executor.SSHExecutor, fname string) (data []byte, err error) {
func readFile(e executor.Executor, fname string) (data []byte, err error) {
cmd := fmt.Sprintf("cat %s", fname)
stdout, stderr, err := e.Execute(cmd, false)
if err != nil {
Expand Down
44 changes: 22 additions & 22 deletions pkg/cluster/executor/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,19 @@ func (e *NativeSSHExecutor) Execute(cmd string, sudo bool, timeout ...time.Durat
defer cancel()
}

var command *exec.Cmd
args := []string{"ssh", "-o", "StrictHostKeyChecking=no", fmt.Sprintf("%s@%s", e.Config.User, e.Config.Host), cmd}
args := []string{"ssh", "-o", "StrictHostKeyChecking=no"}
if e.Config.Password != "" {
command = exec.CommandContext(ctx, "sshpass", append([]string{"-p", e.Config.Password}, args...)...)
} else {
command = exec.CommandContext(ctx, "ssh", args[1:]...)
args = append([]string{"sshpass", "-p", e.Config.Password, "-P", "password"}, args...)
}
if e.Config.KeyFile != "" {
args = append(args, "-i", e.Config.KeyFile)
}
if e.Config.Passphrase != "" {
args = append([]string{"sshpass", "-p", e.Config.Passphrase, "-P", "passphrase"}, args...)
}
args = append(args, fmt.Sprintf("%s@%s", e.Config.User, e.Config.Host), cmd)

command := exec.CommandContext(ctx, args[0], args[1:]...)

stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
Expand Down Expand Up @@ -301,6 +307,15 @@ func (e *NativeSSHExecutor) Execute(cmd string, sudo bool, timeout ...time.Durat
// file from remote to local.
func (e *NativeSSHExecutor) Transfer(src string, dst string, download bool) error {
args := []string{"scp", "-r", "-o", "StrictHostKeyChecking=no"}
if e.Config.Password != "" {
args = append([]string{"sshpass", "-p", e.Config.Password, "-P", "password"}, args...)
}
if e.Config.KeyFile != "" {
args = append(args, "-i", e.Config.KeyFile)
}
if e.Config.Passphrase != "" {
args = append([]string{"sshpass", "-p", e.Config.Passphrase, "-P", "passphrase"}, args...)
}
if download {
targetPath := filepath.Dir(dst)
if err := utils.CreateDir(targetPath); err != nil {
Expand All @@ -311,26 +326,11 @@ func (e *NativeSSHExecutor) Transfer(src string, dst string, download bool) erro
args = append(args, src, fmt.Sprintf("%s@%s:%s", e.Config.User, e.Config.Host, dst))
}

var command *exec.Cmd
if e.Config.Password != "" {
command = exec.Command("sshpass", append([]string{"-p", e.Config.Password}, args...)...)
} else {
command = exec.Command("scp", args[1:]...)
}

command := exec.Command(args[0], args[1:]...)
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
command.Stdout = stdout
command.Stderr = stderr
err := command.Run()

zap.L().Info("SSHCommand",
zap.String("host", e.Config.Host),
zap.Int("port", e.Config.Port),
zap.String("cmd", strings.Join(command.Args, " ")),
zap.Error(err),
zap.String("stdout", stdout.String()),
zap.String("stderr", stderr.String()))

return err
return command.Run()
}

0 comments on commit f5a8c62

Please sign in to comment.