Commit 1baa63f0 authored by Andy Thompson's avatar Andy Thompson

Add a docker commit step to the docker builder that runs instead of export if...

Add a docker commit step to the docker builder that runs instead of export if export_path not present
parent 8b24d990
......@@ -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
}
......
......@@ -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,
......
......@@ -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) {
......
......@@ -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
......
......@@ -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)
......
......@@ -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
......
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) {}
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")
}
}
......@@ -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)
......
......@@ -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")
......
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