github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/hyperv/common/step_create_external_switch.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mitchellh/multistep"
     7  	"github.com/mitchellh/packer/common/uuid"
     8  	"github.com/mitchellh/packer/packer"
     9  )
    10  
    11  // This step creates switch for VM.
    12  //
    13  // Produces:
    14  //   SwitchName string - The name of the Switch
    15  type StepCreateExternalSwitch struct {
    16  	SwitchName    string
    17  	oldSwitchName string
    18  }
    19  
    20  func (s *StepCreateExternalSwitch) Run(state multistep.StateBag) multistep.StepAction {
    21  	driver := state.Get("driver").(Driver)
    22  	ui := state.Get("ui").(packer.Ui)
    23  
    24  	vmName := state.Get("vmName").(string)
    25  	errorMsg := "Error createing external switch: %s"
    26  	var err error
    27  
    28  	ui.Say("Creating external switch...")
    29  
    30  	packerExternalSwitchName := "paes_" + uuid.TimeOrderedUUID()
    31  
    32  	err = driver.CreateExternalVirtualSwitch(vmName, packerExternalSwitchName)
    33  	if err != nil {
    34  		err := fmt.Errorf("Error creating switch: %s", err)
    35  		state.Put(errorMsg, err)
    36  		ui.Error(err.Error())
    37  		s.SwitchName = ""
    38  		return multistep.ActionHalt
    39  	}
    40  
    41  	switchName, err := driver.GetVirtualMachineSwitchName(vmName)
    42  	if err != nil {
    43  		err := fmt.Errorf(errorMsg, err)
    44  		state.Put("error", err)
    45  		ui.Error(err.Error())
    46  		return multistep.ActionHalt
    47  	}
    48  
    49  	if len(switchName) == 0 {
    50  		err := fmt.Errorf(errorMsg, err)
    51  		state.Put("error", "Can't get the VM switch name")
    52  		ui.Error(err.Error())
    53  		return multistep.ActionHalt
    54  	}
    55  
    56  	ui.Say("External switch name is: '" + switchName + "'")
    57  
    58  	if switchName != packerExternalSwitchName {
    59  		s.SwitchName = ""
    60  	} else {
    61  		s.SwitchName = packerExternalSwitchName
    62  		s.oldSwitchName = state.Get("SwitchName").(string)
    63  	}
    64  
    65  	// Set the final name in the state bag so others can use it
    66  	state.Put("SwitchName", switchName)
    67  
    68  	return multistep.ActionContinue
    69  }
    70  
    71  func (s *StepCreateExternalSwitch) Cleanup(state multistep.StateBag) {
    72  	if s.SwitchName == "" {
    73  		return
    74  	}
    75  	driver := state.Get("driver").(Driver)
    76  	ui := state.Get("ui").(packer.Ui)
    77  	vmName := state.Get("vmName").(string)
    78  
    79  	ui.Say("Unregistering and deleting external switch...")
    80  
    81  	var err error = nil
    82  
    83  	errMsg := "Error deleting external switch: %s"
    84  
    85  	// connect the vm to the old switch
    86  	if s.oldSwitchName == "" {
    87  		ui.Error(fmt.Sprintf(errMsg, "the old switch name is empty"))
    88  		return
    89  	}
    90  
    91  	err = driver.ConnectVirtualMachineNetworkAdapterToSwitch(vmName, s.oldSwitchName)
    92  	if err != nil {
    93  		ui.Error(fmt.Sprintf(errMsg, err))
    94  		return
    95  	}
    96  
    97  	state.Put("SwitchName", s.oldSwitchName)
    98  
    99  	err = driver.DeleteVirtualSwitch(s.SwitchName)
   100  	if err != nil {
   101  		ui.Error(fmt.Sprintf(errMsg, err))
   102  	}
   103  }