github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/hyperv/common/driver_ps_4.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"runtime"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/hashicorp/packer/common/powershell"
    12  	"github.com/hashicorp/packer/common/powershell/hyperv"
    13  )
    14  
    15  type HypervPS4Driver struct {
    16  }
    17  
    18  func NewHypervPS4Driver() (Driver, error) {
    19  	appliesTo := "Applies to Windows 8.1, Windows PowerShell 4.0, Windows Server 2012 R2 only"
    20  
    21  	// Check this is Windows
    22  	if runtime.GOOS != "windows" {
    23  		err := fmt.Errorf("%s", appliesTo)
    24  		return nil, err
    25  	}
    26  
    27  	ps4Driver := &HypervPS4Driver{}
    28  
    29  	if err := ps4Driver.Verify(); err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	return ps4Driver, nil
    34  }
    35  
    36  func (d *HypervPS4Driver) IsRunning(vmName string) (bool, error) {
    37  	return hyperv.IsRunning(vmName)
    38  }
    39  
    40  func (d *HypervPS4Driver) IsOff(vmName string) (bool, error) {
    41  	return hyperv.IsOff(vmName)
    42  }
    43  
    44  func (d *HypervPS4Driver) Uptime(vmName string) (uint64, error) {
    45  	return hyperv.Uptime(vmName)
    46  }
    47  
    48  // Start starts a VM specified by the name given.
    49  func (d *HypervPS4Driver) Start(vmName string) error {
    50  	return hyperv.StartVirtualMachine(vmName)
    51  }
    52  
    53  // Stop stops a VM specified by the name given.
    54  func (d *HypervPS4Driver) Stop(vmName string) error {
    55  	return hyperv.StopVirtualMachine(vmName)
    56  }
    57  
    58  func (d *HypervPS4Driver) Verify() error {
    59  
    60  	if err := d.verifyPSVersion(); err != nil {
    61  		return err
    62  	}
    63  
    64  	if err := d.verifyPSHypervModule(); err != nil {
    65  		return err
    66  	}
    67  
    68  	if err := d.verifyHypervPermissions(); err != nil {
    69  		return err
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  // Get mac address for VM.
    76  func (d *HypervPS4Driver) Mac(vmName string) (string, error) {
    77  	res, err := hyperv.Mac(vmName)
    78  
    79  	if err != nil {
    80  		return res, err
    81  	}
    82  
    83  	if res == "" {
    84  		err := fmt.Errorf("%s", "No mac address.")
    85  		return res, err
    86  	}
    87  
    88  	return res, err
    89  }
    90  
    91  // Get ip address for mac address.
    92  func (d *HypervPS4Driver) IpAddress(mac string) (string, error) {
    93  	res, err := hyperv.IpAddress(mac)
    94  
    95  	if err != nil {
    96  		return res, err
    97  	}
    98  
    99  	if res == "" {
   100  		err := fmt.Errorf("%s", "No ip address.")
   101  		return res, err
   102  	}
   103  	return res, err
   104  }
   105  
   106  // Get host name from ip address
   107  func (d *HypervPS4Driver) GetHostName(ip string) (string, error) {
   108  	return powershell.GetHostName(ip)
   109  }
   110  
   111  func (d *HypervPS4Driver) GetVirtualMachineGeneration(vmName string) (uint, error) {
   112  	return hyperv.GetVirtualMachineGeneration(vmName)
   113  }
   114  
   115  // Finds the IP address of a host adapter connected to switch
   116  func (d *HypervPS4Driver) GetHostAdapterIpAddressForSwitch(switchName string) (string, error) {
   117  	res, err := hyperv.GetHostAdapterIpAddressForSwitch(switchName)
   118  
   119  	if err != nil {
   120  		return res, err
   121  	}
   122  
   123  	if res == "" {
   124  		err := fmt.Errorf("%s", "No ip address.")
   125  		return res, err
   126  	}
   127  	return res, err
   128  }
   129  
   130  // Type scan codes to virtual keyboard of vm
   131  func (d *HypervPS4Driver) TypeScanCodes(vmName string, scanCodes string) error {
   132  	return hyperv.TypeScanCodes(vmName, scanCodes)
   133  }
   134  
   135  // Get network adapter address
   136  func (d *HypervPS4Driver) GetVirtualMachineNetworkAdapterAddress(vmName string) (string, error) {
   137  	return hyperv.GetVirtualMachineNetworkAdapterAddress(vmName)
   138  }
   139  
   140  //Set the vlan to use for switch
   141  func (d *HypervPS4Driver) SetNetworkAdapterVlanId(switchName string, vlanId string) error {
   142  	return hyperv.SetNetworkAdapterVlanId(switchName, vlanId)
   143  }
   144  
   145  //Set the vlan to use for machine
   146  func (d *HypervPS4Driver) SetVirtualMachineVlanId(vmName string, vlanId string) error {
   147  	return hyperv.SetVirtualMachineVlanId(vmName, vlanId)
   148  }
   149  
   150  func (d *HypervPS4Driver) SetVmNetworkAdapterMacAddress(vmName string, mac string) error {
   151  	return hyperv.SetVmNetworkAdapterMacAddress(vmName, mac)
   152  }
   153  
   154  func (d *HypervPS4Driver) UntagVirtualMachineNetworkAdapterVlan(vmName string, switchName string) error {
   155  	return hyperv.UntagVirtualMachineNetworkAdapterVlan(vmName, switchName)
   156  }
   157  
   158  func (d *HypervPS4Driver) CreateExternalVirtualSwitch(vmName string, switchName string) error {
   159  	return hyperv.CreateExternalVirtualSwitch(vmName, switchName)
   160  }
   161  
   162  func (d *HypervPS4Driver) GetVirtualMachineSwitchName(vmName string) (string, error) {
   163  	return hyperv.GetVirtualMachineSwitchName(vmName)
   164  }
   165  
   166  func (d *HypervPS4Driver) ConnectVirtualMachineNetworkAdapterToSwitch(vmName string, switchName string) error {
   167  	return hyperv.ConnectVirtualMachineNetworkAdapterToSwitch(vmName, switchName)
   168  }
   169  
   170  func (d *HypervPS4Driver) DeleteVirtualSwitch(switchName string) error {
   171  	return hyperv.DeleteVirtualSwitch(switchName)
   172  }
   173  
   174  func (d *HypervPS4Driver) CreateVirtualSwitch(switchName string, switchType string) (bool, error) {
   175  	return hyperv.CreateVirtualSwitch(switchName, switchType)
   176  }
   177  
   178  func (d *HypervPS4Driver) AddVirtualMachineHardDrive(vmName string, vhdFile string, vhdName string, vhdSizeBytes int64, diskBlockSize int64, controllerType string) error {
   179  	return hyperv.AddVirtualMachineHardDiskDrive(vmName, vhdFile, vhdName, vhdSizeBytes, diskBlockSize, controllerType)
   180  }
   181  
   182  func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, harddrivePath string, vhdPath string, ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool, fixedVHD bool) error {
   183  	return hyperv.CreateVirtualMachine(vmName, path, harddrivePath, vhdPath, ram, diskSize, diskBlockSize, switchName, generation, diffDisks, fixedVHD)
   184  }
   185  
   186  func (d *HypervPS4Driver) CloneVirtualMachine(cloneFromVmxcPath string, cloneFromVmName string, cloneFromSnapshotName string, cloneAllSnapshots bool, vmName string, path string, harddrivePath string, ram int64, switchName string) error {
   187  	return hyperv.CloneVirtualMachine(cloneFromVmxcPath, cloneFromVmName, cloneFromSnapshotName, cloneAllSnapshots, vmName, path, harddrivePath, ram, switchName)
   188  }
   189  
   190  func (d *HypervPS4Driver) DeleteVirtualMachine(vmName string) error {
   191  	return hyperv.DeleteVirtualMachine(vmName)
   192  }
   193  
   194  func (d *HypervPS4Driver) SetVirtualMachineCpuCount(vmName string, cpu uint) error {
   195  	return hyperv.SetVirtualMachineCpuCount(vmName, cpu)
   196  }
   197  
   198  func (d *HypervPS4Driver) SetVirtualMachineMacSpoofing(vmName string, enable bool) error {
   199  	return hyperv.SetVirtualMachineMacSpoofing(vmName, enable)
   200  }
   201  
   202  func (d *HypervPS4Driver) SetVirtualMachineDynamicMemory(vmName string, enable bool) error {
   203  	return hyperv.SetVirtualMachineDynamicMemory(vmName, enable)
   204  }
   205  
   206  func (d *HypervPS4Driver) SetVirtualMachineSecureBoot(vmName string, enable bool, templateName string) error {
   207  	return hyperv.SetVirtualMachineSecureBoot(vmName, enable, templateName)
   208  }
   209  
   210  func (d *HypervPS4Driver) SetVirtualMachineVirtualizationExtensions(vmName string, enable bool) error {
   211  	return hyperv.SetVirtualMachineVirtualizationExtensions(vmName, enable)
   212  }
   213  
   214  func (d *HypervPS4Driver) EnableVirtualMachineIntegrationService(vmName string, integrationServiceName string) error {
   215  	return hyperv.EnableVirtualMachineIntegrationService(vmName, integrationServiceName)
   216  }
   217  
   218  func (d *HypervPS4Driver) ExportVirtualMachine(vmName string, path string) error {
   219  	return hyperv.ExportVirtualMachine(vmName, path)
   220  }
   221  
   222  func (d *HypervPS4Driver) CompactDisks(expPath string, vhdDir string) error {
   223  	return hyperv.CompactDisks(expPath, vhdDir)
   224  }
   225  
   226  func (d *HypervPS4Driver) CopyExportedVirtualMachine(expPath string, outputPath string, vhdDir string, vmDir string) error {
   227  	return hyperv.CopyExportedVirtualMachine(expPath, outputPath, vhdDir, vmDir)
   228  }
   229  
   230  func (d *HypervPS4Driver) RestartVirtualMachine(vmName string) error {
   231  	return hyperv.RestartVirtualMachine(vmName)
   232  }
   233  
   234  func (d *HypervPS4Driver) CreateDvdDrive(vmName string, isoPath string, generation uint) (uint, uint, error) {
   235  	return hyperv.CreateDvdDrive(vmName, isoPath, generation)
   236  }
   237  
   238  func (d *HypervPS4Driver) MountDvdDrive(vmName string, path string, controllerNumber uint, controllerLocation uint) error {
   239  	return hyperv.MountDvdDrive(vmName, path, controllerNumber, controllerLocation)
   240  }
   241  
   242  func (d *HypervPS4Driver) SetBootDvdDrive(vmName string, controllerNumber uint, controllerLocation uint, generation uint) error {
   243  	return hyperv.SetBootDvdDrive(vmName, controllerNumber, controllerLocation, generation)
   244  }
   245  
   246  func (d *HypervPS4Driver) UnmountDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {
   247  	return hyperv.UnmountDvdDrive(vmName, controllerNumber, controllerLocation)
   248  }
   249  
   250  func (d *HypervPS4Driver) DeleteDvdDrive(vmName string, controllerNumber uint, controllerLocation uint) error {
   251  	return hyperv.DeleteDvdDrive(vmName, controllerNumber, controllerLocation)
   252  }
   253  
   254  func (d *HypervPS4Driver) MountFloppyDrive(vmName string, path string) error {
   255  	return hyperv.MountFloppyDrive(vmName, path)
   256  }
   257  
   258  func (d *HypervPS4Driver) UnmountFloppyDrive(vmName string) error {
   259  	return hyperv.UnmountFloppyDrive(vmName)
   260  }
   261  
   262  func (d *HypervPS4Driver) verifyPSVersion() error {
   263  
   264  	log.Printf("Enter method: %s", "verifyPSVersion")
   265  	// check PS is available and is of proper version
   266  	versionCmd := "$host.version.Major"
   267  
   268  	var ps powershell.PowerShellCmd
   269  	cmdOut, err := ps.Output(versionCmd)
   270  	if err != nil {
   271  		return err
   272  	}
   273  
   274  	versionOutput := strings.TrimSpace(cmdOut)
   275  	log.Printf("%s output: %s", versionCmd, versionOutput)
   276  
   277  	ver, err := strconv.ParseInt(versionOutput, 10, 32)
   278  
   279  	if err != nil {
   280  		return err
   281  	}
   282  
   283  	if ver < 4 {
   284  		err := fmt.Errorf("%s", "Windows PowerShell version 4.0 or higher is expected")
   285  		return err
   286  	}
   287  
   288  	return nil
   289  }
   290  
   291  func (d *HypervPS4Driver) verifyPSHypervModule() error {
   292  
   293  	log.Printf("Enter method: %s", "verifyPSHypervModule")
   294  
   295  	versionCmd := "function foo(){try{ $commands = Get-Command -Module Hyper-V;if($commands.Length -eq 0){return $false} }catch{return $false}; return $true} foo"
   296  
   297  	var ps powershell.PowerShellCmd
   298  	cmdOut, err := ps.Output(versionCmd)
   299  	if err != nil {
   300  		return err
   301  	}
   302  
   303  	if powershell.IsFalse(cmdOut) {
   304  		err := fmt.Errorf("%s", "PS Hyper-V module is not loaded. Make sure Hyper-V feature is on.")
   305  		return err
   306  	}
   307  
   308  	return nil
   309  }
   310  
   311  func (d *HypervPS4Driver) isCurrentUserAHyperVAdministrator() (bool, error) {
   312  	//SID:S-1-5-32-578 = 'BUILTIN\Hyper-V Administrators'
   313  	//https://support.microsoft.com/en-us/help/243330/well-known-security-identifiers-in-windows-operating-systems
   314  
   315  	var script = `
   316  $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
   317  $principal = new-object System.Security.Principal.WindowsPrincipal($identity)
   318  $hypervrole = [System.Security.Principal.SecurityIdentifier]"S-1-5-32-578"
   319  return $principal.IsInRole($hypervrole)
   320  `
   321  
   322  	var ps powershell.PowerShellCmd
   323  	cmdOut, err := ps.Output(script)
   324  	if err != nil {
   325  		return false, err
   326  	}
   327  
   328  	return powershell.IsTrue(cmdOut), nil
   329  }
   330  
   331  func (d *HypervPS4Driver) verifyHypervPermissions() error {
   332  
   333  	log.Printf("Enter method: %s", "verifyHypervPermissions")
   334  
   335  	hyperVAdmin, err := d.isCurrentUserAHyperVAdministrator()
   336  	if err != nil {
   337  		log.Printf("Error discovering if current is is a Hyper-V Admin: %s", err)
   338  	}
   339  	if !hyperVAdmin {
   340  
   341  		isAdmin, _ := powershell.IsCurrentUserAnAdministrator()
   342  
   343  		if !isAdmin {
   344  			err := fmt.Errorf("%s", "Current user is not a member of 'Hyper-V Administrators' or 'Administrators' group")
   345  			return err
   346  		}
   347  	}
   348  
   349  	return nil
   350  }
   351  
   352  // Connect connects to a VM specified by the name given.
   353  func (d *HypervPS4Driver) Connect(vmName string) (context.CancelFunc, error) {
   354  	return hyperv.ConnectVirtualMachine(vmName)
   355  }
   356  
   357  // Disconnect disconnects to a VM specified by calling the context cancel function returned
   358  // from Connect.
   359  func (d *HypervPS4Driver) Disconnect(cancel context.CancelFunc) {
   360  	hyperv.DisconnectVirtualMachine(cancel)
   361  }