Support stream output logs.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2017-05-10 11:05:38 +08:00
parent f26bd7f7f7
commit 5b0ba436e2
No known key found for this signature in database
GPG Key ID: 0F84B2110C500B1F
2 changed files with 35 additions and 5 deletions

View File

@ -85,20 +85,34 @@ func (p Plugin) Exec() error {
}
p.log(host, "commands: ", strings.Join(p.Config.Script, "\n"))
outStr, errStr, isTimeout, err := ssh.Run(strings.Join(p.Config.Script, "\n"), p.Config.CommandTimeout)
p.log(host, "outputs:", outStr)
if len(errStr) != 0 {
p.log(host, "errors:", errStr)
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:
}
}
// get exit code or command error.
if err != nil {
errChannel <- err
}
// command time out
if !isTimeout {
errChannel <- fmt.Errorf(commandTimeOut)
}
wg.Done()
}(host)
}

View File

@ -121,6 +121,22 @@ func TestSSHScriptFromKeyFile(t *testing.T) {
assert.Nil(t, err)
}
func TestStreamFromSSHCommand(t *testing.T) {
plugin := Plugin{
Config: Config{
Host: []string{"localhost", "127.0.0.1"},
UserName: "drone-scp",
Port: 22,
KeyPath: "./tests/.ssh/id_rsa",
Script: []string{"whoami", "for i in {1..5}; do echo ${i}; sleep 1; done", "echo 'done'"},
CommandTimeout: 60,
},
}
err := plugin.Exec()
assert.Nil(t, err)
}
func TestSSHScriptWithError(t *testing.T) {
plugin := Plugin{
Config: Config{