github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/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  	ImportFlags []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  	vmPath := state.Get("vm_path").(string)
    22  
    23  	ui.Say(fmt.Sprintf("Importing VM: %s", vmPath))
    24  	if err := driver.Import(s.Name, vmPath, s.ImportFlags); err != nil {
    25  		err := fmt.Errorf("Error importing VM: %s", err)
    26  		state.Put("error", err)
    27  		ui.Error(err.Error())
    28  		return multistep.ActionHalt
    29  	}
    30  
    31  	s.vmName = s.Name
    32  	state.Put("vmName", s.Name)
    33  	return multistep.ActionContinue
    34  }
    35  
    36  func (s *StepImport) Cleanup(state multistep.StateBag) {
    37  	if s.vmName == "" {
    38  		return
    39  	}
    40  
    41  	driver := state.Get("driver").(vboxcommon.Driver)
    42  	ui := state.Get("ui").(packer.Ui)
    43  
    44  	ui.Say("Unregistering and deleting imported VM...")
    45  	if err := driver.Delete(s.vmName); err != nil {
    46  		ui.Error(fmt.Sprintf("Error deleting VM: %s", err))
    47  	}
    48  }