github.com/mitchellh/packer@v1.3.2/builder/virtualbox/common/driver.go (about)

     1  package common
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"runtime"
     9  	"strings"
    10  )
    11  
    12  // A driver is able to talk to VirtualBox and perform certain
    13  // operations with it. Some of the operations on here may seem overly
    14  // specific, but they were built specifically in mind to handle features
    15  // of the VirtualBox builder for Packer, and to abstract differences in
    16  // versions out of the builder steps, so sometimes the methods are
    17  // extremely specific.
    18  type Driver interface {
    19  	// Create a SATA controller.
    20  	CreateSATAController(vm string, controller string, portcount int) error
    21  
    22  	// Create a SCSI controller.
    23  	CreateSCSIController(vm string, controller string) error
    24  
    25  	// Delete a VM by name
    26  	Delete(string) error
    27  
    28  	// Import a VM
    29  	Import(string, string, []string) error
    30  
    31  	// The complete path to the Guest Additions ISO
    32  	Iso() (string, error)
    33  
    34  	// Checks if the VM with the given name is running.
    35  	IsRunning(string) (bool, error)
    36  
    37  	// Stop stops a running machine, forcefully.
    38  	Stop(string) error
    39  
    40  	// SuppressMessages should do what needs to be done in order to
    41  	// suppress any annoying popups from VirtualBox.
    42  	SuppressMessages() error
    43  
    44  	// VBoxManage executes the given VBoxManage command
    45  	VBoxManage(...string) error
    46  
    47  	// Verify checks to make sure that this driver should function
    48  	// properly. If there is any indication the driver can't function,
    49  	// this will return an error.
    50  	Verify() error
    51  
    52  	// Version reads the version of VirtualBox that is installed.
    53  	Version() (string, error)
    54  }
    55  
    56  func NewDriver() (Driver, error) {
    57  	var vboxmanagePath string
    58  
    59  	// On Windows, we check VBOX_INSTALL_PATH env var for the path
    60  	if runtime.GOOS == "windows" {
    61  		vars := []string{"VBOX_INSTALL_PATH", "VBOX_MSI_INSTALL_PATH"}
    62  		for _, key := range vars {
    63  			value := os.Getenv(key)
    64  			if value != "" {
    65  				log.Printf(
    66  					"[DEBUG] builder/virtualbox: %s = %s", key, value)
    67  				vboxmanagePath = findVBoxManageWindows(value)
    68  			}
    69  			if vboxmanagePath != "" {
    70  				break
    71  			}
    72  		}
    73  	}
    74  
    75  	if vboxmanagePath == "" {
    76  		var err error
    77  		vboxmanagePath, err = exec.LookPath("VBoxManage")
    78  		if err != nil {
    79  			return nil, err
    80  		}
    81  	}
    82  
    83  	log.Printf("VBoxManage path: %s", vboxmanagePath)
    84  	driver := &VBox42Driver{vboxmanagePath}
    85  	if err := driver.Verify(); err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	return driver, nil
    90  }
    91  
    92  func findVBoxManageWindows(paths string) string {
    93  	for _, path := range strings.Split(paths, ";") {
    94  		path = filepath.Join(path, "VBoxManage.exe")
    95  		if _, err := os.Stat(path); err == nil {
    96  			return path
    97  		}
    98  	}
    99  
   100  	return ""
   101  }