forked from ContainerSolutions/cobblerclient
-
Notifications
You must be signed in to change notification settings - Fork 5
/
system.go
336 lines (281 loc) · 11.6 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
/*
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"
"reflect"
"github.com/fatih/structs"
"github.com/mitchellh/mapstructure"
)
// System is a created system.
type System struct {
// These are internal fields and cannot be modified.
Ctime float64 `mapstructure:"ctime" cobbler:"noupdate"` // TODO: convert to time
Depth int `mapstructure:"depth" cobbler:"noupdate"`
ID string `mapstructure:"uid" cobbler:"noupdate"`
IPv6Autoconfiguration bool `mapstructure:"ipv6_autoconfiguration" cobbler:"noupdate"`
Mtime float64 `mapstructure:"mtime" cobbler:"noupdate"` // TODO: convert to time
ReposEnabled bool `mapstructure:"repos_enabled" cobbler:"noupdate"`
BootFiles string `mapstructure:"boot_files"`
Comment string `mapstructure:"comment"`
EnableGPXE bool `mapstructure:"enable_gpxe"`
FetchableFiles string `mapstructure:"fetchable_files"`
Gateway string `mapstructure:"gateway"`
Hostname string `mapstructure:"hostname"`
Image string `mapstructure:"image"`
Interfaces map[string]interface{} `mapstructure:"interfaces" cobbler:"noupdate"`
IPv6DefaultDevice string `mapstructure:"ipv6_default_device"`
KernelOptions string `mapstructure:"kernel_options"`
KernelOptionsPost string `mapstructure:"kernel_options_post"`
Kickstart string `mapstructure:"kickstart"`
KSMeta string `mapstructure:"ks_meta"`
LDAPEnabled bool `mapstructure:"ldap_enabled"`
LDAPType string `mapstructure:"ldap_type"`
MGMTClasses []string `mapstructure:"mgmt_classes"`
MGMTParameters string `mapstructure:"mgmt_parameters"`
MonitEnabled bool `mapstructure:"monit_enabled"`
Name string `mapstructure:"name"`
NameServersSearch []string `mapstructure:"name_servers_search"`
NameServers []string `mapstructure:"name_servers"`
NetbootEnabled bool `mapstructure:"netboot_enabled"`
Owners []string `mapstructure:"owners"`
PowerAddress string `mapstructure:"power_address"`
PowerID string `mapstructure:"power_id"`
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"`
RedHatManagementServer string `mapstructure:"redhat_management_server"`
Status string `mapstructure:"status"`
TemplateFiles string `mapstructure:"template_files"`
TemplateRemoteKickstarts int `mapstructure:"template_remote_kickstarts"`
VirtAutoBoot string `mapstructure:"virt_auto_boot"`
VirtCPUs string `mapstructure:"virt_cpus"`
VirtDiskDriver string `mapstructure:"virt_disk_driver"`
VirtFileSize string `mapstructure:"virt_file_size"`
VirtPath string `mapstructure:"virt_path"`
VirtPXEBoot int `mapstructure:"virt_pxe_boot"`
VirtRam string `mapstructure:"virt_ram"`
VirtType string `mapstructure:"virt_type"`
Client
}
// Interface is an interface in a system.
type Interface struct {
CNAMEs []string `mapstructure:"cnames" structs:"cnames"`
DHCPTag string `mapstructure:"dhcp_tag" structs:"dhcp_tag"`
DNSName string `mapstructure:"dns_name" structs:"dns_name"`
BondingOpts string `mapstructure:"bonding_opts" structs:"bonding_opts"`
BridgeOpts string `mapstructure:"bridge_opts" structs:"bridge_opts"`
Gateway string `mapstructure:"if_gateway" structs:"if_gateway"`
InterfaceType string `mapstructure:"interface_type" structs:"interface_type"`
InterfaceMaster string `mapstructure:"interface_master" structs:"interface_master"`
IPAddress string `mapstructure:"ip_address" structs:"ip_address"`
IPv6Address string `mapstructure:"ipv6_address" structs:"ipv6_address"`
IPv6Secondaries []string `mapstructure:"ipv6_secondaries" structs:"ipv6_secondaries"`
IPv6MTU string `mapstructure:"ipv6_mtu" structs:"ipv6_mtu"`
IPv6StaticRoutes []string `mapstructure:"ipv6_static_routes" structs:"ipv6_static_routes"`
IPv6DefaultGateway string `mapstructure:"ipv6_default_gateway structs:"ipv6_default_gateway"`
MACAddress string `mapstructure:"mac_address" structs:"mac_address"`
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
// GetSystems returns all systems in Cobbler.
func (c *Client) GetSystems() ([]*System, error) {
var systems []*System
result, err := c.Call("get_systems", "", c.Token)
if err != nil {
return nil, err
}
for _, s := range result.([]interface{}) {
var system System
decodedResult, err := decodeCobblerItem(s, &system)
if err != nil {
return nil, err
}
decodedSystem := decodedResult.(*System)
decodedSystem.Client = *c
systems = append(systems, decodedSystem)
}
return systems, nil
}
// GetSystem returns a single system obtained by its name.
func (c *Client) GetSystem(name string) (*System, error) {
var system System
result, err := c.Call("get_system", name, c.Token)
if err != nil {
return &system, err
}
if result == "~" {
return nil, fmt.Errorf("System %s not found.", name)
}
decodeResult, err := decodeCobblerItem(result, &system)
if err != nil {
return nil, err
}
s := decodeResult.(*System)
s.Client = *c
return s, 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); 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 system.BootFiles == "" {
system.BootFiles = "<<inherit>>"
}
if system.FetchableFiles == "" {
system.FetchableFiles = "<<inherit>>"
}
if system.MGMTParameters == "" {
system.MGMTParameters = "<<inherit>>"
}
if system.PowerType == "" {
system.PowerType = "ipmilan"
}
if system.Status == "" {
system.Status = "production"
}
if system.VirtAutoBoot == "" {
system.VirtAutoBoot = "0"
}
if system.VirtCPUs == "" {
system.VirtCPUs = "<<inherit>>"
}
if system.VirtDiskDriver == "" {
system.VirtDiskDriver = "<<inherit>>"
}
if system.VirtFileSize == "" {
system.VirtFileSize = "<<inherit>>"
}
if system.VirtPath == "" {
system.VirtPath = "<<inherit>>"
}
if system.VirtRam == "" {
system.VirtRam = "<<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.Call("save_system", newId, c.Token); err != nil {
return nil, err
}
// Return a clean copy of the system
return c.GetSystem(system.Name)
}
// 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)
}
// DeleteSystem deletes a single system by its name.
func (c *Client) DeleteSystem(name string) error {
_, err := c.Call("remove_system", name, c.Token)
return err
}
func (s *System) CreateInterface(name string, iface Interface) error {
i := structs.Map(iface)
nic := make(map[string]interface{})
for key, value := range i {
attrName := fmt.Sprintf("%s-%s", key, name)
nic[attrName] = value
}
systemId, err := s.Client.GetItemHandle("system", s.Name)
if err != nil {
return err
}
if _, err := s.Client.Call("modify_system", systemId, "modify_interface", nic, s.Client.Token); err != nil {
return err
}
// Save the final system
if _, err := s.Client.Call("save_system", systemId, s.Client.Token); err != nil {
return err
}
return nil
}
// 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)
}
}
// 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.Call("modify_system", systemId, "delete_interface", name, s.Client.Token); err != nil {
return err
}
// Save the final system
if _, err := s.Client.Call("save_system", systemId, s.Client.Token); err != nil {
return err
}
return nil
}