github.com/alouche/packer@v0.3.7/builder/vmware/driver_workstation9.go (about)

     1  package vmware
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"strings"
    10  )
    11  
    12  // Workstation9Driver is a driver that can run VMware Workstation 9
    13  // on non-Windows platforms.
    14  type Workstation9Driver struct {
    15  	AppPath          string
    16  	VdiskManagerPath string
    17  	VmrunPath        string
    18  }
    19  
    20  func (d *Workstation9Driver) CompactDisk(diskPath string) error {
    21  	defragCmd := exec.Command(d.VdiskManagerPath, "-d", diskPath)
    22  	if _, _, err := runAndLog(defragCmd); err != nil {
    23  		return err
    24  	}
    25  
    26  	shrinkCmd := exec.Command(d.VdiskManagerPath, "-k", diskPath)
    27  	if _, _, err := runAndLog(shrinkCmd); err != nil {
    28  		return err
    29  	}
    30  
    31  	return nil
    32  }
    33  
    34  func (d *Workstation9Driver) CreateDisk(output string, size string, type_id string) error {
    35  	cmd := exec.Command(d.VdiskManagerPath, "-c", "-s", size, "-a", "lsilogic", "-t", type_id, output)
    36  	if _, _, err := runAndLog(cmd); err != nil {
    37  		return err
    38  	}
    39  
    40  	return nil
    41  }
    42  
    43  func (d *Workstation9Driver) IsRunning(vmxPath string) (bool, error) {
    44  	vmxPath, err := filepath.Abs(vmxPath)
    45  	if err != nil {
    46  		return false, err
    47  	}
    48  
    49  	cmd := exec.Command(d.VmrunPath, "-T", "ws", "list")
    50  	stdout, _, err := runAndLog(cmd)
    51  	if err != nil {
    52  		return false, err
    53  	}
    54  
    55  	for _, line := range strings.Split(stdout, "\n") {
    56  		if line == vmxPath {
    57  			return true, nil
    58  		}
    59  	}
    60  
    61  	return false, nil
    62  }
    63  
    64  func (d *Workstation9Driver) Start(vmxPath string, headless bool) error {
    65  	guiArgument := "gui"
    66  	if headless {
    67  		guiArgument = "nogui"
    68  	}
    69  
    70  	cmd := exec.Command(d.VmrunPath, "-T", "ws", "start", vmxPath, guiArgument)
    71  	if _, _, err := runAndLog(cmd); err != nil {
    72  		return err
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  func (d *Workstation9Driver) Stop(vmxPath string) error {
    79  	cmd := exec.Command(d.VmrunPath, "-T", "ws", "stop", vmxPath, "hard")
    80  	if _, _, err := runAndLog(cmd); err != nil {
    81  		return err
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  func (d *Workstation9Driver) Verify() error {
    88  	var err error
    89  	if d.AppPath == "" {
    90  		if d.AppPath, err = workstationFindVMware(); err != nil {
    91  			return err
    92  		}
    93  	}
    94  
    95  	if d.VmrunPath == "" {
    96  		if d.VmrunPath, err = workstationFindVmrun(); err != nil {
    97  			return err
    98  		}
    99  	}
   100  
   101  	if d.VdiskManagerPath == "" {
   102  		if d.VdiskManagerPath, err = workstationFindVdiskManager(); err != nil {
   103  			return err
   104  		}
   105  	}
   106  
   107  	log.Printf("VMware app path: %s", d.AppPath)
   108  	log.Printf("vmrun path: %s", d.VmrunPath)
   109  	log.Printf("vdisk-manager path: %s", d.VdiskManagerPath)
   110  
   111  	if _, err := os.Stat(d.AppPath); err != nil {
   112  		return fmt.Errorf("VMware application not found: %s", d.AppPath)
   113  	}
   114  
   115  	if _, err := os.Stat(d.VmrunPath); err != nil {
   116  		return fmt.Errorf("'vmrun' application not found: %s", d.VmrunPath)
   117  	}
   118  
   119  	if _, err := os.Stat(d.VdiskManagerPath); err != nil {
   120  		return fmt.Errorf("'vmrun' application not found: %s", d.VdiskManagerPath)
   121  	}
   122  
   123  	// Check to see if it APPEARS to be licensed.
   124  	if err := workstationCheckLicense(); err != nil {
   125  		return err
   126  	}
   127  
   128  	return nil
   129  }
   130  
   131  func (d *Workstation9Driver) ToolsIsoPath(flavor string) string {
   132  	return workstationToolsIsoPath(flavor)
   133  }
   134  
   135  func (d *Workstation9Driver) DhcpLeasesPath(device string) string {
   136  	return workstationDhcpLeasesPath(device)
   137  }
   138  
   139  func (d *Workstation9Driver) VmnetnatConfPath() string {
   140  	return workstationVmnetnatConfPath()
   141  }