2013-12-22 18:19:19 -05:00
|
|
|
package ovf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This step imports an OVF VM into VirtualBox.
|
|
|
|
type StepImport struct {
|
|
|
|
Name string
|
|
|
|
SourcePath string
|
2014-02-12 18:29:13 -05:00
|
|
|
ImportOpts string
|
2013-12-22 18:19:19 -05:00
|
|
|
|
|
|
|
vmName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepImport) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Importing VM: %s", s.SourcePath))
|
2014-02-12 18:29:13 -05:00
|
|
|
if err := driver.Import(s.Name, s.SourcePath, s.ImportOpts); err != nil {
|
2013-12-22 18:19:19 -05:00
|
|
|
err := fmt.Errorf("Error importing VM: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
s.vmName = s.Name
|
|
|
|
state.Put("vmName", s.Name)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepImport) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.vmName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Unregistering and deleting imported VM...")
|
|
|
|
if err := driver.Delete(s.vmName); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting VM: %s", err))
|
|
|
|
}
|
|
|
|
}
|