diff --git a/builder/vmware/common/artifact.go b/builder/vmware/common/artifact.go index cb61cd2f4..b6d3c4cd6 100644 --- a/builder/vmware/common/artifact.go +++ b/builder/vmware/common/artifact.go @@ -3,6 +3,7 @@ package common import ( "fmt" "os" + "path/filepath" "github.com/hashicorp/packer/packer" ) @@ -11,6 +12,12 @@ import ( const BuilderId = "mitchellh.vmware" const BuilderIdESX = "mitchellh.vmware-esx" +const ( + ArtifactConfFormat = "artifact.conf.format" + ArtifactConfKeepRegistered = "artifact.conf.keep_registered" + ArtifactConfSkipExport = "artifact.conf.skip_export" +) + // Artifact is the result of running the VMware builder, namely a set // of files associated with the resulting machine. type artifact struct { @@ -48,7 +55,7 @@ func NewLocalArtifact(id string, dir string) (packer.Artifact, error) { }, nil } -func NewArtifact(dir OutputDir, files []string, esxi bool) (packer.Artifact, err) { +func NewArtifact(dir OutputDir, files []string, config map[string]string, esxi bool) (packer.Artifact, error) { builderID := BuilderId if esxi { builderID = BuilderIdESX @@ -69,7 +76,7 @@ func (a *artifact) Files() []string { return a.f } -func (*artifact) Id() string { +func (a *artifact) Id() string { return a.id } @@ -78,7 +85,7 @@ func (a *artifact) String() string { } func (a *artifact) State(name string) interface{} { - return nil + return a.config[name] } func (a *artifact) Destroy() error { diff --git a/builder/vmware/common/driver.go b/builder/vmware/common/driver.go index ed37ff207..b230d84b9 100644 --- a/builder/vmware/common/driver.go +++ b/builder/vmware/common/driver.go @@ -84,6 +84,7 @@ type Driver interface { // system, or an error if the driver couldn't be initialized. func NewDriver(dconfig *DriverConfig, config *SSHConfig, commConfig *communicator.Config, vmName string) (Driver, error) { drivers := []Driver{} + if dconfig.RemoteType != "" { drivers = []Driver{ &ESX5Driver{ @@ -91,7 +92,7 @@ func NewDriver(dconfig *DriverConfig, config *SSHConfig, commConfig *communicato Port: dconfig.RemotePort, Username: dconfig.RemoteUser, Password: dconfig.RemotePassword, - PrivateKey: dconfig.RemotePrivateKey, + PrivateKeyFile: dconfig.RemotePrivateKey, Datastore: dconfig.RemoteDatastore, CacheDatastore: dconfig.RemoteCacheDatastore, CacheDirectory: dconfig.RemoteCacheDirectory, diff --git a/builder/vmware/common/driver_esx5.go b/builder/vmware/common/driver_esx5.go index edb74d051..e53a73d54 100644 --- a/builder/vmware/common/driver_esx5.go +++ b/builder/vmware/common/driver_esx5.go @@ -16,7 +16,6 @@ import ( "strings" "time" - commonssh "github.com/hashicorp/packer/common/ssh" "github.com/hashicorp/packer/communicator/ssh" "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/multistep" @@ -28,7 +27,7 @@ import ( // ESX5 driver talks to an ESXi5 hypervisor remotely over SSH to build // virtual machines. This driver can only manage one machine at a time. type ESX5Driver struct { - base vmwcommon.VmwareDriver + base VmwareDriver Host string Port uint @@ -184,13 +183,13 @@ func (d *ESX5Driver) IsDestroyed() (bool, error) { } func (d *ESX5Driver) UploadISO(localPath string, checksum string, checksumType string) (string, error) { - finalPath := d.cachePath(localPath) + finalPath := d.CachePath(localPath) if err := d.mkdir(filepath.ToSlash(filepath.Dir(finalPath))); err != nil { return "", err } log.Printf("Verifying checksum of %s", finalPath) - if d.verifyChecksum(checksumType, checksum, finalPath) { + if d.VerifyChecksum(checksumType, checksum, finalPath) { log.Println("Initial checksum matched, no upload needed.") return finalPath, nil } @@ -203,7 +202,7 @@ func (d *ESX5Driver) UploadISO(localPath string, checksum string, checksumType s } func (d *ESX5Driver) RemoveCache(localPath string) error { - finalPath := d.cachePath(localPath) + finalPath := d.CachePath(localPath) log.Printf("Removing remote cache path %s (local %s)", finalPath, localPath) return d.sh("rm", "-f", strconv.Quote(finalPath)) } @@ -564,7 +563,7 @@ func (d *ESX5Driver) datastorePath(path string) string { return filepath.ToSlash(filepath.Join("/vmfs/volumes", d.Datastore, dirPath, filepath.Base(path))) } -func (d *ESX5Driver) cachePath(path string) string { +func (d *ESX5Driver) CachePath(path string) string { return filepath.ToSlash(filepath.Join("/vmfs/volumes", d.CacheDatastore, d.CacheDirectory, filepath.Base(path))) } @@ -662,7 +661,7 @@ func (d *ESX5Driver) Download(src, dst string) error { return d.comm.Download(d.datastorePath(src), file) } -func (d *ESX5Driver) verifyChecksum(ctype string, hash string, file string) bool { +func (d *ESX5Driver) VerifyChecksum(ctype string, hash string, file string) bool { if ctype == "none" { if err := d.sh("stat", strconv.Quote(file)); err != nil { return false @@ -731,7 +730,7 @@ func (d *ESX5Driver) esxcli(args ...string) (*esxcliReader, error) { return &esxcliReader{r, header}, nil } -func (d *ESX5Driver) GetVmwareDriver() vmwcommon.VmwareDriver { +func (d *ESX5Driver) GetVmwareDriver() VmwareDriver { return d.base } diff --git a/builder/vmware/common/export_config.go b/builder/vmware/common/export_config.go index fae944cb9..663972847 100644 --- a/builder/vmware/common/export_config.go +++ b/builder/vmware/common/export_config.go @@ -3,7 +3,7 @@ package common import ( "fmt" - "github.com/mitchellh/packer/template/interpolate" + "github.com/hashicorp/packer/template/interpolate" ) type ExportConfig struct { diff --git a/builder/vmware/common/remote_artifact.go b/builder/vmware/common/remote_artifact.go index 37dbc1812..4cc1b2a2f 100644 --- a/builder/vmware/common/remote_artifact.go +++ b/builder/vmware/common/remote_artifact.go @@ -4,12 +4,6 @@ import ( "fmt" ) -const ( - ArtifactConfFormat = "artifact.conf.format" - ArtifactConfKeepRegistered = "artifact.conf.keep_registered" - ArtifactConfSkipExport = "artifact.conf.skip_export" -) - // Artifact is the result of running the VMware builder, namely a set // of files associated with the resulting machine. type RemoteArtifact struct { @@ -28,7 +22,7 @@ func (a *RemoteArtifact) Files() []string { return a.f } -func (*RemoteArtifact) Id() string { +func (a *RemoteArtifact) Id() string { return a.id } diff --git a/builder/vmware/common/step_configure_vmx.go b/builder/vmware/common/step_configure_vmx.go index 03162ed35..2c47590c1 100644 --- a/builder/vmware/common/step_configure_vmx.go +++ b/builder/vmware/common/step_configure_vmx.go @@ -1,7 +1,6 @@ package common import ( - "bytes" "context" "fmt" "log" @@ -29,7 +28,6 @@ type StepConfigureVMX struct { func (s *StepConfigureVMX) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { log.Printf("Configuring VMX...\n") - var vmxContents []byte var err error ui := state.Get("ui").(packer.Ui) diff --git a/builder/vmware/common/step_export.go b/builder/vmware/common/step_export.go index 3b0cf5920..fb891151a 100644 --- a/builder/vmware/common/step_export.go +++ b/builder/vmware/common/step_export.go @@ -71,7 +71,7 @@ func (s *StepExport) Run(_ context.Context, state multistep.StateBag) multistep. // Export the VM if s.OutputDir == "" { - s.OutputDir = c.VMName + "." + s.Format + s.OutputDir = s.VMName + "." + s.Format } if s.Format == "ova" { diff --git a/builder/vmware/common/step_register.go b/builder/vmware/common/step_register.go index 0fbc1cc95..0d9b93b7b 100644 --- a/builder/vmware/common/step_register.go +++ b/builder/vmware/common/step_register.go @@ -13,11 +13,13 @@ type StepRegister struct { registeredPath string Format string KeepRegistered bool + SkipExport bool } func (s *StepRegister) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) + vmxPath := state.Get("vmx_path").(string) if remoteDriver, ok := driver.(RemoteDriver); ok { @@ -51,7 +53,7 @@ func (s *StepRegister) Cleanup(state multistep.StateBag) { } if remoteDriver, ok := driver.(RemoteDriver); ok { - if s.Format == "" || config.SkipExport { + if s.Format == "" || s.SkipExport { ui.Say("Unregistering virtual machine...") if err := remoteDriver.Unregister(s.registeredPath); err != nil { ui.Error(fmt.Sprintf("Error unregistering VM: %s", err)) diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 91050d0f2..0ad07a055 100644 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -318,6 +318,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &vmwcommon.StepRegister{ Format: b.config.Format, KeepRegistered: b.config.KeepRegistered, + SkipExport: b.config.SkipExport, }, &vmwcommon.StepRun{ DurationBeforeStop: 5 * time.Second, @@ -405,7 +406,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe return nil, err } - return vmwcommon.NewArtifact(dir, files, b.config.RemoteType != ""), nil + config := make(map[string]string) + config[vmwcommon.ArtifactConfKeepRegistered] = strconv.FormatBool(b.config.KeepRegistered) + config[vmwcommon.ArtifactConfFormat] = b.config.Format + config[vmwcommon.ArtifactConfSkipExport] = strconv.FormatBool(b.config.SkipExport) + + return vmwcommon.NewArtifact(dir, files, config, b.config.RemoteType != "") } func (b *Builder) Cancel() { diff --git a/builder/vmware/iso/step_remote_upload.go b/builder/vmware/iso/step_remote_upload.go index 8391753a6..f034d1fc3 100644 --- a/builder/vmware/iso/step_remote_upload.go +++ b/builder/vmware/iso/step_remote_upload.go @@ -36,10 +36,10 @@ func (s *stepRemoteUpload) Run(_ context.Context, state multistep.StateBag) mult checksum := config.ISOChecksum checksumType := config.ISOChecksumType - if esx5, ok := remote.(*ESX5Driver); ok { - remotePath := esx5.cachePath(path) + if esx5, ok := remote.(*vmwcommon.ESX5Driver); ok { + remotePath := esx5.CachePath(path) - if esx5.verifyChecksum(checksumType, checksum, remotePath) { + if esx5.VerifyChecksum(checksumType, checksum, remotePath) { ui.Say("Remote cache was verified skipping remote upload...") state.Put(s.Key, remotePath) return multistep.ActionContinue @@ -68,7 +68,7 @@ func (s *stepRemoteUpload) Cleanup(state multistep.StateBag) { driver := state.Get("driver").(vmwcommon.Driver) - remote, ok := driver.(RemoteDriver) + remote, ok := driver.(vmwcommon.RemoteDriver) if !ok { return } diff --git a/builder/vmware/vmx/builder.go b/builder/vmware/vmx/builder.go index f5cab8ae4..eb4ee7342 100644 --- a/builder/vmware/vmx/builder.go +++ b/builder/vmware/vmx/builder.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "log" + "strconv" "time" vmwcommon "github.com/hashicorp/packer/builder/vmware/common" @@ -105,6 +106,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &vmwcommon.StepRegister{ Format: b.config.Format, KeepRegistered: b.config.KeepRegistered, + SkipExport: b.config.SkipExport, }, &vmwcommon.StepRun{ DurationBeforeStop: 5 * time.Second, @@ -191,7 +193,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe return nil, err } - return vmwcommon.NewArtifact(dir, files, b.config.RemoteType != ""), nil + config := make(map[string]string) + config[vmwcommon.ArtifactConfKeepRegistered] = strconv.FormatBool(b.config.KeepRegistered) + config[vmwcommon.ArtifactConfFormat] = b.config.Format + config[vmwcommon.ArtifactConfSkipExport] = strconv.FormatBool(b.config.SkipExport) + + return vmwcommon.NewArtifact(dir, files, config, b.config.RemoteType != "") } // Cancel. diff --git a/post-processor/vsphere-template/post-processor.go b/post-processor/vsphere-template/post-processor.go index deed5c2d7..d6b4fa769 100644 --- a/post-processor/vsphere-template/post-processor.go +++ b/post-processor/vsphere-template/post-processor.go @@ -8,7 +8,7 @@ import ( "strings" "time" - "github.com/hashicorp/packer/builder/vmware/iso" + vmwcommon "github.com/hashicorp/packer/builder/vmware/common" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/multistep" @@ -19,8 +19,8 @@ import ( ) var builtins = map[string]string{ - vsphere.BuilderId: "vmware", - iso.BuilderIdESX: "vmware", + vsphere.BuilderId: "vmware", + vmwcommon.BuilderIdESX: "vmware", } type Config struct { @@ -96,9 +96,9 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac "Artifact type %s does not fit this requirement", artifact.BuilderId()) } - f := artifact.State(iso.ArtifactConfFormat) - k := artifact.State(iso.ArtifactConfKeepRegistered) - s := artifact.State(iso.ArtifactConfSkipExport) + f := artifact.State(vmwcommon.ArtifactConfFormat) + k := artifact.State(vmwcommon.ArtifactConfKeepRegistered) + s := artifact.State(vmwcommon.ArtifactConfSkipExport) if f != "" && k != "true" && s == "false" { return nil, false, errors.New("To use this post-processor with exporting behavior you need set keep_registered as true")