github.com/rothwerx/packer@v0.9.0/builder/virtualbox/common/step_export.go (about) 1 package common 2 3 import ( 4 "fmt" 5 "github.com/mitchellh/multistep" 6 "github.com/mitchellh/packer/packer" 7 "log" 8 "path/filepath" 9 "strings" 10 "time" 11 ) 12 13 // This step cleans up forwarded ports and exports the VM to an OVF. 14 // 15 // Uses: 16 // 17 // Produces: 18 // exportPath string - The path to the resulting export. 19 type StepExport struct { 20 Format string 21 OutputDir string 22 ExportOpts []string 23 SkipNatMapping bool 24 } 25 26 func (s *StepExport) Run(state multistep.StateBag) multistep.StepAction { 27 driver := state.Get("driver").(Driver) 28 ui := state.Get("ui").(packer.Ui) 29 vmName := state.Get("vmName").(string) 30 31 // Wait a second to ensure VM is really shutdown. 32 log.Println("1 second timeout to ensure VM is really shutdown") 33 time.Sleep(1 * time.Second) 34 ui.Say("Preparing to export machine...") 35 36 // Clear out the Packer-created forwarding rule 37 if !s.SkipNatMapping { 38 ui.Message(fmt.Sprintf( 39 "Deleting forwarded port mapping for the communicator (SSH, WinRM, etc) (host port %d)", 40 state.Get("sshHostPort"))) 41 command := []string{"modifyvm", vmName, "--natpf1", "delete", "packercomm"} 42 if err := driver.VBoxManage(command...); err != nil { 43 err := fmt.Errorf("Error deleting port forwarding rule: %s", err) 44 state.Put("error", err) 45 ui.Error(err.Error()) 46 return multistep.ActionHalt 47 } 48 } 49 50 // Export the VM to an OVF 51 outputPath := filepath.Join(s.OutputDir, vmName+"."+s.Format) 52 53 command := []string{ 54 "export", 55 vmName, 56 "--output", 57 outputPath, 58 } 59 command = append(command, s.ExportOpts...) 60 61 ui.Say("Exporting virtual machine...") 62 ui.Message(fmt.Sprintf("Executing: %s", strings.Join(command, " "))) 63 err := driver.VBoxManage(command...) 64 if err != nil { 65 err := fmt.Errorf("Error exporting virtual machine: %s", err) 66 state.Put("error", err) 67 ui.Error(err.Error()) 68 return multistep.ActionHalt 69 } 70 71 state.Put("exportPath", outputPath) 72 73 return multistep.ActionContinue 74 } 75 76 func (s *StepExport) Cleanup(state multistep.StateBag) {}