From 079d6f4d4392bd911d4cc7f76b8b48fbfd90e7b7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 3 Jun 2013 14:44:34 -0700 Subject: [PATCH] packer: Introduce Cancel() method to Builder --- builder/amazonebs/builder.go | 3 +++ packer/builder.go | 17 ++++++++++------- packer/builder_test.go | 5 +++++ packer/plugin/builder.go | 3 +++ packer/plugin/builder_test.go | 2 ++ packer/rpc/builder.go | 4 ++++ packer/rpc/builder_test.go | 5 +++++ packer/ui_test.go | 2 +- 8 files changed, 33 insertions(+), 8 deletions(-) diff --git a/builder/amazonebs/builder.go b/builder/amazonebs/builder.go index 032135001..69c337d16 100644 --- a/builder/amazonebs/builder.go +++ b/builder/amazonebs/builder.go @@ -102,3 +102,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact { // Build the artifact and return it return &artifact{state["amis"].(map[string]string)} } + +func (b *Builder) Cancel() { +} diff --git a/packer/builder.go b/packer/builder.go index e48eea0a4..341c77247 100644 --- a/packer/builder.go +++ b/packer/builder.go @@ -2,14 +2,17 @@ package packer // Implementers of Builder are responsible for actually building images // on some platform given some configuration. -// -// Prepare is responsible for reading in some configuration, in the raw form -// of map[string]interface{}, and storing that state for use later. Any setup -// should be done in this method. Note that NO side effects should really take -// place in prepare. It is meant as a state setup step only. -// -// Run is where the actual build should take place. It takes a Build and a Ui. type Builder interface { + // Prepare is responsible for reading in some configuration, in the raw form + // of map[string]interface{}, and storing that state for use later. Any setup + // should be done in this method. Note that NO side effects should really take + // place in prepare. It is meant as a state setup step only. Prepare(config interface{}) error + + // Run is where the actual build should take place. It takes a Build and a Ui. Run(ui Ui, hook Hook) Artifact + + // Cancel cancels a possibly running Builder. This should block until + // the builder actually cancels and cleans up after itself. + Cancel() } diff --git a/packer/builder_test.go b/packer/builder_test.go index 9ddacad11..e6fba7997 100644 --- a/packer/builder_test.go +++ b/packer/builder_test.go @@ -6,6 +6,7 @@ type TestBuilder struct { runCalled bool runHook Hook runUi Ui + cancelCalled bool } func (tb *TestBuilder) Prepare(config interface{}) error { @@ -20,3 +21,7 @@ func (tb *TestBuilder) Run(ui Ui, h Hook) Artifact { tb.runUi = ui return nil } + +func (tb *TestBuilder) Cancel() { + tb.cancelCalled = true +} diff --git a/packer/plugin/builder.go b/packer/plugin/builder.go index d020167e3..b1a6855e5 100644 --- a/packer/plugin/builder.go +++ b/packer/plugin/builder.go @@ -31,6 +31,9 @@ func (b *cmdBuilder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact { return b.builder.Run(ui, hook) } +func (b *cmdBuilder) Cancel() { +} + func (c *cmdBuilder) checkExit(p interface{}, cb func()) { if c.client.Exited() && cb != nil { cb() diff --git a/packer/plugin/builder_test.go b/packer/plugin/builder_test.go index 460bc7e4e..43ebd6e56 100644 --- a/packer/plugin/builder_test.go +++ b/packer/plugin/builder_test.go @@ -17,6 +17,8 @@ func (helperBuilder) Run(packer.Ui, packer.Hook) packer.Artifact { return nil } +func (helperBuilder) Cancel() {} + func TestBuilder_NoExist(t *testing.T) { assert := asserts.NewTestingAsserts(t, true) diff --git a/packer/rpc/builder.go b/packer/rpc/builder.go index 52ccdac9c..7f961137c 100644 --- a/packer/rpc/builder.go +++ b/packer/rpc/builder.go @@ -60,6 +60,10 @@ func (b *builder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact { return Artifact(client) } +func (b *builder) Cancel() { + +} + func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *error) error { err := b.builder.Prepare(args.Config) if err != nil { diff --git a/packer/rpc/builder_test.go b/packer/rpc/builder_test.go index cd91f402f..790161acf 100644 --- a/packer/rpc/builder_test.go +++ b/packer/rpc/builder_test.go @@ -15,6 +15,7 @@ type testBuilder struct { runCalled bool runHook packer.Hook runUi packer.Ui + cancelCalled bool } func (b *testBuilder) Prepare(config interface{}) error { @@ -30,6 +31,10 @@ func (b *testBuilder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact { return testBuilderArtifact } +func (b *testBuilder) Cancel() { + b.cancelCalled = true +} + func TestBuilderRPC(t *testing.T) { assert := asserts.NewTestingAsserts(t, true) diff --git a/packer/ui_test.go b/packer/ui_test.go index 9f84af489..1b7f3c7ae 100644 --- a/packer/ui_test.go +++ b/packer/ui_test.go @@ -20,7 +20,7 @@ func TestColoredUi(t *testing.T) { ui := &ColoredUi{UiColorRed, bufferUi} ui.Say("foo") - assert.Equal(readWriter(bufferUi), "\033[0;31;40mfoo\033[0m\n", "should have color") + assert.Equal(readWriter(bufferUi), "\033[1;31;40mfoo\033[0m\n", "should have color") } func TestPrefixedUi(t *testing.T) {