Skip to content

Commit

Permalink
Set job name to container and catch exit 1 situations
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammedikinci committed Apr 17, 2022
1 parent 68b2896 commit 7cf28c4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
11 changes: 11 additions & 0 deletions pkg/job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pin

type Job struct {
Name string
Image string
Script []string
WorkDir string
CopyFiles bool
Status int
Next *Job
}
9 changes: 2 additions & 7 deletions pkg/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ import (

type Workflow []Job

type Job struct {
Image string
Script []string
WorkDir string
CopyFiles bool
}

func parse() (Workflow, error) {
var workflow Workflow = Workflow{}

Expand All @@ -30,6 +23,8 @@ func parse() (Workflow, error) {
return nil, err
}

job.Name = v

workflow = append(workflow, job)
}

Expand Down
68 changes: 57 additions & 11 deletions pkg/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"bytes"
"context"
"errors"
"io"
"log"
"os"
Expand Down Expand Up @@ -66,7 +67,7 @@ func (r *runner) jobRunner() error {
resp, err := r.cli.ContainerCreate(r.ctx, &container.Config{
Image: r.currentJob.Image,
Tty: true,
}, nil, nil, nil, "")
}, nil, nil, nil, r.currentJob.Name)

if err != nil {
return err
Expand All @@ -90,22 +91,14 @@ func (r *runner) jobRunner() error {
}
}

r.infoLog.Println("Container stopping")

if err := r.cli.ContainerStop(r.ctx, r.containerResponse.ID, nil); err != nil {
if err := r.stopCurrentContainer(); err != nil {
return err
}

r.infoLog.Println("Container stopped")

r.infoLog.Println("Container removing")

if err := r.cli.ContainerRemove(r.ctx, r.containerResponse.ID, types.ContainerRemoveOptions{}); err != nil {
if err := r.removeCurrentContainer(); err != nil {
return err
}

r.infoLog.Println("Container removed")

r.infoLog.Println("Job ended")

return nil
Expand Down Expand Up @@ -139,6 +132,35 @@ func (r *runner) commandRunner(command string) error {

io.Copy(os.Stdout, res.Reader)

status, err := r.cli.ContainerExecInspect(r.ctx, exec.ID)
if err != nil {
return err
}

if status.ExitCode != 0 {
r.infoLog.Println("Command execution failed")

r.cli.ContainerKill(r.ctx, r.containerResponse.ID, "KILL")

// TODO: Print detail exit 1 logs
out, err := r.cli.ContainerLogs(r.ctx, r.containerResponse.ID, types.ContainerLogsOptions{ShowStdout: true})
if err != nil {
return err
}

io.Copy(os.Stdout, out)

if err := r.stopCurrentContainer(); err != nil {
return err
}

if err := r.removeCurrentContainer(); err != nil {
return err
}

return errors.New("command execution failed")
}

r.infoLog.Println("Command execution successful")

return nil
Expand Down Expand Up @@ -236,3 +258,27 @@ func (r runner) copyToContainer() error {

return nil
}

func (r runner) stopCurrentContainer() error {
r.infoLog.Println("Container stopping")

if err := r.cli.ContainerStop(r.ctx, r.containerResponse.ID, nil); err != nil {
return err
}

r.infoLog.Println("Container stopped")

return nil
}

func (r runner) removeCurrentContainer() error {
r.infoLog.Println("Container removing")

if err := r.cli.ContainerRemove(r.ctx, r.containerResponse.ID, types.ContainerRemoveOptions{}); err != nil {
return err
}

r.infoLog.Println("Container removed")

return nil
}

0 comments on commit 7cf28c4

Please sign in to comment.