-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
image.go
413 lines (364 loc) · 9.72 KB
/
image.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
package cobblerclient
import (
"fmt"
"reflect"
"time"
)
type Architecture int64
const (
aI386 = iota
aX8664
aIA64
aPPC
aPPC64
aPPC64LE
aPPC64EL
aS390
aS390X
aARM
aAARCH64
)
func (a Architecture) String() string {
switch a {
case aI386:
return "i386"
case aX8664:
return "x86_64"
case aIA64:
return "ia64"
case aPPC:
return "ppc"
case aPPC64:
return "ppc64"
case aPPC64LE:
return "ppc64le"
case aPPC64EL:
return "ppc64el"
case aS390:
return "s390"
case aS390X:
return "s390x"
case aARM:
return "arm"
case aAARCH64:
return "aarch64"
}
return "unknown"
}
type ImageType int64
const (
itDIRECT = iota
itISO
itMEMDISK
itVIRTCLONE
)
func (i ImageType) String() string {
switch i {
case itDIRECT:
return "direct"
case itISO:
return "iso"
case itMEMDISK:
return "memdisk"
case itVIRTCLONE:
return "virt-clone"
}
return "unknown"
}
type VirtType int64
const (
itINHERITED = iota
vtQEMU
itKVM
itXENPV
itXENFV
itVMWARE
itVMWAREW
itOPENVZ
itAUTO
)
func (v VirtType) String() string {
switch v {
case itINHERITED:
return inherit
case vtQEMU:
return "qemu"
case itKVM:
return "kvm"
case itXENPV:
return "xenpv"
case itXENFV:
return "xenfv"
case itVMWARE:
return "vmware"
case itVMWAREW:
return "vmwarew"
case itOPENVZ:
return "openvz"
case itAUTO:
return "auto"
}
return "unknown"
}
type VirtDiskDriver int64
const (
vddINHERITED = iota
vddRAW
vddQCOW2
vddQED
vddVDI
vddVDMK
)
func (v VirtDiskDriver) String() string {
switch v {
case vddINHERITED:
return inherit
case vddRAW:
return "raw"
case vddQCOW2:
return "qcow2"
case vddQED:
return "qed"
case vddVDI:
return "vdi"
case vddVDMK:
return "vdmk"
}
return "unknown"
}
// Image is a created image.
// Get the fields from cobbler/items/image.py
type Image struct {
Item `mapstructure:",squash"`
// Image specific fields
// Arch Architecture
// ImageType ImageType `mapstructure:"image_type"`
// VirtDiskDriver VirtDiskDriver `mapstructure:"virt_disk_driver"`
// VirtType VirtType `mapstructure:"virt_type"`
Arch string `mapstructure:"arch"`
Autoinstall string `mapstructure:"autoinstall"`
Breed string `mapstructure:"breed"`
File string `mapstructure:"file"`
ImageType string `mapstructure:"image_type"`
NetworkCount int `mapstructure:"network_count"`
OsVersion string `mapstructure:"os_version"`
BootLoaders []string `mapstructure:"boot_loaders"`
Menu string `mapstructure:"menu"`
VirtAutoBoot bool `mapstructure:"virt_auto_boot"`
VirtBridge string `mapstructure:"virt_bridge"`
VirtCpus int `mapstructure:"virt_cpus"`
VirtDiskDriver string `mapstructure:"virt_disk_driver"`
VirtFileSize Value[float64] `mapstructure:"virt_file_size"`
VirtPath string `mapstructure:"virt_path"`
VirtRam Value[int] `mapstructure:"virt_ram"`
VirtType string `mapstructure:"virt_type"`
SupportedBootLoaders []string `mapstructure:"supported_boot_loaders" cobbler:"noupdate"`
Client
}
func NewImage() Image {
return Image{
Item: NewItem(),
Arch: "x86_64",
Autoinstall: inherit,
BootLoaders: make([]string, 0),
ImageType: "direct",
VirtCpus: 1,
VirtDiskDriver: "raw",
VirtFileSize: Value[float64]{
IsInherited: true,
},
VirtRam: Value[int]{
IsInherited: true,
},
VirtType: inherit,
SupportedBootLoaders: make([]string, 0),
}
}
func convertRawImage(name string, xmlrpcResult interface{}) (*Image, error) {
var image Image
if xmlrpcResult == "~" {
return nil, fmt.Errorf("profile %s not found", name)
}
decodeResult, err := decodeCobblerItem(xmlrpcResult, &image)
if err != nil {
return nil, err
}
// Now clean the Value structs
decodedImage := decodeResult.(*Image)
err = sanitizeValueMapStruct(&decodedImage.KernelOptions)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedImage.KernelOptionsPost)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedImage.AutoinstallMeta)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedImage.FetchableFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedImage.BootFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedImage.TemplateFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedImage.MgmtParameters)
if err != nil {
return nil, err
}
err = sanitizeValueSliceStruct(&decodedImage.Owners)
if err != nil {
return nil, err
}
err = sanitizeValueSliceStruct(&decodedImage.MgmtClasses)
return decodedImage, nil
}
func convertRawImagesList(xmlrpcResult interface{}) ([]*Image, error) {
var images []*Image
for _, d := range xmlrpcResult.([]interface{}) {
image, err := convertRawImage("unknown", d)
if err != nil {
return nil, err
}
image.Meta = ItemMeta{
IsFlattened: false,
IsResolved: false,
}
images = append(images, image)
}
return images, nil
}
// GetImages returns all images in Cobbler.
func (c *Client) GetImages() ([]*Image, error) {
result, err := c.Call("get_images", "-1", c.Token)
if err != nil {
return nil, err
}
return convertRawImagesList(result)
}
// ListImageNames returns a list of all known image names.
func (c *Client) ListImageNames() ([]string, error) {
return c.GetItemNames("image")
}
// GetImage returns a single image obtained by its name.
func (c *Client) GetImage(name string, flattened, resolved bool) (*Image, error) {
result, err := c.getConcreteItem("get_image", name, flattened, resolved)
if err != nil {
return nil, err
}
image, err := convertRawImage(name, result)
if err != nil {
return nil, err
}
image.Meta = ItemMeta{
IsFlattened: flattened,
IsResolved: resolved,
}
return image, nil
}
// CreateImage creates an image.
func (c *Client) CreateImage(image Image) (*Image, error) {
// To create an image via the Cobbler API, first call new_image to obtain an ID
result, err := c.Call("new_image", c.Token)
if err != nil {
return nil, err
}
newID := result.(string)
// Set the value of all fields
item := reflect.ValueOf(&image).Elem()
if err := c.updateCobblerFields("image", item, newID); err != nil {
return nil, err
}
// Save the final image
if err = c.SaveImage(newID, "new"); err != nil {
return nil, err
}
// Return a clean copy of the image
return c.GetImage(image.Name, false, false)
}
// UpdateImage updates a single image.
func (c *Client) UpdateImage(image *Image) error {
item := reflect.ValueOf(image).Elem()
id, err := c.GetItemHandle("image", image.Name)
if err != nil {
return err
}
if err := c.updateCobblerFields("image", item, id); err != nil {
return err
}
// Save the final image
if err := c.SaveImage(id, "bypass"); err != nil {
return err
}
return nil
}
// DeleteImage deletes a single Image by its name.
func (c *Client) DeleteImage(name string) error {
return c.DeleteImageRecursive(name, false)
}
// DeleteImageRecursive deletes a single Image by its name with the option to do so recursively.
func (c *Client) DeleteImageRecursive(name string, recursive bool) error {
_, err := c.Call("remove_image", name, c.Token, recursive)
return err
}
// FindImage searches for one or more images by any of its attributes.
func (c *Client) FindImage(criteria map[string]interface{}) ([]*Image, error) {
result, err := c.Call("find_image", criteria, true, c.Token)
if err != nil {
return nil, err
}
return convertRawImagesList(result)
}
// FindImageNames searches for one or more distros by any of its attributes.
func (c *Client) FindImageNames(criteria map[string]interface{}) ([]string, error) {
resultUnmarshalled, err := c.Call("find_image", criteria, false, c.Token)
return returnStringSlice(resultUnmarshalled, err)
}
// GetImageHandle gets the internal ID of a Cobbler item.
func (c *Client) GetImageHandle(name string) (string, error) {
res, err := c.Call("get_image_handle", name, c.Token)
return returnString(res, err)
}
// CopyImage duplicates an image on the server with a new name.
func (c *Client) CopyImage(objectId, newName string) error {
_, err := c.Call("copy_image", objectId, newName, c.Token)
return err
}
// RenameImage renames an image with a given object id.
func (c *Client) RenameImage(objectId, newName string) error {
_, err := c.Call("rename_image", objectId, newName, c.Token)
return err
}
// GetImagesSince returns all images which were created after the specified date.
func (c *Client) GetImagesSince(mtime time.Time) ([]*Image, error) {
result, err := c.Call("get_images_since", float64(mtime.Unix()))
if err != nil {
return nil, err
}
return convertRawImagesList(result)
}
// GetImageAsRendered returns the datastructure after it has passed through Cobblers inheritance structure.
func (c *Client) GetImageAsRendered(name string) (map[string]interface{}, error) {
result, err := c.Call("get_image_as_rendered", name, c.Token)
if err != nil {
return nil, err
}
return result.(map[string]interface{}), err
}
// SaveImage saves all changes performed via XML-RPC to disk on the server side.
func (c *Client) SaveImage(objectId, editmode string) error {
_, err := c.Call("save_image", objectId, c.Token, editmode)
return err
}
// GetValidImageBootLoaders retrieves the list of bootloaders that can be assigned to an image.
func (c *Client) GetValidImageBootLoaders(imageName string) ([]string, error) {
resultUnmarshalled, err := c.Call("get_valid_image_boot_loaders", imageName, c.Token)
return returnStringSlice(resultUnmarshalled, err)
}