github.com/mitchellh/packer@v1.3.2/builder/vmware/common/driver_player5_windows.go (about)

     1  // +build windows
     2  
     3  package common
     4  
     5  import (
     6  	"log"
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  	"syscall"
    11  )
    12  
    13  func playerFindVdiskManager() (string, error) {
    14  	path, err := exec.LookPath("vmware-vdiskmanager.exe")
    15  	if err == nil {
    16  		return path, nil
    17  	}
    18  
    19  	return findFile("vmware-vdiskmanager.exe", playerProgramFilePaths()), nil
    20  }
    21  
    22  func playerFindQemuImg() (string, error) {
    23  	path, err := exec.LookPath("qemu-img.exe")
    24  	if err == nil {
    25  		return path, nil
    26  	}
    27  
    28  	return findFile("qemu-img.exe", playerProgramFilePaths()), nil
    29  }
    30  
    31  func playerFindVMware() (string, error) {
    32  	path, err := exec.LookPath("vmplayer.exe")
    33  	if err == nil {
    34  		return path, nil
    35  	}
    36  
    37  	return findFile("vmplayer.exe", playerProgramFilePaths()), nil
    38  }
    39  
    40  func playerFindVmrun() (string, error) {
    41  	path, err := exec.LookPath("vmrun.exe")
    42  	if err == nil {
    43  		return path, nil
    44  	}
    45  
    46  	return findFile("vmrun.exe", playerProgramFilePaths()), nil
    47  }
    48  
    49  func playerToolsIsoPath(flavor string) string {
    50  	return findFile(flavor+".iso", playerProgramFilePaths())
    51  }
    52  
    53  func playerDhcpLeasesPath(device string) string {
    54  	path, err := playerDhcpLeasesPathRegistry()
    55  	if err != nil {
    56  		log.Printf("Error finding leases in registry: %s", err)
    57  	} else if _, err := os.Stat(path); err == nil {
    58  		return path
    59  	}
    60  	return findFile("vmnetdhcp.leases", playerDataFilePaths())
    61  }
    62  
    63  func playerVmDhcpConfPath(device string) string {
    64  	// the device isn't actually used on windows hosts
    65  	path, err := playerDhcpConfigPathRegistry()
    66  	if err != nil {
    67  		log.Printf("Error finding configuration in registry: %s", err)
    68  	} else if _, err := os.Stat(path); err == nil {
    69  		return path
    70  	}
    71  	return findFile("vmnetdhcp.conf", playerDataFilePaths())
    72  }
    73  
    74  func playerVmnetnatConfPath(device string) string {
    75  	// the device isn't actually used on windows hosts
    76  	return findFile("vmnetnat.conf", playerDataFilePaths())
    77  }
    78  
    79  func playerNetmapConfPath() string {
    80  	return findFile("netmap.conf", playerDataFilePaths())
    81  }
    82  
    83  // This reads the VMware installation path from the Windows registry.
    84  func playerVMwareRoot() (s string, err error) {
    85  	key := `SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\vmplayer.exe`
    86  	subkey := "Path"
    87  	s, err = readRegString(syscall.HKEY_LOCAL_MACHINE, key, subkey)
    88  	if err != nil {
    89  		log.Printf(`Unable to read registry key %s\%s`, key, subkey)
    90  		return
    91  	}
    92  
    93  	return normalizePath(s), nil
    94  }
    95  
    96  // This reads the VMware DHCP leases path from the Windows registry.
    97  func playerDhcpLeasesPathRegistry() (s string, err error) {
    98  	key := "SYSTEM\\CurrentControlSet\\services\\VMnetDHCP\\Parameters"
    99  	subkey := "LeaseFile"
   100  	s, err = readRegString(syscall.HKEY_LOCAL_MACHINE, key, subkey)
   101  	if err != nil {
   102  		log.Printf(`Unable to read registry key %s\%s`, key, subkey)
   103  		return
   104  	}
   105  	return normalizePath(s), nil
   106  }
   107  
   108  // This reads the VMware DHCP configuration path from the Windows registry.
   109  func playerDhcpConfigPathRegistry() (s string, err error) {
   110  	key := "SYSTEM\\CurrentControlSet\\services\\VMnetDHCP\\Parameters"
   111  	subkey := "ConfFile"
   112  	s, err = readRegString(syscall.HKEY_LOCAL_MACHINE, key, subkey)
   113  	if err != nil {
   114  		log.Printf(`Unable to read registry key %s\%s`, key, subkey)
   115  		return
   116  	}
   117  	return normalizePath(s), nil
   118  }
   119  
   120  // playerProgramFilesPaths returns a list of paths that are eligible
   121  // to contain program files we may want just as vmware.exe.
   122  func playerProgramFilePaths() []string {
   123  	path, err := playerVMwareRoot()
   124  	if err != nil {
   125  		log.Printf("Error finding VMware root: %s", err)
   126  	}
   127  
   128  	paths := make([]string, 0, 5)
   129  	if os.Getenv("VMWARE_HOME") != "" {
   130  		paths = append(paths, os.Getenv("VMWARE_HOME"))
   131  	}
   132  
   133  	if path != "" {
   134  		paths = append(paths, path)
   135  	}
   136  
   137  	if os.Getenv("ProgramFiles(x86)") != "" {
   138  		paths = append(paths,
   139  			filepath.Join(os.Getenv("ProgramFiles(x86)"), "/VMware/VMware Player"))
   140  	}
   141  
   142  	if os.Getenv("ProgramFiles") != "" {
   143  		paths = append(paths,
   144  			filepath.Join(os.Getenv("ProgramFiles"), "/VMware/VMware Player"))
   145  	}
   146  
   147  	if os.Getenv("QEMU_HOME") != "" {
   148  		paths = append(paths, os.Getenv("QEMU_HOME"))
   149  	}
   150  
   151  	if os.Getenv("ProgramFiles(x86)") != "" {
   152  		paths = append(paths,
   153  			filepath.Join(os.Getenv("ProgramFiles(x86)"), "/QEMU"))
   154  	}
   155  
   156  	if os.Getenv("ProgramFiles") != "" {
   157  		paths = append(paths,
   158  			filepath.Join(os.Getenv("ProgramFiles"), "/QEMU"))
   159  	}
   160  
   161  	if os.Getenv("SystemDrive") != "" {
   162  		paths = append(paths,
   163  			filepath.Join(os.Getenv("SystemDrive"), "/QEMU"))
   164  	}
   165  
   166  	return paths
   167  }
   168  
   169  // playerDataFilePaths returns a list of paths that are eligible
   170  // to contain data files we may want such as vmnet NAT configuration files.
   171  func playerDataFilePaths() []string {
   172  	leasesPath, err := playerDhcpLeasesPathRegistry()
   173  	if err != nil {
   174  		log.Printf("Error getting DHCP leases path: %s", err)
   175  	}
   176  
   177  	if leasesPath != "" {
   178  		leasesPath = filepath.Dir(leasesPath)
   179  	}
   180  
   181  	paths := make([]string, 0, 5)
   182  	if os.Getenv("VMWARE_DATA") != "" {
   183  		paths = append(paths, os.Getenv("VMWARE_DATA"))
   184  	}
   185  
   186  	if leasesPath != "" {
   187  		paths = append(paths, leasesPath)
   188  	}
   189  
   190  	if os.Getenv("ProgramData") != "" {
   191  		paths = append(paths,
   192  			filepath.Join(os.Getenv("ProgramData"), "/VMware"))
   193  	}
   194  
   195  	if os.Getenv("ALLUSERSPROFILE") != "" {
   196  		paths = append(paths,
   197  			filepath.Join(os.Getenv("ALLUSERSPROFILE"), "/Application Data/VMware"))
   198  	}
   199  
   200  	return paths
   201  }