cmd: support logging reads & writes from sshbytestream to a file.

This commit is contained in:
Christian Schwarz 2017-05-12 21:11:09 +02:00
parent 74719ad846
commit 6f84bf665d
3 changed files with 49 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/mitchellh/mapstructure"
"github.com/zrepl/zrepl/rpc"
"github.com/zrepl/zrepl/sshbytestream"
. "github.com/zrepl/zrepl/util"
"github.com/zrepl/zrepl/zfs"
yaml "gopkg.in/yaml.v2"
"io"
@ -37,6 +38,8 @@ type SSHTransport struct {
TransportOpenCommand []string `mapstructure:"transport_open_command"`
SSHCommand string `mapstructure:"ssh_command"`
Options []string
ConnLogReadFile string `mapstructure:"connlog_read_file"`
ConnLogWriteFile string `mapstructure:"connlog_write_file"`
}
type InitialReplPolicy string
@ -393,6 +396,10 @@ func (t SSHTransport) Connect() (r rpc.RPCRequester, err error) {
if stream, err = sshbytestream.Outgoing(rpcTransport); err != nil {
return
}
stream, err = NewReadWriteCloserLogger(stream, t.ConnLogReadFile, t.ConnLogWriteFile)
if err != nil {
return
}
return rpc.ConnectByteStreamRPC(stream)
}

View File

@ -148,7 +148,7 @@ func doRun(c *cli.Context) error {
Repeats: true,
RunFunc: func(log jobrun.Logger) error {
log.Printf("doing pull: %v", pull)
return doPull(pull, log)
return doPull(pull, c, log)
},
}
@ -183,7 +183,7 @@ func doRun(c *cli.Context) error {
return nil
}
func doPull(pull Pull, log jobrun.Logger) (err error) {
func doPull(pull Pull, c *cli.Context, log jobrun.Logger) (err error) {
if lt, ok := pull.From.Transport.(LocalTransport); ok {
lt.SetHandler(Handler{

View File

@ -2,8 +2,48 @@ package util
import (
"io"
"os"
)
type ReadWriteCloserLogger struct {
RWC io.ReadWriteCloser
ReadFile *os.File
WriteFile *os.File
}
func NewReadWriteCloserLogger(rwc io.ReadWriteCloser, readlog, writelog string) (l *ReadWriteCloserLogger, err error) {
l = &ReadWriteCloserLogger{
RWC: rwc,
}
flags := os.O_CREATE | os.O_WRONLY
if l.ReadFile, err = os.OpenFile(readlog, flags, 0600); err != nil {
return
}
if l.WriteFile, err = os.OpenFile(writelog, flags, 0600); err != nil {
return
}
return
}
func (c *ReadWriteCloserLogger) Read(buf []byte) (n int, err error) {
n, err = c.RWC.Read(buf)
if _, writeErr := c.ReadFile.Write(buf[0:n]); writeErr != nil {
panic(writeErr)
}
return
}
func (c *ReadWriteCloserLogger) Write(buf []byte) (n int, err error) {
n, err = c.RWC.Write(buf)
if _, writeErr := c.WriteFile.Write(buf[0:n]); writeErr != nil {
panic(writeErr)
}
return
}
func (c *ReadWriteCloserLogger) Close() error {
return c.RWC.Close()
}
type ChainedReader struct {
Readers []io.Reader
curReader int