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