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

     1  package vmware
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"strings"
     9  )
    10  
    11  // Fusion5Driver is a driver that can run VMWare Fusion 5.
    12  type Fusion5Driver struct {
    13  	// This is the path to the "VMware Fusion.app"
    14  	AppPath string
    15  }
    16  
    17  func (d *Fusion5Driver) CompactDisk(diskPath string) error {
    18  	defragCmd := exec.Command(d.vdiskManagerPath(), "-d", diskPath)
    19  	if _, _, err := runAndLog(defragCmd); err != nil {
    20  		return err
    21  	}
    22  
    23  	shrinkCmd := exec.Command(d.vdiskManagerPath(), "-k", diskPath)
    24  	if _, _, err := runAndLog(shrinkCmd); err != nil {
    25  		return err
    26  	}
    27  
    28  	return nil
    29  }
    30  
    31  func (d *Fusion5Driver) CreateDisk(output string, size string, type_id string) error {
    32  	cmd := exec.Command(d.vdiskManagerPath(), "-c", "-s", size, "-a", "lsilogic", "-t", type_id, output)
    33  	if _, _, err := runAndLog(cmd); err != nil {
    34  		return err
    35  	}
    36  
    37  	return nil
    38  }
    39  
    40  func (d *Fusion5Driver) IsRunning(vmxPath string) (bool, error) {
    41  	vmxPath, err := filepath.Abs(vmxPath)
    42  	if err != nil {
    43  		return false, err
    44  	}
    45  
    46  	cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "list")
    47  	stdout, _, err := runAndLog(cmd)
    48  	if err != nil {
    49  		return false, err
    50  	}
    51  
    52  	for _, line := range strings.Split(stdout, "\n") {
    53  		if line == vmxPath {
    54  			return true, nil
    55  		}
    56  	}
    57  
    58  	return false, nil
    59  }
    60  
    61  func (d *Fusion5Driver) Start(vmxPath string, headless bool) error {
    62  	guiArgument := "gui"
    63  	if headless == true {
    64  		guiArgument = "nogui"
    65  	}
    66  
    67  	cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "start", vmxPath, guiArgument)
    68  	if _, _, err := runAndLog(cmd); err != nil {
    69  		return err
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  func (d *Fusion5Driver) Stop(vmxPath string) error {
    76  	cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "stop", vmxPath, "hard")
    77  	if _, _, err := runAndLog(cmd); err != nil {
    78  		return err
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  func (d *Fusion5Driver) Verify() error {
    85  	if _, err := os.Stat(d.AppPath); err != nil {
    86  		if os.IsNotExist(err) {
    87  			return fmt.Errorf("Fusion application not found at path: %s", d.AppPath)
    88  		}
    89  
    90  		return err
    91  	}
    92  
    93  	if _, err := os.Stat(d.vmrunPath()); err != nil {
    94  		if os.IsNotExist(err) {
    95  			return fmt.Errorf("Critical application 'vmrun' not found at path: %s", d.vmrunPath())
    96  		}
    97  
    98  		return err
    99  	}
   100  
   101  	if _, err := os.Stat(d.vdiskManagerPath()); err != nil {
   102  		if os.IsNotExist(err) {
   103  			return fmt.Errorf("Critical application vdisk manager not found at path: %s", d.vdiskManagerPath())
   104  		}
   105  
   106  		return err
   107  	}
   108  
   109  	return nil
   110  }
   111  
   112  func (d *Fusion5Driver) vdiskManagerPath() string {
   113  	return filepath.Join(d.AppPath, "Contents", "Library", "vmware-vdiskmanager")
   114  }
   115  
   116  func (d *Fusion5Driver) vmrunPath() string {
   117  	return filepath.Join(d.AppPath, "Contents", "Library", "vmrun")
   118  }
   119  
   120  func (d *Fusion5Driver) ToolsIsoPath(k string) string {
   121  	return filepath.Join(d.AppPath, "Contents", "Library", "isoimages", k+".iso")
   122  }
   123  
   124  func (d *Fusion5Driver) DhcpLeasesPath(device string) string {
   125  	return "/var/db/vmware/vmnet-dhcpd-" + device + ".leases"
   126  }