From 622b9f459a4fb02ffbcfaa6ce77b1ae1d15d1fd9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 5 Sep 2014 15:06:07 -0700 Subject: [PATCH] builder/docker: lock around Login --- builder/docker/driver.go | 5 +++-- builder/docker/driver_docker.go | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/builder/docker/driver.go b/builder/docker/driver.go index 1adc4e46901..6ecd4af02cc 100644 --- a/builder/docker/driver.go +++ b/builder/docker/driver.go @@ -20,10 +20,11 @@ type Driver interface { // Import imports a container from a tar file Import(path, repo string) (string, error) - // Login + // Login. This will lock the driver from performing another Login + // until Logout is called. Therefore, any users MUST call Logout. Login(repo, email, username, password string) error - // Logout + // Logout. This can only be called if Login succeeded. Logout(repo string) error // Pull should pull down the given image. diff --git a/builder/docker/driver_docker.go b/builder/docker/driver_docker.go index 26788104ab8..ab1c1637d01 100644 --- a/builder/docker/driver_docker.go +++ b/builder/docker/driver_docker.go @@ -3,17 +3,21 @@ package docker import ( "bytes" "fmt" - "github.com/mitchellh/packer/packer" "io" "log" "os" "os/exec" "strings" + "sync" + + "github.com/mitchellh/packer/packer" ) type DockerDriver struct { Ui packer.Ui Tpl *packer.ConfigTemplate + + l sync.Mutex } func (d *DockerDriver) DeleteImage(id string) error { @@ -110,6 +114,8 @@ func (d *DockerDriver) Import(path string, repo string) (string, error) { } func (d *DockerDriver) Login(repo, email, user, pass string) error { + d.l.Lock() + args := []string{"login"} if email != "" { args = append(args, "-e", email) @@ -125,7 +131,12 @@ func (d *DockerDriver) Login(repo, email, user, pass string) error { } cmd := exec.Command("docker", args...) - return runAndStream(cmd, d.Ui) + err := runAndStream(cmd, d.Ui) + if err != nil { + d.l.Unlock() + } + + return err } func (d *DockerDriver) Logout(repo string) error { @@ -135,7 +146,9 @@ func (d *DockerDriver) Logout(repo string) error { } cmd := exec.Command("docker", args...) - return runAndStream(cmd, d.Ui) + err := runAndStream(cmd, d.Ui) + d.l.Unlock() + return err } func (d *DockerDriver) Pull(image string) error {