Merge pull request #1818 from jwthomp/feature/image_name_parse

providers/docker: Support for more complexly named image repos
This commit is contained in:
Mitchell Hashimoto 2015-05-06 09:02:41 -07:00
commit d8485ef506
1 changed files with 20 additions and 26 deletions

View File

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