diff --git a/pkg/build/util.go b/pkg/build/util.go index eb96530b3..3829e99ea 100644 --- a/pkg/build/util.go +++ b/pkg/build/util.go @@ -5,6 +5,7 @@ import ( "crypto/sha1" "fmt" "io" + "strings" ) // createUID is a helper function that will @@ -26,3 +27,57 @@ func createRandom() []byte { } return k } + +// list of service aliases and their full, canonical names +var defaultServices = map[string]string{ + "cassandra": "relateiq/cassandra:latest", + "couchdb": "bradrydzewski/couchdb:1.5", + "elasticsearch": "bradrydzewski/elasticsearch:0.90", + "memcached": "bradrydzewski/memcached", + "mongodb": "bradrydzewski/mongodb:2.4", + "mysql": "bradrydzewski/mysql:5.5", + "neo4j": "bradrydzewski/neo4j:1.9", + "postgres": "bradrydzewski/postgres:9.1", + "redis": "bradrydzewski/redis:2.8", + "rabbitmq": "bradrydzewski/rabbitmq:3.2", + "riak": "guillermo/riak:latest", + "zookeeper": "jplock/zookeeper:3.4.5", +} + +// parseImageName parses a Docker image name, in the format owner/name:tag, +// and returns each segment. +// +// If the owner is blank, it is assumed to be an official drone image, +// and will be prefixed with the appropriate owner name. +// +// If the tag is empty, it is assumed to be the latest version. +func parseImageName(image string) (owner, name, tag string) { + owner = "bradrydzewski" // this will eventually change to drone + name = image + tag = "latest" + + // first we check to see if the image name is an alias + // for a known service. + // + // TODO I'm not a huge fan of this code here. Maybe it + // should get handled when the yaml is parsed, and + // convert the image and service names in the yaml + // to fully qualified names? + if cname, ok := defaultServices[image]; ok { + name = cname + } + + parts := strings.Split(name, "/") + if len(parts) == 2 { + owner = parts[0] + name = parts[1] + } + + parts = strings.Split(name, ":") + if len(parts) == 2 { + name = parts[0] + tag = parts[1] + } + + return +} diff --git a/pkg/build/util_test.go b/pkg/build/util_test.go new file mode 100644 index 000000000..ae19b154c --- /dev/null +++ b/pkg/build/util_test.go @@ -0,0 +1,36 @@ +package build + +import "testing" + +func TestParseImageName(t *testing.T) { + images := []struct { + owner string + name string + tag string + cname string + }{ + // full image name with all 3 sections present + {"johnsmith", "redis", "2.8", "johnsmith/redis:2.8"}, + // image name with no tag specified + {"johnsmith", "redis", "latest", "johnsmith/redis"}, + // image name with no owner specified + {"bradrydzewski", "redis", "2.8", "redis:2.8"}, + // image name with ownly name specified + {"bradrydzewski", "redis2", "latest", "redis2"}, + // image name that is a known alias + {"relateiq", "cassandra", "latest", "cassandra"}, + } + + for _, img := range images { + owner, name, tag := parseImageName(img.cname) + if owner != img.owner { + t.Errorf("Expected image %s with owner %s, got %s", img.cname, img.owner, owner) + } + if name != img.name { + t.Errorf("Expected image %s with name %s, got %s", img.cname, img.name, name) + } + if tag != img.tag { + t.Errorf("Expected image %s with tag %s, got %s", img.cname, img.tag, tag) + } + } +}