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" "github.com/appleboy/easyssh-proxy"
) )
var wg sync.WaitGroup
const ( const (
missingHostOrUser = "Error: missing server host or user" missingHostOrUser = "Error: missing server host or user"
missingPasswordOrKey = "Error: can't connect without a private SSH key or password" 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) return fmt.Errorf(setPasswordandKey)
} }
wg := sync.WaitGroup{}
wg.Add(len(p.Config.Host)) wg.Add(len(p.Config.Host))
errChannel := make(chan error, 1) errChannel := make(chan error, 1)
finished := make(chan bool, 1) finished := make(chan bool, 1)
@ -88,7 +87,7 @@ func (p Plugin) Exec() error {
stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream(strings.Join(p.Config.Script, "\n"), p.Config.CommandTimeout) stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream(strings.Join(p.Config.Script, "\n"), p.Config.CommandTimeout)
if err != nil { if err != nil {
errChannel <- err errChannel <- err
} } else {
// read from the output channel until the done signal is passed // read from the output channel until the done signal is passed
stillGoing := true stillGoing := true
isTimeout := true isTimeout := true
@ -113,6 +112,8 @@ func (p Plugin) Exec() error {
if !isTimeout { if !isTimeout {
errChannel <- fmt.Errorf(commandTimeOut) errChannel <- fmt.Errorf(commandTimeOut)
} }
}
wg.Done() wg.Done()
}(host) }(host)
} }