2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-21 07:36:07 -04:00
|
|
|
"fmt"
|
2016-03-29 17:56:26 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/uuid"
|
2015-06-21 07:36:07 -04:00
|
|
|
)
|
|
|
|
|
2018-07-05 20:45:40 -04:00
|
|
|
// This step creates an external switch for the VM.
|
2015-06-21 07:36:07 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// SwitchName string - The name of the Switch
|
|
|
|
type StepCreateExternalSwitch struct {
|
2015-10-30 04:23:30 -04:00
|
|
|
SwitchName string
|
2015-06-21 07:36:07 -04:00
|
|
|
oldSwitchName string
|
|
|
|
}
|
|
|
|
|
2018-07-05 20:45:40 -04:00
|
|
|
// Run runs the step required to create an external switch. Depending on
|
|
|
|
// the connectivity of the host machine, the external switch will allow the
|
|
|
|
// build VM to connect to the outside world.
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCreateExternalSwitch) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-10-30 04:23:30 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2015-06-21 07:36:07 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
vmName := state.Get("vmName").(string)
|
2018-03-13 03:22:37 -04:00
|
|
|
errorMsg := "Error creating external switch: %s"
|
2015-06-21 07:36:07 -04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
ui.Say("Creating external switch...")
|
|
|
|
|
2016-03-29 17:56:26 -04:00
|
|
|
packerExternalSwitchName := "paes_" + uuid.TimeOrderedUUID()
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2018-07-05 20:45:40 -04:00
|
|
|
// CreateExternalVirtualSwitch checks for an existing external switch,
|
|
|
|
// creating one if required, and connects the VM to it
|
2015-10-30 04:23:30 -04:00
|
|
|
err = driver.CreateExternalVirtualSwitch(vmName, packerExternalSwitchName)
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
2018-07-05 20:45:40 -04:00
|
|
|
err := fmt.Errorf(errorMsg, err)
|
|
|
|
state.Put("error", err)
|
2015-06-21 07:36:07 -04:00
|
|
|
ui.Error(err.Error())
|
2015-10-30 04:23:30 -04:00
|
|
|
s.SwitchName = ""
|
2015-06-21 07:36:07 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-10-30 04:23:30 -04:00
|
|
|
|
|
|
|
switchName, err := driver.GetVirtualMachineSwitchName(vmName)
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf(errorMsg, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(switchName) == 0 {
|
|
|
|
err := fmt.Errorf(errorMsg, err)
|
|
|
|
state.Put("error", "Can't get the VM switch name")
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("External switch name is: '" + switchName + "'")
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
if switchName != packerExternalSwitchName {
|
2015-06-21 07:36:07 -04:00
|
|
|
s.SwitchName = ""
|
|
|
|
} else {
|
2015-10-30 04:23:30 -04:00
|
|
|
s.SwitchName = packerExternalSwitchName
|
2015-06-21 07:36:07 -04:00
|
|
|
s.oldSwitchName = state.Get("SwitchName").(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the final name in the state bag so others can use it
|
|
|
|
state.Put("SwitchName", switchName)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateExternalSwitch) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.SwitchName == "" {
|
|
|
|
return
|
|
|
|
}
|
2015-10-30 04:23:30 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2015-06-21 07:36:07 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
|
|
|
ui.Say("Unregistering and deleting external switch...")
|
|
|
|
|
|
|
|
errMsg := "Error deleting external switch: %s"
|
|
|
|
|
|
|
|
// connect the vm to the old switch
|
|
|
|
if s.oldSwitchName == "" {
|
|
|
|
ui.Error(fmt.Sprintf(errMsg, "the old switch name is empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-09 07:17:43 -04:00
|
|
|
err := driver.ConnectVirtualMachineNetworkAdapterToSwitch(vmName, s.oldSwitchName)
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(errMsg, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("SwitchName", s.oldSwitchName)
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
err = driver.DeleteVirtualSwitch(s.SwitchName)
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(errMsg, err))
|
|
|
|
}
|
|
|
|
}
|