check to make sure a vm-name isn't already in use before trying to launch a vm with said name.

This commit is contained in:
Megan Marsh 2019-06-13 14:09:45 -07:00
parent ccd4397f26
commit 2e821da84b
5 changed files with 37 additions and 2 deletions

View File

@ -73,6 +73,8 @@ type Driver interface {
DeleteVirtualSwitch(string) error
CheckVMName(string) error
CreateVirtualMachine(string, string, string, int64, int64, int64, string, uint, bool, bool, string) error
AddVirtualMachineHardDrive(string, string, string, int64, int64, string) error

View File

@ -110,6 +110,9 @@ type DriverMock struct {
DeleteVirtualSwitch_SwitchName string
DeleteVirtualSwitch_Err error
CheckVMName_Called bool
CheckVMName_Err error
CreateVirtualSwitch_Called bool
CreateVirtualSwitch_SwitchName string
CreateVirtualSwitch_SwitchType string
@ -421,6 +424,11 @@ func (d *DriverMock) AddVirtualMachineHardDrive(vmName string, vhdFile string, v
return d.AddVirtualMachineHardDrive_Err
}
func (d *DriverMock) CheckVMName(vmName string) error {
d.CheckVMName_Called = true
return d.CheckVMName_Err
}
func (d *DriverMock) CreateVirtualMachine(vmName string, path string, harddrivePath string,
ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint,
diffDisks bool, fixedVHD bool, version string) error {

View File

@ -186,6 +186,10 @@ func (d *HypervPS4Driver) AddVirtualMachineHardDrive(vmName string, vhdFile stri
diskBlockSize, controllerType)
}
func (d *HypervPS4Driver) CheckVMName(vmName string) error {
return hyperv.CheckVMName(vmName)
}
func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64,
diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool,
fixedVHD bool, version string) error {

View File

@ -48,6 +48,14 @@ func (s *StepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis
path = v.(string)
}
err := driver.CheckVMName(s.VMName)
if err != nil {
s.KeepRegistered = true
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Determine if we even have an existing virtual harddrive to attach
harddrivePath := ""
if harddrivePathRaw, ok := state.GetOk("iso_path"); ok {
@ -66,7 +74,7 @@ func (s *StepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis
diskSize := int64(s.DiskSize) * 1024 * 1024
diskBlockSize := int64(s.DiskBlockSize) * 1024 * 1024
err := driver.CreateVirtualMachine(s.VMName, path, harddrivePath, ramSize, diskSize, diskBlockSize,
err = driver.CreateVirtualMachine(s.VMName, path, harddrivePath, ramSize, diskSize, diskBlockSize,
s.SwitchName, s.Generation, s.DifferencingDisk, s.FixedVHD, s.Version)
if err != nil {
err := fmt.Errorf("Error creating virtual machine: %s", err)

View File

@ -283,10 +283,23 @@ Hyper-V\New-VM -Name "{{ .VMName }}" -Path "{{ .Path }}" -MemoryStartupBytes {{
return final, nil
}
func CheckVMName(vmName string) error {
// Check that no vm with the same name is registered, to prevent
// namespace collisions
var gs powershell.PowerShellCmd
getVMCmd := fmt.Sprintf(`Hyper-V\Get-VM -Name "%s"`, vmName)
if err := gs.Run(getVMCmd); err == nil {
return fmt.Errorf("A virtual machine with the name %s is already"+
" defined in Hyper-V. To avoid a name collision, please set your "+
"vm_name to a unique value", vmName)
}
return nil
}
func CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64,
diskSize int64, diskBlockSize int64, switchName string, generation uint,
diffDisks bool, fixedVHD bool, version string) error {
opts := scriptOptions{
Version: version,
VMName: vmName,