forked from terra-farm/go-virtualbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vbmgt.go
82 lines (72 loc) · 1.88 KB
/
vbmgt.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
package virtualbox
import (
"os"
"os/exec"
"os/user"
"path/filepath"
"regexp"
"runtime"
)
var (
manage Command
)
var (
reVMNameUUID = regexp.MustCompile(`"(.+)" {([0-9a-f-]+)}`)
reVMInfoLine = regexp.MustCompile(`(?:"(.+)"|(.+))=(?:"(.*)"|(.*))`)
reColonLine = regexp.MustCompile(`(.+):\s+(.*)`)
reMachineNotFound = regexp.MustCompile(`Could not find a registered machine named '(.+)'`)
)
// Manage returns the Command to run VBoxManage/VBoxControl.
func Manage() Command {
if manage != nil {
return manage
}
sudoer, err := isSudoer()
if err != nil {
Debug("Error getting sudoer status: '%v'", err)
}
if vbprog, err := lookupVBoxProgram("VBoxManage"); err == nil {
manage = command{program: vbprog, sudoer: sudoer, guest: false}
} else if vbprog, err := lookupVBoxProgram("VBoxControl"); err == nil {
manage = command{program: vbprog, sudoer: sudoer, guest: true}
} else {
// Did not find a VirtualBox management command
manage = command{program: "false", sudoer: false, guest: false}
}
Debug("manage: '%+v'", manage)
return manage
}
func lookupVBoxProgram(vbprog string) (string, error) {
if runtime.GOOS == osWindows {
if p := os.Getenv("VBOX_INSTALL_PATH"); p != "" {
vbprog = filepath.Join(p, vbprog+".exe")
} else {
vbprog = filepath.Join("C:\\", "Program Files", "Oracle", "VirtualBox", vbprog+".exe")
}
}
return exec.LookPath(vbprog)
}
func isSudoer() (bool, error) {
me, err := user.Current()
if err != nil {
return false, err
}
Debug("User: '%+v'", me)
if groupIDs, err := me.GroupIds(); runtime.GOOS == "linux" {
if err != nil {
return false, err
}
Debug("groupIDs: '%+v'", groupIDs)
for _, groupID := range groupIDs {
group, err := user.LookupGroupId(groupID)
if err != nil {
return false, err
}
Debug("group: '%+v'", group)
if group.Name == "sudo" {
return true, nil
}
}
}
return false, nil
}