Merge pull request #591 from scrapinghub/docker-host

Use well known DOCKER_HOST syntax for Docker publish
This commit is contained in:
Brad Rydzewski 2014-10-20 19:27:22 -07:00
commit 45f7dfff30
2 changed files with 67 additions and 62 deletions

View file

@ -2,7 +2,6 @@ package publish
import ( import (
"fmt" "fmt"
"strconv"
"github.com/drone/drone/plugin/condition" "github.com/drone/drone/plugin/condition"
"github.com/drone/drone/shared/build/buildfile" "github.com/drone/drone/shared/build/buildfile"
@ -14,8 +13,8 @@ type Docker struct {
Dockerfile string `yaml:"docker_file"` Dockerfile string `yaml:"docker_file"`
// Connection information for the docker server that will build the image // Connection information for the docker server that will build the image
DockerServer string `yaml:"docker_server"` // Same format than DOCKER_HOST envvar, i.e.: tcp://172.16.1.1:2375
DockerServerPort int `yaml:"docker_port"` DockerHost string `yaml:"docker_host"`
// The Docker client version to download. This must match the docker version on the server // The Docker client version to download. This must match the docker version on the server
DockerVersion string `yaml:"docker_version"` DockerVersion string `yaml:"docker_version"`
@ -45,14 +44,10 @@ type Docker struct {
// 3. Push that docker image to index.docker.io. // 3. Push that docker image to index.docker.io.
// 4. Delete the docker image on the server it was build on so we conserve disk space. // 4. Delete the docker image on the server it was build on so we conserve disk space.
func (d *Docker) Write(f *buildfile.Buildfile) { func (d *Docker) Write(f *buildfile.Buildfile) {
if len(d.DockerServer) == 0 || d.DockerServerPort == 0 || len(d.DockerVersion) == 0 || if len(d.DockerHost) == 0 || len(d.DockerVersion) == 0 || len(d.ImageName) == 0 {
len(d.ImageName) == 0 {
f.WriteCmdSilent(`echo -e "Docker Plugin: Missing argument(s)\n\n"`) f.WriteCmdSilent(`echo -e "Docker Plugin: Missing argument(s)\n\n"`)
if len(d.DockerServer) == 0 { if len(d.DockerHost) == 0 {
f.WriteCmdSilent(`echo -e "\tdocker_server not defined in yaml"`) f.WriteCmdSilent(`echo -e "\tdocker_host not defined in yaml"`)
}
if d.DockerServerPort == 0 {
f.WriteCmdSilent(`echo -e "\tdocker_port not defined in yaml"`)
} }
if len(d.DockerVersion) == 0 { if len(d.DockerVersion) == 0 {
f.WriteCmdSilent(`echo -e "\tdocker_version not defined in yaml"`) f.WriteCmdSilent(`echo -e "\tdocker_version not defined in yaml"`)
@ -66,9 +61,6 @@ func (d *Docker) Write(f *buildfile.Buildfile) {
f.WriteCmd("wget -qO- https://get.docker.io/builds/Linux/x86_64/docker-" + f.WriteCmd("wget -qO- https://get.docker.io/builds/Linux/x86_64/docker-" +
d.DockerVersion + ".tgz |sudo tar zxf - -C /") d.DockerVersion + ".tgz |sudo tar zxf - -C /")
// Format our Build Server Endpoint
dockerServerUrl := d.DockerServer + ":" + strconv.Itoa(d.DockerServerPort)
dockerPath := "." dockerPath := "."
if len(d.Dockerfile) != 0 { if len(d.Dockerfile) != 0 {
dockerPath = fmt.Sprintf("- < %s", d.Dockerfile) dockerPath = fmt.Sprintf("- < %s", d.Dockerfile)
@ -82,36 +74,29 @@ func (d *Docker) Write(f *buildfile.Buildfile) {
} else { } else {
imageTag = "$(git rev-parse --short HEAD)" imageTag = "$(git rev-parse --short HEAD)"
} }
f.WriteCmd(fmt.Sprintf("docker -H %s build -t %s:%s %s", dockerServerUrl, d.ImageName, imageTag, dockerPath)) f.WriteCmd("export DOCKER_HOST=" + d.DockerHost)
f.WriteCmd(fmt.Sprintf("docker build -t %s:%s %s", d.ImageName, imageTag, dockerPath))
// Login? // Login?
if d.RegistryLogin == true { if d.RegistryLogin == true {
// Are we logging in to a custom Registry? f.WriteCmdSilent(fmt.Sprintf("docker login -u %s -p %s -e %s %s",
if len(d.RegistryLoginUrl) > 0 { d.Username, d.Password, d.Email, d.RegistryLoginUrl))
f.WriteCmdSilent(fmt.Sprintf("docker -H %s login -u %s -p %s -e %s %s",
dockerServerUrl, d.Username, d.Password, d.Email, d.RegistryLoginUrl))
} else {
// Assume index.docker.io
f.WriteCmdSilent(fmt.Sprintf("docker -H %s login -u %s -p %s -e %s",
dockerServerUrl, d.Username, d.Password, d.Email))
}
} }
// Push the tagged image only - Do not push all image tags
f.WriteCmd(fmt.Sprintf("docker push %s:%s", d.ImageName, imageTag))
// Are we overriding the "latest" tag? // Are we overriding the "latest" tag?
if d.PushLatest { if d.PushLatest {
f.WriteCmd(fmt.Sprintf("docker -H %s tag %s:%s %s:latest", f.WriteCmd(fmt.Sprintf("docker tag %s:%s %s:latest",
dockerServerUrl, d.ImageName, imageTag, d.ImageName)) d.ImageName, imageTag, d.ImageName))
f.WriteCmd(fmt.Sprintf("docker push %s:latest", d.ImageName))
} }
f.WriteCmd(fmt.Sprintf("docker -H %s push %s", dockerServerUrl, d.ImageName))
// Delete the image from the docker server we built on. // Delete the image from the docker server we built on.
if !d.KeepBuild { if !d.KeepBuild {
f.WriteCmd(fmt.Sprintf("docker -H %s rmi %s:%s", f.WriteCmd(fmt.Sprintf("docker rmi %s:%s", d.ImageName, imageTag))
dockerServerUrl, d.ImageName, imageTag))
if d.PushLatest { if d.PushLatest {
f.WriteCmd(fmt.Sprintf("docker -H %s rmi %s:latest", f.WriteCmd(fmt.Sprintf("docker rmi %s:latest", d.ImageName))
dockerServerUrl, d.ImageName))
} }
} }
} }

View file

@ -24,25 +24,48 @@ func setUpWithDrone(input string) (string, error) {
return bf.String(), err return bf.String(), err
} }
// DockerHost and version test (no auth)
var dockerHostYaml = `
publish:
docker:
docker_host: tcp://server:1000
docker_version: 1.3.0
image_name: registry/image
`
func TestDockerHost(t *testing.T) {
response, err := setUpWithDrone(dockerHostYaml)
t.Log(privateRegistryNoAuthYaml)
if err != nil {
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
}
expected := "export DOCKER_HOST=tcp://server:1000"
if !strings.Contains(response, expected) {
t.Fatalf("Response: " + response + " doesn't export correct " +
"DOCKER_HOST envvar: expected " + expected + "\n\n")
}
expected = "https://get.docker.io/builds/Linux/x86_64/docker-1.3.0.tgz"
if !strings.Contains(response, expected) {
t.Fatalf("Response: " + response + " doesn't download from:" + expected + "\n\n")
}
}
// Private Registry Test (no auth) // Private Registry Test (no auth)
var privateRegistryNoAuthYaml = ` var privateRegistryNoAuthYaml = `
publish: publish:
docker: docker:
dockerfile: file_path dockerfile: file_path
docker_server: server docker_host: tcp://server:1000
docker_port: 1000
docker_version: 1.0 docker_version: 1.0
registry_login: false registry_login: false
image_name: registry/image image_name: registry/image
` `
func TestPrivateRegistryNoAuth(t *testing.T) { func TestPrivateRegistryNoAuth(t *testing.T) {
response, err := setUpWithDrone(privateRegistryNoAuthYaml) response, err := setUpWithDrone(privateRegistryNoAuthYaml)
t.Log(privateRegistryNoAuthYaml) t.Log(privateRegistryNoAuthYaml)
if err != nil { if err != nil {
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error()) t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
} }
if !strings.Contains(response, "docker -H server:1000 build -t registry/image:$(git rev-parse --short HEAD)") { if !strings.Contains(response, "docker build -t registry/image:$(git rev-parse --short HEAD)") {
t.Fatalf("Response: " + response + " doesn't contain registry in image-names: expected registry/image\n\n") t.Fatalf("Response: " + response + " doesn't contain registry in image-names: expected registry/image\n\n")
} }
} }
@ -52,8 +75,7 @@ var privateRegistryAuthYaml = `
publish: publish:
docker: docker:
dockerfile: file_path dockerfile: file_path
docker_server: server docker_host: tcp://server:1000
docker_port: 1000
docker_version: 1.0 docker_version: 1.0
registry_login_url: https://registry:8000/v1/ registry_login_url: https://registry:8000/v1/
registry_login: true registry_login: true
@ -69,12 +91,12 @@ func TestPrivateRegistryAuth(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error()) t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
} }
if !strings.Contains(response, "docker -H server:1000 login -u username -p password -e email@example.com https://registry:8000/v1/") { if !strings.Contains(response, "docker login -u username -p password -e email@example.com https://registry:8000/v1/") {
t.Log("\n\n\n\ndocker -H server:1000 login -u username -p xxxxxxxx -e email@example.com https://registry:8000/v1/\n\n\n\n") t.Log("\n\n\n\ndocker login -u username -p xxxxxxxx -e email@example.com https://registry:8000/v1/\n\n\n\n")
t.Fatalf("Response: " + response + " doesn't contain private registry login\n\n") t.Fatalf("Response: " + response + " doesn't contain private registry login\n\n")
} }
if !strings.Contains(response, "docker -H server:1000 build -t registry/image:$(git rev-parse --short HEAD) .") { if !strings.Contains(response, "docker build -t registry/image:$(git rev-parse --short HEAD) .") {
t.Log("docker -H server:1000 build -t registry/image:$(git rev-parse --short HEAD) .") t.Log("docker build -t registry/image:$(git rev-parse --short HEAD) .")
t.Fatalf("Response: " + response + " doesn't contain registry in image-names\n\n") t.Fatalf("Response: " + response + " doesn't contain registry in image-names\n\n")
} }
} }
@ -83,8 +105,7 @@ func TestPrivateRegistryAuth(t *testing.T) {
var overrideLatestTagYaml = ` var overrideLatestTagYaml = `
publish: publish:
docker: docker:
docker_server: server docker_host: tcp://server:1000
docker_port: 1000
docker_version: 1.0 docker_version: 1.0
username: username username: username
password: password password: password
@ -99,20 +120,22 @@ func TestOverrideLatestTag(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error()) t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
} }
if !strings.Contains(response, "docker -H server:1000 build -t username/image:$(git rev-parse --short HEAD) .") { if !strings.Contains(response, "docker build -t username/image:$(git rev-parse --short HEAD) .") {
t.Fatalf("Response: " + response + " doesn't contain the git-ref tagged image\n\n") t.Fatalf("Response: " + response + " doesn't contain the git-ref tagged image\n\n")
} }
if !strings.Contains(response, "docker -H server:1000 tag username/image:$(git rev-parse --short HEAD) username/image:latest") { if !strings.Contains(response, "docker tag username/image:$(git rev-parse --short HEAD) username/image:latest") {
t.Fatalf("Response: " + response + " doesn't contain 'latest' tag command\n\n") t.Fatalf("Response: " + response + " doesn't contain 'latest' tag command\n\n")
} }
if !strings.Contains(response, "docker push username/image:latest") {
t.Fatalf("Response: " + response + " doesn't contain push command\n\n")
}
} }
// Keep builds Test // Keep builds Test
var keepBuildsYaml = ` var keepBuildsYaml = `
publish: publish:
docker: docker:
docker_server: server docker_host: tcp://server:1000
docker_port: 1000
docker_version: 1.0 docker_version: 1.0
keep_build: true keep_build: true
username: username username: username
@ -127,7 +150,7 @@ func TestKeepBuilds(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error()) t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
} }
if strings.Contains(response, "docker -H server:1000 rmi") { if strings.Contains(response, "docker rmi") {
t.Fatalf("Response: " + response + " incorrectly instructs the docker server to remove the builds when it shouldn't\n\n") t.Fatalf("Response: " + response + " incorrectly instructs the docker server to remove the builds when it shouldn't\n\n")
} }
} }
@ -136,8 +159,7 @@ func TestKeepBuilds(t *testing.T) {
var customTagYaml = ` var customTagYaml = `
publish: publish:
docker: docker:
docker_server: server docker_host: tcp://server:1000
docker_port: 1000
docker_version: 1.0 docker_version: 1.0
custom_tag: release-0.1 custom_tag: release-0.1
username: username username: username
@ -155,10 +177,10 @@ func TestCustomTag(t *testing.T) {
if strings.Contains(response, "$(git rev-parse --short HEAD)") { if strings.Contains(response, "$(git rev-parse --short HEAD)") {
t.Fatalf("Response: " + response + " is tagging images from git-refs when it should use a custom tag\n\n") t.Fatalf("Response: " + response + " is tagging images from git-refs when it should use a custom tag\n\n")
} }
if !strings.Contains(response, "docker -H server:1000 build -t username/image:release-0.1") { if !strings.Contains(response, "docker build -t username/image:release-0.1") {
t.Fatalf("Response: " + response + " isn't tagging images using our custom tag\n\n") t.Fatalf("Response: " + response + " isn't tagging images using our custom tag\n\n")
} }
if !strings.Contains(response, "docker -H server:1000 push username/image") { if !strings.Contains(response, "docker push username/image:release-0.1") {
t.Fatalf("Response: " + response + " doesn't push the custom tagged image\n\n") t.Fatalf("Response: " + response + " doesn't push the custom tagged image\n\n")
} }
} }
@ -184,8 +206,7 @@ var validYaml = `
publish: publish:
docker: docker:
docker_file: file_path docker_file: file_path
docker_server: server docker_host: tcp://server:1000
docker_port: 1000
docker_version: 1.0 docker_version: 1.0
username: user username: user
password: password password: password
@ -202,19 +223,19 @@ func TestValidYaml(t *testing.T) {
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error()) t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
} }
if !strings.Contains(response, "docker -H server:1000 tag user/image:$(git rev-parse --short HEAD) user/image:latest") { if !strings.Contains(response, "docker tag user/image:$(git rev-parse --short HEAD) user/image:latest") {
t.Fatalf("Response: " + response + " doesn't contain tag command for latest\n\n") t.Fatalf("Response: " + response + " doesn't contain tag command for latest\n\n")
} }
if !strings.Contains(response, "docker -H server:1000 build -t user/image:$(git rev-parse --short HEAD) - <") { if !strings.Contains(response, "docker build -t user/image:$(git rev-parse --short HEAD) - <") {
t.Fatalf("Response: " + response + "doesn't contain build command for commit hash\n\n") t.Fatalf("Response: " + response + "doesn't contain build command for commit hash\n\n")
} }
if !strings.Contains(response, "docker -H server:1000 login -u user -p password -e email") { if !strings.Contains(response, "docker login -u user -p password -e email") {
t.Fatalf("Response: " + response + " doesn't contain login command\n\n") t.Fatalf("Response: " + response + " doesn't contain login command\n\n")
} }
if !strings.Contains(response, "docker -H server:1000 push user/image") { if !strings.Contains(response, "docker push user/image:$(git rev-parse --short HEAD)") {
t.Fatalf("Response: " + response + " doesn't contain push command\n\n") t.Fatalf("Response: " + response + " doesn't contain push command\n\n")
} }
if !strings.Contains(response, "docker -H server:1000 rmi user/image:"+ if !strings.Contains(response, "docker rmi user/image:"+
"$(git rev-parse --short HEAD)") { "$(git rev-parse --short HEAD)") {
t.Fatalf("Response: " + response + " doesn't contain remove image command\n\n") t.Fatalf("Response: " + response + " doesn't contain remove image command\n\n")
} }
@ -223,8 +244,7 @@ func TestValidYaml(t *testing.T) {
var withoutDockerFileYaml = ` var withoutDockerFileYaml = `
publish: publish:
docker: docker:
docker_server: server docker_host: tcp://server:1000
docker_port: 1000
docker_version: 1.0 docker_version: 1.0
image_name: user/image image_name: user/image
username: user username: user
@ -239,7 +259,7 @@ func TestWithoutDockerFile(t *testing.T) {
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error()) t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
} }
if !strings.Contains(response, "docker -H server:1000 build -t user/image:$(git rev-parse --short HEAD) .") { if !strings.Contains(response, "docker build -t user/image:$(git rev-parse --short HEAD) .") {
t.Fatalf("Response: " + response + " doesn't contain build command\n\n") t.Fatalf("Response: " + response + " doesn't contain build command\n\n")
} }
} }