2015-02-13 05:13:58 -05:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-02-13 05:13:58 -05:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2017-02-10 03:22:02 -05:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-02-13 05:13:58 -05:00
|
|
|
)
|
|
|
|
|
2018-07-02 08:35:07 -04:00
|
|
|
// This step exports a VM built on ESXi using ovftool
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// display_name string
|
2015-02-13 05:13:58 -05:00
|
|
|
type StepExport struct {
|
2017-01-10 06:44:11 -05:00
|
|
|
Format string
|
|
|
|
SkipExport bool
|
2017-03-10 05:43:45 -05:00
|
|
|
OutputDir string
|
2015-02-13 05:13:58 -05:00
|
|
|
}
|
|
|
|
|
2018-07-02 08:35:07 -04:00
|
|
|
func (s *StepExport) generateArgs(c *Config, displayName string, hidePassword bool) []string {
|
2015-10-23 19:51:03 -04:00
|
|
|
password := url.QueryEscape(c.RemotePassword)
|
|
|
|
if hidePassword {
|
|
|
|
password = "****"
|
|
|
|
}
|
2017-02-10 03:22:02 -05:00
|
|
|
args := []string{
|
2015-10-23 19:51:03 -04:00
|
|
|
"--noSSLVerify=true",
|
|
|
|
"--skipManifestCheck",
|
|
|
|
"-tt=" + s.Format,
|
2018-07-02 08:35:07 -04:00
|
|
|
"vi://" + c.RemoteUser + ":" + password + "@" + c.RemoteHost + "/" + displayName,
|
2017-03-10 05:43:45 -05:00
|
|
|
s.OutputDir,
|
2015-10-23 19:51:03 -04:00
|
|
|
}
|
2017-02-10 03:22:02 -05:00
|
|
|
return append(c.OVFToolOptions, args...)
|
2015-10-23 19:51:03 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepExport) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-02-13 05:13:58 -05:00
|
|
|
c := state.Get("config").(*Config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2017-01-10 06:44:11 -05:00
|
|
|
// Skip export if requested
|
|
|
|
if c.SkipExport {
|
|
|
|
ui.Say("Skipping export of virtual machine...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-04-23 13:24:49 -04:00
|
|
|
if c.RemoteType != "esx5" {
|
|
|
|
ui.Say("Skipping export of virtual machine (export is allowed only for ESXi)...")
|
2015-02-13 05:13:58 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
ovftool := "ovftool"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
ovftool = "ovftool.exe"
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := exec.LookPath(ovftool); err != nil {
|
|
|
|
err := fmt.Errorf("Error %s not found: %s", ovftool, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Export the VM
|
2017-03-10 05:43:45 -05:00
|
|
|
if s.OutputDir == "" {
|
|
|
|
s.OutputDir = c.VMName + "." + s.Format
|
|
|
|
}
|
2015-02-13 05:13:58 -05:00
|
|
|
|
|
|
|
if s.Format == "ova" {
|
2017-03-10 05:43:45 -05:00
|
|
|
os.MkdirAll(s.OutputDir, 0755)
|
2015-02-13 05:13:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Exporting virtual machine...")
|
2018-07-02 08:35:07 -04:00
|
|
|
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), " ")))
|
2015-02-13 05:13:58 -05:00
|
|
|
var out bytes.Buffer
|
2018-07-02 08:35:07 -04:00
|
|
|
cmd := exec.Command(ovftool, s.generateArgs(c, displayName, false)...)
|
2015-02-13 05:13:58 -05:00
|
|
|
cmd.Stdout = &out
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
err := fmt.Errorf("Error exporting virtual machine: %s\n%s\n", err, out.String())
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("%s", out.String()))
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepExport) Cleanup(state multistep.StateBag) {}
|