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