diff --git a/packer/build.go b/packer/build.go index be1bf05b9..7a1723da4 100644 --- a/packer/build.go +++ b/packer/build.go @@ -8,6 +8,7 @@ type Build interface { Name() string Prepare(Ui) error Run(Ui) Artifact + Cancel() } // A build struct represents a single build job, the result of which should @@ -87,3 +88,7 @@ func (b *coreBuild) Run(ui Ui) Artifact { hook := &DispatchHook{hooks} return b.builder.Run(ui, hook) } + +// Cancels the build if it is running. +func (b *coreBuild) Cancel() { +} diff --git a/packer/rpc/build.go b/packer/rpc/build.go index 189defe75..e8f0efed5 100644 --- a/packer/rpc/build.go +++ b/packer/rpc/build.go @@ -68,6 +68,12 @@ func (b *build) Run(ui packer.Ui) packer.Artifact { return Artifact(client) } +func (b *build) Cancel() { + if err := b.client.Call("Build.Cancel", new(interface{}), new(interface{})); err != nil { + panic(err) + } +} + func (b *BuildServer) Name(args *interface{}, reply *string) error { *reply = b.build.Name() return nil @@ -98,3 +104,8 @@ func (b *BuildServer) Run(args *BuildRunArgs, reply *string) error { *reply = serveSingleConn(server) return nil } + +func (b *BuildServer) Cancel(args *interface{}, reply *interface{}) error { + b.build.Cancel() + return nil +} diff --git a/packer/rpc/build_test.go b/packer/rpc/build_test.go index 371093e6f..387c187e4 100644 --- a/packer/rpc/build_test.go +++ b/packer/rpc/build_test.go @@ -15,6 +15,7 @@ type testBuild struct { prepareUi packer.Ui runCalled bool runUi packer.Ui + cancelCalled bool } func (b *testBuild) Name() string { @@ -34,6 +35,10 @@ func (b *testBuild) Run(ui packer.Ui) packer.Artifact { return testBuildArtifact } +func (b *testBuild) Cancel() { + b.cancelCalled = true +} + func TestBuildRPC(t *testing.T) { assert := asserts.NewTestingAsserts(t, true) @@ -70,6 +75,10 @@ func TestBuildRPC(t *testing.T) { assert.True(ui.sayCalled, "say should be called") assert.Equal(ui.sayMessage, "format", "message should be correct") } + + // Test Cancel + bClient.Cancel() + assert.True(b.cancelCalled, "cancel should be called") } func TestBuild_ImplementsBuild(t *testing.T) {