From 586606ddececbb5af5277ac31ca3738d4ada06e0 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 16 May 2017 15:35:21 +0800 Subject: [PATCH] upgrade easyssh to 1.1.6 Signed-off-by: Bo-Yi Wu --- plugin.go | 6 +- .../appleboy/easyssh-proxy/README.md | 57 ++++++++++++++++++- .../appleboy/easyssh-proxy/easyssh.go | 39 ++++++------- vendor/vendor.json | 10 ++-- 4 files changed, 84 insertions(+), 28 deletions(-) diff --git a/plugin.go b/plugin.go index 6811b8f..d48db66 100644 --- a/plugin.go +++ b/plugin.go @@ -92,12 +92,12 @@ func (p Plugin) Exec() error { errChannel <- err } else { // read from the output channel until the done signal is passed - stillGoing := true isTimeout := true - for stillGoing { + loop: + for { select { case isTimeout = <-doneChan: - stillGoing = false + break loop case outline := <-stdoutChan: p.log(host, "out:", outline) case errline := <-stderrChan: diff --git a/vendor/github.com/appleboy/easyssh-proxy/README.md b/vendor/github.com/appleboy/easyssh-proxy/README.md index e0eef55..25bffe0 100644 --- a/vendor/github.com/appleboy/easyssh-proxy/README.md +++ b/vendor/github.com/appleboy/easyssh-proxy/README.md @@ -1,6 +1,11 @@ # easyssh-proxy -[![GoDoc](https://godoc.org/github.com/appleboy/easyssh-proxy?status.svg)](https://godoc.org/github.com/appleboy/easyssh-proxy) [![Build Status](http://drone.wu-boy.com/api/badges/appleboy/easyssh-proxy/status.svg)](http://drone.wu-boy.com/appleboy/easyssh-proxy) [![codecov](https://codecov.io/gh/appleboy/easyssh-proxy/branch/master/graph/badge.svg)](https://codecov.io/gh/appleboy/easyssh-proxy) [![Go Report Card](https://goreportcard.com/badge/github.com/appleboy/easyssh-proxy)](https://goreportcard.com/report/github.com/appleboy/easyssh-proxy) [![Sourcegraph](https://sourcegraph.com/github.com/appleboy/easyssh-proxy/-/badge.svg)](https://sourcegraph.com/github.com/appleboy/easyssh-proxy?badge) +[![GoDoc](https://godoc.org/github.com/appleboy/easyssh-proxy?status.svg)](https://godoc.org/github.com/appleboy/easyssh-proxy) +[![Build Status](http://drone.wu-boy.com/api/badges/appleboy/easyssh-proxy/status.svg)](http://drone.wu-boy.com/appleboy/easyssh-proxy) +[![codecov](https://codecov.io/gh/appleboy/easyssh-proxy/branch/master/graph/badge.svg)](https://codecov.io/gh/appleboy/easyssh-proxy) +[![Go Report Card](https://goreportcard.com/badge/github.com/appleboy/easyssh-proxy)](https://goreportcard.com/report/github.com/appleboy/easyssh-proxy) +[![Sourcegraph](https://sourcegraph.com/github.com/appleboy/easyssh-proxy/-/badge.svg)](https://sourcegraph.com/github.com/appleboy/easyssh-proxy?badge) +[![Release](https://github-release-version.herokuapp.com/github/appleboy/easyssh-proxy/release.svg?style=flat)](https://github.com/appleboy/easyssh-proxy/releases/latest) easyssh-proxy provides a simple implementation of some SSH protocol features in Go. @@ -124,3 +129,53 @@ See [example/proxy/proxy.go](./example/proxy/proxy.go) }, } ``` + +### SSH Stream Log + +See [example/stream/stream.go](./example/stream/stream.go) + +[embedmd]:# (example/stream/stream.go go /func/ /^}$/) +```go +func main() { + // Create MakeConfig instance with remote username, server address and path to private key. + ssh := &easyssh.MakeConfig{ + Server: "localhost", + User: "drone-scp", + KeyPath: "./tests/.ssh/id_rsa", + Port: "22", + Timeout: 60 * time.Second, + } + + // Call Run method with command you want to run on remote server. + stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream("for i in {1..5}; do echo ${i}; sleep 1; done; exit 2;", 60) + // Handle errors + if err != nil { + panic("Can't run remote command: " + err.Error()) + } else { + // read from the output channel until the done signal is passed + isTimeout := true + loop: + for { + select { + case isTimeout = <-doneChan: + break loop + case outline := <-stdoutChan: + fmt.Println("out:", outline) + case errline := <-stderrChan: + fmt.Println("err:", errline) + case err = <-errChan: + } + } + + // get exit code or command error. + if err != nil { + fmt.Println("err: " + err.Error()) + } + + // command time out + if !isTimeout { + fmt.Println("Error: command timeout") + } + } +} +``` diff --git a/vendor/github.com/appleboy/easyssh-proxy/easyssh.go b/vendor/github.com/appleboy/easyssh-proxy/easyssh.go index 3f264e6..08aa356 100644 --- a/vendor/github.com/appleboy/easyssh-proxy/easyssh.go +++ b/vendor/github.com/appleboy/easyssh-proxy/easyssh.go @@ -156,25 +156,31 @@ func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) { // Stream returns one channel that combines the stdout and stderr of the command // as it is run on the remote machine, and another that sends true when the // command is done. The sessions and channels will then be closed. -func (ssh_conf *MakeConfig) Stream(command string, timeout int) (stdout chan string, stderr chan string, done chan bool, errChan chan error, err error) { +func (ssh_conf *MakeConfig) Stream(command string, timeout int) (<-chan string, <-chan string, <-chan bool, <-chan error, error) { + // continuously send the command's output over the channel + stdoutChan := make(chan string) + stderrChan := make(chan string) + doneChan := make(chan bool) + errChan := make(chan error) + // connect to remote host session, err := ssh_conf.connect() if err != nil { - return stdout, stderr, done, errChan, err + return stdoutChan, stderrChan, doneChan, errChan, err } // defer session.Close() // connect to both outputs (they are of type io.Reader) outReader, err := session.StdoutPipe() if err != nil { - return stdout, stderr, done, errChan, err + return stdoutChan, stderrChan, doneChan, errChan, err } errReader, err := session.StderrPipe() if err != nil { - return stdout, stderr, done, errChan, err + return stdoutChan, stderrChan, doneChan, errChan, err } err = session.Start(command) if err != nil { - return stdout, stderr, done, errChan, err + return stdoutChan, stderrChan, doneChan, errChan, err } // combine outputs, create a line-by-line scanner @@ -182,16 +188,11 @@ func (ssh_conf *MakeConfig) Stream(command string, timeout int) (stdout chan str stderrReader := io.MultiReader(errReader) stdoutScanner := bufio.NewScanner(stdoutReader) stderrScanner := bufio.NewScanner(stderrReader) - // continuously send the command's output over the channel - stdoutChan := make(chan string) - stderrChan := make(chan string) - done = make(chan bool) - errChan = make(chan error) - go func(stdoutScanner, stderrScanner *bufio.Scanner, stdoutChan, stderrChan chan string, done chan bool, errChan chan error) { + go func(stdoutScanner, stderrScanner *bufio.Scanner, stdoutChan, stderrChan chan string, doneChan chan bool, errChan chan error) { defer close(stdoutChan) defer close(stderrChan) - defer close(done) + defer close(doneChan) defer close(errChan) defer session.Close() @@ -212,15 +213,15 @@ func (ssh_conf *MakeConfig) Stream(command string, timeout int) (stdout chan str select { case <-res: errChan <- session.Wait() - done <- true + doneChan <- true case <-timeoutChan: stderrChan <- "Run Command Timeout!" errChan <- nil - done <- false + doneChan <- false } - }(stdoutScanner, stderrScanner, stdoutChan, stderrChan, done, errChan) + }(stdoutScanner, stderrScanner, stdoutChan, stderrChan, doneChan, errChan) - return stdoutChan, stderrChan, done, errChan, err + return stdoutChan, stderrChan, doneChan, errChan, err } // Run command on remote machine and returns its stdout as a string @@ -230,11 +231,11 @@ func (ssh_conf *MakeConfig) Run(command string, timeout int) (outStr string, err return outStr, errStr, isTimeout, err } // read from the output channel until the done signal is passed - stillGoing := true - for stillGoing { +loop: + for { select { case isTimeout = <-doneChan: - stillGoing = false + break loop case outline := <-stdoutChan: if outline != "" { outStr += outline + "\n" diff --git a/vendor/vendor.json b/vendor/vendor.json index f9f125f..2033f9f 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -3,12 +3,12 @@ "ignore": "test", "package": [ { - "checksumSHA1": "YgrgNVNBf7Ro0f3KuiHewOCHrwo=", + "checksumSHA1": "EcF7T9tPEMMJfuRdPBB3NdRUg4c=", "path": "github.com/appleboy/easyssh-proxy", - "revision": "14882d1d04ac6a85700586997695fcd936470e86", - "revisionTime": "2017-05-11T07:07:30Z", - "version": "1.1.5", - "versionExact": "1.1.5" + "revision": "33d87eae3a018c3312e32cc4eb4578d5a563aabd", + "revisionTime": "2017-05-16T07:22:25Z", + "version": "1.1.6", + "versionExact": "1.1.6" }, { "checksumSHA1": "dvabztWVQX8f6oMLRyv4dLH+TGY=",