fix: get exit code from ssh run command. (#78)

This commit is contained in:
Bo-Yi Wu 2017-05-09 09:38:31 +08:00 committed by GitHub
parent 356b2ae6cc
commit b6c973ef1e
3 changed files with 48 additions and 18 deletions

View File

@ -121,6 +121,23 @@ func TestSSHScriptFromKeyFile(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
} }
func TestSSHScriptWithError(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{"exit 1"},
CommandTimeout: 60,
},
}
err := plugin.Exec()
// Process exited with status 1
assert.NotNil(t, err)
}
func TestSSHCommandTimeOut(t *testing.T) { func TestSSHCommandTimeOut(t *testing.T) {
plugin := Plugin{ plugin := Plugin{
Config: Config{ Config: Config{

View File

@ -155,36 +155,44 @@ func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) {
// Stream returns one channel that combines the stdout and stderr of the command // 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 // 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. // 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, err error) { func (ssh_conf *MakeConfig) Stream(command string, timeout int) (stdout chan string, stderr chan string, done chan bool, errChan chan error, err error) {
// connect to remote host // connect to remote host
session, err := ssh_conf.connect() session, err := ssh_conf.connect()
if err != nil { if err != nil {
return stdout, stderr, done, err return stdout, stderr, done, errChan, err
} }
// defer session.Close()
// connect to both outputs (they are of type io.Reader) // connect to both outputs (they are of type io.Reader)
outReader, err := session.StdoutPipe() outReader, err := session.StdoutPipe()
if err != nil { if err != nil {
return stdout, stderr, done, err return stdout, stderr, done, errChan, err
} }
errReader, err := session.StderrPipe() errReader, err := session.StderrPipe()
if err != nil { if err != nil {
return stdout, stderr, done, err return stdout, stderr, done, errChan, err
} }
err = session.Start(command)
if err != nil {
return stdout, stderr, done, errChan, err
}
// combine outputs, create a line-by-line scanner // combine outputs, create a line-by-line scanner
stdoutReader := io.MultiReader(outReader) stdoutReader := io.MultiReader(outReader)
stderrReader := io.MultiReader(errReader) stderrReader := io.MultiReader(errReader)
err = session.Start(command)
stdoutScanner := bufio.NewScanner(stdoutReader) stdoutScanner := bufio.NewScanner(stdoutReader)
stderrScanner := bufio.NewScanner(stderrReader) stderrScanner := bufio.NewScanner(stderrReader)
// continuously send the command's output over the channel // continuously send the command's output over the channel
stdoutChan := make(chan string) stdoutChan := make(chan string)
stderrChan := make(chan string) stderrChan := make(chan string)
done = make(chan bool) done = make(chan bool)
errChan = make(chan error)
go func(stdoutScanner, stderrScanner *bufio.Scanner, stdoutChan, stderrChan chan string, done chan bool) { go func(stdoutScanner, stderrScanner *bufio.Scanner, stdoutChan, stderrChan chan string, done chan bool, errChan chan error) {
defer close(stdoutChan) defer close(stdoutChan)
defer close(stderrChan) defer close(stderrChan)
defer close(done) defer close(done)
defer close(errChan)
defer session.Close()
timeoutChan := time.After(time.Duration(timeout) * time.Second) timeoutChan := time.After(time.Duration(timeout) * time.Second)
res := make(chan bool, 1) res := make(chan bool, 1)
@ -204,21 +212,22 @@ func (ssh_conf *MakeConfig) Stream(command string, timeout int) (stdout chan str
case <-res: case <-res:
stdoutChan <- "" stdoutChan <- ""
stderrChan <- "" stderrChan <- ""
errChan <- session.Wait()
done <- true done <- true
case <-timeoutChan: case <-timeoutChan:
stdoutChan <- "" stdoutChan <- ""
stderrChan <- "Run Command Timeout!" stderrChan <- "Run Command Timeout!"
errChan <- nil
done <- false done <- false
} }
}(stdoutScanner, stderrScanner, stdoutChan, stderrChan, done, errChan)
session.Close() return stdoutChan, stderrChan, done, errChan, err
}(stdoutScanner, stderrScanner, stdoutChan, stderrChan, done)
return stdoutChan, stderrChan, done, err
} }
// Run command on remote machine and returns its stdout as a string // Run command on remote machine and returns its stdout as a string
func (ssh_conf *MakeConfig) Run(command string, timeout int) (outStr string, errStr string, isTimeout bool, err error) { func (ssh_conf *MakeConfig) Run(command string, timeout int) (outStr string, errStr string, isTimeout bool, err error) {
stdoutChan, stderrChan, doneChan, err := ssh_conf.Stream(command, timeout) stdoutChan, stderrChan, doneChan, errChan, err := ssh_conf.Stream(command, timeout)
if err != nil { if err != nil {
return outStr, errStr, isTimeout, err return outStr, errStr, isTimeout, err
} }
@ -236,6 +245,7 @@ func (ssh_conf *MakeConfig) Run(command string, timeout int) (outStr string, err
if errline != "" { if errline != "" {
errStr += errline + "\n" errStr += errline + "\n"
} }
case err = <-errChan:
} }
} }
// return the concatenation of all signals from the output channel // return the concatenation of all signals from the output channel
@ -266,17 +276,20 @@ func (ssh_conf *MakeConfig) Scp(sourceFile string, etargetFile string) error {
} }
go func() { go func() {
w, _ := session.StdinPipe() w, err := session.StdinPipe()
if err != nil {
return
}
defer w.Close()
fmt.Fprintln(w, "C0644", srcStat.Size(), targetFile) fmt.Fprintln(w, "C0644", srcStat.Size(), targetFile)
if srcStat.Size() > 0 { if srcStat.Size() > 0 {
io.Copy(w, src) io.Copy(w, src)
fmt.Fprint(w, "\x00") fmt.Fprint(w, "\x00")
w.Close()
} else { } else {
fmt.Fprint(w, "\x00") fmt.Fprint(w, "\x00")
w.Close()
} }
}() }()

10
vendor/vendor.json vendored
View File

@ -3,12 +3,12 @@
"ignore": "test", "ignore": "test",
"package": [ "package": [
{ {
"checksumSHA1": "L3PugNJJOEpRmRbD+27LgTZC2E4=", "checksumSHA1": "/Sb+z/BicEjhh6Ib7Tawr64BNHA=",
"path": "github.com/appleboy/easyssh-proxy", "path": "github.com/appleboy/easyssh-proxy",
"revision": "a13ed86767b8e8a24d8147a4909a702e7cf6b465", "revision": "cf4f2bae639f692dbc7b3f852be35125a1a645e3",
"revisionTime": "2017-04-14T13:46:38Z", "revisionTime": "2017-05-09T01:16:07Z",
"version": "1.1.2", "version": "1.1.3",
"versionExact": "1.1.2" "versionExact": "1.1.3"
}, },
{ {
"checksumSHA1": "dvabztWVQX8f6oMLRyv4dLH+TGY=", "checksumSHA1": "dvabztWVQX8f6oMLRyv4dLH+TGY=",