github.com/rothwerx/packer@v0.9.0/builder/parallels/pvm/step_import.go (about)

     1  package pvm
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	parallelscommon "github.com/mitchellh/packer/builder/parallels/common"
     7  	"github.com/mitchellh/packer/packer"
     8  )
     9  
    10  // This step imports an PVM VM into Parallels.
    11  type StepImport struct {
    12  	Name       string
    13  	SourcePath string
    14  	vmName     string
    15  }
    16  
    17  func (s *StepImport) Run(state multistep.StateBag) multistep.StepAction {
    18  	driver := state.Get("driver").(parallelscommon.Driver)
    19  	ui := state.Get("ui").(packer.Ui)
    20  	config := state.Get("config").(*Config)
    21  
    22  	ui.Say(fmt.Sprintf("Importing VM: %s", s.SourcePath))
    23  	if err := driver.Import(s.Name, s.SourcePath, config.OutputDir, config.ReassignMac); 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  
    37  	if s.vmName == "" {
    38  		return
    39  	}
    40  
    41  	driver := state.Get("driver").(parallelscommon.Driver)
    42  	ui := state.Get("ui").(packer.Ui)
    43  
    44  	ui.Say("Unregistering virtual machine...")
    45  	if err := driver.Prlctl("unregister", s.vmName); err != nil {
    46  		ui.Error(fmt.Sprintf("Error unregistering virtual machine: %s", err))
    47  	}
    48  }