2013-06-12 21:07:08 -04:00
|
|
|
package virtualbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-10-09 12:59:47 -04:00
|
|
|
"log"
|
2013-10-09 13:14:09 -04:00
|
|
|
"path/filepath"
|
2013-10-09 12:59:47 -04:00
|
|
|
"time"
|
2013-06-12 21:07:08 -04:00
|
|
|
)
|
|
|
|
|
2013-07-04 17:44:48 -04:00
|
|
|
// This step cleans up forwarded ports and exports the VM to an OVF.
|
2013-06-12 21:07:08 -04:00
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// exportPath string - The path to the resulting export.
|
|
|
|
type stepExport struct{}
|
|
|
|
|
2013-08-31 15:44:58 -04:00
|
|
|
func (s *stepExport) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(*config)
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
2013-06-12 21:07:08 -04:00
|
|
|
|
2013-10-09 12:59:47 -04:00
|
|
|
// Wait a second to ensure VM is really shutdown.
|
|
|
|
log.Println("1 second timeout to ensure VM is really shutdown")
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2013-07-05 14:00:18 -04:00
|
|
|
// Clear out the Packer-created forwarding rule
|
2013-07-09 15:38:34 -04:00
|
|
|
ui.Say("Preparing to export machine...")
|
2013-08-31 15:44:58 -04:00
|
|
|
ui.Message(fmt.Sprintf("Deleting forwarded port mapping for SSH (host port %d)", state.Get("sshHostPort")))
|
2013-07-04 17:44:48 -04:00
|
|
|
command := []string{"modifyvm", vmName, "--natpf1", "delete", "packerssh"}
|
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
|
|
|
err := fmt.Errorf("Error deleting port forwarding rule: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-07-04 17:44:48 -04:00
|
|
|
ui.Error(err.Error())
|
2013-07-05 14:00:18 -04:00
|
|
|
return multistep.ActionHalt
|
2013-07-04 17:44:48 -04:00
|
|
|
}
|
|
|
|
|
2013-07-09 15:38:34 -04:00
|
|
|
// Remove the attached floppy disk, if it exists
|
2013-08-31 15:44:58 -04:00
|
|
|
if _, ok := state.GetOk("floppy_path"); ok {
|
2013-07-09 15:38:34 -04:00
|
|
|
ui.Message("Removing floppy drive...")
|
|
|
|
command := []string{
|
|
|
|
"storageattach", vmName,
|
|
|
|
"--storagectl", "Floppy Controller",
|
|
|
|
"--port", "0",
|
|
|
|
"--device", "0",
|
|
|
|
"--medium", "none",
|
|
|
|
}
|
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error removing floppy: %s", err))
|
2013-07-09 15:38:34 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-05 14:00:18 -04:00
|
|
|
// Export the VM to an OVF
|
2013-09-06 22:08:06 -04:00
|
|
|
outputPath := filepath.Join(config.OutputDir, vmName+"."+config.Format)
|
2013-06-12 21:07:08 -04:00
|
|
|
|
2013-07-04 17:44:48 -04:00
|
|
|
command = []string{
|
2013-06-12 21:07:08 -04:00
|
|
|
"export",
|
|
|
|
vmName,
|
|
|
|
"--output",
|
|
|
|
outputPath,
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Exporting virtual machine...")
|
|
|
|
err := driver.VBoxManage(command...)
|
|
|
|
if err != nil {
|
2013-06-20 00:07:53 -04:00
|
|
|
err := fmt.Errorf("Error exporting virtual machine: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:07:53 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-12 21:07:08 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("exportPath", outputPath)
|
2013-06-12 21:07:08 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:44:58 -04:00
|
|
|
func (s *stepExport) Cleanup(state multistep.StateBag) {}
|