Simplified endpoint login behaviour

This commit is contained in:
David Dyball 2014-08-09 02:16:40 +01:00
parent 39695c45bd
commit cf19824727
2 changed files with 32 additions and 105 deletions

View file

@ -3,7 +3,6 @@ package publish
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"strings"
"github.com/drone/drone/pkg/build/buildfile" "github.com/drone/drone/pkg/build/buildfile"
"github.com/drone/drone/pkg/build/repo" "github.com/drone/drone/pkg/build/repo"
@ -22,16 +21,9 @@ type Docker struct {
// Optional Arguments to allow finer-grained control of registry // Optional Arguments to allow finer-grained control of registry
// endpoints // endpoints
RegistryHost string `yaml:"registry_host"` RegistryLoginUrl string `yaml:"registry_login_url"`
RegistryProtocol string `yaml:"registry_protocol"`
RegistryPort int `yaml:"registry_port"`
RegistryLogin bool `yaml:"registry_login"`
RegistryLoginUri string `yaml:"registry_login_uri"`
// Allow setting Repo + Image names for delivery
// NOTE: RepoName is not compatible with private Registries
RepoName string `yaml:"repo_name"`
ImageName string `yaml:"image_name"` ImageName string `yaml:"image_name"`
RegistryLogin bool `yaml:"registry_login"`
// Authentication credentials for index.docker.io // Authentication credentials for index.docker.io
Username string `yaml:"username"` Username string `yaml:"username"`
@ -42,9 +34,8 @@ type Docker struct {
KeepBuild bool `yaml:"keep_build"` KeepBuild bool `yaml:"keep_build"`
// Do we want to override "latest" automatically with this build? // Do we want to override "latest" automatically with this build?
PushLatest bool `yaml:"push_latest"` PushLatest bool `yaml:"push_latest"`
CustomTag string `yaml:"custom_tag"`
Branch string `yaml:"branch,omitempty"` Branch string `yaml:"branch"`
Tag string `yaml:"custom_tag"`
} }
// Write adds commands to the buildfile to do the following: // Write adds commands to the buildfile to do the following:
@ -63,11 +54,6 @@ func (d *Docker) Write(f *buildfile.Buildfile, r *repo.Repo) {
return return
} }
if len(d.RepoName) > 0 && len(d.RegistryHost) > 0 {
f.WriteCmdSilent(`echo -e "Docker Plugin: Invalid Arguments Specified\n\n cannot combine repo_name and registry_host\n\t(It's not possible to host sub-repo's on private registries)\n"`)
return
}
// Ensure correct apt-get has the https method-driver as per (http://askubuntu.com/questions/165676/) // Ensure correct apt-get has the https method-driver as per (http://askubuntu.com/questions/165676/)
f.WriteCmd("sudo apt-get install apt-transport-https") f.WriteCmd("sudo apt-get install apt-transport-https")
@ -82,42 +68,6 @@ func (d *Docker) Write(f *buildfile.Buildfile, r *repo.Repo) {
// Format our Build Server Endpoint // Format our Build Server Endpoint
dockerServerUrl := d.DockerServer + ":" + strconv.Itoa(d.DockerServerPort) dockerServerUrl := d.DockerServer + ":" + strconv.Itoa(d.DockerServerPort)
// Construct Image BaseName
// e.g. "docker.mycompany.com/myimage" for private registries
// "myuser/myimage" for index.docker.io
imageBaseName := ""
if len(d.RegistryHost) > 0 {
imageBaseName = fmt.Sprintf("%s/%s",d.RegistryHost,d.ImageName)
} else {
if len(d.RepoName) > 0 {
imageBaseName = fmt.Sprintf("%s/%s",d.RepoName,d.ImageName)
} else {
imageBaseName = fmt.Sprintf("%s/%s",d.Username,d.ImageName)
}
}
registryLoginEndpoint := ""
// Gather information to build our Registry Endpoint for private registries
if len(d.RegistryHost) > 0 {
// Set Protocol
if len(d.RegistryProtocol) > 0 {
registryLoginEndpoint = fmt.Sprintf("%s://%s", d.RegistryProtocol,d.RegistryHost)
} else {
registryLoginEndpoint = fmt.Sprintf("http://%s", d.RegistryHost)
}
// Set Port
if d.RegistryPort > 0 {
registryLoginEndpoint = fmt.Sprintf("%s:%d",registryLoginEndpoint,d.RegistryPort)
}
// Set Login URI
if len(d.RegistryLoginUri) > 0 {
registryLoginEndpoint = fmt.Sprintf("%s/%s",registryLoginEndpoint,strings.TrimPrefix(d.RegistryLoginUri,"/"))
} else {
registryLoginEndpoint = fmt.Sprintf("%s/v1/",registryLoginEndpoint)
}
}
dockerPath := "." dockerPath := "."
if len(d.Dockerfile) != 0 { if len(d.Dockerfile) != 0 {
dockerPath = fmt.Sprintf("- < %s", d.Dockerfile) dockerPath = fmt.Sprintf("- < %s", d.Dockerfile)
@ -126,37 +76,41 @@ func (d *Docker) Write(f *buildfile.Buildfile, r *repo.Repo) {
// Run the command commands to build and deploy the image. // Run the command commands to build and deploy the image.
// Are we setting a custom tag, or do we use the git hash? // Are we setting a custom tag, or do we use the git hash?
imageTag := "" imageTag := ""
if len(d.Tag) > 0 { if len(d.CustomTag) > 0 {
imageTag = d.Tag imageTag = d.CustomTag
} 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, imageBaseName, imageTag, dockerPath)) f.WriteCmd(fmt.Sprintf("docker -H %s build -t %s:%s %s", dockerServerUrl, d.ImageName, imageTag, dockerPath))
// Login? // Login?
if len(d.RegistryHost) > 0 && d.RegistryLogin == true { if d.RegistryLogin == true {
f.WriteCmdSilent(fmt.Sprintf("docker -H %s login -u %s -p %s -e %s %s", // Are we logging in to a custom Registry?
dockerServerUrl, d.Username, d.Password, d.Email, registryLoginEndpoint)) if len(d.RegistryLoginUrl) > 0 {
} else if len(d.RegistryHost) == 0 { f.WriteCmdSilent(fmt.Sprintf("docker -H %s login -u %s -p %s -e %s %s",
// Assume that because no private registry is specified it requires auth dockerServerUrl, d.Username, d.Password, d.Email, d.RegistryLoginUrl))
// for index.docker.io } else {
f.WriteCmdSilent(fmt.Sprintf("docker -H %s login -u %s -p %s -e %s", // Assume index.docker.io
dockerServerUrl, d.Username, d.Password, d.Email)) f.WriteCmdSilent(fmt.Sprintf("docker -H %s login -u %s -p %s -e %s",
dockerServerUrl, d.Username, d.Password, d.Email))
}
} }
// 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 -H %s tag %s:%s %s:latest",
dockerServerUrl, imageBaseName, imageTag, imageBaseName)) dockerServerUrl, d.ImageName, imageTag, d.ImageName))
} }
f.WriteCmd(fmt.Sprintf("docker -H %s push %s", dockerServerUrl, imageBaseName)) 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 -H %s rmi %s:%s",
dockerServerUrl, imageBaseName, imageTag)) dockerServerUrl, d.ImageName, imageTag))
f.WriteCmd(fmt.Sprintf("docker -H %s rmi %s:latest", if d.PushLatest {
dockerServerUrl, imageBaseName)) f.WriteCmd(fmt.Sprintf("docker -H %s rmi %s:latest",
dockerServerUrl, d.ImageName))
}
} }
} }

