未验证 提交 b398810a 编写于 作者: M Medya Ghazizadeh 提交者: GitHub

Merge pull request #5917 from tstromberg/include-output-exec-err

SSHRunner: include stdout/stderr with error message
......@@ -18,6 +18,7 @@ package command
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
......@@ -39,7 +40,7 @@ type ExecRunner struct{}
// RunCmd implements the Command Runner interface to run a exec.Cmd object
func (*ExecRunner) RunCmd(cmd *exec.Cmd) (*RunResult, error) {
rr := &RunResult{Args: cmd.Args}
glog.Infof("(ExecRunner) Run: %v", rr.Command())
glog.Infof("Run: %v", rr.Command())
var outb, errb io.Writer
if cmd.Stdout == nil {
......@@ -62,19 +63,19 @@ func (*ExecRunner) RunCmd(cmd *exec.Cmd) (*RunResult, error) {
start := time.Now()
err := cmd.Run()
elapsed := time.Since(start)
if exitError, ok := err.(*exec.ExitError); ok {
rr.ExitCode = exitError.ExitCode()
}
// Decrease log spam
if elapsed > (1 * time.Second) {
glog.Infof("Completed: %s: (%s)", rr.Command(), elapsed)
}
if err == nil {
// Reduce log spam
if elapsed > (1 * time.Second) {
glog.Infof("(ExecRunner) Done: %v: (%s)", rr.Command(), elapsed)
}
} else {
if exitError, ok := err.(*exec.ExitError); ok {
rr.ExitCode = exitError.ExitCode()
}
glog.Infof("(ExecRunner) Non-zero exit: %v: %v (%s)\n%s", rr.Command(), err, elapsed, rr.Output())
err = errors.Wrapf(err, "command failed: %s\nstdout: %s\nstderr: %s", rr.Command(), rr.Stdout.String(), rr.Stderr.String())
return rr, nil
}
return rr, err
return rr, fmt.Errorf("%s: %v\nstdout:\n%s\nstderr:\n%s", rr.Command(), err, rr.Stdout.String(), rr.Stderr.String())
}
// Copy copies a file and its permissions
......
......@@ -99,7 +99,7 @@ func teeSSH(s *ssh.Session, cmd string, outB io.Writer, errB io.Writer) error {
// RunCmd implements the Command Runner interface to run a exec.Cmd object
func (s *SSHRunner) RunCmd(cmd *exec.Cmd) (*RunResult, error) {
rr := &RunResult{Args: cmd.Args}
glog.Infof("(SSHRunner) Run: %v", rr.Command())
glog.Infof("Run: %v", rr.Command())
var outb, errb io.Writer
start := time.Now()
......@@ -131,20 +131,21 @@ func (s *SSHRunner) RunCmd(cmd *exec.Cmd) (*RunResult, error) {
}
}()
elapsed := time.Since(start)
err = teeSSH(sess, shellquote.Join(cmd.Args...), outb, errb)
elapsed := time.Since(start)
if exitError, ok := err.(*exec.ExitError); ok {
rr.ExitCode = exitError.ExitCode()
}
// Decrease log spam
if elapsed > (1 * time.Second) {
glog.Infof("Completed: %s: (%s)", rr.Command(), elapsed)
}
if err == nil {
// Reduce log spam
if elapsed > (1 * time.Second) {
glog.Infof("(SSHRunner) Done: %v: (%s)", rr.Command(), elapsed)
}
} else {
if exitError, ok := err.(*exec.ExitError); ok {
rr.ExitCode = exitError.ExitCode()
}
glog.Infof("(SSHRunner) Non-zero exit: %v: %v (%s)\n%s", rr.Command(), err, elapsed, rr.Output())
return rr, nil
}
return rr, err
return rr, fmt.Errorf("%s: %v\nstdout:\n%s\nstderr:\n%s", rr.Command(), err, rr.Stdout.String(), rr.Stderr.String())
}
// Copy copies a file to the remote over SSH.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册