-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
linuxpackage.go
238 lines (201 loc) · 6.53 KB
/
linuxpackage.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
package cobblerclient
import (
"fmt"
"reflect"
"time"
)
// Package is a created package.
// Get the fields from cobbler/items/package.py
type Package struct {
Resource `mapstructure:",squash"`
// Package specific attributes
Installer string `mapstructure:"installer"`
Version string `mapstructure:"version"`
}
func NewPackage() Package {
return Package{
Resource: NewResource(),
}
}
func convertRawLinuxPackage(name string, xmlrpcResult interface{}) (*Package, error) {
var linuxpackage Package
if xmlrpcResult == "~" {
return nil, fmt.Errorf("package %s not found", name)
}
decodeResult, err := decodeCobblerItem(xmlrpcResult, &linuxpackage)
if err != nil {
return nil, err
}
// Now clean the Value structs
decodedPackage := decodeResult.(*Package)
err = sanitizeValueMapStruct(&decodedPackage.KernelOptions)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedPackage.KernelOptionsPost)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedPackage.AutoinstallMeta)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedPackage.FetchableFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedPackage.BootFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedPackage.TemplateFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedPackage.MgmtParameters)
if err != nil {
return nil, err
}
err = sanitizeValueSliceStruct(&decodedPackage.Owners)
if err != nil {
return nil, err
}
err = sanitizeValueSliceStruct(&decodedPackage.MgmtClasses)
return decodedPackage, nil
}
func convertRawLinuxPackageList(xmlrpcResult interface{}) ([]*Package, error) {
var linuxpackages []*Package
for _, d := range xmlrpcResult.([]interface{}) {
linuxpackage, err := convertRawLinuxPackage("unknown", d)
if err != nil {
return nil, err
}
linuxpackage.Meta = ItemMeta{
IsFlattened: false,
IsResolved: false,
}
linuxpackages = append(linuxpackages, linuxpackage)
}
return linuxpackages, nil
}
// GetPackages returns all packages in Cobbler.
func (c *Client) GetPackages() ([]*Package, error) {
result, err := c.Call("get_packages", "-1", c.Token)
if err != nil {
return nil, err
}
return convertRawLinuxPackageList(result)
}
// GetPackage returns a single package obtained by its name.
func (c *Client) GetPackage(name string, flattened, resolved bool) (*Package, error) {
result, err := c.getConcreteItem("get_package", name, flattened, resolved)
if err != nil {
return nil, err
}
linuxpackage, err := convertRawLinuxPackage(name, result)
if err != nil {
return nil, err
}
linuxpackage.Meta = ItemMeta{
IsFlattened: flattened,
IsResolved: resolved,
}
return linuxpackage, nil
}
// CreatePackage creates a package.
func (c *Client) CreatePackage(linuxpackage Package) (*Package, error) {
// Make sure a package with the same name does not already exist
if _, err := c.GetPackage(linuxpackage.Name, false, false); err == nil {
return nil, fmt.Errorf("a Package with the name %s already exists", linuxpackage.Name)
}
result, err := c.Call("new_package", c.Token)
if err != nil {
return nil, err
}
newID := result.(string)
item := reflect.ValueOf(&linuxpackage).Elem()
if err := c.updateCobblerFields("package", item, newID); err != nil {
return nil, err
}
if err = c.SavePackage(newID, "new"); err != nil {
return nil, err
}
return c.GetPackage(linuxpackage.Name, false, false)
}
// UpdatePackage updates a single package.
func (c *Client) UpdatePackage(linuxpackage *Package) error {
item := reflect.ValueOf(linuxpackage).Elem()
id, err := c.GetItemHandle("package", linuxpackage.Name)
if err != nil {
return err
}
if err := c.updateCobblerFields("package", item, id); err != nil {
return err
}
if err = c.SavePackage(id, "bypass"); err != nil {
return err
}
return nil
}
// ListPackageNames returns a list of all packages names currently available in Cobbler.
func (c *Client) ListPackageNames() ([]string, error) {
return c.GetItemNames("package")
}
// FindPackage is the search method that allows looking for a package by any of its attributes.
func (c *Client) FindPackage(criteria map[string]interface{}) ([]*Package, error) {
result, err := c.Call("find_package", criteria, true, c.Token)
if err != nil {
return nil, err
}
return convertRawLinuxPackageList(result)
}
// FindPackageNames is searching for one or more packages by any of its attributes.
func (c *Client) FindPackageNames(criteria map[string]interface{}) ([]string, error) {
resultUnmarshalled, err := c.Call("find_package", criteria, false, c.Token)
return returnStringSlice(resultUnmarshalled, err)
}
// GetPackageHandle gets the internal ID of a Cobbler item.
func (c *Client) GetPackageHandle(name string) (string, error) {
res, err := c.Call("get_package_handle", name, c.Token)
return returnString(res, err)
}
// CopyPackage duplicates a given package on the server with a new name.
func (c *Client) CopyPackage(objectId, newName string) error {
_, err := c.Call("copy_package", objectId, newName, c.Token)
return err
}
// DeletePackage deletes a single Package by its name.
func (c *Client) DeletePackage(name string) error {
return c.DeletePackageRecursive(name, false)
}
// DeletePackageRecursive deletes a single Package by its name with the option to do so recursively.
func (c *Client) DeletePackageRecursive(name string, recursive bool) error {
_, err := c.Call("remove_package", name, c.Token, recursive)
return err
}
// RenamePackage renames a package with a given object id.
func (c *Client) RenamePackage(objectId, newName string) error {
_, err := c.Call("rename_package", objectId, newName, c.Token)
return err
}
// GetPackagesSince returns all packages that have been edited since a given timestamp.
func (c *Client) GetPackagesSince(mtime time.Time) ([]*Package, error) {
result, err := c.Call("get_packages_since", float64(mtime.Unix()))
if err != nil {
return nil, err
}
return convertRawLinuxPackageList(result)
}
// GetPackageAsRendered returns the datastructure after it has passed through Cobblers inheritance structure.
func (c *Client) GetPackageAsRendered(name string) (map[string]interface{}, error) {
result, err := c.Call("get_package_as_rendered", name, c.Token)
if err != nil {
return nil, err
}
return result.(map[string]interface{}), err
}
// SavePackage saves all changes performed via XML-RPC to disk on the server side.
func (c *Client) SavePackage(objectId, editmode string) error {
_, err := c.Call("save_package", objectId, c.Token, editmode)
return err
}