From 30fd526766b1695ef4e96bb7752d2cd9e4eaac76 Mon Sep 17 00:00:00 2001 From: Chris Bednarski Date: Fri, 25 Sep 2015 23:55:01 -0700 Subject: [PATCH] Use env.Get, remove sha256 checks, and change from regexp to strconv to test for int --- client/driver/docker.go | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/client/driver/docker.go b/client/driver/docker.go index 06f1bac96..1ae1f9b1d 100644 --- a/client/driver/docker.go +++ b/client/driver/docker.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "log" - "regexp" "strconv" "strings" @@ -14,11 +13,6 @@ import ( "github.com/hashicorp/nomad/nomad/structs" ) -var ( - reDockerSha = regexp.MustCompile("^[a-f0-9]{64}$") - reNumeric = regexp.MustCompile("^[0-9]+$") -) - type DockerDriver struct { DriverContext } @@ -50,15 +44,14 @@ func (d *DockerDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool return false, nil } - node.Attributes["driver.docker"] = "true" - env, err := client.Version() - for _, item := range *env { - parts := strings.Split(item, "=") - if parts[0] == "Version" { - node.Attributes["driver.docker.version"] = parts[1] - } + if err != nil { + // We connected to the daemon but couldn't read the version so something + // is broken. + return false, err } + node.Attributes["driver.docker"] = "true" + node.Attributes["driver.docker.version"] = env.Get("Version") return true, nil } @@ -132,7 +125,7 @@ func createContainer(ctx *ExecContext, task *structs.Task, logger *log.Logger) d // Otherwise we'll setup a direct 1:1 mapping from the host port to // the container, and assume that the process inside will read the // environment variable and bind to the correct port. - if reNumeric.MatchString(label) { + if _, err := strconv.Atoi(label); err == nil { dockerPorts[docker.Port(label+"/tcp")] = []docker.PortBinding{docker.PortBinding{HostIP: network.IP, HostPort: strconv.Itoa(port)}} dockerPorts[docker.Port(label+"/udp")] = []docker.PortBinding{docker.PortBinding{HostIP: network.IP, HostPort: strconv.Itoa(port)}} logger.Printf("[DEBUG] driver.docker: allocated port %s:%d -> %s (mapped)", network.IP, port, label) @@ -221,12 +214,6 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle return nil, fmt.Errorf("Failed to determine image id for `%s`: %s", image, err) } } - - // Sanity check - if !reDockerSha.MatchString(dockerImage.ID) { - return nil, fmt.Errorf("Image id not in expected format (sha256); found %s", dockerImage.ID) - } - d.logger.Printf("[DEBUG] driver.docker: using image %s", dockerImage.ID) d.logger.Printf("[INFO] driver.docker: identified image %s as %s", image, dockerImage.ID) @@ -236,11 +223,6 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle d.logger.Printf("[ERR] driver.docker: %s", err) return nil, fmt.Errorf("Failed to create container from image %s", image) } - - // Sanity check - if !reDockerSha.MatchString(container.ID) { - return nil, fmt.Errorf("Container id not in expected format (sha256); found %s", container.ID) - } d.logger.Printf("[INFO] driver.docker: created container %s", container.ID) // Start the container