From daaf2e2b255d04c6c60061cac0390816e9edf1fe Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 10 May 2017 11:30:52 +0800 Subject: [PATCH] fix block channel. Signed-off-by: Bo-Yi Wu --- plugin.go | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/plugin.go b/plugin.go index c08987a..c5c86ac 100644 --- a/plugin.go +++ b/plugin.go @@ -11,8 +11,6 @@ import ( "github.com/appleboy/easyssh-proxy" ) -var wg sync.WaitGroup - const ( missingHostOrUser = "Error: missing server host or user" missingPasswordOrKey = "Error: can't connect without a private SSH key or password" @@ -59,6 +57,7 @@ func (p Plugin) Exec() error { return fmt.Errorf(setPasswordandKey) } + wg := sync.WaitGroup{} wg.Add(len(p.Config.Host)) errChannel := make(chan error, 1) finished := make(chan bool, 1) @@ -88,31 +87,33 @@ func (p Plugin) Exec() error { stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream(strings.Join(p.Config.Script, "\n"), p.Config.CommandTimeout) if err != nil { errChannel <- err - } - // read from the output channel until the done signal is passed - stillGoing := true - isTimeout := true - for stillGoing { - select { - case isTimeout = <-doneChan: - stillGoing = false - case outline := <-stdoutChan: - p.log(host, "outputs:", outline) - case errline := <-stderrChan: - p.log(host, "errors:", errline) - case err = <-errChan: + } else { + // read from the output channel until the done signal is passed + stillGoing := true + isTimeout := true + for stillGoing { + select { + case isTimeout = <-doneChan: + stillGoing = false + case outline := <-stdoutChan: + p.log(host, "outputs:", outline) + case errline := <-stderrChan: + p.log(host, "errors:", errline) + case err = <-errChan: + } + } + + // get exit code or command error. + if err != nil { + errChannel <- err + } + + // command time out + if !isTimeout { + errChannel <- fmt.Errorf(commandTimeOut) } } - // get exit code or command error. - if err != nil { - errChannel <- err - } - - // command time out - if !isTimeout { - errChannel <- fmt.Errorf(commandTimeOut) - } wg.Done() }(host) }