-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
system.go
590 lines (516 loc) · 17.9 KB
/
system.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
Copyright 2015 Container Solutions
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cobblerclient
import (
"fmt"
"log"
"reflect"
"time"
"github.com/fatih/structs"
"github.com/go-viper/mapstructure/v2"
)
// System is a created system.
type System struct {
Item `mapstructure:",squash"`
// These are internal fields and cannot be modified.
IPv6Autoconfiguration bool `mapstructure:"ipv6_autoconfiguration" cobbler:"noupdate"`
ReposEnabled bool `mapstructure:"repos_enabled" cobbler:"noupdate"`
Autoinstall string `mapstructure:"autoinstall"`
BootLoaders Value[[]string] `mapstructure:"boot_loaders"`
EnableIPXE Value[bool] `mapstructure:"enable_ipxe"`
Filename string `mapstructure:"filename"`
Gateway string `mapstructure:"gateway"`
Hostname string `mapstructure:"hostname"`
IPv6DefaultDevice string `mapstructure:"ipv6_default_device"`
Image string `mapstructure:"image"`
Interfaces Interfaces `mapstructure:"interfaces" cobbler:"noupdate"`
NameServers []string `mapstructure:"name_servers"`
NameServersSearch []string `mapstructure:"name_servers_search"`
NetbootEnabled bool `mapstructure:"netboot_enabled"`
NextServerv4 string `mapstructure:"next_server_v4"`
NextServerv6 string `mapstructure:"next_server_v6"`
PowerAddress string `mapstructure:"power_address"`
PowerID string `mapstructure:"power_id"`
PowerIdentityFile string `mapstructure:"power_identity_file"`
PowerOptions string `mapstructure:"power_options"`
PowerPass string `mapstructure:"power_pass"`
PowerType string `mapstructure:"power_type"`
PowerUser string `mapstructure:"power_user"`
Profile string `mapstructure:"profile"`
Proxy string `mapstructure:"proxy"`
RedhatManagementKey string `mapstructure:"redhat_management_key"`
SerialBaudRate int `mapstructure:"serial_baud_rate"`
SerialDevice int `mapstructure:"serial_device"`
Server string `mapstructure:"server"`
Status string `mapstructure:"status"`
VirtAutoBoot Value[bool] `mapstructure:"virt_auto_boot"`
VirtCPUs Value[int] `mapstructure:"virt_cpus"`
VirtDiskDriver string `mapstructure:"virt_disk_driver"`
VirtFileSize Value[float64] `mapstructure:"virt_file_size"`
VirtPXEBoot bool `mapstructure:"virt_pxe_boot"`
VirtPath string `mapstructure:"virt_path"`
VirtRAM Value[int] `mapstructure:"virt_ram"`
VirtType string `mapstructure:"virt_type"`
Client
}
// Interface is an interface in a system.
type Interface struct {
BondingOpts string `mapstructure:"bonding_opts" structs:"bonding_opts"`
BridgeOpts string `mapstructure:"bridge_opts" structs:"bridge_opts"`
CNAMEs []string `mapstructure:"cnames" structs:"cnames"`
ConnectedMode bool `mapstructure:"connected_mode" structs:"connected_mode"`
DHCPTag string `mapstructure:"dhcp_tag" structs:"dhcp_tag"`
DNSName string `mapstructure:"dns_name" structs:"dns_name"`
Gateway string `mapstructure:"if_gateway" structs:"if_gateway"`
IPAddress string `mapstructure:"ip_address" structs:"ip_address"`
IPv6Address string `mapstructure:"ipv6_address" structs:"ipv6_address"`
IPv6DefaultGateway string `mapstructure:"ipv6_default_gateway" structs:"ipv6_default_gateway"`
IPv6MTU string `mapstructure:"ipv6_mtu" structs:"ipv6_mtu"`
IPv6Prefix string `mapstructure:"ipv6_prefix" structs:"ipv6_prefix"`
IPv6Secondaries []string `mapstructure:"ipv6_secondaries" structs:"ipv6_secondaries"`
IPv6StaticRoutes []string `mapstructure:"ipv6_static_routes" structs:"ipv6_static_routes"`
InterfaceMaster string `mapstructure:"interface_master" structs:"interface_master"`
InterfaceType string `mapstructure:"interface_type" structs:"interface_type"`
MACAddress string `mapstructure:"mac_address" structs:"mac_address"`
MTU string `mapstructure:"mtu" structs:"mtu"`
Management bool `mapstructure:"management" structs:"management"`
Netmask string `mapstructure:"netmask" structs:"netmask"`
Static bool `mapstructure:"static" structs:"static"`
StaticRoutes []string `mapstructure:"static_routes" structs:"static_routes"`
VirtBridge string `mapstructure:"virt_bridge" structs:"virt_bridge"`
}
// Interfaces is a collection of interfaces in a system.
type Interfaces map[string]Interface
func NewSystem() System {
system := System{
Item: NewItem(),
Autoinstall: inherit,
BootLoaders: Value[[]string]{
IsInherited: true,
},
EnableIPXE: Value[bool]{
IsInherited: true,
},
Filename: inherit,
Interfaces: make(map[string]Interface),
NameServers: make([]string, 0),
NameServersSearch: make([]string, 0),
NetbootEnabled: false,
NextServerv4: inherit,
NextServerv6: inherit,
Proxy: inherit,
RedhatManagementKey: inherit,
SerialBaudRate: -1,
SerialDevice: -1,
Server: inherit,
VirtAutoBoot: Value[bool]{
IsInherited: true,
},
VirtCPUs: Value[int]{
IsInherited: true,
},
VirtDiskDriver: inherit,
VirtFileSize: Value[float64]{
IsInherited: true,
},
VirtPath: inherit,
VirtRAM: Value[int]{
IsInherited: true,
},
VirtType: inherit,
}
// Overwrite defaults from Item
system.Owners = Value[[]string]{
IsInherited: true,
}
system.BootFiles = Value[map[string]interface{}]{
IsInherited: true,
}
system.FetchableFiles = Value[map[string]interface{}]{
IsInherited: true,
}
system.AutoinstallMeta = Value[map[string]interface{}]{
IsInherited: true,
}
system.KernelOptions = Value[map[string]interface{}]{
IsInherited: true,
}
system.KernelOptionsPost = Value[map[string]interface{}]{
IsInherited: true,
}
system.MgmtClasses = Value[[]string]{
IsInherited: true,
}
return system
}
func NewInterface() Interface {
return Interface{
InterfaceType: "na",
CNAMEs: make([]string, 0),
IPv6Secondaries: make([]string, 0),
IPv6StaticRoutes: make([]string, 0),
StaticRoutes: make([]string, 0),
}
}
func (c *Client) convertRawSystem(name string, xmlrpcResult interface{}) (*System, error) {
var system System
if xmlrpcResult == "~" {
return nil, fmt.Errorf("system %s not found", name)
}
decodeResult, err := decodeCobblerItem(xmlrpcResult, &system)
if err != nil {
return nil, err
}
s := decodeResult.(*System)
s.Client = *c
// Now clean the network interface struct
if s.Interfaces == nil {
s.Interfaces = make(map[string]Interface)
}
// Now clean the Value structs
err = sanitizeValueMapStruct(&s.KernelOptions)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&s.KernelOptionsPost)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&s.AutoinstallMeta)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&s.FetchableFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&s.BootFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&s.TemplateFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&s.MgmtParameters)
if err != nil {
return nil, err
}
err = sanitizeValueSliceStruct(&s.Owners)
if err != nil {
return nil, err
}
err = sanitizeValueSliceStruct(&s.MgmtClasses)
if err != nil {
return nil, err
}
err = sanitizeValueSliceStruct(&s.BootLoaders)
return s, err
}
func (c *Client) convertRawSystemsList(xmlrpcResult interface{}) ([]*System, error) {
var systems []*System
for _, s := range xmlrpcResult.([]interface{}) {
system, err := c.convertRawSystem("unkown", s)
if err != nil {
return nil, err
}
system.Meta = ItemMeta{
IsFlattened: false,
IsResolved: false,
}
systems = append(systems, system)
}
return systems, nil
}
// GetSystems returns all systems in Cobbler.
func (c *Client) GetSystems() ([]*System, error) {
result, err := c.Call("get_systems", "", c.Token)
if err != nil {
return nil, err
}
return c.convertRawSystemsList(result)
}
// GetSystem returns a single system obtained by its name.
func (c *Client) GetSystem(name string, flattened, resolved bool) (*System, error) {
result, err := c.getConcreteItem("get_system", name, flattened, resolved)
if err != nil {
return nil, err
}
system, err := c.convertRawSystem(name, result)
if err != nil {
return nil, err
}
system.Meta = ItemMeta{
IsFlattened: flattened,
IsResolved: resolved,
}
return system, nil
}
// CreateSystem creates a system.
// It ensures that either a Profile or Image are set and then sets other default values.
func (c *Client) CreateSystem(system System) (*System, error) {
// Check if a system with the same name already exists
if _, err := c.GetSystem(system.Name, false, false); err == nil {
return nil, fmt.Errorf("a system with the name %s already exists", system.Name)
}
if system.Profile == "" && system.Image == "" {
return nil, fmt.Errorf("a system must have a profile or image set")
}
// Set default values. I guess these aren't taken care of by Cobbler?
if len(system.BootFiles.Data) == 0 {
system.BootFiles.IsInherited = true
}
if len(system.BootLoaders.Data) == 0 {
system.BootLoaders.IsInherited = true
}
if len(system.FetchableFiles.Data) == 0 {
system.FetchableFiles.IsInherited = true
}
if len(system.MgmtParameters.Data) == 0 {
system.MgmtParameters.IsInherited = true
}
if system.PowerType == "" {
system.PowerType = "ipmilanplus"
}
if system.Status == "" {
system.Status = "production"
}
if system.VirtDiskDriver == "" {
system.VirtDiskDriver = inherit
}
if system.VirtPath == "" {
system.VirtPath = inherit
}
if system.VirtType == "" {
system.VirtType = inherit
}
// To create a system via the Cobbler API, first call new_system to obtain an ID
result, err := c.Call("new_system", c.Token)
if err != nil {
return nil, err
}
newID := result.(string)
// Set the value of all fields
item := reflect.ValueOf(&system).Elem()
if err := c.updateCobblerFields("system", item, newID); err != nil {
return nil, err
}
// Save the final system
if err := c.SaveSystem(newID, "new"); err != nil {
return nil, err
}
// Return a clean copy of the system
return c.GetSystem(system.Name, false, false)
}
// UpdateSystem updates a single system.
func (c *Client) UpdateSystem(system *System) error {
item := reflect.ValueOf(system).Elem()
id, err := c.GetItemHandle("system", system.Name)
if err != nil {
return err
}
return c.updateCobblerFields("system", item, id)
}
// SaveSystem saves all changes performed via XML-RPC to disk on the server side.
func (c *Client) SaveSystem(objectId, editmode string) error {
_, err := c.Call("save_system", objectId, c.Token, editmode)
return err
}
// CopySystem duplicates a given system on the server with a new name.
func (c *Client) CopySystem(objectId, newName string) error {
_, err := c.Call("copy_system", objectId, newName, c.Token)
return err
}
// DeleteSystem deletes a single System by its name.
func (c *Client) DeleteSystem(name string) error {
return c.DeleteSystemRecursive(name, false)
}
// DeleteSystemRecursive deletes a single System by its name with the option to do so recursively.
func (c *Client) DeleteSystemRecursive(name string, recursive bool) error {
_, err := c.Call("remove_system", name, c.Token, recursive)
return err
}
func makeInterfaceOptionsMap(name string, iface Interface) map[string]interface{} {
i := structs.Map(iface)
nic := make(map[string]interface{})
for key, value := range i {
attrName := fmt.Sprintf("%s-%s", key, name)
log.Printf("[DEBUG] Cobblerclient: setting interface attr %s to %s", attrName, value)
nic[attrName] = value
}
return nic
}
func (c *Client) ModifyInterface(systemID string, nic map[string]interface{}) error {
editUncasted, err := c.Call("modify_system", systemID, "modify_interface", nic, c.Token)
if err != nil {
return err
}
ok, editSuccessful := editUncasted.(bool)
if !ok {
return fmt.Errorf("editing interfaces of system %s failed due to an invalid return value of the server", systemID)
}
if !editSuccessful {
return fmt.Errorf("editing interface of system %s failed", systemID)
}
return nil
}
// CreateInterface creates network interfaces in Cobbler
func (s *System) CreateInterface(name string, iface Interface) error {
nic := makeInterfaceOptionsMap(name, iface)
systemID, err := s.Client.GetItemHandle("system", s.Name)
if err != nil {
return err
}
err = s.Client.ModifyInterface(systemID, nic)
if err != nil {
return err
}
// Save the final system
err = s.Client.SaveSystem(systemID, "bypass")
if err != nil {
return err
}
return nil
}
// ModifyNetworkInterface updates the attributes of an existing interface
func (s *System) ModifyNetworkInterface(name string, iface Interface) error {
return s.CreateInterface(name, iface)
}
// GetInterfaces returns all interfaces in a System.
func (s *System) GetInterfaces() (Interfaces, error) {
nics := make(Interfaces)
for nicName, nicData := range s.Interfaces {
var nic Interface
if err := mapstructure.Decode(nicData, &nic); err != nil {
return nil, err
}
nics[nicName] = nic
}
return nics, nil
}
// GetInterface returns a single interface in a System.
func (s *System) GetInterface(name string) (Interface, error) {
nics := make(Interfaces)
var iface Interface
for nicName, nicData := range s.Interfaces {
var nic Interface
if err := mapstructure.Decode(nicData, &nic); err != nil {
return iface, err
}
nics[nicName] = nic
}
if iface, ok := nics[name]; ok {
return iface, nil
} else {
return iface, fmt.Errorf("interface %s not found", name)
}
}
func (c *Client) DeleteNetworkInterface(systemID, name string) error {
editUncasted, err := c.Call("modify_system", systemID, "delete_interface", name, c.Token)
if err != nil {
return err
}
ok, editSuccessful := editUncasted.(bool)
if !ok {
return fmt.Errorf("deleting interfaces of system %s failed due to an invalid return value of the server", systemID)
}
if !editSuccessful {
return fmt.Errorf("deleting interface of system %s failed", systemID)
}
return nil
}
// DeleteInterface deletes a single interface in a System.
func (s *System) DeleteInterface(name string) error {
if _, err := s.GetInterface(name); err != nil {
return err
}
systemID, err := s.Client.GetItemHandle("system", s.Name)
if err != nil {
return err
}
if err = s.Client.DeleteNetworkInterface(systemID, name); err != nil {
return err
}
// Save the final system
if err = s.Client.SaveSystem(systemID, "bypass"); err != nil {
return err
}
return nil
}
func (c *Client) RenameNetworkInterface(systemName, oldName, newName string) error {
systemID, err := c.GetItemHandle("system", systemName)
if err != nil {
return err
}
args := make(map[string]string)
args["interface"] = oldName
args["rename_interface"] = newName
unparsedOk, err := c.Call("modify_system", systemID, "rename_interface", args, c.Token)
if err != nil {
return err
}
conversionSuccess, ok := unparsedOk.(bool)
if !conversionSuccess {
return fmt.Errorf("failed to convert return value of interface rename")
}
if !ok {
return fmt.Errorf("failed to rename interface %s to %s", oldName, newName)
}
return nil
}
// RenameInterface renames an Interface.
func (s *System) RenameInterface(name string, newName string) error {
err := s.Client.RenameNetworkInterface(s.Name, name, newName)
if err != nil {
return err
}
iface := s.Interfaces[name]
delete(s.Interfaces, name)
s.Interfaces[newName] = iface
return nil
}
// ListSystemNames returns a list of all system names currently available in Cobbler.
func (c *Client) ListSystemNames() ([]string, error) {
return c.GetItemNames("system")
}
// FindSystem searches for one or more systems by any of its attributes.
func (c *Client) FindSystem(criteria map[string]interface{}) ([]*System, error) {
result, err := c.Call("find_system", criteria, true, c.Token)
if err != nil {
return nil, err
}
return c.convertRawSystemsList(result)
}
// FindSystemNames searches for one or more systems by any of its attributes.
func (c *Client) FindSystemNames(criteria map[string]interface{}) ([]string, error) {
resultUnmarshalled, err := c.Call("find_system", criteria, false, c.Token)
return returnStringSlice(resultUnmarshalled, err)
}
// GetSystemsSince returns all systems which were created after the specified date.
func (c *Client) GetSystemsSince(mtime time.Time) ([]*System, error) {
result, err := c.Call("get_systems_since", float64(mtime.Unix()))
if err != nil {
return nil, err
}
return c.convertRawSystemsList(result)
}
// RenameSystem renames a System with a given object id.
func (c *Client) RenameSystem(objectId, newName string) error {
_, err := c.Call("rename_system", objectId, newName, c.Token)
return err
}
// GetSystemHandle gets the internal ID of a Cobbler item.
func (c *Client) GetSystemHandle(name string) (string, error) {
res, err := c.Call("get_system_handle", name, c.Token)
return returnString(res, err)
}