2017-04-14 19:26:32 +02:00
|
|
|
package sshbytestream
|
|
|
|
|
2017-04-15 15:43:50 +02:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"github.com/zrepl/zrepl/model"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"bytes"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var SSHCommand string = "ssh"
|
|
|
|
|
|
|
|
func Incoming() (wc io.ReadWriteCloser, err error) {
|
2017-04-14 19:26:32 +02:00
|
|
|
// derivce ReadWriteCloser from stdin & stdout
|
2017-04-15 15:43:50 +02:00
|
|
|
return IncomingReadWriteCloser{}, nil
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-15 15:43:50 +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
|
|
|
}
|
|
|
|
|
2017-04-15 15:43:50 +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-15 15:43:50 +02:00
|
|
|
func (f IncomingReadWriteCloser) Close() (err error) {
|
|
|
|
os.Exit(0)
|
|
|
|
return nil
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-15 15:43:50 +02:00
|
|
|
func Outgoing(name string, remote model.SSHTransport) (conn io.ReadWriteCloser, err error) {
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
sshArgs := make([]string, 0, 2 * len(remote.Options) + len(remote.TransportOpenCommand) + 4)
|
|
|
|
sshArgs = append(sshArgs,
|
|
|
|
"-p", fmt.Sprintf("%d", remote.Port),
|
|
|
|
"-o", "BatchMode=yes",
|
|
|
|
)
|
|
|
|
for _,option := range remote.Options {
|
|
|
|
sshArgs = append(sshArgs, "-o", option)
|
|
|
|
}
|
|
|
|
sshArgs = append(sshArgs, fmt.Sprintf("%s@%s", remote.User, remote.Host))
|
|
|
|
sshArgs = append(sshArgs, remote.TransportOpenCommand...)
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(ctx, SSHCommand, sshArgs...)
|
|
|
|
|
|
|
|
var in io.WriteCloser
|
|
|
|
var out io.ReadCloser
|
|
|
|
|
|
|
|
if in, err = cmd.StdinPipe(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-15 15:43:50 +02:00
|
|
|
if out,err = cmd.StdoutPipe(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
f := ForkedSSHReadWriteCloser{
|
|
|
|
RemoteStdin: in,
|
|
|
|
RemoteStdout: out,
|
|
|
|
Cancel: cancel,
|
|
|
|
Command: cmd,
|
|
|
|
exitWaitGroup: &sync.WaitGroup{},
|
|
|
|
}
|
|
|
|
|
|
|
|
f.exitWaitGroup.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer f.exitWaitGroup.Done()
|
|
|
|
stderr, _ := cmd.StderrPipe()
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
io.Copy(&b, stderr)
|
|
|
|
fmt.Println(b.String())
|
|
|
|
fmt.Printf("%v\n", cmd.ProcessState)
|
2017-04-15 17:07:32 +02:00
|
|
|
//panic(err)
|
2017-04-15 15:43:50 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return f, nil
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-15 15:43:50 +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
|
|
|
|
2017-04-15 15:43:50 +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
|
|
|
}
|
|
|
|
|
2017-04-15 15:43:50 +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-15 15:43:50 +02:00
|
|
|
func (f ForkedSSHReadWriteCloser) Close() (err error) {
|
|
|
|
f.Cancel()
|
|
|
|
f.exitWaitGroup.Wait()
|
|
|
|
return nil
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|