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.
This commit is contained in:
DanHam 2018-07-02 13:35:07 +01:00
parent cd7d3812ea
commit 902cea0f30
No known key found for this signature in database
GPG Key ID: 58E79AEDD6AA987E
1 changed files with 12 additions and 4 deletions

View File

@ -14,13 +14,17 @@ import (
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
) )
// This step exports a VM built on ESXi using ovftool
//
// Uses:
// display_name string
type StepExport struct { type StepExport struct {
Format string Format string
SkipExport bool SkipExport bool
OutputDir string 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) password := url.QueryEscape(c.RemotePassword)
if hidePassword { if hidePassword {
password = "****" password = "****"
@ -29,7 +33,7 @@ func (s *StepExport) generateArgs(c *Config, hidePassword bool) []string {
"--noSSLVerify=true", "--noSSLVerify=true",
"--skipManifestCheck", "--skipManifestCheck",
"-tt=" + s.Format, "-tt=" + s.Format,
"vi://" + c.RemoteUser + ":" + password + "@" + c.RemoteHost + "/" + c.VMName, "vi://" + c.RemoteUser + ":" + password + "@" + c.RemoteHost + "/" + displayName,
s.OutputDir, s.OutputDir,
} }
return append(c.OVFToolOptions, args...) 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.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 var out bytes.Buffer
cmd := exec.Command(ovftool, s.generateArgs(c, false)...) cmd := exec.Command(ovftool, s.generateArgs(c, displayName, false)...)
cmd.Stdout = &out cmd.Stdout = &out
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
err := fmt.Errorf("Error exporting virtual machine: %s\n%s\n", err, out.String()) err := fmt.Errorf("Error exporting virtual machine: %s\n%s\n", err, out.String())