Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve error handling #35

Merged
merged 3 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down Expand Up @@ -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") {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
19 changes: 16 additions & 3 deletions internal/container/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(); ; {
Expand All @@ -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)
Expand All @@ -82,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()
Expand Down