Merge pull request #2328 from jefferai/f-simplify-images

Put the image parsing code (mostly) back to how it was before
This commit is contained in:
Paul Hinze 2015-06-12 16:31:50 -05:00
commit 7bda3e6551
1 changed files with 27 additions and 20 deletions

View File

@ -3,7 +3,6 @@ package docker
import ( import (
"fmt" "fmt"
"strings" "strings"
"regexp"
dc "github.com/fsouza/go-dockerclient" dc "github.com/fsouza/go-dockerclient"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
@ -76,28 +75,36 @@ func pullImage(data *Data, client *dc.Client, image string) error {
pullOpts := dc.PullImageOptions{} pullOpts := dc.PullImageOptions{}
// splitImageName := strings.Split(image, ":")
// Breaks apart an image string into host:port, repo, and tag components switch len(splitImageName) {
regex := "^(?:(?P<host>(?:[\\w-]+(?:\\.[\\w-]+)+)(?::[\\d]+)?)/)?(?P<repo>[\\w.-]+(?:/[\\w.-]*)*)*(?::(?P<tag>[\\w.-]*))?"
r, _ := regexp.Compile(regex)
// Result is in form [[image, host, repo, tag]], so we get the head of the // It's in registry:port/username/repo:tag or registry:port/repo:tag format
// outer list to pass the inner list to result case 3:
result := r.FindAllStringSubmatch(image, -1)[0] splitPortRepo := strings.Split(splitImageName[1], "/")
pullOpts.Registry = splitImageName[0] + ":" + splitPortRepo[0]
pullOpts.Tag = splitImageName[2]
pullOpts.Repository = strings.Join(splitPortRepo[1:], "/")
// If the host is not an empty string, then the image is using a private registry // It's either registry:port/username/repo, registry:port/repo,
if (result[1] != "") { // or repo:tag with default registry
pullOpts.Registry = result[1] case 2:
// The repository for a private registry should take the form of host/repo rather than just repo splitPortRepo := strings.Split(splitImageName[1], "/")
pullOpts.Repository = result[1] + "/" + result[2] switch len(splitPortRepo) {
} else if (result[2] != "") { // repo:tag
// Local registries, or the main docker registry will have an image named as just 'repo' case 1:
pullOpts.Repository = result[2] pullOpts.Repository = splitImageName[0]
} pullOpts.Tag = splitImageName[1]
// If there was a tag specified, then set it // registry:port/username/repo or registry:port/repo
if (result[3] != "") { default:
pullOpts.Tag = result[3] pullOpts.Registry = splitImageName[0] + ":" + splitPortRepo[0]
pullOpts.Repository = strings.Join(splitPortRepo[1:], "/")
pullOpts.Tag = "latest"
}
// Plain username/repo or repo
default:
pullOpts.Repository = image
} }
if err := client.PullImage(pullOpts, dc.AuthConfiguration{}); err != nil { if err := client.PullImage(pullOpts, dc.AuthConfiguration{}); err != nil {