-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
cobblerclient.go
400 lines (344 loc) · 11.6 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
/*
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"
"io/ioutil"
"net/http"
"reflect"
)
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
}
// ClientConfig is the URL of Cobbler plus login credentials.
type ClientConfig struct {
URL string
Username string
Password string
}
type ExtendedVersion struct {
Gitdate string
Gitstamp string
Builddate string
Version string
VersionTuple []int
}
// NewClient creates a [Client] struct which is ready for usage.
func NewClient(httpClient HTTPClient, c ClientConfig) Client {
return Client{
httpClient: httpClient,
config: c,
}
}
// 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.
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 res.Body.Close()
body, err := ioutil.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 result, nil
}
// 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
}
// Version is a shorter and easier version representation. Normally you want to call [Client.ExtendedVersion].
func (c *Client) Version() (float64, error) {
res, err := c.Call("version")
return res.(float64), err
}
// ExtendedVersion returns the version information of the server.
func (c *Client) ExtendedVersion() (ExtendedVersion, error) {
extendedVersion := ExtendedVersion{}
data, err := c.Call("extended_version")
if err != nil {
return extendedVersion, err
}
switch data.(type) {
case map[string]interface{}:
data := data.(map[string]interface{})
var versionTuple, err = returnIntSlice(data["version_tuple"], err)
if err != nil {
return extendedVersion, err
}
extendedVersion.Version = data["version"].(string)
extendedVersion.VersionTuple = versionTuple
extendedVersion.Builddate = data["builddate"].(string)
extendedVersion.Gitdate = data["gitdate"].(string)
extendedVersion.Gitstamp = data["gitstamp"].(string)
default:
return extendedVersion, err
}
return extendedVersion, 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
}
// XmlRpcHacks is an internal endpoint that doesn't make sense to be called externally.
func (c *Client) XmlRpcHacks(data interface{}) error {
// FIXME: Make private server-side and remove from here.
_, err := c.Call("xmlrpc_hacks", data)
return err
}
// GetStatus retrieves the current status of installation progress that has been reported to Cobbler.
func (c *Client) GetStatus(mode string) error {
_, err := c.Call("get_status", mode, c.Token)
return err
}
// 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(f, 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
default:
return nil, errors.New("unknown type was nil")
}
}
if f == reflect.Int64 && targetType == reflect.Bool {
if dataVal.Int() > 0 {
return true, nil
} else {
return false, nil
}
}
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()
// 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" TO-DO: figure a more efficient way of targeting name.
for i := 0; i < item.NumField(); i++ {
v := item.Field(i)
fieldType := v.Type().Name()
tag := typeOfT.Field(i).Tag
field := tag.Get("mapstructure")
if fieldType == "Item" {
// Update embedded Item struct if present (should be present once on all items)
err := c.updateCobblerFields(what, reflect.ValueOf(v.Interface()), id)
if err != nil {
return err
}
continue
}
if method == "modify_profile" && field == "name" {
_, err := c.Call(method, id, field, v.Interface(), c.Token)
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" {
continue
}
if field == "" {
continue
}
if result, err := c.Call(method, id, field, v.Interface(), c.Token); err != nil {
return err
} else {
if result.(bool) == false && v.Interface() != false {
// It's possible this is a new field that isn't available on
// older versions.
if cobblerTag == "newfield" {
continue
}
return fmt.Errorf("error updating %s to %s", field, v.Interface())
}
}
}
return nil
}