github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/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) error 21 22 // Delete a VM by name 23 Delete(string) error 24 25 // Import a VM 26 Import(string, string, []string) error 27 28 // The complete path to the Guest Additions ISO 29 Iso() (string, error) 30 31 // Checks if the VM with the given name is running. 32 IsRunning(string) (bool, error) 33 34 // Stop stops a running machine, forcefully. 35 Stop(string) error 36 37 // SuppressMessages should do what needs to be done in order to 38 // suppress any annoying popups from VirtualBox. 39 SuppressMessages() error 40 41 // VBoxManage executes the given VBoxManage command 42 VBoxManage(...string) error 43 44 // Verify checks to make sure that this driver should function 45 // properly. If there is any indication the driver can't function, 46 // this will return an error. 47 Verify() error 48 49 // Version reads the version of VirtualBox that is installed. 50 Version() (string, error) 51 } 52 53 func NewDriver() (Driver, error) { 54 var vboxmanagePath string 55 56 // On Windows, we check VBOX_INSTALL_PATH env var for the path 57 if runtime.GOOS == "windows" { 58 vars := []string{"VBOX_INSTALL_PATH", "VBOX_MSI_INSTALL_PATH"} 59 for _, key := range vars { 60 value := os.Getenv(key) 61 if value != "" { 62 log.Printf( 63 "[DEBUG] builder/virtualbox: %s = %s", key, value) 64 vboxmanagePath = findVBoxManageWindows(value) 65 } 66 if vboxmanagePath != "" { 67 break 68 } 69 } 70 } 71 72 if vboxmanagePath == "" { 73 var err error 74 vboxmanagePath, err = exec.LookPath("VBoxManage") 75 if err != nil { 76 return nil, err 77 } 78 } 79 80 log.Printf("VBoxManage path: %s", vboxmanagePath) 81 driver := &VBox42Driver{vboxmanagePath} 82 if err := driver.Verify(); err != nil { 83 return nil, err 84 } 85 86 return driver, nil 87 } 88 89 func findVBoxManageWindows(paths string) string { 90 for _, path := range strings.Split(paths, ";") { 91 path = filepath.Join(path, "VBoxManage.exe") 92 if _, err := os.Stat(path); err == nil { 93 return path 94 } 95 } 96 97 return "" 98 }