Skip to content

Commit

Permalink
Improve error messages for state scripts errors.
Browse files Browse the repository at this point in the history
Rely on the full error description instead of just the error code.

Changelog: Commit

Signed-off-by: Kristian Amlie <[email protected]>
(cherry picked from commit f5ac3fc)
  • Loading branch information
kacf committed Sep 27, 2017
1 parent d75771e commit 42ab277
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
31 changes: 8 additions & 23 deletions statescript/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,6 @@ func (l Launcher) get(state, action string) ([]os.FileInfo, string, error) {
return scripts, sDir, nil
}

func retCode(err error) int {
defaultFailedCode := -1

if err != nil {
// try to get the exit code
if exitError, ok := err.(*exec.ExitError); ok {
ws := exitError.Sys().(syscall.WaitStatus)
return ws.ExitStatus()
} else {
return defaultFailedCode
}
}
return 0
}

func (l Launcher) getTimeout() time.Duration {
t := time.Duration(l.Timeout) * time.Second
if t == 0 {
Expand All @@ -166,7 +151,7 @@ func (l Launcher) getTimeout() time.Duration {
return t
}

func execute(name string, timeout time.Duration) int {
func execute(name string, timeout time.Duration) error {

cmd := exec.Command(name)

Expand All @@ -186,7 +171,7 @@ func execute(name string, timeout time.Duration) int {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}

if err := cmd.Start(); err != nil {
return retCode(err)
return err
}

var bts []byte
Expand Down Expand Up @@ -214,9 +199,9 @@ func execute(name string, timeout time.Duration) int {
defer timer.Stop()

if err := cmd.Wait(); err != nil {
return retCode(err)
return err
}
return 0
return nil
}

func (l Launcher) ExecuteAll(state, action string, ignoreError bool) error {
Expand Down Expand Up @@ -246,13 +231,13 @@ func (l Launcher) ExecuteAll(state, action string, ignoreError bool) error {
}
}

if ret := execute(filepath.Join(dir, s.Name()), timeout); ret != 0 {
if err = execute(filepath.Join(dir, s.Name()), timeout); err != nil {
// In case of error scripts all should be executed.
if ignoreError {
log.Errorf("statescript: ignoring error executing '%s': %d", s.Name(), ret)
log.Errorf("statescript: ignoring error executing '%s': %s", s.Name(), err.Error())
} else {
return errors.Errorf("statescript: error executing '%s': %d",
s.Name(), ret)
return errors.Errorf("statescript: error executing '%s': %s",
s.Name(), err.Error())
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions statescript/statescript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,17 @@ func TestExecutor(t *testing.T) {
log.SetOutput(&buf)
fileP, err := createArtifactTestScript(tmpArt, "ArtifactInstall_Leave_00", "#!/bin/bash \necho 'error data' >&2")
assert.NoError(t, err)
res := execute(fileP.Name(), 100) // give the script plenty of time to run
assert.Equal(t, 0, res)
err = execute(fileP.Name(), 100) // give the script plenty of time to run
assert.NoError(t, err)
assert.Contains(t, buf.String(), "error data")

buf.Reset()

// write more than 10KB to stderr
fileP, err = createArtifactTestScript(tmpArt, "ArtifactInstall_Leave_11", "#!/bin/bash \nhead -c 89999 </dev/urandom >&2\n exit 1")
assert.NoError(t, err)
res = execute(fileP.Name(), 100)
assert.Equal(t, 1, res)
err = execute(fileP.Name(), 100)
assert.EqualError(t, err, "exit status 1")
assert.Contains(t, buf.String(), "Truncated to 10KB")
}

Expand Down

0 comments on commit 42ab277

Please sign in to comment.