zrepl/sshbytestream/ssh.go

133 lines
2.7 KiB
Go
Raw Normal View History

2017-04-14 19:26:32 +02:00
package sshbytestream
import (
2017-04-26 20:25:53 +02:00
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"sync"
)
2017-04-29 20:09:09 +02:00
type SSHTransport struct {
Host string
User string
Port uint16
IdentityFile string
SSHCommand string
Options []string
2017-04-29 20:09:09 +02:00
}
var SSHCommand string = "ssh"
func Incoming() (wc io.ReadWriteCloser, err error) {
2017-04-14 19:26:32 +02:00
// derivce ReadWriteCloser from stdin & stdout
return IncomingReadWriteCloser{}, nil
2017-04-14 19:26:32 +02:00
}
type IncomingReadWriteCloser struct{}
func (f IncomingReadWriteCloser) Read(p []byte) (n int, err error) {
return os.Stdin.Read(p)
2017-04-14 19:26:32 +02:00
}
func (f IncomingReadWriteCloser) Write(p []byte) (n int, err error) {
return os.Stdout.Write(p)
}
2017-04-14 19:26:32 +02:00
2017-04-26 20:25:53 +02:00
func (f IncomingReadWriteCloser) Close() (err error) {
os.Exit(0)
return nil
2017-04-14 19:26:32 +02:00
}
2017-04-29 20:09:09 +02:00
func Outgoing(remote SSHTransport) (conn io.ReadWriteCloser, err error) {
ctx, cancel := context.WithCancel(context.Background())
sshArgs := make([]string, 0, 2*len(remote.Options)+4)
sshArgs = append(sshArgs,
"-p", fmt.Sprintf("%d", remote.Port),
"-q",
"-i", remote.IdentityFile,
"-o", "BatchMode=yes",
)
2017-04-26 20:25:53 +02:00
for _, option := range remote.Options {
sshArgs = append(sshArgs, "-o", option)
}
sshArgs = append(sshArgs, fmt.Sprintf("%s@%s", remote.User, remote.Host))
var sshCommand = SSHCommand
if len(remote.SSHCommand) > 0 {
sshCommand = SSHCommand
}
cmd := exec.CommandContext(ctx, sshCommand, sshArgs...)
// Clear environment of cmd
cmd.Env = []string{}
var in io.WriteCloser
var out io.ReadCloser
var stderr io.Reader
if in, err = cmd.StdinPipe(); err != nil {
return
}
2017-04-14 19:26:32 +02:00
2017-04-26 20:25:53 +02:00
if out, err = cmd.StdoutPipe(); err != nil {
return
}
if stderr, err = cmd.StderrPipe(); err != nil {
return
}
f := ForkedSSHReadWriteCloser{
2017-04-26 20:25:53 +02:00
RemoteStdin: in,
RemoteStdout: out,
Cancel: cancel,
Command: cmd,
exitWaitGroup: &sync.WaitGroup{},
}
f.exitWaitGroup.Add(1)
if err = cmd.Start(); err != nil {
return
}
go func() {
defer f.exitWaitGroup.Done()
var b bytes.Buffer
if _, err := io.Copy(&b, stderr); err != nil {
panic(err)
}
if err := cmd.Wait(); err != nil {
fmt.Fprintf(os.Stderr, "ssh command exited with error: %v. Stderr:\n%s\n", cmd.ProcessState, b)
//panic(err) TODO
}
}()
return f, nil
2017-04-14 19:26:32 +02:00
}
2017-04-26 20:25:53 +02:00
type ForkedSSHReadWriteCloser struct {
RemoteStdin io.Writer
RemoteStdout io.Reader
Command *exec.Cmd
Cancel context.CancelFunc
exitWaitGroup *sync.WaitGroup
}
2017-04-14 19:26:32 +02:00
func (f ForkedSSHReadWriteCloser) Read(p []byte) (n int, err error) {
return f.RemoteStdout.Read(p)
2017-04-14 19:26:32 +02:00
}
func (f ForkedSSHReadWriteCloser) Write(p []byte) (n int, err error) {
return f.RemoteStdin.Write(p)
}
2017-04-14 19:26:32 +02:00
2017-04-26 20:25:53 +02:00
func (f ForkedSSHReadWriteCloser) Close() (err error) {
f.Cancel()
f.exitWaitGroup.Wait()
return nil
2017-04-26 20:25:53 +02:00
}