-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm_calls.go
351 lines (309 loc) · 11.2 KB
/
vm_calls.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package lib
import (
"bytes"
"fmt"
"strconv"
"github.com/BytemarkHosting/bytemark-client/lib/brain"
"github.com/BytemarkHosting/bytemark-client/util/log"
)
//CreateVirtualMachine creates a virtual machine in the given group.
func (c *bytemarkClient) CreateVirtualMachine(group GroupName, spec brain.VirtualMachineSpec) (vm brain.VirtualMachine, err error) {
err = c.EnsureGroupName(&group)
if err != nil {
return
}
r, err := c.BuildRequest("POST", BrainEndpoint, "/accounts/%s/groups/%s/vm_create", group.Account, group.Group)
if err != nil {
return
}
if spec.IPs != nil {
if spec.IPs.IPv4 == "" && spec.IPs.IPv6 == "" {
spec.IPs = nil
}
}
if spec.Discs != nil {
if len(spec.Discs) == 0 {
spec.Discs = nil
}
for i, disc := range spec.Discs {
newDisc, discErr := disc.Validate()
if discErr != nil {
return vm, discErr
}
spec.Discs[i] = *newDisc
}
labelDiscs(spec.Discs, 0)
}
oldfile := log.LogFile
log.LogFile = nil
_, _, err = r.MarshalAndRun(spec, &vm)
log.LogFile = oldfile
return vm, err
}
// DeleteVirtualMachine deletes the named virtual machine.
// returns nil on success or an error otherwise.
func (c *bytemarkClient) DeleteVirtualMachine(name VirtualMachineName, purge bool) (err error) {
err = c.EnsureVirtualMachineName(&name)
if err != nil {
return err
}
purgePart := ""
if purge {
purgePart = "?purge=true"
}
r, err := c.BuildRequest("DELETE", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s"+purgePart, name.Account, name.Group, name.VirtualMachine)
if err != nil {
return err
}
_, _, err = r.Run(nil, nil)
return err
}
// GetVirtualMachine requests an overview of the named VM, regardless of its deletion status.
func (c *bytemarkClient) GetVirtualMachine(name VirtualMachineName) (vm brain.VirtualMachine, err error) {
var r Request
// If the VM name is numeric, it means it is an internal Bytemark ID,
// so we should use a different endpoint
if _, nErr := strconv.Atoi(name.VirtualMachine); nErr == nil {
r, err = c.BuildRequest("GET", BrainEndpoint, "/virtual_machines/%s?include_deleted=true&view=overview", name.VirtualMachine)
} else {
err = c.EnsureVirtualMachineName(&name)
if err != nil {
return
}
r, err = c.BuildRequest("GET", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s?include_deleted=true&view=overview", name.Account, name.Group, name.VirtualMachine)
}
if err != nil {
return
}
_, _, err = r.Run(nil, &vm)
if err != nil {
return
}
return
}
//MoveVirtualMachine moves the virtual machine to the given name, across groups if needed.
func (c *bytemarkClient) MoveVirtualMachine(oldName VirtualMachineName, newName VirtualMachineName) (err error) {
err = c.EnsureVirtualMachineName(&oldName)
if err != nil {
return
}
err = c.EnsureVirtualMachineName(&newName)
if err != nil {
return
}
// create the change we want to see in the server
change := brain.VirtualMachine{Name: newName.VirtualMachine}
if newName.Group != "" || newName.Account != "" {
// get group
groupName := GroupName{Group: newName.Group, Account: newName.Account}
group, groupErr := c.GetGroup(groupName)
if groupErr != nil {
return groupErr
}
change.GroupID = group.ID
}
// PUT the change
r, err := c.BuildRequest("PUT", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s", oldName.Account, oldName.Group, oldName.VirtualMachine)
if err != nil {
return err
}
_, _, err = r.MarshalAndRun(change, nil)
return err
}
// ReimageVirtualMachine reimages the named virtual machine. This will wipe everything on the first disk in the vm and install a new OS on top of it.
// Note that the machine in question must already be powered off. Once complete, according to the API docs, the vm will be powered on but its autoreboot_on will be false.
func (c *bytemarkClient) ReimageVirtualMachine(name VirtualMachineName, image brain.ImageInstall) (err error) {
err = c.EnsureVirtualMachineName(&name)
if err != nil {
return err
}
r, err := c.BuildRequest("POST", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s/reimage", name.Account, name.Group, name.VirtualMachine)
if err != nil {
return err
}
oldfile := log.LogFile
log.LogFile = nil
_, _, err = r.MarshalAndRun(image, nil)
log.LogFile = oldfile
return err
}
// ResetVirtualMachine resets the named virtual machine. This is like pressing the reset
// button on a physical computer. This does not cause a new process to be started, so does not apply any pending hardware changes.
// returns nil on success or an error otherwise.
func (c *bytemarkClient) ResetVirtualMachine(name VirtualMachineName) (err error) {
err = c.EnsureVirtualMachineName(&name)
if err != nil {
return err
}
r, err := c.BuildRequest("POST", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s/signal", name.Account, name.Group, name.VirtualMachine)
if err != nil {
return err
}
_, _, err = r.Run(bytes.NewBufferString(`{"signal":"reset"}`), nil)
return err
}
// RestartVirtualMachine restarts the named virtual machine. This is
// returns nil on success or an error otherwise.
func (c *bytemarkClient) RestartVirtualMachine(name VirtualMachineName) (err error) {
err = c.EnsureVirtualMachineName(&name)
if err != nil {
return err
}
r, err := c.BuildRequest("PUT", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s", name.Account, name.Group, name.VirtualMachine)
if err != nil {
return err
}
_, _, err = r.Run(bytes.NewBufferString(`{"autoreboot_on":true, "power_on": false}`), nil)
return err
}
// StartVirtualMachine starts the named virtual machine.
// returns nil on success or an error otherwise.
func (c *bytemarkClient) StartVirtualMachine(name VirtualMachineName) (err error) {
err = c.EnsureVirtualMachineName(&name)
if err != nil {
return err
}
r, err := c.BuildRequest("PUT", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s", name.Account, name.Group, name.VirtualMachine)
if err != nil {
return err
}
_, _, err = r.Run(bytes.NewBufferString(`{"autoreboot_on":true, "power_on": true}`), nil)
return err
}
// StopVirtualMachine starts the named virtual machine.
// returns nil on success or an error otherwise.
func (c *bytemarkClient) StopVirtualMachine(name VirtualMachineName) (err error) {
err = c.EnsureVirtualMachineName(&name)
if err != nil {
return err
}
r, err := c.BuildRequest("PUT", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s", name.Account, name.Group, name.VirtualMachine)
if err != nil {
return err
}
_, _, err = r.Run(bytes.NewBufferString(`{"autoreboot_on":false, "power_on": false}`), nil)
return err
}
// ShutdownVirtualMachine sends an ACPI shutdown to the VM. This will cause a graceful shutdown of the machine
// returns nil on success or an error otherwise.
func (c *bytemarkClient) ShutdownVirtualMachine(name VirtualMachineName, stayoff bool) (err error) {
err = c.EnsureVirtualMachineName(&name)
if err != nil {
return
}
var r Request
if stayoff {
r, err = c.BuildRequest("PUT", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s", name.Account, name.Group, name.VirtualMachine)
if err != nil {
return
}
_, _, err = r.Run(bytes.NewBufferString(`{"autoreboot_on":false}`), nil)
if err != nil {
return
}
}
r, err = c.BuildRequest("POST", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s/signal", name.Account, name.Group, name.VirtualMachine)
if err != nil {
return
}
_, _, err = r.Run(bytes.NewBufferString(`{"signal": "powerdown"}`), nil)
return err
}
// UndeleteVirtualMachine changes the deleted flag on a VM back to false.
// Return nil on success, an error otherwise.
func (c *bytemarkClient) UndeleteVirtualMachine(name VirtualMachineName) (err error) {
err = c.EnsureVirtualMachineName(&name)
if err != nil {
return err
}
r, err := c.BuildRequest("PUT", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s", name.Account, name.Group, name.VirtualMachine)
if err != nil {
return err
}
_, _, err = r.Run(bytes.NewBufferString(`{"deleted":false}`), nil)
return err
}
// SetVirtualMachineHardwareProfile specifies the hardware profile on a VM. Optionally locks or unlocks h. profile
// Return nil on success, an error otherwise.
func (c *bytemarkClient) SetVirtualMachineHardwareProfile(name VirtualMachineName, profile string, locked ...bool) (err error) {
err = c.EnsureVirtualMachineName(&name)
if err != nil {
return err
}
r, err := c.BuildRequest("PUT", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s", name.Account, name.Group, name.VirtualMachine)
if err != nil {
return err
}
hwprofileLock := ""
if len(locked) > 0 {
hwprofileLock = `, "hardware_profile_locked": false`
if locked[0] {
hwprofileLock = `, "hardware_profile_locked": true`
}
}
profileJSON := fmt.Sprintf(`{"hardware_profile": "%s"%s}`, profile, hwprofileLock)
_, _, err = r.Run(bytes.NewBufferString(profileJSON), nil)
return err
}
// SetVirtualMachineHardwareProfileLock locks or unlocks the hardware profile of a VM.
// Return nil on success, an error otherwise.
func (c *bytemarkClient) SetVirtualMachineHardwareProfileLock(name VirtualMachineName, locked bool) (err error) {
err = c.EnsureVirtualMachineName(&name)
if err != nil {
return err
}
r, err := c.BuildRequest("PUT", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s", name.Account, name.Group, name.VirtualMachine)
if err != nil {
return err
}
lockJSON := `{"hardware_profile_locked": false}`
if locked {
lockJSON = `{"hardware_profile_locked": true}`
}
_, _, err = r.Run(bytes.NewBufferString(lockJSON), nil)
return err
}
// SetVirtualMachineMemory sets the RAM available to a virtual machine in megabytes
// Return nil on success, an error otherwise.
func (c *bytemarkClient) SetVirtualMachineMemory(name VirtualMachineName, memory int) (err error) {
err = c.EnsureVirtualMachineName(&name)
if err != nil {
return err
}
r, err := c.BuildRequest("PUT", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s", name.Account, name.Group, name.VirtualMachine)
if err != nil {
return err
}
memoryJSON := fmt.Sprintf(`{"memory": %d}`, memory)
_, _, err = r.Run(bytes.NewBufferString(memoryJSON), nil)
return err
}
// SetVirtualMachineCores sets the number of CPUs available to a virtual machine
// Return nil on success, an error otherwise.
func (c *bytemarkClient) SetVirtualMachineCores(name VirtualMachineName, cores int) (err error) {
err = c.EnsureVirtualMachineName(&name)
if err != nil {
return err
}
r, err := c.BuildRequest("PUT", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s", name.Account, name.Group, name.VirtualMachine)
if err != nil {
return err
}
coresJSON := fmt.Sprintf(`{"cores": %d}`, cores)
_, _, err = r.Run(bytes.NewBufferString(coresJSON), nil)
return err
}
// SetVirtualMachineCDROM sets the URL of a CD to attach to a virtual machine. Set url to "" to remove the CD.
// Returns nil on success, an error otherwise.
func (c *bytemarkClient) SetVirtualMachineCDROM(name VirtualMachineName, url string) (err error) {
err = c.EnsureVirtualMachineName(&name)
if err != nil {
return err
}
r, err := c.BuildRequest("PUT", BrainEndpoint, "/accounts/%s/groups/%s/virtual_machines/%s", name.Account, name.Group, name.VirtualMachine)
if err != nil {
return err
}
_, _, err = r.MarshalAndRun(brain.VirtualMachine{CdromURL: url}, nil)
return err
}