github.com/rothwerx/packer@v0.9.0/builder/vmware/iso/step_export.go (about) 1 package iso 2 3 import ( 4 "bytes" 5 "fmt" 6 "github.com/mitchellh/multistep" 7 "github.com/mitchellh/packer/packer" 8 "net/url" 9 "os" 10 "os/exec" 11 "path/filepath" 12 "runtime" 13 "strings" 14 ) 15 16 type StepExport struct { 17 Format string 18 } 19 20 func (s *StepExport) generateArgs(c *Config, outputPath string, hidePassword bool) []string { 21 password := url.QueryEscape(c.RemotePassword) 22 if hidePassword { 23 password = "****" 24 } 25 return []string{ 26 "--noSSLVerify=true", 27 "--skipManifestCheck", 28 "-tt=" + s.Format, 29 "vi://" + c.RemoteUser + ":" + password + "@" + c.RemoteHost + "/" + c.VMName, 30 outputPath, 31 } 32 } 33 34 func (s *StepExport) Run(state multistep.StateBag) multistep.StepAction { 35 c := state.Get("config").(*Config) 36 ui := state.Get("ui").(packer.Ui) 37 38 if c.RemoteType != "esx5" || s.Format == "" { 39 return multistep.ActionContinue 40 } 41 42 ovftool := "ovftool" 43 if runtime.GOOS == "windows" { 44 ovftool = "ovftool.exe" 45 } 46 47 if _, err := exec.LookPath(ovftool); err != nil { 48 err := fmt.Errorf("Error %s not found: %s", ovftool, err) 49 state.Put("error", err) 50 ui.Error(err.Error()) 51 return multistep.ActionHalt 52 } 53 54 // Export the VM 55 outputPath := filepath.Join(c.VMName, c.VMName+"."+s.Format) 56 57 if s.Format == "ova" { 58 os.MkdirAll(outputPath, 0755) 59 } 60 61 ui.Say("Exporting virtual machine...") 62 ui.Message(fmt.Sprintf("Executing: %s %s", ovftool, strings.Join(s.generateArgs(c, outputPath, true), " "))) 63 var out bytes.Buffer 64 cmd := exec.Command(ovftool, s.generateArgs(c, outputPath, false)...) 65 cmd.Stdout = &out 66 if err := cmd.Run(); err != nil { 67 err := fmt.Errorf("Error exporting virtual machine: %s\n%s\n", err, out.String()) 68 state.Put("error", err) 69 ui.Error(err.Error()) 70 return multistep.ActionHalt 71 } 72 73 ui.Message(fmt.Sprintf("%s", out.String())) 74 75 state.Put("exportPath", outputPath) 76 77 return multistep.ActionContinue 78 } 79 80 func (s *StepExport) Cleanup(state multistep.StateBag) {}