Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
(GH-61) Handle tool exit codes
Browse files Browse the repository at this point in the history
Surfaces the exit codes and errors raised by the tool within the container, up to the main application. This affects the exit status of the `exec` command.
  • Loading branch information
da-ar committed Dec 10, 2021
1 parent ebc246f commit cebe503
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
34 changes: 25 additions & 9 deletions pkg/prm/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,19 @@ func (d *Docker) Exec(tool *Tool, args []string, prmConfig Config, paths Directo
return FAILURE, err
}

isDone := make(chan bool)
isError := make(chan error)
toolExit := make(chan container.ContainerWaitOKBody)
// Move this wait into a goroutine
// when its finished it will return and post to the isDone channel
go func(d *Docker, isDone chan bool) {
go func() {
statusCh, errCh := d.Client.ContainerWait(d.Context, resp.ID, container.WaitConditionNotRunning)
select {
case <-errCh:
isDone <- true
case <-statusCh:
isDone <- true
case err := <-errCh:
isError <- err
case status := <-statusCh:
toolExit <- status
}
}(d, isDone)
}()

// parse out the containers logs while we wait for the container to finish
for {
Expand All @@ -306,9 +307,24 @@ func (d *Docker) Exec(tool *Tool, args []string, prmConfig Config, paths Directo
return FAILURE, err
}

if done := <-isDone; done {
return SUCCESS, nil
select {
case err := <-isError:
return FAILURE, err
case exitValues := <-toolExit:
if exitValues.StatusCode == int64(tool.Cfg.Common.SuccessExitCode) {
return SUCCESS, nil
} else {
// If we have more details on why the tool failed, use that info
if exitValues.Error != nil {
err = fmt.Errorf("%s", exitValues.Error.Message)
} else {
// otherwise, just log the exit code
err = fmt.Errorf("Tool exited with code: %d", exitValues.StatusCode)
}
return TOOL_ERROR, err
}
}

}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/prm/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ func (p *Prm) Exec(tool *Tool, args []string) error {
log.Info().Msgf("Tool %s/%s executed successfully", tool.Cfg.Plugin.Author, tool.Cfg.Plugin.Id)
case FAILURE:
log.Error().Msgf("Tool %s/%s failed to execute", tool.Cfg.Plugin.Author, tool.Cfg.Plugin.Id)
return err
case TOOL_ERROR:
log.Error().Msgf("Tool %s/%s encountered an error", tool.Cfg.Plugin.Author, tool.Cfg.Plugin.Id)
return err
case TOOL_NOT_FOUND:
log.Error().Msgf("Tool %s/%s not found", tool.Cfg.Plugin.Author, tool.Cfg.Plugin.Id)
return err
default:
log.Info().Msgf("Tool %s/%s exited with code %d", tool.Cfg.Plugin.Author, tool.Cfg.Plugin.Id, exit)
}
Expand Down

0 comments on commit cebe503

Please sign in to comment.