github.com/mitchellh/packer@v1.3.2/builder/vmware/common/driver_workstation_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  	"errors"
     9  	"fmt"
    10  	"log"
    11  	"os"
    12  	"os/exec"
    13  	"path/filepath"
    14  	"regexp"
    15  	"runtime"
    16  )
    17  
    18  func workstationCheckLicense() error {
    19  	matches, err := filepath.Glob("/etc/vmware/license-ws-*")
    20  	if err != nil {
    21  		return fmt.Errorf("Error looking for VMware license: %s", err)
    22  	}
    23  
    24  	if len(matches) == 0 {
    25  		return errors.New("Workstation does not appear to be licensed. Please license it.")
    26  	}
    27  
    28  	return nil
    29  }
    30  
    31  func workstationFindVdiskManager() (string, error) {
    32  	return exec.LookPath("vmware-vdiskmanager")
    33  }
    34  
    35  func workstationFindVMware() (string, error) {
    36  	return exec.LookPath("vmware")
    37  }
    38  
    39  func workstationFindVmrun() (string, error) {
    40  	return exec.LookPath("vmrun")
    41  }
    42  
    43  // return the base path to vmware's config on the host
    44  func workstationVMwareRoot() (s string, err error) {
    45  	return "/etc/vmware", nil
    46  }
    47  
    48  func workstationDhcpLeasesPath(device string) string {
    49  	base, err := workstationVMwareRoot()
    50  	if err != nil {
    51  		log.Printf("Error finding VMware root: %s", err)
    52  		return ""
    53  	}
    54  
    55  	// Build the base path to VMware configuration for specified device: `/etc/vmware/${device}`
    56  	devicebase := filepath.Join(base, device)
    57  
    58  	// Walk through a list of paths searching for the correct permutation...
    59  	// ...as it appears that in >= WS14 and < WS14, the leases file may be labelled differently.
    60  
    61  	// Docs say we should expect: dhcpd/dhcpd.leases
    62  	paths := []string{"dhcpd/dhcpd.leases", "dhcpd/dhcp.leases", "dhcp/dhcpd.leases", "dhcp/dhcp.leases"}
    63  	for _, p := range paths {
    64  		fp := filepath.Join(devicebase, p)
    65  		if _, err := os.Stat(fp); !os.IsNotExist(err) {
    66  			return fp
    67  		}
    68  	}
    69  
    70  	log.Printf("Error finding VMWare DHCP Server Leases (dhcpd.leases) under device path: %s", devicebase)
    71  	return ""
    72  }
    73  
    74  func workstationDhcpConfPath(device string) string {
    75  	base, err := workstationVMwareRoot()
    76  	if err != nil {
    77  		log.Printf("Error finding VMware root: %s", err)
    78  		return ""
    79  	}
    80  
    81  	// Build the base path to VMware configuration for specified device: `/etc/vmware/${device}`
    82  	devicebase := filepath.Join(base, device)
    83  
    84  	// Walk through a list of paths searching for the correct permutation...
    85  	// ...as it appears that in >= WS14 and < WS14, the dhcp config may be labelled differently.
    86  
    87  	// Docs say we should expect: dhcp/dhcp.conf
    88  	paths := []string{"dhcp/dhcp.conf", "dhcp/dhcpd.conf", "dhcpd/dhcp.conf", "dhcpd/dhcpd.conf"}
    89  	for _, p := range paths {
    90  		fp := filepath.Join(devicebase, p)
    91  		if _, err := os.Stat(fp); !os.IsNotExist(err) {
    92  			return fp
    93  		}
    94  	}
    95  
    96  	log.Printf("Error finding VMWare DHCP Server Configuration (dhcp.conf) under device path: %s", devicebase)
    97  	return ""
    98  }
    99  
   100  func workstationVmnetnatConfPath(device string) string {
   101  	base, err := workstationVMwareRoot()
   102  	if err != nil {
   103  		log.Printf("Error finding VMware root: %s", err)
   104  		return ""
   105  	}
   106  	return filepath.Join(base, device, "nat/nat.conf")
   107  }
   108  
   109  func workstationNetmapConfPath() string {
   110  	base, err := workstationVMwareRoot()
   111  	if err != nil {
   112  		log.Printf("Error finding VMware root: %s", err)
   113  		return ""
   114  	}
   115  	return filepath.Join(base, "netmap.conf")
   116  }
   117  
   118  func workstationToolsIsoPath(flavor string) string {
   119  	return "/usr/lib/vmware/isoimages/" + flavor + ".iso"
   120  }
   121  
   122  func workstationVerifyVersion(version string) error {
   123  	if runtime.GOOS != "linux" {
   124  		return fmt.Errorf("The VMware WS version %s driver is only supported on Linux, and Windows, at the moment. Your OS: %s", version, runtime.GOOS)
   125  	}
   126  
   127  	//TODO(pmyjavec) there is a better way to find this, how?
   128  	//the default will suffice for now.
   129  	vmxpath := "/usr/lib/vmware/bin/vmware-vmx"
   130  
   131  	var stderr bytes.Buffer
   132  	cmd := exec.Command(vmxpath, "-v")
   133  	cmd.Stderr = &stderr
   134  	if err := cmd.Run(); err != nil {
   135  		return err
   136  	}
   137  	return workstationTestVersion(version, stderr.String())
   138  }
   139  
   140  func workstationTestVersion(wanted, versionOutput string) error {
   141  	versionRe := regexp.MustCompile(`(?i)VMware Workstation (\d+)\.`)
   142  	matches := versionRe.FindStringSubmatch(versionOutput)
   143  	if matches == nil {
   144  		return fmt.Errorf(
   145  			"Could not find VMware WS version in output: %s", wanted)
   146  	}
   147  	log.Printf("Detected VMware WS version: %s", matches[1])
   148  
   149  	return compareVersions(matches[1], wanted, "Workstation")
   150  }