-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cobblerclient.go
610 lines (543 loc) · 18.4 KB
/
cobblerclient.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
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 (
"bytes"
"errors"
"fmt"
"github.com/go-viper/mapstructure/v2"
"github.com/kolo/xmlrpc"
"io"
"net/http"
"reflect"
"sort"
"strings"
)
const bodyTypeXML = "text/xml"
// HTTPClient is the interface which defines the API required for the [Client] to work correctly. Normally this
// is satisfied by a [http.DefaultClient].
type HTTPClient interface {
Post(string, string, io.Reader) (*http.Response, error)
}
// Client is the type which all API methods are attached to.
type Client struct {
httpClient HTTPClient
config ClientConfig
// The longevity of this token is defined server side in the setting "auth_token_duration". Per default no token is
// retrieved. A token can be obtained via the [Client.Login] method.
Token string
// To allow for version dependant API calls in the client we cache the major, minor and patch version.
CachedVersion CobblerVersion
}
// ClientConfig is the URL of Cobbler plus login credentials.
type ClientConfig struct {
URL string
Username string
Password string
}
// NewClient creates a [Client] struct which is ready for usage.
func NewClient(httpClient HTTPClient, c ClientConfig) Client {
return Client{
httpClient: httpClient,
config: c,
CachedVersion: CobblerVersion{},
}
}
// Call is the generic method for calling an XML-RPC endpoint in Cobbler that has no dedicated method in the client.
// Normally there should be no need to use this if you are just using the client. In case there is an error closing the
// HTTP connection, it hides all errors that occur during the rest of the method.
func (c *Client) Call(method string, args ...interface{}) (interface{}, error) {
var result interface{}
reqBody, err := xmlrpc.EncodeMethodCall(method, args...)
if err != nil {
return nil, err
}
r := fmt.Sprintf("%s\n", string(reqBody))
res, err := c.httpClient.Post(c.config.URL, bodyTypeXML, bytes.NewReader([]byte(r)))
if err != nil {
return nil, err
}
defer func() {
if closeErr := res.Body.Close(); closeErr != nil {
err = closeErr
}
}()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
resp := xmlrpc.Response(body)
if err = resp.Unmarshal(&result); err != nil {
return nil, err
}
if err = resp.Err(); err != nil {
return nil, err
}
// Return err because the deferred function may set it to non-nil
return result, err
}
func (c *Client) setCachedVersion() error {
if c.CachedVersion != (CobblerVersion{}) {
return nil
}
extendedVersion, err := c.ExtendedVersion()
if err != nil {
return err
}
if len(extendedVersion.VersionTuple) != 3 {
return errors.New("cobblerclient: invalid length of extended version tuple")
}
c.CachedVersion = CobblerVersion{
Major: extendedVersion.VersionTuple[0],
Minor: extendedVersion.VersionTuple[1],
Patch: extendedVersion.VersionTuple[2],
}
return nil
}
func (c *Client) invalidateCachedVersion() {
c.CachedVersion = CobblerVersion{}
}
// GenerateAutoinstall generates the autoinstallation file for a given profile or system.
func (c *Client) GenerateAutoinstall(profile string, system string) (string, error) {
result, err := c.Call("generate_autoinstall", profile, system)
if err != nil {
return "", err
} else {
return result.(string), err
}
}
// LastModifiedTime retrieves the timestamp when any object in Cobbler was last modified.
func (c *Client) LastModifiedTime() (float64, error) {
result, err := c.Call("last_modified_time")
if err != nil {
return 0.0, err
} else {
return result.(float64), err
}
}
// Ping is a simple method to check if the XML-RPC API is available.
func (c *Client) Ping() (bool, error) {
result, err := c.Call("ping")
if err != nil {
return false, err
} else {
return result.(bool), err
}
}
// AutoAddRepos automatically imports any repos server side that are known to the daemon. It is the responsitbility
// of the caller to execute [Client.BackgroundReposync].
func (c *Client) AutoAddRepos() error {
_, err := c.Call("auto_add_repos", c.Token)
return err
}
// GetAutoinstallTemplates retrieves a list of all templates that are in use by Cobbler.
func (c *Client) GetAutoinstallTemplates() error {
_, err := c.Call("get_autoinstall_templates", c.Token)
return err
}
// GetAutoinstallSnippets retrieves a list of all snippets that are in use by Cobbler.
func (c *Client) GetAutoinstallSnippets() error {
_, err := c.Call("get_autoinstall_snippets", c.Token)
return err
}
// IsAutoinstallInUse checks if a given system has reported that it is currently installing.
func (c *Client) IsAutoinstallInUse(name string) error {
_, err := c.Call("is_autoinstall_in_use", name, c.Token)
return err
}
// GenerateIPxe generates the iPXE (formerly gPXE) configuration data.
func (c *Client) GenerateIPxe(profile, image, system string) error {
_, err := c.Call("generate_ipxe", profile, image, system)
return err
}
// GenerateBootCfg generates the bootcfg for a given MS Windows profile or system.
func (c *Client) GenerateBootCfg(profile, system string) error {
_, err := c.Call("generate_bootcfg", profile, system)
return err
}
// GenerateScript generates for either a profile or sytem the requested script.
func (c *Client) GenerateScript(profile, system, name string) error {
_, err := c.Call("generate_script", profile, system, name)
return err
}
// GetBlendedData passes a profile or system through Cobblers inheritance chain and returns the result.
func (c *Client) GetBlendedData(profile, system string) (map[string]interface{}, error) {
result, err := c.Call("get_blended_data", profile, system)
return result.(map[string]interface{}), err
}
// RegisterNewSystem registers a new system without a Cobbler token. This is normally called
// during unattended installation by a script.
func (c *Client) RegisterNewSystem(info map[string]interface{}) error {
_, err := c.Call("register_new_system", info, c.Token)
return err
}
// RunInstallTriggers runs installation triggers for a given object. This is normally called during
// unattended installation.
func (c *Client) RunInstallTriggers(mode string, objtype string, name string, ip string) error {
_, err := c.Call("run_install_triggers", mode, objtype, name, ip, c.Token)
return err
}
// GetReposCompatibleWithProfile returns all repositories that can be potentially assigned to a given profile.
func (c *Client) GetReposCompatibleWithProfile(profile_name string) error {
_, err := c.Call("get_repos_compatible_with_profile", profile_name, c.Token)
return err
}
// FindSystemByDnsName searches for a system with a given DNS name.
func (c *Client) FindSystemByDnsName(dns_name string) error {
_, err := c.Call("find_system_by_dns_name", dns_name)
return err
}
// GetRandomMac generates a random MAC address for use with a virtualized system.
func (c *Client) GetRandomMac() error {
_, err := c.Call("get_random_mac")
return err
}
type StatusOption string
const StatusNormal StatusOption = "normal"
const StatusText StatusOption = "text"
// GetStatus retrieves the current status of installation progress that has been reported to Cobbler. This can be called
// with two modes: "normal" or "text. In case this is called in mode "normal" you might want to use ParseStatus to
// get a parsed version of the data. For mode "text" you can cast the interface to string.
func (c *Client) GetStatus(mode StatusOption) (interface{}, error) {
return c.Call("get_status", mode, c.Token)
}
// InstallationStatus represents the structured return value of GetStatus.
type InstallationStatus struct {
IP string `json:"ip"`
MostRecentStart float64 `json:"most_recent_start"`
MostRecentStop float64 `json:"most_recent_stop"`
MostRecentTarget string `json:"most_recent_target"`
SeenStart int `json:"seen_start"`
SeenStop int `json:"seen_stop"`
State string `json:"state"`
}
// ParseStatus takes the interface returned by GetStatus and converts it into a list of well-defined structs.
func (c *Client) ParseStatus(status interface{}) ([]InstallationStatus, error) {
result := make([]InstallationStatus, 0)
statusStruct, ok := status.(map[string]interface{})
if !ok {
return result, errors.New("cobblerclient: invalid status structure")
}
for k, v := range statusStruct {
installation := InstallationStatus{}
installation.IP = k
statusArray, okArray := v.([]interface{})
if !okArray {
return result, errors.New("cobblerclient: invalid status structure")
}
mostRecentStart, err := convertToFloat(statusArray[0])
if err != nil {
return result, err
}
mostRecentStop, err := convertToFloat(statusArray[1])
if err != nil {
return result, err
}
mostRecentTarget, okTarget := statusArray[2].(string)
if !okTarget {
return result, errors.New("cobblerclient: invalid status structure")
}
seenStart, err := convertToInt(statusArray[3])
if err != nil {
return result, err
}
seenStop, err := convertToInt(statusArray[4])
if err != nil {
return result, err
}
state, stateOk := statusArray[5].(string)
if !stateOk {
return result, errors.New("cobblerclient: invalid status structure")
}
installation.MostRecentStart = mostRecentStart
installation.MostRecentStop = mostRecentStop
installation.MostRecentTarget = mostRecentTarget
installation.SeenStart = seenStart
installation.SeenStop = seenStop
installation.State = state
result = append(result, installation)
}
return result, nil
}
// SyncDhcp updates the DHCP configuration synchronous.
func (c *Client) SyncDhcp() error {
_, err := c.Call("sync_dhcp", c.Token)
return err
}
// GetConfigData retrieves configuration data for a given host.
func (c *Client) GetConfigData(hostname string) error {
_, err := c.Call("get_config_data", hostname)
return err
}
// IsValueInherit safely verifies if a given value is set to the magic "<<inherit>>".
func (c *Client) IsValueInherit(value interface{}) bool {
if value == nil {
return false
}
stringValue, ok := value.(string)
if !ok {
return false
}
return stringValue == inherit
}
// cobblerDataHacks is a hook for the mapstructure decoder. It's only used by
// decodeCobblerItem and should never be invoked directly.
// It's used to smooth out issues with converting fields and types from Cobbler.
func cobblerDataHacks(fromType, targetType reflect.Kind, data interface{}) (interface{}, error) {
dataVal := reflect.ValueOf(data)
// Cobbler uses ~ internally to mean None/nil
if dataVal.String() == "~" {
switch targetType {
case reflect.String:
return "", nil
case reflect.Slice:
return []string{}, nil
case reflect.Map:
return map[string]interface{}{}, nil
case reflect.Int:
return -1, nil
case reflect.Interface:
return nil, nil
case reflect.Array:
return []string{}, nil
case reflect.Struct:
return Value[interface{}]{RawData: nil}, nil
default:
return nil, errors.New("unknown type was nil")
}
}
if fromType == reflect.Int64 && targetType == reflect.Bool {
// XML-RPC Integer Booleans
return convertXmlRpcBool(dataVal.Interface())
}
if targetType == reflect.Struct {
// This must be a value that may or may not be inherited or flattened (dual-homed types)
switch fromType {
case reflect.String:
valueStruct := Value[interface{}]{}
valueStruct.IsInherited = dataVal.String() == inherit
valueStruct.RawData = data
return valueStruct, nil
case reflect.Slice:
// Slice that may or may not be inherited
valueStruct := Value[[]interface{}]{}
valueStruct.RawData = data
return valueStruct, nil
case reflect.Map:
// This can be: Top-level Map, paged search results, page-info struct, network interface or an inherited struct
mapKeys := dataVal.MapKeys()
sort.SliceStable(mapKeys, func(i, j int) bool {
return mapKeys[i].String() < mapKeys[j].String()
})
if len(mapKeys) == 2 && mapKeys[0].String() == "items" && mapKeys[1].String() == "pageinfo" {
// Paged search results
return data, nil
}
if len(mapKeys) == 10 && mapKeys[0].String() == "end_item" {
// Page-Info struct
return data, nil
}
if len(mapKeys) == 23 && mapKeys[0].String() == "bonding_opts" {
// Network Interface struct
return data, nil
}
for _, key := range mapKeys {
// If the uid key is in the map then it is the top level Map
if key.String() == "uid" {
return data, nil
}
}
valueStruct := Value[map[string]interface{}]{}
valueStruct.Data = make(map[string]interface{})
valueStruct.RawData = data
return valueStruct, nil
case reflect.Int64:
// Int that may or may not be inherited
valueStruct := Value[int]{}
integerValue, err := convertToInt(data)
valueStruct.Data = integerValue
valueStruct.RawData = data
if err != nil {
return Value[int]{}, err
}
return valueStruct, nil
case reflect.Float64:
// Float that may or may not be inherited
valueStruct := Value[float64]{}
floatValue, err := convertToFloat(data)
valueStruct.Data = floatValue
valueStruct.RawData = data
if err != nil {
return Value[float64]{}, err
}
return valueStruct, nil
case reflect.Bool:
// Bool that may or may not be inherited
valueStruct := Value[bool]{}
valueStruct.Data = data.(bool)
valueStruct.RawData = data
return valueStruct, nil
default:
return nil, fmt.Errorf("unknown type %s fromType for Inherited or Flattened Value", fromType)
}
}
return data, nil
}
// decodeCobblerItem is a custom mapstructure decoder to handler Cobbler's uniqueness.
func decodeCobblerItem(raw interface{}, result interface{}) (interface{}, error) {
var metadata mapstructure.Metadata
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Metadata: &metadata,
Result: result,
WeaklyTypedInput: true,
DecodeHook: cobblerDataHacks,
})
if err != nil {
return nil, err
}
if err = decoder.Decode(raw); err != nil {
return nil, err
}
return result, nil
}
// updateCobblerFields updates all fields in a Cobbler Item structure.
func (c *Client) updateCobblerFields(what string, item reflect.Value, id string) error {
method := fmt.Sprintf("modify_%s", what)
typeOfT := item.Type()
// Update embedded Item struct
for i := 0; i < item.NumField(); i++ {
v := item.Field(i)
fieldType := v.Type().Name()
if fieldType == "Item" {
err := c.updateCobblerFields(what, reflect.ValueOf(v.Interface()), id)
if err != nil {
return err
}
break
}
if fieldType == "Resource" {
err := c.updateCobblerFields(what, reflect.ValueOf(v.Interface()), id)
if err != nil {
return err
}
break
}
}
// Fields that can inherit from other items can only be set after the parent is set.
// Fields that inherit from settings can be modified without this constraint.
if method == "modify_profile" {
// In Cobbler v3.3.0, if profile name isn't created first, an empty child gets written to the distro, which
// causes a ValueError: "calling find with no arguments"
nameField := item.FieldByName("Name")
_, err := c.Call(method, id, "name", nameField.String(), c.Token)
if err != nil {
return err
}
parentField := item.FieldByName("Parent")
if parentField != (reflect.Value{}) {
err = c.updateSingleField(method, id, "parent", parentField.String(), "")
if err != nil {
return err
}
}
distroField := item.FieldByName("Distro")
if distroField != (reflect.Value{}) {
err = c.updateSingleField(method, id, "distro", distroField.String(), "")
if err != nil {
return err
}
}
}
if method == "modify_system" {
profileField := item.FieldByName("Profile")
if profileField != (reflect.Value{}) {
err := c.updateSingleField(method, id, "profile", profileField.String(), "")
if err != nil {
return err
}
}
imageField := item.FieldByName("Image")
if imageField != (reflect.Value{}) {
err := c.updateSingleField(method, id, "image", imageField.String(), "")
if err != nil {
return err
}
}
interfaceField := item.FieldByName("Interfaces")
if interfaceField != (reflect.Value{}) {
err := c.updateInterfaces(id, interfaceField.Interface())
if err != nil {
return err
}
}
}
for i := 0; i < item.NumField(); i++ {
v := item.Field(i)
tag := typeOfT.Field(i).Tag
fieldType := v.Type().Name()
field := tag.Get("mapstructure")
cobblerTag := tag.Get("cobbler")
if cobblerTag == "noupdate" || fieldType == "Item" || fieldType == "Resource" || fieldType == "Meta" {
continue
}
if field == "" || field == "parent" || field == "distro" || field == "profile" || field == "image" || field == "interfaces" {
// Skip fields that are empty or have been set previously
continue
}
if method == "modify_profile" && field == "name" {
// Field set above
continue
}
fieldValue := v.Interface()
if strings.HasPrefix(fieldType, "Value") {
if v.FieldByName("IsInherited").Bool() {
fieldValue = inherit
} else {
fieldValue = v.FieldByName("Data").Interface()
}
}
err := c.updateSingleField(method, id, field, fieldValue, cobblerTag)
if err != nil {
return err
}
}
return nil
}
func (c *Client) updateSingleField(method, id, field string, fieldValue interface{}, cobblerTag string) error {
if result, err := c.Call(method, id, field, fieldValue, c.Token); err != nil {
return err
} else {
if !result.(bool) && fieldValue != false {
// It's possible this is a new field that isn't available on older versions.
if cobblerTag == "newfield" {
return nil
}
return fmt.Errorf("error updating field \"%s\" to \"%s\"", field, fieldValue)
}
}
return nil
}
// updateInterfaces takes care of pushing interface modifications. Since interfaces don't have unique identifiers in
// Cobbler 3.3.x. As such no reliable tracking of operations can be done when interfaces are renamed. As such this only
// handles modification and creation of interfaces.
func (c *Client) updateInterfaces(systemId string, interfaceData interface{}) error {
interfaceMap := interfaceData.(Interfaces)
for name, iface := range interfaceMap {
res := makeInterfaceOptionsMap(name, iface)
err := c.ModifyInterface(systemId, res)
if err != nil {
return err
}
}
return nil
}