From 902cea0f306d06cf748c64c9c1b4d8c113daedb1 Mon Sep 17 00:00:00 2001 From: DanHam Date: Mon, 2 Jul 2018 13:35:07 +0100 Subject: [PATCH] ESXi: Fix failure to export VM when `displayName` differs from `vm_name` ovftool requires we pass in `displayName` as part of the source locator string so that it can successfully determine the VM intended for export. --- builder/vmware/iso/step_export.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/builder/vmware/iso/step_export.go b/builder/vmware/iso/step_export.go index 50b1efd9f..c39443c8f 100644 --- a/builder/vmware/iso/step_export.go +++ b/builder/vmware/iso/step_export.go @@ -14,13 +14,17 @@ import ( "github.com/hashicorp/packer/packer" ) +// This step exports a VM built on ESXi using ovftool +// +// Uses: +// display_name string type StepExport struct { Format string SkipExport bool OutputDir string } -func (s *StepExport) generateArgs(c *Config, hidePassword bool) []string { +func (s *StepExport) generateArgs(c *Config, displayName string, hidePassword bool) []string { password := url.QueryEscape(c.RemotePassword) if hidePassword { password = "****" @@ -29,7 +33,7 @@ func (s *StepExport) generateArgs(c *Config, hidePassword bool) []string { "--noSSLVerify=true", "--skipManifestCheck", "-tt=" + s.Format, - "vi://" + c.RemoteUser + ":" + password + "@" + c.RemoteHost + "/" + c.VMName, + "vi://" + c.RemoteUser + ":" + password + "@" + c.RemoteHost + "/" + displayName, s.OutputDir, } return append(c.OVFToolOptions, args...) @@ -72,9 +76,13 @@ func (s *StepExport) Run(_ context.Context, state multistep.StateBag) multistep. } ui.Say("Exporting virtual machine...") - ui.Message(fmt.Sprintf("Executing: %s %s", ovftool, strings.Join(s.generateArgs(c, true), " "))) + var displayName string + if v, ok := state.GetOk("display_name"); ok { + displayName = v.(string) + } + ui.Message(fmt.Sprintf("Executing: %s %s", ovftool, strings.Join(s.generateArgs(c, displayName, true), " "))) var out bytes.Buffer - cmd := exec.Command(ovftool, s.generateArgs(c, false)...) + cmd := exec.Command(ovftool, s.generateArgs(c, displayName, false)...) cmd.Stdout = &out if err := cmd.Run(); err != nil { err := fmt.Errorf("Error exporting virtual machine: %s\n%s\n", err, out.String())