Support force option for docker-tag.

This commit is contained in:
FGtatsuro 2015-04-18 13:12:28 +09:00
parent 419f3271f1
commit 09f379a928
5 changed files with 57 additions and 5 deletions

View File

@ -44,7 +44,7 @@ type Driver interface {
StopContainer(id string) error
// TagImage tags the image with the given ID
TagImage(id string, repo string) error
TagImage(id string, repo string, force bool) error
// Verify verifies that the driver can run
Verify() error

View File

@ -235,9 +235,15 @@ func (d *DockerDriver) StopContainer(id string) error {
return exec.Command("docker", "rm", id).Run()
}
func (d *DockerDriver) TagImage(id string, repo string) error {
func (d *DockerDriver) TagImage(id string, repo string, force bool) error {
args := []string{"tag"}
if force {
args = append(args, "-f")
}
args = append(args, id, repo)
var stderr bytes.Buffer
cmd := exec.Command("docker", "tag", id, repo)
cmd := exec.Command("docker", args...)
cmd.Stderr = &stderr
if err := cmd.Start(); err != nil {

View File

@ -44,6 +44,7 @@ type MockDriver struct {
TagImageCalled bool
TagImageImageId string
TagImageRepo string
TagImageForce bool
TagImageErr error
ExportReader io.Reader
@ -151,10 +152,11 @@ func (d *MockDriver) StopContainer(id string) error {
return d.StopError
}
func (d *MockDriver) TagImage(id string, repo string) error {
func (d *MockDriver) TagImage(id string, repo string, force bool) error {
d.TagImageCalled = true
d.TagImageImageId = id
d.TagImageRepo = repo
d.TagImageForce = force
return d.TagImageErr
}

View File

@ -15,6 +15,7 @@ type Config struct {
Repository string `mapstructure:"repository"`
Tag string `mapstructure:"tag"`
Force bool
tpl *packer.ConfigTemplate
}
@ -87,7 +88,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
ui.Message("Tagging image: " + artifact.Id())
ui.Message("Repository: " + importRepo)
err := driver.TagImage(artifact.Id(), importRepo)
err := driver.TagImage(artifact.Id(), importRepo, p.config.Force)
if err != nil {
return nil, false, err
}

View File

@ -69,4 +69,47 @@ func TestPostProcessor_PostProcess(t *testing.T) {
if driver.TagImageRepo != "foo:bar" {
t.Fatal("bad repo")
}
if driver.TagImageForce {
t.Fatal("bad force. force=false in default")
}
}
func TestPostProcessor_PostProcess_Force(t *testing.T) {
driver := &docker.MockDriver{}
p := &PostProcessor{Driver: driver}
config := testConfig()
config["force"] = true
_, err := common.DecodeConfig(&p.config, config)
if err != nil {
t.Fatalf("err %s", err)
}
artifact := &packer.MockArtifact{
BuilderIdValue: dockerimport.BuilderId,
IdValue: "1234567890abcdef",
}
result, keep, err := p.PostProcess(testUi(), artifact)
if _, ok := result.(packer.Artifact); !ok {
t.Fatal("should be instance of Artifact")
}
if !keep {
t.Fatal("should keep")
}
if err != nil {
t.Fatalf("err: %s", err)
}
if !driver.TagImageCalled {
t.Fatal("should call TagImage")
}
if driver.TagImageImageId != "1234567890abcdef" {
t.Fatal("bad image id")
}
if driver.TagImageRepo != "foo:bar" {
t.Fatal("bad repo")
}
if !driver.TagImageForce {
t.Fatal("bad force")
}
}