Skip to content
This repository has been archived by the owner on Jul 28, 2020. It is now read-only.

Commit

Permalink
Prefix Hyper-V commands with 'Hyper-V\'
Browse files Browse the repository at this point in the history
Helps in cases where Powershell Modules with conflicting names are
installed.

Issue brought up by @shouah in:
#111
  • Loading branch information
Anthony Emengo authored and pivotal committed Aug 23, 2019
1 parent 61d8738 commit b36c82e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
26 changes: 13 additions & 13 deletions driver/hyperv/hyperv.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ func (d *HyperV) CreateVM(name string, cpus int, memory int, efiPath string) (st
cfDevVHD = filepath.Join(d.Config.StateDir, "disk.vhdx")
)

command := fmt.Sprintf("New-VM -Name %s -Generation 2 -NoVHD", name)
command := fmt.Sprintf("Hyper-V\\New-VM -Name %s -Generation 2 -NoVHD", name)
_, err := d.Powershell.Output(command)
if err != nil {
return "", fmt.Errorf("creating new vm: %s", err)
}

command = fmt.Sprintf("Set-VM -Name %s "+
command = fmt.Sprintf("Hyper-V\\Set-VM -Name %s "+
"-AutomaticStartAction Nothing "+
"-AutomaticStopAction ShutDown "+
"-CheckpointType Disabled "+
Expand All @@ -30,26 +30,26 @@ func (d *HyperV) CreateVM(name string, cpus int, memory int, efiPath string) (st
return "", fmt.Errorf("setting vm properites (memoryMB:%d, cpus:%d): %s", memory, cpus, err)
}

command = fmt.Sprintf(`Add-VMDvdDrive -VMName %s -Path "%s"`, name, efiPath)
command = fmt.Sprintf(`Hyper-V\Add-VMDvdDrive -VMName %s -Path "%s"`, name, efiPath)
_, err = d.Powershell.Output(command)
if err != nil {
return "", fmt.Errorf("adding dvd drive %s: %s", efiPath, err)
}

command = fmt.Sprintf("Remove-VMNetworkAdapter -VMName %s", name)
command = fmt.Sprintf("Hyper-V\\Remove-VMNetworkAdapter -VMName %s", name)
_, err = d.Powershell.Output(command)
if err != nil {
fmt.Printf("failed to remove network adapter: %s", err)
}

command = fmt.Sprintf("Add-VMHardDiskDrive -VMName %s "+
command = fmt.Sprintf("Hyper-V\\Add-VMHardDiskDrive -VMName %s "+
`-Path "%s"`, name, cfDevVHD)
_, err = d.Powershell.Output(command)
if err != nil {
return "", fmt.Errorf("adding vhd %s : %s", cfDevVHD, err)
}

command = fmt.Sprintf("Set-VMFirmware "+
command = fmt.Sprintf("Hyper-V\\Set-VMFirmware "+
"-VMName %s "+
"-EnableSecureBoot Off "+
"-FirstBootDevice $cdrom",
Expand All @@ -59,7 +59,7 @@ func (d *HyperV) CreateVM(name string, cpus int, memory int, efiPath string) (st
return "", fmt.Errorf("setting firmware: %s", err)
}

command = fmt.Sprintf("Set-VMComPort "+
command = fmt.Sprintf("Hyper-V\\Set-VMComPort "+
"-VMName %s "+
"-number 1 "+
"-Path \\\\.\\pipe\\cfdev-com",
Expand All @@ -69,7 +69,7 @@ func (d *HyperV) CreateVM(name string, cpus int, memory int, efiPath string) (st
return "", fmt.Errorf("setting com port: %s", err)
}

output, err := d.Powershell.Output("((Get-VM -Name cfdev).Id).Guid")
output, err := d.Powershell.Output("((Hyper-V\\Get-VM -Name cfdev).Id).Guid")
if err != nil {
return "", fmt.Errorf("fetching VM Guid: %s", err)
}
Expand All @@ -85,7 +85,7 @@ func (d *HyperV) StartVM(vmName string) error {
return fmt.Errorf("hyperv vm with name %s does not exist", vmName)
}

command := fmt.Sprintf("Start-VM -Name %s", vmName)
command := fmt.Sprintf("Hyper-V\\Start-VM -Name %s", vmName)
if _, err := d.Powershell.Output(command); err != nil {
return fmt.Errorf("start-vm: %s", err)
}
Expand All @@ -100,7 +100,7 @@ func (d *HyperV) StopVM(vmName string) error {
return nil
}

command := fmt.Sprintf("Stop-VM -Name %s -Turnoff", vmName)
command := fmt.Sprintf("Hyper-V\\Stop-VM -Name %s -Turnoff", vmName)
if _, err := d.Powershell.Output(command); err != nil {
return fmt.Errorf("stopping vm: %s", err)
}
Expand All @@ -115,7 +115,7 @@ func (d *HyperV) DestroyVM(vmName string) error {
return nil
}

command := fmt.Sprintf("Remove-VM -Name %s -Force", vmName)
command := fmt.Sprintf("Hyper-V\\Remove-VM -Name %s -Force", vmName)
if _, err := d.Powershell.Output(command); err != nil {
return fmt.Errorf("removing vm: %s", err)
}
Expand All @@ -128,7 +128,7 @@ func (d *HyperV) IsVMRunning(vmName string) (bool, error) {
return false, err
}

command := fmt.Sprintf("Get-VM -Name %s | format-list -Property State", vmName)
command := fmt.Sprintf("Hyper-V\\Get-VM -Name %s | format-list -Property State", vmName)
output, err := d.Powershell.Output(command)
if err != nil {
return false, err
Expand All @@ -142,7 +142,7 @@ func (d *HyperV) IsVMRunning(vmName string) (bool, error) {
}

func (d *HyperV) exists(vmName string) (bool, error) {
command := fmt.Sprintf("Get-VM -Name %s*", vmName)
command := fmt.Sprintf("Hyper-V\\Get-VM -Name %s*", vmName)
output, err := d.Powershell.Output(command)
if err != nil {
return false, fmt.Errorf("getting vms: %s", err)
Expand Down
6 changes: 3 additions & 3 deletions driver/hyperv/ip_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (d *HyperV) RemoveLoopbackAliases(switchName string, addrs ...string) error
return nil
}

_, err = d.Powershell.Output(fmt.Sprintf("Remove-VMSwitch -Name %s -force", switchName))
_, err = d.Powershell.Output(fmt.Sprintf("Hyper-V\\Remove-VMSwitch -Name %s -force", switchName))
return err
}

Expand Down Expand Up @@ -78,12 +78,12 @@ func (d *HyperV) createSwitchIfNotExist(switchName string) error {
return nil
}

_, err = d.Powershell.Output(fmt.Sprintf("New-VMSwitch -Name %s -SwitchType Internal -Notes 'Switch for CF Dev Networking'", switchName))
_, err = d.Powershell.Output(fmt.Sprintf("Hyper-V\\New-VMSwitch -Name %s -SwitchType Internal -Notes 'Switch for CF Dev Networking'", switchName))
return err
}

func (d *HyperV) switchExists(switchName string) (bool, error) {
output, err := d.Powershell.Output(fmt.Sprintf("Get-VMSwitch %s*", switchName))
output, err := d.Powershell.Output(fmt.Sprintf("Hyper-V\\Get-VMSwitch %s*", switchName))
if err != nil {
return false, err
} else if output == "" {
Expand Down

0 comments on commit b36c82e

Please sign in to comment.