From 55c4cb57a830d8620b0e8787783aaa0cc42c7d8b Mon Sep 17 00:00:00 2001 From: bashbunni Date: Wed, 17 Aug 2022 09:00:36 -0700 Subject: [PATCH 1/3] Clean up container when canary fails Signed-off-by: bashbunni --- internal/container/docker.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/container/docker.go b/internal/container/docker.go index 8687fff..5636c22 100644 --- a/internal/container/docker.go +++ b/internal/container/docker.go @@ -64,12 +64,14 @@ func (c *DockerContainer) Start() error { return err } if info.State.Status == "exited" { + c.Remove() return errors.New("container failed to start") } if info.State.Running { break } if time.Since(startTime) > (time.Second * 10) { + c.Remove() return errors.New("container failed to start after 10 seconds") } time.Sleep(time.Second) From 1dc267f21161c022394147bd80a8cd5780127273 Mon Sep 17 00:00:00 2001 From: bashbunni Date: Wed, 17 Aug 2022 09:45:18 -0700 Subject: [PATCH 2/3] Add errors for docker issues Signed-off-by: bashbunni --- cmd/validate.go | 7 ++++++- internal/container/docker.go | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/cmd/validate.go b/cmd/validate.go index c0ca613..982f80e 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" + "github.com/nvidia/container-canary/internal/container" "github.com/nvidia/container-canary/internal/validator" "github.com/spf13/cobra" ) @@ -31,7 +32,7 @@ var validateCmd = &cobra.Command{ Long: ``, Args: imageArg, SilenceUsage: true, - SilenceErrors: true, + SilenceErrors: false, RunE: func(cmd *cobra.Command, args []string) error { file, err := cmd.Flags().GetString("file") if err != nil { @@ -68,6 +69,10 @@ func imageArg(cmd *cobra.Command, args []string) error { return errors.New("too many arguments") } + if err := container.CheckForDocker(); err != nil { + return err + } + image := args[0] if validator.CheckImage(cmd, image, "docker") { diff --git a/internal/container/docker.go b/internal/container/docker.go index 5636c22..8a58a80 100644 --- a/internal/container/docker.go +++ b/internal/container/docker.go @@ -52,10 +52,10 @@ func (c *DockerContainer) Start() error { commandArgs = append(commandArgs, c.Command...) } - _, err := exec.Command("docker", commandArgs...).Output() - if err != nil && strings.Contains(err.Error(), "executable file not found ") { - return errors.New("unable to find 'docker' on the PATH, please ensure Docker is installed and running (you can check this by running 'docker info')") + if err := CheckForDocker(); err != nil { + return err } + _, err := exec.Command("docker", commandArgs...).Output() c.runCommand = fmt.Sprintf("docker %s", strings.Join(commandArgs, " ")) for startTime := time.Now(); ; { @@ -84,6 +84,17 @@ func (c *DockerContainer) Start() error { return nil } +func CheckForDocker() error { + if _, err := exec.LookPath("docker"); err != nil { + return errors.New("Docker is missing") + } + + if _, err := exec.Command("docker", "ps").Output(); err != nil { + return errors.New("Docker requires root privileges to run") + } + return nil +} + // Remove a container func (c DockerContainer) Remove() error { _, err := exec.Command("docker", "rm", "-f", c.Name).Output() From 249f88b4a0dc62391d5f221edfa41ad732756bbe Mon Sep 17 00:00:00 2001 From: bashbunni Date: Wed, 17 Aug 2022 09:45:37 -0700 Subject: [PATCH 3/3] Update errors package Signed-off-by: bashbunni --- go.mod | 1 + go.sum | 1 + 2 files changed, 2 insertions(+) diff --git a/go.mod b/go.mod index d5ed706..26ffc1f 100644 --- a/go.mod +++ b/go.mod @@ -32,6 +32,7 @@ require ( github.com/muesli/cancelreader v0.2.1 // indirect github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/spf13/pflag v1.0.5 // indirect diff --git a/go.sum b/go.sum index 6fc5d85..43bc7b9 100644 --- a/go.sum +++ b/go.sum @@ -360,6 +360,7 @@ github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144T github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=