github.com/mitchellh/packer@v1.3.2/builder/vmware/iso/step_export.go (about)

     1  package iso
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"net/url"
     8  	"os"
     9  	"os/exec"
    10  	"runtime"
    11  	"strings"
    12  
    13  	"github.com/hashicorp/packer/helper/multistep"
    14  	"github.com/hashicorp/packer/packer"
    15  )
    16  
    17  // This step exports a VM built on ESXi using ovftool
    18  //
    19  // Uses:
    20  //   display_name string
    21  type StepExport struct {
    22  	Format     string
    23  	SkipExport bool
    24  	OutputDir  string
    25  }
    26  
    27  func (s *StepExport) generateArgs(c *Config, displayName string, hidePassword bool) []string {
    28  	password := url.QueryEscape(c.RemotePassword)
    29  	if hidePassword {
    30  		password = "****"
    31  	}
    32  	args := []string{
    33  		"--noSSLVerify=true",
    34  		"--skipManifestCheck",
    35  		"-tt=" + s.Format,
    36  		"vi://" + c.RemoteUser + ":" + password + "@" + c.RemoteHost + "/" + displayName,
    37  		s.OutputDir,
    38  	}
    39  	return append(c.OVFToolOptions, args...)
    40  }
    41  
    42  func (s *StepExport) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    43  	c := state.Get("config").(*Config)
    44  	ui := state.Get("ui").(packer.Ui)
    45  
    46  	// Skip export if requested
    47  	if c.SkipExport {
    48  		ui.Say("Skipping export of virtual machine...")
    49  		return multistep.ActionContinue
    50  	}
    51  
    52  	if c.RemoteType != "esx5" {
    53  		ui.Say("Skipping export of virtual machine (export is allowed only for ESXi)...")
    54  		return multistep.ActionContinue
    55  	}
    56  
    57  	ovftool := "ovftool"
    58  	if runtime.GOOS == "windows" {
    59  		ovftool = "ovftool.exe"
    60  	}
    61  
    62  	if _, err := exec.LookPath(ovftool); err != nil {
    63  		err := fmt.Errorf("Error %s not found: %s", ovftool, err)
    64  		state.Put("error", err)
    65  		ui.Error(err.Error())
    66  		return multistep.ActionHalt
    67  	}
    68  
    69  	// Export the VM
    70  	if s.OutputDir == "" {
    71  		s.OutputDir = c.VMName + "." + s.Format
    72  	}
    73  
    74  	if s.Format == "ova" {
    75  		os.MkdirAll(s.OutputDir, 0755)
    76  	}
    77  
    78  	ui.Say("Exporting virtual machine...")
    79  	var displayName string
    80  	if v, ok := state.GetOk("display_name"); ok {
    81  		displayName = v.(string)
    82  	}
    83  	ui.Message(fmt.Sprintf("Executing: %s %s", ovftool, strings.Join(s.generateArgs(c, displayName, true), " ")))
    84  	var out bytes.Buffer
    85  	cmd := exec.Command(ovftool, s.generateArgs(c, displayName, false)...)
    86  	cmd.Stdout = &out
    87  	if err := cmd.Run(); err != nil {
    88  		err := fmt.Errorf("Error exporting virtual machine: %s\n%s\n", err, out.String())
    89  		state.Put("error", err)
    90  		ui.Error(err.Error())
    91  		return multistep.ActionHalt
    92  	}
    93  
    94  	ui.Message(fmt.Sprintf("%s", out.String()))
    95  
    96  	return multistep.ActionContinue
    97  }
    98  
    99  func (s *StepExport) Cleanup(state multistep.StateBag) {}