Skip to content

Commit

Permalink
Shell command logs getting from log file
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammedikinci committed Apr 25, 2022
1 parent da4615d commit 8640586
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ require (
github.com/docker/docker v20.10.14+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -159,6 +161,12 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
Expand Down Expand Up @@ -318,6 +326,7 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -343,6 +352,7 @@ golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
38 changes: 29 additions & 9 deletions pkg/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/fatih/color"
)

type runner struct {
Expand Down Expand Up @@ -62,7 +63,9 @@ func (r *runner) jobRunner() error {
}
}

color.Set(color.FgGreen)
r.infoLog.Println("Start creating container")
color.Unset()

resp, err := r.cli.ContainerCreate(r.ctx, &container.Config{
Image: r.currentJob.Image,
Expand All @@ -79,7 +82,9 @@ func (r *runner) jobRunner() error {
return err
}

color.Set(color.FgGreen)
r.infoLog.Println("Starting the container")
color.Unset()

if err := r.cli.ContainerStart(r.ctx, r.containerResponse.ID, types.ContainerStartOptions{}); err != nil {
return err
Expand All @@ -97,13 +102,15 @@ func (r *runner) jobRunner() error {
return err
}

color.Set(color.FgGreen)
r.infoLog.Println("Job ended")
color.Unset()

return nil
}

func (r *runner) prepareAndRunShellCommandScript() error {
shellFileContains := ""
shellFileContains := "#!/bin/sh\nexec > /shell_command_output.log 2>&1\n"

for _, cmd := range r.currentJob.Script {
shellFileContains += cmd + "\n"
Expand Down Expand Up @@ -137,6 +144,9 @@ func (r *runner) prepareAndRunShellCommandScript() error {
return err
}

// not neccessary to handle any error
os.Remove(".pin/shell_command.sh")

return nil
}

Expand All @@ -156,7 +166,7 @@ func (r *runner) commandRunner(command string) error {
return err
}

res, err := r.cli.ContainerExecAttach(r.ctx, exec.ID, types.ExecStartCheck{})
res, err := r.cli.ContainerExecAttach(r.ctx, exec.ID, types.ExecStartCheck{Tty: true})
if err != nil {
return err
}
Expand All @@ -169,17 +179,19 @@ func (r *runner) commandRunner(command string) error {
}

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

r.cli.ContainerKill(r.ctx, r.containerResponse.ID, "KILL")
r.infoLog.Println("=======================")
r.infoLog.Println("Command Log:")

// 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
if reader, _, err := r.cli.CopyFromContainer(r.ctx, r.containerResponse.ID, "/shell_command_output.log"); err == nil {
io.Copy(os.Stdout, reader)
}
r.infoLog.Println("=======================")
color.Unset()

io.Copy(os.Stdout, out)
r.cli.ContainerKill(r.ctx, r.containerResponse.ID, "KILL")

if err := r.stopCurrentContainer(); err != nil {
return err
Expand All @@ -206,7 +218,9 @@ func (r runner) checkTheImageAvailable() (bool, error) {

for _, v := range images {
if r.currentJob.Image == v.RepoTags[0] {
color.Set(color.FgGreen)
r.infoLog.Println("Image is available")
color.Unset()
return true, nil
}
}
Expand All @@ -215,7 +229,9 @@ func (r runner) checkTheImageAvailable() (bool, error) {
}

func (r runner) pullImage() error {
color.Set(color.FgBlue)
r.infoLog.Printf("Image pulling: %s", r.currentJob.Image)
color.Unset()

reader, err := r.cli.ImagePull(r.ctx, r.currentJob.Image, types.ImagePullOptions{})

Expand Down Expand Up @@ -328,25 +344,29 @@ func (r runner) sendShellCommandFile() error {
}

func (r runner) stopCurrentContainer() error {
color.Set(color.FgBlue)
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")
color.Unset()

return nil
}

func (r runner) removeCurrentContainer() error {
color.Set(color.FgBlue)
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")
color.Unset()

return nil
}

0 comments on commit 8640586

Please sign in to comment.