Merge pull request #2055 from FGtatsuro/docker_tag_force

Support force option for docker-tag.
This commit is contained in:
Mitchell Hashimoto 2015-05-29 16:05:00 -07:00
commit 350aa4e522
5 changed files with 57 additions and 5 deletions

View File

@ -46,7 +46,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

@ -240,9 +240,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

@ -46,6 +46,7 @@ type MockDriver struct {
TagImageCalled bool
TagImageImageId string
TagImageRepo string
TagImageForce bool
TagImageErr error
ExportReader io.Reader
@ -156,10 +157,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

@ -18,6 +18,7 @@ type Config struct {
Repository string `mapstructure:"repository"`
Tag string `mapstructure:"tag"`
Force bool
ctx interpolate.Context
}
@ -64,7 +65,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

@ -68,4 +68,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")
}
}