2013-12-21 18:00:48 -05:00
|
|
|
package common
|
2013-06-11 18:45:52 -04:00
|
|
|
|
|
|
|
import (
|
2013-06-11 19:48:01 -04:00
|
|
|
"bytes"
|
2013-06-11 18:52:46 -04:00
|
|
|
"fmt"
|
2013-06-11 18:45:52 -04:00
|
|
|
"log"
|
|
|
|
"os/exec"
|
2013-06-24 01:44:58 -04:00
|
|
|
"regexp"
|
2013-06-11 19:48:01 -04:00
|
|
|
"strings"
|
2013-06-11 18:52:46 -04:00
|
|
|
"time"
|
2013-06-11 18:45:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type VBox42Driver struct {
|
|
|
|
// This is the path to the "VBoxManage" application.
|
|
|
|
VBoxManagePath string
|
2013-10-20 18:54:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *VBox42Driver) CreateSATAController(vmName string, name string) error {
|
|
|
|
version, err := d.Version()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-10-23 09:05:00 -04:00
|
|
|
portCountArg := "--sataportcount"
|
2013-10-20 18:54:34 -04:00
|
|
|
if strings.HasPrefix(version, "4.3") {
|
2013-10-23 09:05:00 -04:00
|
|
|
portCountArg = "--portcount"
|
2013-10-20 18:54:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
command := []string{
|
|
|
|
"storagectl", vmName,
|
|
|
|
"--name", name,
|
|
|
|
"--add", "sata",
|
|
|
|
portCountArg, "1",
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.VBoxManage(command...)
|
2013-06-11 18:45:52 -04:00
|
|
|
}
|
|
|
|
|
2013-12-22 18:19:19 -05:00
|
|
|
func (d *VBox42Driver) Delete(name string) error {
|
|
|
|
return d.VBoxManage("unregistervm", name, "--delete")
|
|
|
|
}
|
|
|
|
|
2014-02-12 18:29:13 -05:00
|
|
|
func (d *VBox42Driver) Import(name, path, opts string) error {
|
2013-12-22 18:19:19 -05:00
|
|
|
args := []string{
|
|
|
|
"import", path,
|
|
|
|
"--vsys", "0",
|
|
|
|
"--vmname", name,
|
2014-02-12 18:29:13 -05:00
|
|
|
"--options", opts,
|
2013-12-22 18:19:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return d.VBoxManage(args...)
|
|
|
|
}
|
|
|
|
|
2013-06-12 21:02:42 -04:00
|
|
|
func (d *VBox42Driver) IsRunning(name string) (bool, error) {
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
|
|
|
|
cmd := exec.Command(d.VBoxManagePath, "showvminfo", name, "--machinereadable")
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range strings.Split(stdout.String(), "\n") {
|
2013-07-25 12:28:04 -04:00
|
|
|
// Need to trim off CR character when running in windows
|
2013-07-25 21:49:39 -04:00
|
|
|
line = strings.TrimRight(line, "\r")
|
2013-07-25 12:28:04 -04:00
|
|
|
|
2013-06-12 21:02:42 -04:00
|
|
|
if line == `VMState="running"` {
|
|
|
|
return true, nil
|
|
|
|
}
|
2013-06-24 12:32:08 -04:00
|
|
|
|
|
|
|
// We consider "stopping" to still be running. We wait for it to
|
|
|
|
// be completely stopped or some other state.
|
|
|
|
if line == `VMState="stopping"` {
|
|
|
|
return true, nil
|
|
|
|
}
|
2013-07-09 15:39:28 -04:00
|
|
|
|
|
|
|
// We consider "paused" to still be running. We wait for it to
|
|
|
|
// be completely stopped or some other state.
|
|
|
|
if line == `VMState="paused"` {
|
|
|
|
return true, nil
|
|
|
|
}
|
2013-06-12 21:02:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *VBox42Driver) Stop(name string) error {
|
|
|
|
if err := d.VBoxManage("controlvm", name, "poweroff"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-19 11:49:23 -05:00
|
|
|
// We sleep here for a little bit to let the session "unlock"
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2013-06-12 21:02:42 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:45:52 -04:00
|
|
|
func (d *VBox42Driver) SuppressMessages() error {
|
|
|
|
extraData := map[string]string{
|
|
|
|
"GUI/RegistrationData": "triesLeft=0",
|
|
|
|
"GUI/SuppressMessages": "confirmInputCapture,remindAboutAutoCapture,remindAboutMouseIntegrationOff,remindAboutMouseIntegrationOn,remindAboutWrongColorDepth",
|
2013-06-11 23:07:11 -04:00
|
|
|
"GUI/UpdateDate": fmt.Sprintf("1 d, %d-01-01, stable", time.Now().Year()+1),
|
2013-06-11 18:52:46 -04:00
|
|
|
"GUI/UpdateCheckCount": "60",
|
2013-06-11 18:45:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range extraData {
|
2013-06-11 19:12:19 -04:00
|
|
|
if err := d.VBoxManage("setextradata", "global", k, v); err != nil {
|
2013-06-11 18:45:52 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-11 19:12:19 -04:00
|
|
|
func (d *VBox42Driver) VBoxManage(args ...string) error {
|
2013-06-11 19:48:01 -04:00
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
|
2013-06-11 18:45:52 -04:00
|
|
|
log.Printf("Executing VBoxManage: %#v", args)
|
|
|
|
cmd := exec.Command(d.VBoxManagePath, args...)
|
2013-06-11 19:48:01 -04:00
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
2013-06-11 18:45:52 -04:00
|
|
|
|
2013-06-24 00:19:41 -04:00
|
|
|
stdoutString := strings.TrimSpace(stdout.String())
|
|
|
|
stderrString := strings.TrimSpace(stderr.String())
|
|
|
|
|
|
|
|
if _, ok := err.(*exec.ExitError); ok {
|
|
|
|
err = fmt.Errorf("VBoxManage error: %s", stderrString)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("stdout: %s", stdoutString)
|
|
|
|
log.Printf("stderr: %s", stderrString)
|
2013-06-11 19:48:01 -04:00
|
|
|
|
|
|
|
return err
|
2013-06-11 18:45:52 -04:00
|
|
|
}
|
2013-06-11 19:12:19 -04:00
|
|
|
|
|
|
|
func (d *VBox42Driver) Verify() error {
|
|
|
|
return nil
|
|
|
|
}
|
2013-06-24 01:44:58 -04:00
|
|
|
|
|
|
|
func (d *VBox42Driver) Version() (string, error) {
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
|
|
|
|
cmd := exec.Command(d.VBoxManagePath, "--version")
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2013-07-01 13:59:04 -04:00
|
|
|
versionOutput := strings.TrimSpace(stdout.String())
|
|
|
|
log.Printf("VBoxManage --version output: %s", versionOutput)
|
2013-10-02 20:11:42 -04:00
|
|
|
|
2013-10-02 20:13:22 -04:00
|
|
|
// If the "--version" output contains vboxdrv, then this is indicative
|
|
|
|
// of problems with the VirtualBox setup and we shouldn't really continue,
|
|
|
|
// whether or not we can read the version.
|
|
|
|
if strings.Contains(versionOutput, "vboxdrv") {
|
|
|
|
return "", fmt.Errorf("VirtualBox is not properly setup: %s", versionOutput)
|
|
|
|
}
|
|
|
|
|
2013-06-24 01:44:58 -04:00
|
|
|
versionRe := regexp.MustCompile("[^.0-9]")
|
2013-07-01 13:59:04 -04:00
|
|
|
matches := versionRe.Split(versionOutput, 2)
|
2013-10-02 20:11:42 -04:00
|
|
|
if len(matches) == 0 || matches[0] == "" {
|
2013-07-01 13:59:04 -04:00
|
|
|
return "", fmt.Errorf("No version found: %s", versionOutput)
|
2013-06-24 01:44:58 -04:00
|
|
|
}
|
|
|
|
|
2013-06-29 16:45:30 -04:00
|
|
|
log.Printf("VirtualBox version: %s", matches[0])
|
2013-06-24 01:44:58 -04:00
|
|
|
return matches[0], nil
|
|
|
|
}
|