Commit 350aa4e5 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Merge pull request #2055 from FGtatsuro/docker_tag_force

Support force option for docker-tag.
parents b0c63ce8 09f379a9
...@@ -46,7 +46,7 @@ type Driver interface { ...@@ -46,7 +46,7 @@ type Driver interface {
StopContainer(id string) error StopContainer(id string) error
// TagImage tags the image with the given ID // 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 verifies that the driver can run
Verify() error Verify() error
......
...@@ -240,9 +240,15 @@ func (d *DockerDriver) StopContainer(id string) error { ...@@ -240,9 +240,15 @@ func (d *DockerDriver) StopContainer(id string) error {
return exec.Command("docker", "rm", id).Run() 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 var stderr bytes.Buffer
cmd := exec.Command("docker", "tag", id, repo) cmd := exec.Command("docker", args...)
cmd.Stderr = &stderr cmd.Stderr = &stderr
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
......
...@@ -46,6 +46,7 @@ type MockDriver struct { ...@@ -46,6 +46,7 @@ type MockDriver struct {
TagImageCalled bool TagImageCalled bool
TagImageImageId string TagImageImageId string
TagImageRepo string TagImageRepo string
TagImageForce bool
TagImageErr error TagImageErr error
ExportReader io.Reader ExportReader io.Reader
...@@ -156,10 +157,11 @@ func (d *MockDriver) StopContainer(id string) error { ...@@ -156,10 +157,11 @@ func (d *MockDriver) StopContainer(id string) error {
return d.StopError 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.TagImageCalled = true
d.TagImageImageId = id d.TagImageImageId = id
d.TagImageRepo = repo d.TagImageRepo = repo
d.TagImageForce = force
return d.TagImageErr return d.TagImageErr
} }
......
...@@ -18,6 +18,7 @@ type Config struct { ...@@ -18,6 +18,7 @@ type Config struct {
Repository string `mapstructure:"repository"` Repository string `mapstructure:"repository"`
Tag string `mapstructure:"tag"` Tag string `mapstructure:"tag"`
Force bool
ctx interpolate.Context ctx interpolate.Context
} }
...@@ -64,7 +65,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac ...@@ -64,7 +65,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
ui.Message("Tagging image: " + artifact.Id()) ui.Message("Tagging image: " + artifact.Id())
ui.Message("Repository: " + importRepo) ui.Message("Repository: " + importRepo)
err := driver.TagImage(artifact.Id(), importRepo) err := driver.TagImage(artifact.Id(), importRepo, p.config.Force)
if err != nil { if err != nil {
return nil, false, err return nil, false, err
} }
......
...@@ -68,4 +68,47 @@ func TestPostProcessor_PostProcess(t *testing.T) { ...@@ -68,4 +68,47 @@ func TestPostProcessor_PostProcess(t *testing.T) {
if driver.TagImageRepo != "foo:bar" { if driver.TagImageRepo != "foo:bar" {
t.Fatal("bad repo") 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")
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment