github.com/tonnydourado/packer@v0.6.1-0.20140701134019-5d0cd9676a37/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 if installPath := os.Getenv("VBOX_INSTALL_PATH"); installPath != "" { 59 log.Printf("[DEBUG] builder/virtualbox: VBOX_INSTALL_PATH: %s", 60 installPath) 61 for _, path := range strings.Split(installPath, ";") { 62 path = filepath.Join(path, "VBoxManage.exe") 63 if _, err := os.Stat(path); err == nil { 64 vboxmanagePath = path 65 break 66 } 67 } 68 } 69 } 70 71 if vboxmanagePath == "" { 72 var err error 73 vboxmanagePath, err = exec.LookPath("VBoxManage") 74 if err != nil { 75 return nil, err 76 } 77 } 78 79 log.Printf("VBoxManage path: %s", vboxmanagePath) 80 driver := &VBox42Driver{vboxmanagePath} 81 if err := driver.Verify(); err != nil { 82 return nil, err 83 } 84 85 return driver, nil 86 }