packer-cn/builder/virtualbox/step_export.go

59 lines
1.5 KiB
Go
Raw Normal View History

2013-06-12 21:07:08 -04:00
package virtualbox
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"path/filepath"
)
// 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{}
func (s *stepExport) Run(state map[string]interface{}) multistep.StepAction {
config := state["config"].(*config)
driver := state["driver"].(Driver)
ui := state["ui"].(packer.Ui)
vmName := state["vmName"].(string)
2013-07-05 14:00:18 -04:00
// Clear out the Packer-created forwarding rule
ui.Say(fmt.Sprintf("Deleting forwarded port mapping for SSH (host port %d)", state["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-07-05 14:00:18 -04:00
state["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-06-12 21:07:08 -04:00
outputPath := filepath.Join(config.OutputDir, "packer.ovf")
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)
state["error"] = err
ui.Error(err.Error())
2013-06-12 21:07:08 -04:00
return multistep.ActionHalt
}
state["exportPath"] = outputPath
return multistep.ActionContinue
}
func (s *stepExport) Cleanup(state map[string]interface{}) {}