Skip to content

Commit

Permalink
dockerstatus: use CommandTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
langston-barrett committed May 31, 2016
1 parent 224175c commit b5f085c
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions dockerstatus/dockerstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit b5f085c

Please sign in to comment.