github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/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 } 24 25 func (s *StepExport) Run(state multistep.StateBag) multistep.StepAction { 26 driver := state.Get("driver").(Driver) 27 ui := state.Get("ui").(packer.Ui) 28 vmName := state.Get("vmName").(string) 29 30 // Wait a second to ensure VM is really shutdown. 31 log.Println("1 second timeout to ensure VM is really shutdown") 32 time.Sleep(1 * time.Second) 33 34 // Clear out the Packer-created forwarding rule 35 ui.Say("Preparing to export machine...") 36 ui.Message(fmt.Sprintf( 37 "Deleting forwarded port mapping for SSH (host port %d)", 38 state.Get("sshHostPort"))) 39 command := []string{"modifyvm", vmName, "--natpf1", "delete", "packerssh"} 40 if err := driver.VBoxManage(command...); err != nil { 41 err := fmt.Errorf("Error deleting port forwarding rule: %s", err) 42 state.Put("error", err) 43 ui.Error(err.Error()) 44 return multistep.ActionHalt 45 } 46 47 // Export the VM to an OVF 48 outputPath := filepath.Join(s.OutputDir, vmName+"."+s.Format) 49 50 command = []string{ 51 "export", 52 vmName, 53 "--output", 54 outputPath, 55 } 56 57 command = append(command, s.ExportOpts...) 58 59 ui.Say("Exporting virtual machine...") 60 ui.Message(fmt.Sprintf("Executing: %s", strings.Join(command, " "))) 61 err := driver.VBoxManage(command...) 62 if err != nil { 63 err := fmt.Errorf("Error exporting virtual machine: %s", err) 64 state.Put("error", err) 65 ui.Error(err.Error()) 66 return multistep.ActionHalt 67 } 68 69 state.Put("exportPath", outputPath) 70 71 return multistep.ActionContinue 72 } 73 74 func (s *StepExport) Cleanup(state multistep.StateBag) {}