github.com/kaixiang/packer@v0.5.2-0.20140114230416-1f5786b0d7f1/builder/virtualbox/ovf/step_import.go (about) 1 package ovf 2 3 import ( 4 "fmt" 5 "github.com/mitchellh/multistep" 6 vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" 7 "github.com/mitchellh/packer/packer" 8 ) 9 10 // This step imports an OVF VM into VirtualBox. 11 type StepImport struct { 12 Name string 13 SourcePath string 14 15 vmName string 16 } 17 18 func (s *StepImport) Run(state multistep.StateBag) multistep.StepAction { 19 driver := state.Get("driver").(vboxcommon.Driver) 20 ui := state.Get("ui").(packer.Ui) 21 22 ui.Say(fmt.Sprintf("Importing VM: %s", s.SourcePath)) 23 if err := driver.Import(s.Name, s.SourcePath); err != nil { 24 err := fmt.Errorf("Error importing VM: %s", err) 25 state.Put("error", err) 26 ui.Error(err.Error()) 27 return multistep.ActionHalt 28 } 29 30 s.vmName = s.Name 31 state.Put("vmName", s.Name) 32 return multistep.ActionContinue 33 } 34 35 func (s *StepImport) Cleanup(state multistep.StateBag) { 36 if s.vmName == "" { 37 return 38 } 39 40 driver := state.Get("driver").(vboxcommon.Driver) 41 ui := state.Get("ui").(packer.Ui) 42 43 ui.Say("Unregistering and deleting imported VM...") 44 if err := driver.Delete(s.vmName); err != nil { 45 ui.Error(fmt.Sprintf("Error deleting VM: %s", err)) 46 } 47 }