diff --git a/builder/vsphere/clone/builder.go b/builder/vsphere/clone/builder.go index 10b524b86..359b0fbf5 100644 --- a/builder/vsphere/clone/builder.go +++ b/builder/vsphere/clone/builder.go @@ -108,5 +108,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack VM: state.Get("vm").(*driver.VirtualMachine), StateData: map[string]interface{}{"generated_data": state.Get("generated_data")}, } + if b.config.Export != nil { + artifact.Outconfig = &b.config.Export.OutputDir + } + return artifact, nil } diff --git a/builder/vsphere/common/artifact.go b/builder/vsphere/common/artifact.go index 95d18abfa..d81a45a29 100644 --- a/builder/vsphere/common/artifact.go +++ b/builder/vsphere/common/artifact.go @@ -1,14 +1,17 @@ package common import ( + "os" + "github.com/hashicorp/packer/builder/vsphere/driver" ) const BuilderId = "jetbrains.vsphere" type Artifact struct { - Name string - VM *driver.VirtualMachine + Outconfig *OutputConfig + Name string + VM *driver.VirtualMachine // StateData should store data such as GeneratedData // to be shared with post-processors @@ -20,6 +23,10 @@ func (a *Artifact) BuilderId() string { } func (a *Artifact) Files() []string { + if a.Outconfig != nil { + files, _ := a.Outconfig.ListFiles() + return files + } return []string{} } @@ -36,5 +43,8 @@ func (a *Artifact) State(name string) interface{} { } func (a *Artifact) Destroy() error { + if a.Outconfig != nil { + os.RemoveAll(a.Outconfig.OutputDir) + } return a.VM.Destroy() } diff --git a/builder/vsphere/common/output_config.go b/builder/vsphere/common/output_config.go index 3a1538312..5708f7f9e 100644 --- a/builder/vsphere/common/output_config.go +++ b/builder/vsphere/common/output_config.go @@ -5,6 +5,8 @@ package common import ( "fmt" + "os" + "path/filepath" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/template/interpolate" @@ -28,3 +30,20 @@ func (c *OutputConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig return nil } + +// Stolen from output_dir_local.go in vmware builder. +func (c *OutputConfig) ListFiles() ([]string, error) { + files := make([]string, 0, 10) + + visit := func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() { + files = append(files, path) + } + return nil + } + + return files, filepath.Walk(c.OutputDir, visit) +} diff --git a/builder/vsphere/common/step_export.go b/builder/vsphere/common/step_export.go index 06cf21b86..1388f3d99 100644 --- a/builder/vsphere/common/step_export.go +++ b/builder/vsphere/common/step_export.go @@ -55,7 +55,8 @@ type ExportConfig struct { // include iso and img image files that are attached to the VM Images bool `mapstructure:"images"` // generate manifest using sha1, sha256, sha512. Defaults to 'sha256'. Use 'none' for no manifest. - Manifest string `mapstructure:"manifest"` + Manifest string `mapstructure:"manifest"` + // Directory on the computer running Packer to export files to OutputDir OutputConfig `mapstructure:",squash"` // Advanced ovf export options. Options can include: // * mac - MAC address is exported for all ethernet devices diff --git a/builder/vsphere/iso/builder.go b/builder/vsphere/iso/builder.go index 1a45d2066..8a4db95b6 100644 --- a/builder/vsphere/iso/builder.go +++ b/builder/vsphere/iso/builder.go @@ -153,10 +153,16 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack if _, ok := state.GetOk("vm"); !ok { return nil, nil } + artifact := &common.Artifact{ Name: b.config.VMName, VM: state.Get("vm").(*driver.VirtualMachine), StateData: map[string]interface{}{"generated_data": state.Get("generated_data")}, } + + if b.config.Export != nil { + artifact.Outconfig = &b.config.Export.OutputDir + } + return artifact, nil } diff --git a/website/pages/docs/post-processors/vsphere-template.mdx b/website/pages/docs/post-processors/vsphere-template.mdx index 75d040e4f..f4a52da5f 100644 --- a/website/pages/docs/post-processors/vsphere-template.mdx +++ b/website/pages/docs/post-processors/vsphere-template.mdx @@ -1,13 +1,9 @@ --- description: > The Packer vSphere Template post-processor takes an artifact from the - VMware-iso builder, built on ESXi (i.e. remote) or an artifact from the - vSphere - - post-processor and allows to mark a VM as a template and leaving it in a path - - of choice. + [vSphere](/docs/post-processors/vsphere) post-processor, marks the VM as a + template, and leaves it in the path of your choice. layout: docs page_title: vSphere Template - Post-Processors sidebar_title: vSphere Template @@ -19,8 +15,8 @@ Type: `vsphere-template` The Packer vSphere Template post-processor takes an artifact from the VMware-iso builder, built on ESXi (i.e. remote) or an artifact from the -[vSphere](/docs/post-processors/vsphere) post-processor and allows to mark -a VM as a template and leaving it in a path of choice. +[vSphere](/docs/post-processors/vsphere) post-processor, marks the VM as a +template, and leaves it in the path of your choice. ## Example