fix block channel.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2017-05-10 11:30:52 +08:00
parent 5b0ba436e2
commit daaf2e2b25
No known key found for this signature in database
GPG Key ID: 0F84B2110C500B1F

View File

@ -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)
}