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

     1  // +build !windows
     2  
     3  // These functions are compatible with WS 9 and 10 on *NIX
     4  package common
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"log"
    10  	"os"
    11  	"os/exec"
    12  	"path/filepath"
    13  	"regexp"
    14  	"runtime"
    15  )
    16  
    17  func playerFindVdiskManager() (string, error) {
    18  	return exec.LookPath("vmware-vdiskmanager")
    19  }
    20  
    21  func playerFindQemuImg() (string, error) {
    22  	return exec.LookPath("qemu-img")
    23  }
    24  
    25  func playerFindVMware() (string, error) {
    26  	return exec.LookPath("vmplayer")
    27  }
    28  
    29  func playerFindVmrun() (string, error) {
    30  	return exec.LookPath("vmrun")
    31  }
    32  
    33  func playerToolsIsoPath(flavor string) string {
    34  	return "/usr/lib/vmware/isoimages/" + flavor + ".iso"
    35  }
    36  
    37  // return the base path to vmware's config on the host
    38  func playerVMwareRoot() (s string, err error) {
    39  	return "/etc/vmware", nil
    40  }
    41  
    42  func playerDhcpLeasesPath(device string) string {
    43  	base, err := playerVMwareRoot()
    44  	if err != nil {
    45  		log.Printf("Error finding VMware root: %s", err)
    46  		return ""
    47  	}
    48  
    49  	// Build the base path to VMware configuration for specified device: `/etc/vmware/${device}`
    50  	devicebase := filepath.Join(base, device)
    51  
    52  	// Walk through a list of paths searching for the correct permutation...
    53  	// ...as it appears that in >= WS14 and < WS14, the leases file may be labelled differently.
    54  
    55  	// Docs say we should expect: dhcpd/dhcpd.leases
    56  	paths := []string{"dhcpd/dhcpd.leases", "dhcpd/dhcp.leases", "dhcp/dhcpd.leases", "dhcp/dhcp.leases"}
    57  	for _, p := range paths {
    58  		fp := filepath.Join(devicebase, p)
    59  		if _, err := os.Stat(fp); !os.IsNotExist(err) {
    60  			return fp
    61  		}
    62  	}
    63  
    64  	log.Printf("Error finding VMWare DHCP Server Leases (dhcpd.leases) under device path: %s", devicebase)
    65  	return ""
    66  }
    67  
    68  func playerVmDhcpConfPath(device string) string {
    69  	base, err := playerVMwareRoot()
    70  	if err != nil {
    71  		log.Printf("Error finding VMware root: %s", err)
    72  		return ""
    73  	}
    74  
    75  	// Build the base path to VMware configuration for specified device: `/etc/vmware/${device}`
    76  	devicebase := filepath.Join(base, device)
    77  
    78  	// Walk through a list of paths searching for the correct permutation...
    79  	// ...as it appears that in >= WS14 and < WS14, the dhcp config may be labelled differently.
    80  
    81  	// Docs say we should expect: dhcp/dhcp.conf
    82  	paths := []string{"dhcp/dhcp.conf", "dhcp/dhcpd.conf", "dhcpd/dhcp.conf", "dhcpd/dhcpd.conf"}
    83  	for _, p := range paths {
    84  		fp := filepath.Join(devicebase, p)
    85  		if _, err := os.Stat(fp); !os.IsNotExist(err) {
    86  			return fp
    87  		}
    88  	}
    89  
    90  	log.Printf("Error finding VMWare DHCP Server Configuration (dhcp.conf) under device path: %s", devicebase)
    91  	return ""
    92  }
    93  
    94  func playerVmnetnatConfPath(device string) string {
    95  	base, err := playerVMwareRoot()
    96  	if err != nil {
    97  		log.Printf("Error finding VMware root: %s", err)
    98  		return ""
    99  	}
   100  	return filepath.Join(base, device, "nat/nat.conf")
   101  }
   102  
   103  func playerNetmapConfPath() string {
   104  	base, err := playerVMwareRoot()
   105  	if err != nil {
   106  		log.Printf("Error finding VMware root: %s", err)
   107  		return ""
   108  	}
   109  	return filepath.Join(base, "netmap.conf")
   110  }
   111  
   112  func playerVerifyVersion(version string) error {
   113  	if runtime.GOOS != "linux" {
   114  		return fmt.Errorf("The VMWare Player version %s driver is only supported on Linux, and Windows, at the moment. Your OS: %s", version, runtime.GOOS)
   115  	}
   116  
   117  	//TODO(pmyjavec) there is a better way to find this, how?
   118  	//the default will suffice for now.
   119  	vmxpath := "/usr/lib/vmware/bin/vmware-vmx"
   120  
   121  	var stderr bytes.Buffer
   122  	cmd := exec.Command(vmxpath, "-v")
   123  	cmd.Stderr = &stderr
   124  	if err := cmd.Run(); err != nil {
   125  		return err
   126  	}
   127  
   128  	versionRe := regexp.MustCompile(`(?i)VMware Player (\d+)\.`)
   129  	matches := versionRe.FindStringSubmatch(stderr.String())
   130  	if matches == nil {
   131  		return fmt.Errorf(
   132  			"Could not find VMWare Player version in output: %s", stderr.String())
   133  	}
   134  	log.Printf("Detected VMWare Player version: %s", matches[1])
   135  
   136  	return compareVersions(matches[1], version, "Player")
   137  }