From ba22090bc9eabeeb0377cc8f27815abb6ce1960c Mon Sep 17 00:00:00 2001 From: Alexander Laamanen Date: Tue, 31 Jan 2017 12:05:49 +0200 Subject: [PATCH] Move step_register and step_uload_vmx from iso to common. --- builder/vmware/common/driver_esx5.go | 4 ++-- builder/vmware/common/step_configure_vmx.go | 9 +-------- builder/vmware/{iso => common}/step_register.go | 15 +++++++-------- .../vmware/{iso => common}/step_register_test.go | 2 +- builder/vmware/{iso => common}/step_upload_vmx.go | 9 +++++---- builder/vmware/iso/builder.go | 7 ++++--- builder/vmware/vmx/builder.go | 7 +++++++ 7 files changed, 27 insertions(+), 26 deletions(-) rename builder/vmware/{iso => common}/step_register.go (80%) rename builder/vmware/{iso => common}/step_register_test.go (99%) rename builder/vmware/{iso => common}/step_upload_vmx.go (85%) diff --git a/builder/vmware/common/driver_esx5.go b/builder/vmware/common/driver_esx5.go index deb5f5634..7519c586f 100644 --- a/builder/vmware/common/driver_esx5.go +++ b/builder/vmware/common/driver_esx5.go @@ -47,8 +47,8 @@ func (d *ESX5Driver) Clone(dst, src string, linked bool) error { linesToArray := func(lines string) []string { return strings.Split(strings.Trim(lines, "\n"), "\n") } - d.SetOutputDir(dst) - srcVmx := d.datastorePath(path.Dir(src)) + d.SetOutputDir(path.Dir(dst)) + srcVmx := d.datastorePath(src) dstVmx := d.datastorePath(dst) srcDir := path.Dir(srcVmx) dstDir := path.Dir(dstVmx) diff --git a/builder/vmware/common/step_configure_vmx.go b/builder/vmware/common/step_configure_vmx.go index 5a4f61b2a..719a96902 100644 --- a/builder/vmware/common/step_configure_vmx.go +++ b/builder/vmware/common/step_configure_vmx.go @@ -30,7 +30,6 @@ func (s *StepConfigureVMX) Run(_ context.Context, state multistep.StateBag) mult var vmxContents []byte var err error - driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) vmxPath := state.Get("vmx_path").(string) @@ -75,13 +74,7 @@ func (s *StepConfigureVMX) Run(_ context.Context, state multistep.StateBag) mult } } - if remoteDriver, ok := driver.(RemoteDriver); ok { - var buf bytes.Buffer - buf.WriteString(EncodeVMX(vmxData)) - err = remoteDriver.WriteFile(vmxPath, buf.Bytes()) - } else { - err = WriteVMX(vmxPath, vmxData) - } + err = WriteVMX(vmxPath, vmxData) if err != nil { err := fmt.Errorf("Error writing VMX file: %s", err) diff --git a/builder/vmware/iso/step_register.go b/builder/vmware/common/step_register.go similarity index 80% rename from builder/vmware/iso/step_register.go rename to builder/vmware/common/step_register.go index 46b713c79..0fbc1cc95 100644 --- a/builder/vmware/iso/step_register.go +++ b/builder/vmware/common/step_register.go @@ -1,11 +1,10 @@ -package iso +package common import ( "context" "fmt" "time" - vmwcommon "github.com/hashicorp/packer/builder/vmware/common" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) @@ -13,14 +12,15 @@ import ( type StepRegister struct { registeredPath string Format string + KeepRegistered bool } func (s *StepRegister) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { - driver := state.Get("driver").(vmwcommon.Driver) + driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) vmxPath := state.Get("vmx_path").(string) - if remoteDriver, ok := driver.(vmwcommon.RemoteDriver); ok { + if remoteDriver, ok := driver.(RemoteDriver); ok { ui.Say("Registering remote VM...") if err := remoteDriver.Register(vmxPath); err != nil { err := fmt.Errorf("Error registering VM: %s", err) @@ -40,18 +40,17 @@ func (s *StepRegister) Cleanup(state multistep.StateBag) { return } - driver := state.Get("driver").(vmwcommon.Driver) + driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) - config := state.Get("config").(*Config) _, cancelled := state.GetOk(multistep.StateCancelled) _, halted := state.GetOk(multistep.StateHalted) - if (config.KeepRegistered) && (!cancelled && !halted) { + if (s.KeepRegistered) && (!cancelled && !halted) { ui.Say("Keeping virtual machine registered with ESX host (keep_registered = true)") return } - if remoteDriver, ok := driver.(vmwcommon.RemoteDriver); ok { + if remoteDriver, ok := driver.(RemoteDriver); ok { if s.Format == "" || config.SkipExport { ui.Say("Unregistering virtual machine...") if err := remoteDriver.Unregister(s.registeredPath); err != nil { diff --git a/builder/vmware/iso/step_register_test.go b/builder/vmware/common/step_register_test.go similarity index 99% rename from builder/vmware/iso/step_register_test.go rename to builder/vmware/common/step_register_test.go index 099e067ab..f3148a18a 100644 --- a/builder/vmware/iso/step_register_test.go +++ b/builder/vmware/common/step_register_test.go @@ -1,4 +1,4 @@ -package iso +package common import ( "context" diff --git a/builder/vmware/iso/step_upload_vmx.go b/builder/vmware/common/step_upload_vmx.go similarity index 85% rename from builder/vmware/iso/step_upload_vmx.go rename to builder/vmware/common/step_upload_vmx.go index b05795508..646225a7c 100644 --- a/builder/vmware/iso/step_upload_vmx.go +++ b/builder/vmware/common/step_upload_vmx.go @@ -1,11 +1,11 @@ -package iso +package common import ( "context" "fmt" + "log" "path/filepath" - vmwcommon "github.com/hashicorp/packer/builder/vmware/common" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) @@ -24,15 +24,16 @@ type StepUploadVMX struct { } func (c *StepUploadVMX) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { - driver := state.Get("driver").(vmwcommon.Driver) + driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) vmxPath := state.Get("vmx_path").(string) if c.RemoteType == "esx5" { - remoteDriver, ok := driver.(vmwcommon.RemoteDriver) + remoteDriver, ok := driver.(RemoteDriver) if ok { remoteVmxPath := filepath.ToSlash(filepath.Join(fmt.Sprintf("%s", remoteDriver), filepath.Base(vmxPath))) + log.Printf("Uploading VMX file from %s to %s", vmxPath, remoteVmxPath) if err := remoteDriver.Upload(remoteVmxPath, vmxPath); err != nil { state.Put("error", fmt.Errorf("Error writing VMX: %s", err)) return multistep.ActionHalt diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 054ff4664..8f175560d 100644 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -341,8 +341,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe VNCPortMax: b.config.VNCPortMax, VNCDisablePassword: b.config.VNCDisablePassword, }, - &StepRegister{ - Format: b.config.Format, + &vmwcommon.StepRegister{ + Format: b.config.Format, + KeepRegistered: b.config.KeepRegistered, }, &vmwcommon.StepRun{ DurationBeforeStop: 5 * time.Second, @@ -387,7 +388,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet, VNCEnabled: !b.config.DisableVNC, }, - &StepUploadVMX{ + &vmwcommon.StepUploadVMX{ RemoteType: b.config.RemoteType, }, &StepExport{ diff --git a/builder/vmware/vmx/builder.go b/builder/vmware/vmx/builder.go index 42a550f29..64d89d887 100644 --- a/builder/vmware/vmx/builder.go +++ b/builder/vmware/vmx/builder.go @@ -80,6 +80,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe HTTPPortMin: b.config.HTTPPortMin, HTTPPortMax: b.config.HTTPPortMax, }, + &vmwcommon.StepUploadVMX{ + RemoteType: b.config.RemoteType, + }, + &vmwcommon.StepRegister{ + Format: "foo", + KeepRegistered: false, + }, &vmwcommon.StepConfigureVNC{ Enabled: !b.config.DisableVNC, VNCBindAddress: b.config.VNCBindAddress,