From 6f84bf665d3b0daf6acb4641674a479cd0d6a65a Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Fri, 12 May 2017 21:11:09 +0200 Subject: [PATCH] cmd: support logging reads & writes from sshbytestream to a file. --- cmd/config.go | 7 +++++++ cmd/main.go | 4 ++-- util/io.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index e1563b1..5b2bcdd 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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) } diff --git a/cmd/main.go b/cmd/main.go index 702f2ae..a50848a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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{ diff --git a/util/io.go b/util/io.go index cf10bd1..5010115 100644 --- a/util/io.go +++ b/util/io.go @@ -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