From b5f085c143cc333c024cd454581216069d69b10e Mon Sep 17 00:00:00 2001 From: Langston Barrett Date: Tue, 31 May 2016 15:51:28 +0100 Subject: [PATCH] dockerstatus: use CommandTimeout --- dockerstatus/dockerstatus.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/dockerstatus/dockerstatus.go b/dockerstatus/dockerstatus.go index 20e4040..d7dbba0 100644 --- a/dockerstatus/dockerstatus.go +++ b/dockerstatus/dockerstatus.go @@ -7,19 +7,23 @@ import ( "os/exec" "regexp" "strings" + "time" + "github.com/CiscoCloud/distributive/chkutil" "github.com/CiscoCloud/distributive/tabular" ) +var dockerCmdTimeout, _ = time.ParseDuration("10s") + // DockerImageRepositories returns a slice of the names of the Docker images // present on the host (what's under the REPOSITORIES column of `docker images`) func DockerImageRepositories() (images []string, err error) { cmd := exec.Command("docker", "images") - out, err := cmd.CombinedOutput() + out, err := chkutil.CommandTimeout(cmd, dockerCmdTimeout) if err != nil { // try escalating to sudo, the error might have been one of permissions cmd = exec.Command("sudo", "docker", "images") - out, err = cmd.CombinedOutput() + out, err = chkutil.CommandTimeout(cmd, dockerCmdTimeout) if err != nil { return images, err } @@ -50,16 +54,15 @@ func parseRunningContainers(output string) (containers []string) { func RunningContainers() (containers []string, err error) { outputFormat := `{{.Image}}\t{{.Status}}\t{{.Names}}` cmd := exec.Command("docker", "ps", "-a", "--format", outputFormat) - out, err := cmd.CombinedOutput() + out, err := chkutil.CommandTimeout(cmd, dockerCmdTimeout) if err != nil { cmd = exec.Command("sudo", "docker", "ps", "-a", "--format", outputFormat) - out, err = cmd.CombinedOutput() + out, err = chkutil.CommandTimeout(cmd, dockerCmdTimeout) if err != nil { - return containers, err + return []string{}, err } - } else if out == nil { - err = fmt.Errorf("The command %v produced no output", cmd.Args) - return containers, err + } else if out == "" { + return []string{}, fmt.Errorf("Command produced no output: %v", cmd.Args) } return parseRunningContainers(string(out)), nil }