packer-cn/builder/virtualbox/common/step_export.go

70 lines
1.7 KiB
Go
Raw Normal View History

2013-12-22 13:40:39 -05:00
package common
2013-06-12 21:07:08 -04:00
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
2013-10-09 13:14:09 -04:00
"path/filepath"
"time"
2013-06-12 21:07:08 -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.
2013-12-22 13:40:39 -05:00
type StepExport struct {
Format string
OutputDir string
}
2013-06-12 21:07:08 -04:00
2013-12-22 13:40:39 -05:00
func (s *StepExport) Run(state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
2013-08-31 15:44:58 -04:00
ui := state.Get("ui").(packer.Ui)
vmName := state.Get("vmName").(string)
2013-06-12 21:07:08 -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
ui.Say("Preparing to export machine...")
ui.Message(fmt.Sprintf(
"Deleting forwarded port mapping for SSH (host port %d)",
state.Get("sshHostPort")))
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)
ui.Error(err.Error())
2013-07-05 14:00:18 -04:00
return multistep.ActionHalt
}
2013-07-05 14:00:18 -04:00
// Export the VM to an OVF
2013-12-22 13:40:39 -05:00
outputPath := filepath.Join(s.OutputDir, vmName+"."+s.Format)
2013-06-12 21:07:08 -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 {
err := fmt.Errorf("Error exporting virtual machine: %s", err)
2013-08-31 15:44:58 -04:00
state.Put("error", err)
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-12-22 13:40:39 -05:00
func (s *StepExport) Cleanup(state multistep.StateBag) {}