From bee879409af06d577b30b6df3ccf1b96fc05b1c7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 25 Dec 2013 11:27:53 -0700 Subject: [PATCH] builder/vmware: new artifact type that is common --- builder/vmware/common/artifact.go | 61 ++++++++++++++++++++++++++ builder/vmware/common/artifact_test.go | 43 ++++++++++++++++++ builder/vmware/iso/artifact.go | 5 +-- builder/vmware/iso/builder.go | 5 +-- builder/vmware/iso/output_dir.go | 4 ++ 5 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 builder/vmware/common/artifact.go create mode 100644 builder/vmware/common/artifact_test.go diff --git a/builder/vmware/common/artifact.go b/builder/vmware/common/artifact.go new file mode 100644 index 000000000..ddc2b1a35 --- /dev/null +++ b/builder/vmware/common/artifact.go @@ -0,0 +1,61 @@ +package common + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/mitchellh/packer/packer" +) + +// BuilderId for the local artifacts +const BuilderId = "mitchellh.vmware" + +// Artifact is the result of running the VMware builder, namely a set +// of files associated with the resulting machine. +type localArtifact struct { + dir string + f []string +} + +// NewLocalArtifact returns a VMware artifact containing the files +// in the given directory. +func NewLocalArtifact(dir string) (packer.Artifact, error) { + files := make([]string, 0, 5) + visit := func(path string, info os.FileInfo, err error) error { + if !info.IsDir() { + files = append(files, path) + } + + return err + } + + if err := filepath.Walk(dir, visit); err != nil { + return nil, err + } + + return &localArtifact{ + dir: dir, + f: files, + }, nil +} + +func (a *localArtifact) BuilderId() string { + return BuilderId +} + +func (a *localArtifact) Files() []string { + return a.f +} + +func (*localArtifact) Id() string { + return "VM" +} + +func (a *localArtifact) String() string { + return fmt.Sprintf("VM files in directory: %s", a.dir) +} + +func (a *localArtifact) Destroy() error { + return os.RemoveAll(a.dir) +} diff --git a/builder/vmware/common/artifact_test.go b/builder/vmware/common/artifact_test.go new file mode 100644 index 000000000..a0379b82d --- /dev/null +++ b/builder/vmware/common/artifact_test.go @@ -0,0 +1,43 @@ +package common + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + + "github.com/mitchellh/packer/packer" +) + +func TestLocalArtifact_impl(t *testing.T) { + var _ packer.Artifact = new(localArtifact) +} + +func TestNewLocalArtifact(t *testing.T) { + td, err := ioutil.TempDir("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(td) + + err = ioutil.WriteFile(filepath.Join(td, "a"), []byte("foo"), 0644) + if err != nil { + t.Fatalf("err: %s", err) + } + + if err := os.Mkdir(filepath.Join(td, "b"), 0755); err != nil { + t.Fatalf("err: %s", err) + } + + a, err := NewLocalArtifact(td) + if err != nil { + t.Fatalf("err: %s", err) + } + + if a.BuilderId() != BuilderId { + t.Fatalf("bad: %#v", a.BuilderId()) + } + if len(a.Files()) != 1 { + t.Fatalf("should length 1: %d", len(a.Files())) + } +} diff --git a/builder/vmware/iso/artifact.go b/builder/vmware/iso/artifact.go index 56924c3df..d6ddf8921 100644 --- a/builder/vmware/iso/artifact.go +++ b/builder/vmware/iso/artifact.go @@ -2,14 +2,13 @@ package iso import ( "fmt" - "os" ) // Artifact is the result of running the VMware builder, namely a set // of files associated with the resulting machine. type Artifact struct { builderId string - dir string + dir OutputDir f []string } @@ -30,5 +29,5 @@ func (a *Artifact) String() string { } func (a *Artifact) Destroy() error { - return os.RemoveAll(a.dir) + return a.dir.RemoveAll() } diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 30209eb34..52bf48daa 100644 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -16,7 +16,6 @@ import ( "time" ) -const BuilderId = "mitchellh.vmware" const BuilderIdESX = "mitchellh.vmware-esx" type Builder struct { @@ -454,14 +453,14 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe } // Set the proper builder ID - builderId := BuilderId + builderId := vmwcommon.BuilderId if b.config.RemoteType != "" { builderId = BuilderIdESX } return &Artifact{ builderId: builderId, - dir: b.config.OutputDir, + dir: dir, f: files, }, nil } diff --git a/builder/vmware/iso/output_dir.go b/builder/vmware/iso/output_dir.go index 5013d6905..0c08a225f 100644 --- a/builder/vmware/iso/output_dir.go +++ b/builder/vmware/iso/output_dir.go @@ -60,3 +60,7 @@ func (d *localOutputDir) RemoveAll() error { func (d *localOutputDir) SetOutputDir(path string) { d.dir = path } + +func (d *localOutputDir) String() string { + return d.dir +}