packer-cn/builder/virtualbox/ovf/step_import.go

49 lines
1.2 KiB
Go
Raw Normal View History

package ovf
import (
"fmt"
2017-04-04 16:39:01 -04:00
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
)
// This step imports an OVF VM into VirtualBox.
type StepImport struct {
Name string
ImportFlags []string
vmName string
}
func (s *StepImport) Run(state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(vboxcommon.Driver)
ui := state.Get("ui").(packer.Ui)
vmPath := state.Get("vm_path").(string)
ui.Say(fmt.Sprintf("Importing VM: %s", vmPath))
if err := driver.Import(s.Name, vmPath, s.ImportFlags); err != nil {
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))
}
}