View file

@ -24,30 +24,6 @@ func setUpWithDrone(input string) (string, error) {
return bf.String(), err return bf.String(), err
} }
// Private Registry + RepoName (invalid config)
var privateRegistryRepoNameYaml = `
publish:
docker:
docker_server: server
docker_port: 1000
docker_version: 1.0
registry_host: server
registry_login: false
repo_name: company
image_name: image
`
func TestPrivateRegistryRepoName(t *testing.T) {
response, err := setUpWithDrone(privateRegistryRepoNameYaml)
t.Log(privateRegistryRepoNameYaml)
if err != nil {
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
}
if !strings.Contains(response, "Docker Plugin: Invalid Arguments Specified") {
t.Fatalf("registry_host + repo_name should produce an invalid config error, it didn't")
}
}
// Private Registry Test (no auth) // Private Registry Test (no auth)
var privateRegistryNoAuthYaml = ` var privateRegistryNoAuthYaml = `
publish: publish:
@ -56,9 +32,8 @@ publish:
docker_server: server docker_server: server
docker_port: 1000 docker_port: 1000
docker_version: 1.0 docker_version: 1.0
registry_host: registry
registry_login: false registry_login: false
image_name: image image_name: registry/image
` `
func TestPrivateRegistryNoAuth(t *testing.T) { func TestPrivateRegistryNoAuth(t *testing.T) {
response, err := setUpWithDrone(privateRegistryNoAuthYaml) response, err := setUpWithDrone(privateRegistryNoAuthYaml)
@ -79,14 +54,12 @@ publish:
docker_server: server docker_server: server
docker_port: 1000 docker_port: 1000
docker_version: 1.0 docker_version: 1.0
registry_host: registry registry_login_url: https://registry:8000/v1/
registry_protocol: https
registry_port: 8000
registry_login: true registry_login: true
username: username username: username
password: password password: password
email: email@example.com email: email@example.com
image_name: image image_name: registry/image
` `
func TestPrivateRegistryAuth(t *testing.T) { func TestPrivateRegistryAuth(t *testing.T) {
response, err := setUpWithDrone(privateRegistryAuthYaml) response, err := setUpWithDrone(privateRegistryAuthYaml)
@ -114,7 +87,7 @@ publish:
username: username username: username
password: password password: password
email: email@example.com email: email@example.com
image_name: image image_name: username/image
push_latest: true push_latest: true
` `
func TestOverrideLatestTag(t *testing.T) { func TestOverrideLatestTag(t *testing.T) {
@ -166,7 +139,7 @@ publish:
username: username username: username
password: password password: password
email: email@example.com email: email@example.com
image_name: image image_name: username/image
` `
func TestCustomTag(t *testing.T) { func TestCustomTag(t *testing.T) {
response, err := setUpWithDrone(customTagYaml) response, err := setUpWithDrone(customTagYaml)
@ -209,12 +182,12 @@ publish:
docker_server: server docker_server: server
docker_port: 1000 docker_port: 1000
docker_version: 1.0 docker_version: 1.0
repo_base_name: base_repo
username: user username: user
password: password password: password
email: email email: email
image_name: image image_name: user/image
push_latest: true push_latest: true
registry_login: true
` `
func TestValidYaml(t *testing.T) { func TestValidYaml(t *testing.T) {
@ -248,7 +221,7 @@ publish:
docker_server: server docker_server: server
docker_port: 1000 docker_port: 1000
docker_version: 1.0 docker_version: 1.0
image_name: image image_name: user/image
username: user username: user
password: password password: password
email: email email: email