Skip to content

Commit

Permalink
builder/docker: lock around Login
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Sep 5, 2014
1 parent e9611df commit 622b9f4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
5 changes: 3 additions & 2 deletions builder/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 16 additions & 3 deletions builder/docker/driver_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 622b9f4

Please sign in to comment.