forked from pitstopcloud/virtualbox-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvbox.go
208 lines (171 loc) · 4.76 KB
/
vbox.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package virtualbox
import (
"bytes"
"errors"
"fmt"
"os/exec"
"os/user"
"path/filepath"
"regexp"
"strings"
"github.com/golang/glog"
)
const (
VBoxManage = "VBoxManage"
NatPortBase = 10900
)
var DefaultVBBasePath = GetDefaultVBBasePath()
var reColonLine = regexp.MustCompile(`([^:]+):\s+(.*)`)
// parses lines like the following
// foo="bar"
var reKeyEqVal = regexp.MustCompile(`([^=]+)=\s*(.*)`)
// Config for the Manager
type Config struct {
// BasePath is the base filesystem location for managing this provider's configuration
// Defaults to $HOME/.vbm/VBox BasePath string
BasePath string
// VirtualBoxPath is where the VirtualBox cmd is available on the local machine
VirtualBoxPath string
Groups []string
// expected to be managed by this tool
Networks []Network
}
// VBox uses the VBoxManage command for its functionality
type VBox struct {
Config Config
Verbose bool
// as discovered and includes networks created out of band (not through this api)
// TODO: Merge them to a single map and provide accessors for specific filtering
HostOnlyNws map[string]*Network
BridgedNws map[string]*Network
InternalNws map[string]*Network
NatNws map[string]*Network
}
func NewVBox(config Config) *VBox {
if config.BasePath == "" {
config.BasePath = DefaultVBBasePath
}
return &VBox{
Config: config,
HostOnlyNws: make(map[string]*Network),
BridgedNws: make(map[string]*Network),
InternalNws: make(map[string]*Network),
NatNws: make(map[string]*Network),
}
}
func GetDefaultVBBasePath() string {
user, err := user.Current()
if err != nil {
panic(fmt.Errorf("basepath not supplied and default location cannot be determined %v", err))
}
return fmt.Sprintf("%s/VirtualBox VMs", user.HomeDir)
}
func IsVBoxError(err error) bool {
_, ok := err.(VBoxError)
return ok
}
//VBoxError are errors that are returned as error by Virtualbox cli on stderr
type VBoxError string
func (ve VBoxError) Error() string {
return string(ve)
}
func (vb *VBox) getVMBaseDir(vm *VirtualMachine) string {
var group string
if vm.Spec.Group != "" {
group = vm.Spec.Group
}
return filepath.Join(vb.Config.BasePath, group, vm.Spec.Name)
}
func (vb *VBox) getVMSettingsFile(vm *VirtualMachine) string {
return filepath.Join(vb.getVMBaseDir(vm), vm.Spec.Name+".vbox")
}
func (vb *VBox) manage(args ...string) (string, error) {
vboxManage := vboxManagePath()
cmd := exec.Command(vboxManage, args...)
glog.V(4).Infof("COMMAND: %v %v", vboxManage, strings.Join(args, " "))
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
stderrStr := stderr.String()
if err != nil {
if ee, ok := err.(*exec.Error); ok && ee.Err == exec.ErrNotFound {
return "", errors.New("unable to find VBoxManage command in path")
}
return "", VBoxError(stderrStr)
}
glog.V(10).Infof("STDOUT:\n{\n%v}", stdout.String())
glog.V(10).Infof("STDERR:\n{\n%v}", stderrStr)
return string(stdout.Bytes()), err
}
func (vb *VBox) modify(vm *VirtualMachine, args ...string) (string, error) {
return vb.manage(append([]string{"modifyvm", vm.UUIDOrName()}, args...)...)
}
func (vb *VBox) control(vm *VirtualMachine, args ...string) (string, error) {
return vb.manage(append([]string{"controlvm", vm.UUIDOrName()}, args...)...)
}
func (vb *VBox) ListDHCPServers() (map[string]*DHCPServer, error) {
listOutput, err := vb.manage("list", "dhcpservers")
if err != nil {
return nil, err
}
m := make(map[string]*DHCPServer)
var dhcpServer *DHCPServer
err = parseKeyValues(listOutput, reColonLine, func(key, val string) error {
switch key {
case "NetworkName":
dhcpServer = &DHCPServer{}
m[val] = dhcpServer
dhcpServer.NetworkName = val
case "IP":
dhcpServer.IPAddress = val
case "upperIPAddress":
dhcpServer.UpperIPAddress = val
case "lowerIPAddress":
dhcpServer.LowerIPAddress = val
case "NetworkMask":
dhcpServer.NetworkMask = val
case "Enabled":
dhcpServer.Enabled = val == "Yes"
}
return nil
})
if err != nil {
return nil, err
}
return m, nil
}
func (vb *VBox) ListOSTypes() (map[string]*OSType, error) {
listOutput, err := vb.manage("list", "ostypes")
if err != nil {
return nil, err
}
m := make(map[string]*OSType)
var osType *OSType
err = parseKeyValues(listOutput, reColonLine, func(key, val string) error {
switch key {
case "ID":
osType = &OSType{}
m[val] = osType
osType.ID = val
case "Description":
osType.Description = val
case "Family ID":
osType.FamilyID = val
case "Family Desc":
osType.FamilyDescription = val
case "64 bit":
osType.Bit64 = val == "true"
}
return nil
})
if err != nil {
return nil, err
}
return m, nil
}
func (vb *VBox) MarkHDImmutable(hdPath string) error {
vb.manage("modifyhd", hdPath, "--type", "immutable")
return nil
}