/
guest.go
471 lines (434 loc) · 16 KB
/
guest.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package rest
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"time"
)
// Guest describes a guest record from the rest api
type Guest struct {
Address string `json:"address,omitempty"`
ADGroup string `json:"ADGroup,omitempty"`
AgentInstalled bool `json:"agentInstalled"`
AgentMetadata *GuestAgentMetadata `json:"agentMetadata"`
AgentVersion string `json:"agentVersion,omitempty"`
Backup *GuestBackup `json:"backup,omitempty"`
Cpus int `json:"cpus,omitempty"`
Currentmem int `json:"currentmem,omitempty"`
Disks []GuestDisk `json:"disks,omitempty"`
DuplicateMitigation bool `json:"duplicateMitigation"`
Error *GuestError `json:"error,omitempty"`
External bool `json:"external"`
DisablePortCheck bool `json:"disablePortCheck"`
Gateway string `json:"gateway,omitempty"`
GPU bool `json:"gpu"`
GuestState string `json:"guestState,omitempty"`
HasBeenReady bool `json:"hasBeenReady,omitempty"`
Hostid string `json:"hostid,omitempty"`
HostDevices []HostDevice `json:"hostDevices,omitempty"`
Hostname string `json:"hostname,omitempty"`
Interfaces []GuestNetwork `json:"interfaces,omitempty"`
Memory int `json:"memory,omitempty"`
MigrationMetadata *GuestMigrationMetadata `json:"migrationMetadata,omitempty"`
MigrationProcessing bool `json:"migrationProcessing"`
Name string `json:"name,omitempty"`
Os string `json:"os,omitempty"`
Persistent bool `json:"persistent,omitempty"`
PoolID string `json:"poolId,omitempty"`
PreviousGuestState string `json:"previousGuestState,omitempty"`
ProfileID string `json:"profileId,omitempty"`
PublishedIP string `json:"publishedIp,omitempty"`
RdpUserInjected bool `json:"rdpUserInjected,omitempty"`
Realm string `json:"realm,omitempty"`
SessionInfo *GuestSessionInfo `json:"sessionInfo,omitempty"`
Stamp interface{} `json:"stamp,omitempty"`
Standalone bool `json:"standalone"`
StateChronology *StateChronology `json:"stateChronology,omitempty"`
Tags []string `json:"tags,omitempty"`
TargetState []string `json:"targetState,omitempty"`
TemplateName string `json:"templateName,omitempty"`
UserVolume *UserVolume `json:"userVolume,omitempty"`
Username string `json:"username"`
UserSessionState string `json:"userSessionState,omitempty"`
UserSession *UserSession `json:"userSession,omitempty"`
UUID string `json:"uuid,omitempty"`
BrokerOptions *GuestBrokerOptions `json:"brokerOptions,omitempty"`
}
// GuestBrokerOptions allows configuring broker connection settings for a guest
type GuestBrokerOptions struct {
DefaultConnection string `json:"defaultConnection,omitempty"`
Connections []GuestBrokerConnection `json:"connections,omitempty"`
}
// GuestBrokerConnection contains settings for a broker connection to a guest
type GuestBrokerConnection struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Port uint `json:"port,omitempty"`
Protocol string `json:"protocol,omitempty"`
DisableHtml5 bool `json:"disableHtml5"`
Gateway struct {
Disabled bool `json:"disabled"`
Protocols []string `json:"prococols,omitempty"`
Persistent bool `json:"persistent"`
} `json:"gateway"`
}
// GuestDisk is the structure for GuestDisk object in db
type GuestDisk struct {
Type string `json:"type"`
Path string `json:"path"`
DiskDriver string `json:"diskDriver"`
Format string `json:"format"`
Filename string `json:"filename"`
StorageID string `json:"storageId"`
Size int `json:"size"`
Device string `json:"dev"`
Backing string `json:"backing"`
SerialNumber string `json:"serial"`
OSVolume int `json:"osvolume"`
BootOrder int `json:"bootOrder"`
Cache string `json:"cache"`
}
// GuestNetwork is the structure for guest network interfaces
type GuestNetwork struct {
Emulation string `json:"emulation"`
MacAddress string `json:"macAddress"`
NetworkType string `json:"network"`
Vlan int `json:"vlan"`
Bus string `json:"bus"`
IPAddress string `json:"ipAddress"`
}
// GuestError is a struct for errors in the guest record
type GuestError struct {
Code string `json:"code"`
Message string `json:"message"`
}
// GuestAgentMetadata contains information fabout the hive agent on the guest
type GuestAgentMetadata struct {
ActualVersion string `json:"actualVersion"`
ExpectedVersion string `json:"expectedVersion"`
Installed bool `json:"installed"`
State string `json:"state"`
UpdateStatus string `json:"updateStatus"`
}
// GuestSessionInfo is struct for sessioninfo of user on VM
type GuestSessionInfo struct {
SessionID int `json:"sessionId"`
SessionState string `json:"sessionState"`
SourceIP string `json:"sourceIP"`
SourceName string `json:"sourceName"`
SessionWorkstationName string `json:"sessionWorkstationName"`
SessionResolution string `json:"sessionResolution"`
}
// GuestSnapshot is contains snapshot information about the guest
type GuestSnapshot struct {
Name string `json:"name"`
Domain string `json:"domain"`
Current string `json:"current"`
State string `json:"state"`
Location string `json:"location"`
Metadata string `json:"metadata"`
}
// GuestBackup is struct for backup data of VM
type GuestBackup struct {
Enabled bool `json:"enabled"`
State string `json:"state"`
Frequency string `json:"frequency"`
TargetStorageID string `json:"targetStorageId"`
LastBackup interface{} `json:"lastBackup"`
StateMessage string `json:"stateMessage"`
DiskFrozen bool `json:"diskFrozen"`
}
type GuestMigrationMetadata struct {
SourceHostId string `json:"sourceHostId"`
SourceHostname string `json:"sourceHostname"`
DestinationHostId string `json:"destinationHostId"`
DestinationHostname string `json:"destinationHostname"`
Progress int `json:"progress"`
MigratableXml string `json:"migratableXml"`
}
// HostDevice is information about a device forwarded to the guest from the host
type HostDevice struct {
Type string `json:"type"`
Model string `json:"model"`
Managed bool `json:"managed"`
Domain int `json:"domain"`
Bus int `json:"bus"`
Slot int `json:"slot"`
Func int `json:"func"`
UUID string `json:"uuid"`
}
// StateChronology state tracking for the guest
type StateChronology struct {
Current string `json:"current"`
Next string `json:"next"`
Target string `json:"target"`
}
// UserSession contains user session information for a guest
type UserSession struct {
UserSessionState string `json:"userSessionState"`
LastUserLoginTime interface{} `json:"lastUserLoginTime"`
LastLoginDuration int `json:"lastLoginDuration"`
DisconnectTime interface{} `json:"disconnectTime"`
}
// UserVolume is the struct for uservolume on guest record
type UserVolume struct {
State string `json:"state,omitempty"`
LastReplication interface{} `json:"lastReplication,omitempty"`
RepliacationRequested interface{} `json:"replicationRequested,omitempty"`
Source string `json:"source,omitempty"`
Target string `json:"target,omitempty"`
RunningBackup bool `json:"runningBackup,omitempty"`
StateMessage interface{} `json:"stateMessage,omitempty"`
DiskFrozen bool `json:"diskFrozen,omitempty"`
BackupSchedule int `json:"backupSchedule,omitempty"`
}
func (guest Guest) String() string {
json, _ := json.MarshalIndent(guest, "", " ")
return string(json)
}
// ListGuests returns an array of all guests with an optional filter string
func (client *Client) ListGuests(query string) ([]Guest, error) {
var guests []Guest
path := "guests"
if query != "" {
path += "?" + query
}
body, err := client.request("GET", path, nil)
if err != nil {
return guests, err
}
err = json.Unmarshal(body, &guests)
return guests, err
}
// GetGuest requests a single guest by name
func (client *Client) GetGuest(name string) (*Guest, error) {
var guest Guest
if name == "" {
return nil, errors.New("name cannot be empty")
}
body, err := client.request("GET", "guest/"+url.PathEscape(name), nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &guest)
return &guest, err
}
// Shutdown asks the guest operation system to shutdown
func (guest *Guest) Shutdown(client *Client) error {
if guest.Name == "" {
return errors.New("name cannot be empty")
}
_, err := client.request("POST", "guest/"+url.PathEscape(guest.Name)+"/shutdown", nil)
return err
}
// Reboot asks the guest operation system to reboot
func (guest *Guest) Reboot(client *Client) error {
if guest.Name == "" {
return errors.New("name cannot be empty")
}
_, err := client.request("POST", "guest/"+url.PathEscape(guest.Name)+"/reboot", nil)
return err
}
// Refresh recreates the guest with the latest pool configuration
func (guest *Guest) Refresh(client *Client) error {
if guest.Name == "" {
return errors.New("name cannot be empty")
}
if guest.Standalone {
return guest.Delete(client)
}
_, err := client.request("POST", "guest/"+url.PathEscape(guest.Name)+"/refresh", nil)
return err
}
// Poweron starts a powered off guest
func (guest *Guest) Poweron(client *Client) error {
if guest.Name == "" {
return errors.New("name cannot be empty")
}
_, err := client.request("POST", "guest/"+url.PathEscape(guest.Name)+"/poweron", nil)
return err
}
// Poweroff forces a guest to powers off
func (guest *Guest) Poweroff(client *Client) error {
if guest.Name == "" {
return errors.New("name cannot be empty")
}
_, err := client.request("POST", "guest/"+url.PathEscape(guest.Name)+"/poweroff", nil)
return err
}
// Reset forces a guest to hard reset
func (guest *Guest) Reset(client *Client) error {
if guest.Name == "" {
return errors.New("name cannot be empty")
}
_, err := client.request("POST", "guest/"+url.PathEscape(guest.Name)+"/reset", nil)
return err
}
// Update a guest record
func (guest *Guest) Update(client *Client) (string, error) {
var result string
jsonValue, _ := json.Marshal(guest)
body, err := client.request("PUT", "guest/"+url.PathEscape(guest.Name), jsonValue)
if err == nil {
result = string(body)
}
return result, err
}
// Delete deletes a guest
func (guest *Guest) Delete(client *Client) error {
if guest.Name == "" {
return errors.New("name cannot be empty")
}
if guest.External {
_, err := client.request("DELETE", "guest/"+url.PathEscape(guest.Name), nil)
return err
}
_, err := client.request("POST", "guest/"+url.PathEscape(guest.Name)+"/delete", nil)
return err
}
// StartBackup requests starting a backup immediately
func (guest *Guest) StartBackup(client *Client, storageId string) (*Task, error) {
if guest.Name == "" {
return nil, errors.New("name cannot be empty")
}
data := map[string]interface{}{}
if storageId != "" {
data["storageId"] = storageId
}
jsonValue, err := json.Marshal(data)
if err != nil {
return nil, err
}
return client.getTaskFromResponse(client.request("POST", "guest/"+url.PathEscape(guest.Name)+"/backup", jsonValue))
}
// StartBackup requests starting a backup immediately
func (guest *Guest) ListBackups(client *Client, storageId string) ([]string, error) {
if guest.Name == "" {
return nil, errors.New("name cannot be empty")
}
data := map[string]interface{}{}
if storageId != "" {
data["storageId"] = storageId
}
jsonValue, err := json.Marshal(data)
if err != nil {
return nil, err
}
backups := []string{}
body, err := client.request("GET", "guest/"+url.PathEscape(guest.Name)+"/backups", jsonValue)
if err != nil {
return backups, err
}
err = json.Unmarshal(body, &backups)
return backups, err
}
// Restore restores a guest from a backup
func (guest *Guest) Restore(client *Client, storageId, backup string) (*Task, error) {
if guest.Name == "" {
return nil, errors.New("name cannot be empty")
}
data := map[string]interface{}{}
if storageId != "" {
data["storageId"] = storageId
}
if backup != "" {
data["backup"] = backup
}
jsonValue, err := json.Marshal(data)
if err != nil {
return nil, err
}
return client.getTaskFromResponse(client.request("POST", "guest/"+url.PathEscape(guest.Name)+"/restore", jsonValue))
}
// Migrate migrate a guest to a different host
func (guest *Guest) Migrate(client *Client, destinationHostid string) error {
if guest.Name == "" {
return errors.New("name cannot be empty")
}
jsonData := map[string]string{"destinationId": destinationHostid}
jsonValue, err := json.Marshal(jsonData)
if err != nil {
return err
}
_, err = client.request("POST", "guest/"+url.PathEscape(guest.Name)+"/migrate", jsonValue)
return err
}
func checkGuestState(guest Guest) bool {
for _, v := range guest.TargetState {
if v == guest.GuestState {
return true
}
}
return false
}
// WaitForGuest waits for a guest state to match the targetState
func (guest Guest) WaitForGuest(client *Client, timeout time.Duration) error {
return guest.WaitForGuestWithContext(context.Background(), client, timeout)
}
// WaitForGuestWithContext waits for a guest state to match the targetState
func (guest Guest) WaitForGuestWithContext(ctx context.Context, client *Client, timeout time.Duration) error {
if checkGuestState(guest) {
return nil
}
newVal := Guest{}
feed, err := client.GetChangeFeedWithContext(ctx, "guest", map[string]string{"name": guest.Name}, false)
if err != nil {
return err
}
timer := time.NewTimer(timeout)
if timeout <= 0 && !timer.Stop() {
<-timer.C
}
for {
select {
case <-timer.C:
return fmt.Errorf("timed out")
case msg := <-feed.Data:
if msg.Error != nil {
feed.Close()
return msg.Error
}
err = json.Unmarshal(msg.NewValue, &newVal)
if err != nil {
err = fmt.Errorf("error with json unmarshal: %v", err)
feed.Close()
return err
}
if checkGuestState(newVal) {
feed.Close()
return nil
}
}
}
}
// ResetRecord forces a guest record to be reset so it can be rebuilt
func (guest *Guest) ResetRecord(client *Client) error {
if guest.Name == "" {
return errors.New("name cannot be empty")
}
_, err := client.request("POST", "guest/"+url.PathEscape(guest.Name)+"/resetRecord", nil)
return err
}
// ExternalGuest is used to add external guest records to the system
type ExternalGuest struct {
GuestName string `json:"guestName,omitempty"`
Address string `json:"address,omitempty"`
Username string `json:"username,omitempty"`
ADGroup string `json:"ADGroup,omitempty"`
Realm string `json:"realm,omitempty"`
OS string `json:"os,omitempty"`
DisablePortCheck bool `json:"disablePortCheck"`
BrokerOptions GuestBrokerOptions `json:"brokerOptions,omitempty"`
}
// Create creates a new pool
func (guest *ExternalGuest) Create(client *Client) (string, error) {
var result string
jsonValue, _ := json.Marshal(guest)
body, err := client.request("POST", "guest/external", jsonValue)
if err == nil {
result = string(body)
}
return result, err
}