diff --git a/apps/distgo/cmd/artifacts/artifacts.go b/apps/distgo/cmd/artifacts/artifacts.go index 6119deb5..f94fd74f 100644 --- a/apps/distgo/cmd/artifacts/artifacts.go +++ b/apps/distgo/cmd/artifacts/artifacts.go @@ -15,6 +15,7 @@ package artifacts import ( + "fmt" "path/filepath" "strconv" @@ -25,6 +26,19 @@ import ( "github.com/palantir/godel/apps/distgo/pkg/osarch" ) +// DockerArtifacts returns a map from product name to a slice that contains all of the Docker repository:tag labels +// defined for the products. +func DockerArtifacts(buildSpecsWithDeps []params.ProductBuildSpecWithDeps) map[string][]string { + output := make(map[string][]string) + for _, spec := range buildSpecsWithDeps { + for _, currImage := range spec.Spec.DockerImages { + imageName := fmt.Sprint(currImage.Repository, ":", currImage.Tag) + output[spec.Spec.ProductName] = append(output[spec.Spec.ProductName], imageName) + } + } + return output +} + // DistArtifacts returns a map from product name to OrderedStringSliceMap, where the values of the OrderedStringSliceMap // contains the mapping from the String representation of the index of the dist type ("0", "1", etc.) to the paths for // the artifact for that type. diff --git a/apps/distgo/cmd/artifacts/artifacts_test.go b/apps/distgo/cmd/artifacts/artifacts_test.go index 77ec78a5..0b053686 100644 --- a/apps/distgo/cmd/artifacts/artifacts_test.go +++ b/apps/distgo/cmd/artifacts/artifacts_test.go @@ -366,6 +366,90 @@ func TestDistArtifacts(t *testing.T) { } } +func TestDockerArtifacts(t *testing.T) { + tmpDir, cleanup, err := dirs.TempDir("", "") + defer cleanup() + require.NoError(t, err) + + for i, currCase := range []struct { + specs func(projectDir string) []params.ProductBuildSpecWithDeps + want map[string][]string + }{ + { + specs: func(projectDir string) []params.ProductBuildSpecWithDeps { + return []params.ProductBuildSpecWithDeps{} + }, + want: map[string][]string{}, + }, + { + specs: func(projectDir string) []params.ProductBuildSpecWithDeps { + return []params.ProductBuildSpecWithDeps{ + createSpecWithDockerImages(projectDir, "foo", "0.1.0", nil, + params.DockerImage{ + Repository: "foo/foo", + Tag: "snapshot", + }, + ), + } + }, + want: map[string][]string{ + "foo": {"foo/foo:snapshot"}, + }, + }, + { + specs: func(projectDir string) []params.ProductBuildSpecWithDeps { + return []params.ProductBuildSpecWithDeps{ + createSpecWithDockerImages(projectDir, "foo", "0.1.0", nil, + params.DockerImage{ + Repository: "foo/foo", + Tag: "snapshot", + }, + ), + createSpecWithDockerImages(projectDir, "bar", "0.1.0", nil, + params.DockerImage{ + Repository: "bar/bar", + Tag: "snapshot", + }, + ), + } + }, + want: map[string][]string{ + "foo": {"foo/foo:snapshot"}, + "bar": {"bar/bar:snapshot"}, + }, + }, + { + specs: func(projectDir string) []params.ProductBuildSpecWithDeps { + return []params.ProductBuildSpecWithDeps{ + createSpecWithDockerImages(projectDir, "foo", "0.1.0", nil, + params.DockerImage{ + Repository: "foo/foo", + Tag: "snapshot", + }, + params.DockerImage{ + Repository: "foo/foo", + Tag: "release", + }, + ), + } + }, + want: map[string][]string{ + "foo": { + "foo/foo:snapshot", + "foo/foo:release", + }, + }, + }, + } { + currProjectDir, err := ioutil.TempDir(tmpDir, "") + require.NoError(t, err) + + got := artifacts.DockerArtifacts(currCase.specs(currProjectDir)) + require.NoError(t, err, "Case %d", i) + assert.Equal(t, currCase.want, got, "Case %d", i) + } +} + func toAbs(input map[string][]string, baseDir string) map[string][]string { absWant := make(map[string][]string, len(input)) for k, v := range input { @@ -398,6 +482,23 @@ func createSpecWithDists(projectDir, productName, productVersion string, osArchs } } +func createSpecWithDockerImages(projectDir, productName, productVersion string, osArchs []osarch.OSArch, images ...params.DockerImage) params.ProductBuildSpecWithDeps { + return params.ProductBuildSpecWithDeps{ + Spec: params.ProductBuildSpec{ + Product: params.Product{ + Build: params.Build{ + OutputDir: "build", + OSArchs: osArchs, + }, + DockerImages: images, + }, + ProjectDir: projectDir, + ProductName: productName, + ProductVersion: productVersion, + }, + } +} + func toMap(input map[string]artifacts.OrderedStringSliceMap) map[string][]string { output := make(map[string][]string, len(input)) for product, m := range input { diff --git a/apps/distgo/cmd/artifacts/cmd.go b/apps/distgo/cmd/artifacts/cmd.go index 84ae9a47..88b7aad5 100644 --- a/apps/distgo/cmd/artifacts/cmd.go +++ b/apps/distgo/cmd/artifacts/cmd.go @@ -49,6 +49,7 @@ func Command() cli.Command { Subcommands: []cli.Command{ buildArtifactsCommand("build", "Print the paths to the build artifacts for products"), artifactsCommand("dist", "Print the paths to the distribution artifacts for products", distArtifactsAction), + artifactsCommand("docker", "Print the labels for the Docker tags for products", dockerArtifactsAction), }, } } @@ -120,3 +121,13 @@ func buildArtifactsAction(ctx cli.Context, specs []params.ProductBuildSpecWithDe func distArtifactsAction(ctx cli.Context, specs []params.ProductBuildSpecWithDeps, absPath bool) (map[string]OrderedStringSliceMap, error) { return DistArtifacts(specs, absPath) } + +func dockerArtifactsAction(ctx cli.Context, specs []params.ProductBuildSpecWithDeps, absPath bool) (map[string]OrderedStringSliceMap, error) { + artifacts := make(map[string]OrderedStringSliceMap) + for k, v := range DockerArtifacts(specs) { + ordered := newOrderedStringSliceMap() + ordered.PutValues(k, v) + artifacts[k] = ordered + } + return artifacts, nil +} diff --git a/apps/distgo/cmd/docker/products.go b/apps/distgo/cmd/docker/products.go index 439efa06..4b4e87b2 100644 --- a/apps/distgo/cmd/docker/products.go +++ b/apps/distgo/cmd/docker/products.go @@ -1,3 +1,17 @@ +// Copyright 2016 Palantir Technologies, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package docker import ( diff --git a/apps/distgo/cmd/docker/products_test.go b/apps/distgo/cmd/docker/products_test.go index 9ff5ecd2..3377e515 100644 --- a/apps/distgo/cmd/docker/products_test.go +++ b/apps/distgo/cmd/docker/products_test.go @@ -1,3 +1,17 @@ +// Copyright 2016 Palantir Technologies, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package docker import (