This repository has been archived by the owner on Sep 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
vbm.go
114 lines (91 loc) · 3.04 KB
/
vbm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package virtualbox
import (
"bufio"
"bytes"
"errors"
"fmt"
"os/exec"
"regexp"
"strings"
"github.com/docker/machine/libmachine/log"
)
var (
reColonLine = regexp.MustCompile(`(.+):\s+(.*)`)
reEqualLine = regexp.MustCompile(`(.+)=(.*)`)
reEqualQuoteLine = regexp.MustCompile(`"(.+)"="(.*)"`)
reMachineNotFound = regexp.MustCompile(`Could not find a registered machine named '(.+)'`)
ErrMachineNotExist = errors.New("machine does not exist")
ErrVBMNotFound = errors.New("VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path")
vboxManageCmd = detectVBoxManageCmd()
)
// VBoxManager defines the interface to communicate to VirtualBox.
type VBoxManager interface {
vbm(args ...string) error
vbmOut(args ...string) (string, error)
vbmOutErr(args ...string) (string, string, error)
}
// VBoxCmdManager communicates with VirtualBox through the commandline using `VBoxManage`.
type VBoxCmdManager struct{}
func (v *VBoxCmdManager) vbm(args ...string) error {
_, _, err := v.vbmOutErr(args...)
return err
}
func (v *VBoxCmdManager) vbmOut(args ...string) (string, error) {
stdout, _, err := v.vbmOutErr(args...)
return stdout, err
}
func (v *VBoxCmdManager) vbmOutErr(args ...string) (string, string, error) {
cmd := exec.Command(vboxManageCmd, args...)
log.Debugf("COMMAND: %v %v", vboxManageCmd, strings.Join(args, " "))
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
stderrStr := stderr.String()
if len(args) > 0 {
log.Debugf("STDOUT:\n{\n%v}", stdout.String())
log.Debugf("STDERR:\n{\n%v}", stderrStr)
}
if err != nil {
if ee, ok := err.(*exec.Error); ok && ee.Err == exec.ErrNotFound {
err = ErrVBMNotFound
}
}
if err == nil || strings.HasPrefix(err.Error(), "exit status ") {
// VBoxManage will sometimes not set the return code, but has a fatal error
// such as VBoxManage.exe: error: VT-x is not available. (VERR_VMX_NO_VMX)
if strings.Contains(stderrStr, "error:") {
err = fmt.Errorf("%v %v failed:\n%v", vboxManageCmd, strings.Join(args, " "), stderrStr)
}
}
return stdout.String(), stderrStr, err
}
func checkVBoxManageVersion(version string) error {
if !strings.HasPrefix(version, "5.") && !strings.HasPrefix(version, "4.") {
return fmt.Errorf("We support Virtualbox starting with version 5. Your VirtualBox install is %q. Please upgrade at https://www.virtualbox.org", version)
}
if !strings.HasPrefix(version, "5.") {
log.Warnf("You are using version %s of VirtualBox. If you encouter issues, you might want to upgrade to version 5 at https://www.virtualbox.org", version)
}
return nil
}
func parseKeyValues(stdOut string, regexp *regexp.Regexp, callback func(key, val string) error) error {
r := strings.NewReader(stdOut)
s := bufio.NewScanner(r)
for s.Scan() {
line := s.Text()
if line == "" {
continue
}
res := regexp.FindStringSubmatch(line)
if res == nil {
continue
}
key, val := res[1], res[2]
if err := callback(key, val); err != nil {
return err
}
}
return s.Err()
}