2013-12-21 18:00:48 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A driver is able to talk to VirtualBox and perform certain
|
|
|
|
// operations with it. Some of the operations on here may seem overly
|
|
|
|
// specific, but they were built specifically in mind to handle features
|
|
|
|
// of the VirtualBox builder for Packer, and to abstract differences in
|
|
|
|
// versions out of the builder steps, so sometimes the methods are
|
|
|
|
// extremely specific.
|
|
|
|
type Driver interface {
|
|
|
|
// Create a SATA controller.
|
2017-03-22 08:09:25 -04:00
|
|
|
CreateSATAController(vm string, controller string, portcount int) error
|
2015-01-16 11:34:12 -05:00
|
|
|
|
|
|
|
// Create a SCSI controller.
|
|
|
|
CreateSCSIController(vm string, controller string) error
|
2013-12-21 18:00:48 -05:00
|
|
|
|
2013-12-22 18:19:19 -05:00
|
|
|
// Delete a VM by name
|
|
|
|
Delete(string) error
|
|
|
|
|
|
|
|
// Import a VM
|
2014-09-05 13:23:37 -04:00
|
|
|
Import(string, string, []string) error
|
2013-12-22 18:19:19 -05:00
|
|
|
|
2014-05-06 18:37:49 -04:00
|
|
|
// The complete path to the Guest Additions ISO
|
|
|
|
Iso() (string, error)
|
|
|
|
|
2013-12-21 18:00:48 -05:00
|
|
|
// Checks if the VM with the given name is running.
|
|
|
|
IsRunning(string) (bool, error)
|
|
|
|
|
|
|
|
// Stop stops a running machine, forcefully.
|
|
|
|
Stop(string) error
|
|
|
|
|
|
|
|
// SuppressMessages should do what needs to be done in order to
|
|
|
|
// suppress any annoying popups from VirtualBox.
|
|
|
|
SuppressMessages() error
|
|
|
|
|
|
|
|
// VBoxManage executes the given VBoxManage command
|
|
|
|
VBoxManage(...string) error
|
|
|
|
|
|
|
|
// Verify checks to make sure that this driver should function
|
|
|
|
// properly. If there is any indication the driver can't function,
|
|
|
|
// this will return an error.
|
|
|
|
Verify() error
|
|
|
|
|
|
|
|
// Version reads the version of VirtualBox that is installed.
|
|
|
|
Version() (string, error)
|
2019-02-16 08:32:22 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
CreateSnapshot(string, string) error
|
|
|
|
|
|
|
|
//
|
|
|
|
HasSnapshots(string) (bool, error)
|
|
|
|
|
|
|
|
//
|
|
|
|
GetCurrentSnapshot(string) (string, error)
|
|
|
|
|
|
|
|
//
|
|
|
|
SetSnapshot(string, string) error
|
|
|
|
|
|
|
|
//
|
|
|
|
DeleteSnapshot(string, string) error
|
|
|
|
|
|
|
|
//
|
|
|
|
SnapshotExists(string, string) (bool, error)
|
2019-03-10 10:38:38 -04:00
|
|
|
|
|
|
|
//
|
|
|
|
GetParentSnapshot(string, string) (string, error)
|
2013-12-21 18:00:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDriver() (Driver, error) {
|
|
|
|
var vboxmanagePath string
|
|
|
|
|
|
|
|
// On Windows, we check VBOX_INSTALL_PATH env var for the path
|
|
|
|
if runtime.GOOS == "windows" {
|
2014-09-04 00:08:57 -04:00
|
|
|
vars := []string{"VBOX_INSTALL_PATH", "VBOX_MSI_INSTALL_PATH"}
|
|
|
|
for _, key := range vars {
|
|
|
|
value := os.Getenv(key)
|
|
|
|
if value != "" {
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] builder/virtualbox: %s = %s", key, value)
|
|
|
|
vboxmanagePath = findVBoxManageWindows(value)
|
|
|
|
}
|
|
|
|
if vboxmanagePath != "" {
|
|
|
|
break
|
2013-12-21 18:00:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vboxmanagePath == "" {
|
|
|
|
var err error
|
|
|
|
vboxmanagePath, err = exec.LookPath("VBoxManage")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("VBoxManage path: %s", vboxmanagePath)
|
|
|
|
driver := &VBox42Driver{vboxmanagePath}
|
|
|
|
if err := driver.Verify(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return driver, nil
|
|
|
|
}
|
2014-09-04 00:08:57 -04:00
|
|
|
|
|
|
|
func findVBoxManageWindows(paths string) string {
|
|
|
|
for _, path := range strings.Split(paths, ";") {
|
|
|
|
path = filepath.Join(path, "VBoxManage.exe")
|
|
|
|
if _, err := os.Stat(path); err == nil {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|