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"
)
// 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())