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

     1  package vmware
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"log"
     7  	"os/exec"
     8  	"runtime"
     9  	"strings"
    10  )
    11  
    12  // A driver is able to talk to VMware, control virtual machines, etc.
    13  type Driver interface {
    14  	// CompactDisk compacts a virtual disk.
    15  	CompactDisk(string) error
    16  
    17  	// CreateDisk creates a virtual disk with the given size.
    18  	CreateDisk(string, string, string) error
    19  
    20  	// Checks if the VMX file at the given path is running.
    21  	IsRunning(string) (bool, error)
    22  
    23  	// Start starts a VM specified by the path to the VMX given.
    24  	Start(string, bool) error
    25  
    26  	// Stop stops a VM specified by the path to the VMX given.
    27  	Stop(string) error
    28  
    29  	// Get the path to the VMware ISO for the given flavor.
    30  	ToolsIsoPath(string) string
    31  
    32  	// Get the path to the DHCP leases file for the given device.
    33  	DhcpLeasesPath(string) string
    34  
    35  	// Verify checks to make sure that this driver should function
    36  	// properly. This should check that all the files it will use
    37  	// appear to exist and so on. If everything is okay, this doesn't
    38  	// return an error. Otherwise, this returns an error.
    39  	Verify() error
    40  }
    41  
    42  // NewDriver returns a new driver implementation for this operating
    43  // system, or an error if the driver couldn't be initialized.
    44  func NewDriver() (Driver, error) {
    45  	drivers := []Driver{}
    46  
    47  	switch runtime.GOOS {
    48  	case "darwin":
    49  		drivers = []Driver{
    50  			&Fusion5Driver{
    51  				AppPath: "/Applications/VMware Fusion.app",
    52  			},
    53  		}
    54  	case "linux":
    55  		drivers = []Driver{
    56  			new(Workstation9Driver),
    57  			new(Player5LinuxDriver),
    58  		}
    59  	case "windows":
    60  		drivers = []Driver{
    61  			new(Workstation9Driver),
    62  		}
    63  	default:
    64  		return nil, fmt.Errorf("can't find driver for OS: %s", runtime.GOOS)
    65  	}
    66  
    67  	errs := ""
    68  	for _, driver := range drivers {
    69  		err := driver.Verify()
    70  		if err == nil {
    71  			return driver, nil
    72  		}
    73  		errs += "* " + err.Error() + "\n"
    74  	}
    75  
    76  	return nil, fmt.Errorf(
    77  		"Unable to initialize any driver for this platform. The errors\n"+
    78  			"from each driver are shown below. Please fix at least one driver\n"+
    79  			"to continue:\n%s", errs)
    80  }
    81  
    82  func runAndLog(cmd *exec.Cmd) (string, string, error) {
    83  	var stdout, stderr bytes.Buffer
    84  
    85  	log.Printf("Executing: %s %v", cmd.Path, cmd.Args[1:])
    86  	cmd.Stdout = &stdout
    87  	cmd.Stderr = &stderr
    88  	err := cmd.Run()
    89  
    90  	stdoutString := strings.TrimSpace(stdout.String())
    91  	stderrString := strings.TrimSpace(stderr.String())
    92  
    93  	if _, ok := err.(*exec.ExitError); ok {
    94  		err = fmt.Errorf("VMware error: %s", stderrString)
    95  	}
    96  
    97  	log.Printf("stdout: %s", stdoutString)
    98  	log.Printf("stderr: %s", stderrString)
    99  
   100  	// Replace these for Windows, we only want to deal with Unix
   101  	// style line endings.
   102  	returnStdout := strings.Replace(stdout.String(), "\r\n", "\n", -1)
   103  	returnStderr := strings.Replace(stderr.String(), "\r\n", "\n", -1)
   104  
   105  	return returnStdout, returnStderr, err
   106  }