diff --git a/builder/docker/builder.go b/builder/docker/builder.go index f8c3832e9..a4659219d 100644 --- a/builder/docker/builder.go +++ b/builder/docker/builder.go @@ -35,6 +35,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &StepPull{}, &StepRun{}, &StepProvision{}, + &StepCommit{}, &StepExport{}, } @@ -64,8 +65,17 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe return nil, rawErr.(error) } + var artifact packer.Artifact // No errors, must've worked - artifact := &ExportArtifact{path: b.config.ExportPath} + if b.config.Export { + artifact = &ExportArtifact{path: b.config.ExportPath} + } else { + artifact = &ImportArtifact{ + IdValue: state.Get("image_id").(string), + BuilderIdValue: "packer.post-processor.docker-import", + Driver: driver, + } + } return artifact, nil } diff --git a/builder/docker/config.go b/builder/docker/config.go index 045bf19b8..1045ff26a 100644 --- a/builder/docker/config.go +++ b/builder/docker/config.go @@ -10,6 +10,7 @@ type Config struct { common.PackerConfig `mapstructure:",squash"` ExportPath string `mapstructure:"export_path"` + Export bool Image string Pull bool RunCommand []string `mapstructure:"run_command"` @@ -71,10 +72,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { } } - if c.ExportPath == "" { - errs = packer.MultiErrorAppend(errs, - fmt.Errorf("export_path must be specified")) - } + c.Export = c.ExportPath != "" if c.Image == "" { errs = packer.MultiErrorAppend(errs, diff --git a/builder/docker/config_test.go b/builder/docker/config_test.go index 3b279ab22..ea65d8114 100644 --- a/builder/docker/config_test.go +++ b/builder/docker/config_test.go @@ -46,13 +46,19 @@ func TestConfigPrepare_exportPath(t *testing.T) { // No export path delete(raw, "export_path") - _, warns, errs := NewConfig(raw) - testConfigErr(t, warns, errs) + c, warns, errs := NewConfig(raw) + testConfigOk(t, warns, errs) + if c.Export { + t.Fatal("should not export") + } // Good export path raw["export_path"] = "good" - _, warns, errs = NewConfig(raw) + c, warns, errs = NewConfig(raw) testConfigOk(t, warns, errs) + if !c.Export { + t.Fatal("should export") + } } func TestConfigPrepare_image(t *testing.T) { diff --git a/builder/docker/driver.go b/builder/docker/driver.go index aaed2fc52..f0cf55821 100644 --- a/builder/docker/driver.go +++ b/builder/docker/driver.go @@ -8,6 +8,9 @@ import ( // Docker. The Driver interface also allows the steps to be tested since // a mock driver can be shimmed in. type Driver interface { + // Commit the container to a tag + Commit(id string) (string, error) + // Delete an image that is imported into Docker DeleteImage(id string) error diff --git a/builder/docker/driver_docker.go b/builder/docker/driver_docker.go index 5c43e0078..c9b1557e9 100644 --- a/builder/docker/driver_docker.go +++ b/builder/docker/driver_docker.go @@ -35,6 +35,23 @@ func (d *DockerDriver) DeleteImage(id string) error { return nil } +func (d *DockerDriver) Commit(id string) (string, error) { + var stdout bytes.Buffer + cmd := exec.Command("docker", "commit", id) + cmd.Stdout = &stdout + + if err := cmd.Start(); err != nil { + return "", err + } + + if err := cmd.Wait(); err != nil { + err = fmt.Errorf("Error committing container: %s", err) + return "", err + } + + return strings.TrimSpace(stdout.String()), nil +} + func (d *DockerDriver) Export(id string, dst io.Writer) error { var stderr bytes.Buffer cmd := exec.Command("docker", "export", id) diff --git a/builder/docker/driver_mock.go b/builder/docker/driver_mock.go index a48bb99f8..e45c34e88 100644 --- a/builder/docker/driver_mock.go +++ b/builder/docker/driver_mock.go @@ -6,6 +6,11 @@ import ( // MockDriver is a driver implementation that can be used for tests. type MockDriver struct { + CommitCalled bool + CommitContainerId string + CommitImageId string + CommitErr error + DeleteImageCalled bool DeleteImageId string DeleteImageErr error @@ -39,6 +44,12 @@ type MockDriver struct { VerifyCalled bool } +func (d *MockDriver) Commit(id string) (string, error) { + d.CommitCalled = true + d.CommitContainerId = id + return d.CommitImageId, d.CommitErr +} + func (d *MockDriver) DeleteImage(id string) error { d.DeleteImageCalled = true d.DeleteImageId = id diff --git a/builder/docker/step_commit.go b/builder/docker/step_commit.go new file mode 100644 index 000000000..8b6489495 --- /dev/null +++ b/builder/docker/step_commit.go @@ -0,0 +1,40 @@ +package docker + +import ( + "fmt" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" +) + +// StepCommit commits the container to a image. +type StepCommit struct { + imageId string +} + +func (s *StepCommit) Run(state multistep.StateBag) multistep.StepAction { + config := state.Get("config").(*Config) + driver := state.Get("driver").(Driver) + containerId := state.Get("container_id").(string) + ui := state.Get("ui").(packer.Ui) + + if config.Export { + return multistep.ActionContinue + } + + ui.Say("Committing the container") + imageId, err := driver.Commit(containerId) + if err != nil { + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + + // Save the container ID + s.imageId = imageId + state.Put("image_id", s.imageId) + ui.Message(fmt.Sprintf("Image ID: %s", s.imageId)) + + return multistep.ActionContinue +} + +func (s *StepCommit) Cleanup(state multistep.StateBag) {} diff --git a/builder/docker/step_commit_test.go b/builder/docker/step_commit_test.go new file mode 100644 index 000000000..6e1520dea --- /dev/null +++ b/builder/docker/step_commit_test.go @@ -0,0 +1,95 @@ +package docker + +import ( + "errors" + "github.com/mitchellh/multistep" + "testing" +) + +func testStepCommitState(t *testing.T) multistep.StateBag { + state := testState(t) + state.Put("container_id", "foo") + return state +} + +func TestStepCommit_impl(t *testing.T) { + var _ multistep.Step = new(StepCommit) +} + +func TestStepCommit(t *testing.T) { + state := testStepCommitState(t) + step := new(StepCommit) + defer step.Cleanup(state) + + config := state.Get("config").(*Config) + config.Export = false + driver := state.Get("driver").(*MockDriver) + driver.CommitImageId = "bar" + + // run the step + if action := step.Run(state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + + // verify we did the right thing + if !driver.CommitCalled { + t.Fatal("should've called") + } + + // verify the ID is saved + idRaw, ok := state.GetOk("image_id") + if !ok { + t.Fatal("should've saved ID") + } + + id := idRaw.(string) + if id != driver.CommitImageId { + t.Fatalf("bad: %#v", id) + } +} + +func TestStepCommit_skip(t *testing.T) { + state := testStepCommitState(t) + step := new(StepCommit) + defer step.Cleanup(state) + + config := state.Get("config").(*Config) + config.Export = true + driver := state.Get("driver").(*MockDriver) + + // run the step + if action := step.Run(state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + + // verify we did the right thing + if driver.CommitCalled { + t.Fatal("shouldn't have called") + } + + // verify the ID is not saved + if _, ok := state.GetOk("image_id"); ok { + t.Fatal("shouldn't save image ID") + } +} + +func TestStepCommit_error(t *testing.T) { + state := testStepCommitState(t) + step := new(StepCommit) + defer step.Cleanup(state) + + config := state.Get("config").(*Config) + config.Export = false + driver := state.Get("driver").(*MockDriver) + driver.CommitErr = errors.New("foo") + + // run the step + if action := step.Run(state); action != multistep.ActionHalt { + t.Fatalf("bad action: %#v", action) + } + + // verify the ID is not saved + if _, ok := state.GetOk("image_id"); ok { + t.Fatal("shouldn't save image ID") + } +} diff --git a/builder/docker/step_export.go b/builder/docker/step_export.go index 69d6f483c..4530236f8 100644 --- a/builder/docker/step_export.go +++ b/builder/docker/step_export.go @@ -12,6 +12,11 @@ type StepExport struct{} func (s *StepExport) Run(state multistep.StateBag) multistep.StepAction { config := state.Get("config").(*Config) + + if !config.Export { + return multistep.ActionContinue + } + driver := state.Get("driver").(Driver) containerId := state.Get("container_id").(string) ui := state.Get("ui").(packer.Ui) diff --git a/builder/docker/step_export_test.go b/builder/docker/step_export_test.go index d07d547c2..d0232965b 100644 --- a/builder/docker/step_export_test.go +++ b/builder/docker/step_export_test.go @@ -34,6 +34,7 @@ func TestStepExport(t *testing.T) { config := state.Get("config").(*Config) config.ExportPath = tf.Name() + config.Export = true driver := state.Get("driver").(*MockDriver) driver.ExportReader = bytes.NewReader([]byte("data!")) @@ -61,6 +62,26 @@ func TestStepExport(t *testing.T) { } } +func TestStepExport_skip(t *testing.T) { + state := testStepExportState(t) + step := new(StepExport) + defer step.Cleanup(state) + + config := state.Get("config").(*Config) + config.Export = false + driver := state.Get("driver").(*MockDriver) + + // run the step + if action := step.Run(state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + + // verify we did the right thing + if driver.ExportCalled { + t.Fatal("shouldn't have exported") + } +} + func TestStepExport_error(t *testing.T) { state := testStepExportState(t) step := new(StepExport) @@ -79,6 +100,7 @@ func TestStepExport_error(t *testing.T) { config := state.Get("config").(*Config) config.ExportPath = tf.Name() + config.Export = true driver := state.Get("driver").(*MockDriver) driver.ExportError = errors.New("foo")