From 0f354c79d17e244a4d00731ab55ab08e28f219bb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 18 Jun 2013 16:01:14 -0700 Subject: [PATCH] packer: Add Destroy method to artifact [GH-18] --- builder/amazonebs/artifact.go | 5 +++++ builder/digitalocean/artifact.go | 5 +++++ builder/vmware/artifact.go | 9 ++++++++- packer/artifact.go | 5 +++++ packer/artifact_test.go | 4 ++++ packer/rpc/artifact.go | 19 +++++++++++++++++++ packer/rpc/artifact_test.go | 4 ++++ 7 files changed, 50 insertions(+), 1 deletion(-) diff --git a/builder/amazonebs/artifact.go b/builder/amazonebs/artifact.go index cab22538d..f68a0da3c 100644 --- a/builder/amazonebs/artifact.go +++ b/builder/amazonebs/artifact.go @@ -1,6 +1,7 @@ package amazonebs import ( + "errors" "fmt" "strings" ) @@ -33,3 +34,7 @@ func (a *artifact) String() string { return fmt.Sprintf("AMIs were created:\n\n%s", strings.Join(amiStrings, "\n")) } + +func (a *artifact) Destroy() error { + return errors.New("not implemented yet") +} diff --git a/builder/digitalocean/artifact.go b/builder/digitalocean/artifact.go index 7afa35c48..ddd9cdfa3 100644 --- a/builder/digitalocean/artifact.go +++ b/builder/digitalocean/artifact.go @@ -1,6 +1,7 @@ package digitalocean import ( + "errors" "fmt" ) @@ -25,3 +26,7 @@ func (a *Artifact) Id() string { func (a *Artifact) String() string { return fmt.Sprintf("A snapshot was created: %v", a.snapshotName) } + +func (a *Artifact) Destroy() error { + return errors.New("not implemented yet") +} diff --git a/builder/vmware/artifact.go b/builder/vmware/artifact.go index 6ebbf748c..379b969b2 100644 --- a/builder/vmware/artifact.go +++ b/builder/vmware/artifact.go @@ -1,6 +1,9 @@ package vmware -import "fmt" +import ( + "fmt" + "os" +) // Artifact is the result of running the VMware builder, namely a set // of files associated with the resulting machine. @@ -24,3 +27,7 @@ func (*Artifact) Id() string { func (a *Artifact) String() string { return fmt.Sprintf("VM files in directory: %s", a.dir) } + +func (a *Artifact) Destroy() error { + return os.RemoveAll(a.dir) +} diff --git a/packer/artifact.go b/packer/artifact.go index 7d349a3d5..486c322f6 100644 --- a/packer/artifact.go +++ b/packer/artifact.go @@ -24,4 +24,9 @@ type Artifact interface { // Returns human-readable output that describes the artifact created. // This is used for UI output. It can be multiple lines. String() string + + // Destroy deletes the artifact. Packer calls this for various reasons, + // such as if a post-processor has processed this artifact and it is + // no longer needed. + Destroy() error } diff --git a/packer/artifact_test.go b/packer/artifact_test.go index 8051c6fe7..9c16db6c1 100644 --- a/packer/artifact_test.go +++ b/packer/artifact_test.go @@ -17,3 +17,7 @@ func (*TestArtifact) Id() string { func (*TestArtifact) String() string { return "string" } + +func (*TestArtifact) Destroy() error { + return nil +} diff --git a/packer/rpc/artifact.go b/packer/rpc/artifact.go index 64b0ef3da..d71f8831a 100644 --- a/packer/rpc/artifact.go +++ b/packer/rpc/artifact.go @@ -41,6 +41,15 @@ func (a *artifact) String() (result string) { return } +func (a *artifact) Destroy() error { + var result error + if err := a.client.Call("Artifact.Destroy", new(interface{}), &result); err != nil { + return err + } + + return result +} + func (s *ArtifactServer) BuilderId(args *interface{}, reply *string) error { *reply = s.artifact.BuilderId() return nil @@ -60,3 +69,13 @@ func (s *ArtifactServer) String(args *interface{}, reply *string) error { *reply = s.artifact.String() return nil } + +func (s *ArtifactServer) Destroy(args *interface{}, reply *error) error { + err := s.artifact.Destroy() + if err != nil { + err = NewBasicError(err) + } + + *reply = err + return nil +} diff --git a/packer/rpc/artifact_test.go b/packer/rpc/artifact_test.go index e665d384f..bc2f88a19 100644 --- a/packer/rpc/artifact_test.go +++ b/packer/rpc/artifact_test.go @@ -25,6 +25,10 @@ func (testArtifact) String() string { return "string" } +func (testArtifact) Destroy() error { + return nil +} + func TestArtifactRPC(t *testing.T) { assert := asserts.NewTestingAsserts(t, true)