Skip to content

Commit

Permalink
Add method to get exit codes
Browse files Browse the repository at this point in the history
[tester]
  • Loading branch information
LordRalex committed Mar 16, 2023
1 parent c1da18a commit e230021
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
7 changes: 7 additions & 0 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ type Environment interface {
SendCode(code int) error

GetBase() *BaseEnvironment

GetLastExitCode() int
}

type BaseEnvironment struct {
Expand All @@ -82,6 +84,7 @@ type BaseEnvironment struct {
ExecutionFunction ExecutionFunction `json:"-"`
WaitFunction func() (err error) `json:"-"`
ServerId string `json:"-"`
LastExitCode int `json:"-"`
}

type ExecutionData struct {
Expand Down Expand Up @@ -165,6 +168,10 @@ func (e *BaseEnvironment) GetBase() *BaseEnvironment {
return e
}

func (e *BaseEnvironment) GetLastExitCode() int {
return e.LastExitCode
}

func (e *BaseEnvironment) Log(l *log.Logger, format string, obj ...interface{}) {
msg := fmt.Sprintf("[%s] ", e.ServerId) + format
l.Printf(msg, obj...)
Expand Down
22 changes: 9 additions & 13 deletions environments/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@ func (d *docker) dockerExecuteAsync(steps pufferpanel.ExecutionData) error {
defer d.connection.Close()
wrapper := d.CreateWrapper()
_, _ = io.Copy(wrapper, d.connection.Reader)
//because we use the auto-delete, we don't manually stop the container
//c, _ := d.getClient()
//err = c.ContainerStop(context.Background(), d.ContainerId, nil)
//if err != nil {
// logging.Error.Printf("Error stopping container "+d.ContainerId, err)
//}
}()

go func() {
var exitCode int

okChan, errChan := dockerClient.ContainerWait(ctx, d.ContainerId, container.WaitConditionRemoved)
select {
case _ = <-okChan:
case info := <-okChan:
exitCode = cast.ToInt(info.StatusCode)
case chanErr := <-errChan:
exitCode = 1
if chanErr != nil {
d.Log(logging.Error, "Error from error channel, awaiting exit `%v`\n", chanErr)
}
Expand All @@ -130,11 +130,7 @@ func (d *docker) dockerExecuteAsync(steps pufferpanel.ExecutionData) error {
_ = d.WSManager.WriteMessage(msg)

if steps.Callback != nil {
if err == nil {
steps.Callback(0)
} else {
steps.Callback(1)
}
steps.Callback(exitCode)
}
}()

Expand Down Expand Up @@ -441,7 +437,7 @@ func (d *docker) createContainer(client *client.Client, ctx context.Context, cmd
}

hostConfig := &container.HostConfig{
AutoRemove: true,
//AutoRemove: true,
NetworkMode: container.NetworkMode(d.NetworkMode),
Resources: d.Resources,
Binds: bindDirs,
Expand Down
1 change: 1 addition & 0 deletions environments/standard/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func (s *standard) handleClose(callback func(exitCode int)) {
} else {
exitCode = s.mainProcess.ProcessState.ExitCode()
}
s.LastExitCode = exitCode

if s.mainProcess != nil && s.mainProcess.Process != nil {
_ = s.mainProcess.Process.Release()
Expand Down
1 change: 1 addition & 0 deletions environments/tty/tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (t *tty) handleClose(callback func(exitCode int)) {
} else {
exitCode = t.mainProcess.ProcessState.ExitCode()
}
t.LastExitCode = exitCode

if err != nil {
t.Log(logging.Error, "Error waiting on process: %s\n", err)
Expand Down
6 changes: 6 additions & 0 deletions templatetester/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bufio"
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/pufferpanel/pufferpanel/v3/config"
Expand Down Expand Up @@ -184,6 +186,10 @@ func main() {
err = runServer(prg)
panicIf(err)

if prg.RunningEnvironment.GetLastExitCode() != 0 {
panicIf(errors.New(fmt.Sprintf("exit code status %d", prg.RunningEnvironment.GetLastExitCode())))
}

err = prg.Destroy()
panicIf(err)
}
Expand Down

0 comments on commit e230021

Please sign in to comment.