github.com/rothwerx/packer@v0.9.0/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  	ImportFlags []string
    15  
    16  	vmName string
    17  }
    18  
    19  func (s *StepImport) Run(state multistep.StateBag) multistep.StepAction {
    20  	driver := state.Get("driver").(vboxcommon.Driver)
    21  	ui := state.Get("ui").(packer.Ui)
    22  
    23  	ui.Say(fmt.Sprintf("Importing VM: %s", s.SourcePath))
    24  	if err := driver.Import(s.Name, s.SourcePath, 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  }