From a45c7fb0eadeb27df99877134951df33cc2582b7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 14 Jun 2013 12:22:19 -0700 Subject: [PATCH] packer: Add SetDebug to Build objects --- packer/build.go | 15 +++++++++++++++ packer/rpc/build.go | 11 +++++++++++ packer/rpc/build_test.go | 21 +++++++++++++++------ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/packer/build.go b/packer/build.go index 3f4bf8df0..bc166a8d2 100644 --- a/packer/build.go +++ b/packer/build.go @@ -21,6 +21,12 @@ type Build interface { // Cancel will cancel a running build. This will block until the build // is actually completely cancelled. Cancel() + + // SetDebug will enable/disable debug mode. Debug mode is always + // enabled by adding the additional key "packer_debug" to boolean + // true in the configuration of the various components. This must + // be called prior to Prepare. + SetDebug(bool) } // A build struct represents a single build job, the result of which should @@ -34,6 +40,7 @@ type coreBuild struct { hooks map[string][]Hook provisioners []coreBuildProvisioner + debug bool prepareCalled bool } @@ -103,6 +110,14 @@ func (b *coreBuild) Run(ui Ui, cache Cache) (Artifact, error) { return b.builder.Run(ui, hook, cache) } +func (b *coreBuild) SetDebug(val bool) { + if b.prepareCalled { + panic("prepare has already been called") + } + + b.debug = val +} + // Cancels the build if it is running. func (b *coreBuild) Cancel() { b.builder.Cancel() diff --git a/packer/rpc/build.go b/packer/rpc/build.go index 53577a542..bd33a0482 100644 --- a/packer/rpc/build.go +++ b/packer/rpc/build.go @@ -58,6 +58,12 @@ func (b *build) Run(ui packer.Ui, cache packer.Cache) (packer.Artifact, error) { return Artifact(client), nil } +func (b *build) SetDebug(val bool) { + if err := b.client.Call("Build.SetDebug", val, new(interface{})); err != nil { + panic(err) + } +} + func (b *build) Cancel() { if err := b.client.Call("Build.Cancel", new(interface{}), new(interface{})); err != nil { panic(err) @@ -93,6 +99,11 @@ func (b *BuildServer) Run(args *BuildRunArgs, reply *string) error { return nil } +func (b *BuildServer) SetDebug(val *bool, reply *interface{}) error { + b.build.SetDebug(*val) + 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 959f5d3ba..253f6c082 100644 --- a/packer/rpc/build_test.go +++ b/packer/rpc/build_test.go @@ -11,12 +11,13 @@ import ( var testBuildArtifact = &testArtifact{} type testBuild struct { - nameCalled bool - prepareCalled bool - runCalled bool - runCache packer.Cache - runUi packer.Ui - cancelCalled bool + nameCalled bool + prepareCalled bool + runCalled bool + runCache packer.Cache + runUi packer.Ui + setDebugCalled bool + cancelCalled bool errRunResult bool } @@ -43,6 +44,10 @@ func (b *testBuild) Run(ui packer.Ui, cache packer.Cache) (packer.Artifact, erro } } +func (b *testBuild) SetDebug(bool) { + b.setDebugCalled = true +} + func (b *testBuild) Cancel() { b.cancelCalled = true } @@ -93,6 +98,10 @@ func TestBuildRPC(t *testing.T) { _, err = bClient.Run(ui, cache) assert.NotNil(err, "should not nil") + // Test SetDebug + bClient.SetDebug(true) + assert.True(b.setDebugCalled, "should be called") + // Test Cancel bClient.Cancel() assert.True(b.cancelCalled, "cancel should be called")