-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrepo.go
269 lines (231 loc) · 7.55 KB
/
repo.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
/*
Copyright 2017 HomeAway, Inc
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"
"time"
)
// Repo is a created repo.
// Get the fileds from cobbler/items/repo.py
type Repo struct {
Item `mapstructure:",squash"`
// These are internal fields and cannot be modified.
TreeBuildTime string `mapstructure:"tree_build_time" cobbler:"noupdate"`
AptComponents []string `mapstructure:"apt_components"`
AptDists []string `mapstructure:"apt_dists"`
Arch string `mapstructure:"arch"`
Breed string `mapstructure:"breed"`
CreateRepoFlags Value[string] `mapstructure:"createrepo_flags"`
Environment map[string]string `mapstructure:"environment"`
KeepUpdated bool `mapstructure:"keep_updated"`
Mirror string `mapstructure:"mirror"`
MirrorLocally bool `mapstructure:"mirror_locally"`
MirrorType string `mapstructure:"mirror_type"`
Priority int `mapstructure:"priority"`
Proxy Value[string] `mapstructure:"proxy" cobbler:"newfield"`
RsyncOpts map[string]string `mapstructure:"rsyncopts"`
RpmList []string `mapstructure:"rpm_list"`
YumOpts map[string]string `mapstructure:"yumopts"`
}
func NewRepo() Repo {
return Repo{
Item: NewItem(),
AptComponents: make([]string, 0),
AptDists: make([]string, 0),
Arch: none,
Breed: none,
CreateRepoFlags: Value[string]{
IsInherited: true,
},
Environment: make(map[string]string),
MirrorType: "baseurl",
Proxy: Value[string]{
IsInherited: true,
},
RsyncOpts: make(map[string]string),
RpmList: make([]string, 0),
YumOpts: make(map[string]string),
}
}
func convertRawRepo(name string, xmlrpcResult interface{}) (*Repo, error) {
var repo Repo
if xmlrpcResult == "~" {
return nil, fmt.Errorf("profile %s not found", name)
}
decodeResult, err := decodeCobblerItem(xmlrpcResult, &repo)
if err != nil {
return nil, err
}
// Now clean the Value structs
decodedRepo := decodeResult.(*Repo)
err = sanitizeValueMapStruct(&decodedRepo.KernelOptions)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedRepo.KernelOptionsPost)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedRepo.AutoinstallMeta)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedRepo.FetchableFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedRepo.BootFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedRepo.TemplateFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedRepo.MgmtParameters)
if err != nil {
return nil, err
}
err = sanitizeValueSliceStruct(&decodedRepo.Owners)
if err != nil {
return nil, err
}
err = sanitizeValueSliceStruct(&decodedRepo.MgmtClasses)
return decodedRepo, nil
}
func convertRawReposList(xmlrpcResult interface{}) ([]*Repo, error) {
var repos []*Repo
for _, r := range xmlrpcResult.([]interface{}) {
repo, err := convertRawRepo("unknown", r)
if err != nil {
return nil, err
}
repo.Meta = ItemMeta{
IsFlattened: false,
IsResolved: false,
}
repos = append(repos, repo)
}
return repos, nil
}
// GetRepos returns all repos in Cobbler.
func (c *Client) GetRepos() ([]*Repo, error) {
result, err := c.Call("get_repos", "-1", c.Token)
if err != nil {
return nil, err
}
return convertRawReposList(result)
}
// GetRepo returns a single repo obtained by its name.
func (c *Client) GetRepo(name string, flattened, resolved bool) (*Repo, error) {
result, err := c.getConcreteItem("get_repo", name, flattened, resolved)
if err != nil {
return nil, err
}
repo, err := convertRawRepo(name, result)
if err != nil {
return nil, err
}
repo.Meta = ItemMeta{
IsFlattened: flattened,
IsResolved: resolved,
}
return repo, nil
}
// CreateRepo creates a repo.
func (c *Client) CreateRepo(repo Repo) (*Repo, error) {
// Make sure a repo with the same name does not already exist
if _, err := c.GetRepo(repo.Name, false, false); err == nil {
return nil, fmt.Errorf("a Repo with the name %s already exists", repo.Name)
}
result, err := c.Call("new_repo", c.Token)
if err != nil {
return nil, err
}
newID := result.(string)
item := reflect.ValueOf(&repo).Elem()
if err := c.updateCobblerFields("repo", item, newID); err != nil {
return nil, err
}
if err := c.SaveRepo(newID, "new"); err != nil {
return nil, err
}
return c.GetRepo(repo.Name, false, false)
}
// UpdateRepo updates a single repo.
func (c *Client) UpdateRepo(repo *Repo) error {
item := reflect.ValueOf(repo).Elem()
id, err := c.GetItemHandle("repo", repo.Name)
if err != nil {
return err
}
if err := c.updateCobblerFields("repo", item, id); err != nil {
return err
}
return c.SaveRepo(id, "bypass")
}
// SaveRepo saves all changes performed via XML-RPC to disk on the server side.
func (c *Client) SaveRepo(objectId, editmode string) error {
_, err := c.Call("save_repo", objectId, c.Token, editmode)
return err
}
// CopyRepo duplicates a given repository on the server with a new name.
func (c *Client) CopyRepo(objectId, newName string) error {
_, err := c.Call("copy_repo", objectId, newName, c.Token)
return err
}
// DeleteRepo deletes a single Repo by its name.
func (c *Client) DeleteRepo(name string) error {
return c.DeleteRepoRecursive(name, false)
}
// DeleteRepoRecursive deletes a single Repo by its name with the option to do so recursively.
func (c *Client) DeleteRepoRecursive(name string, recursive bool) error {
_, err := c.Call("remove_repo", name, c.Token, recursive)
return err
}
// ListRepoNames returns the names of all repositories that exist in Cobbler.
func (c *Client) ListRepoNames() ([]string, error) {
return c.GetItemNames("repo")
}
// FindRepo searches for one or more repositories by any of its attributes.
func (c *Client) FindRepo(criteria map[string]interface{}) ([]*Repo, error) {
result, err := c.Call("find_repo", criteria, true, c.Token)
if err != nil {
return nil, err
}
return convertRawReposList(result)
}
// FindRepoNames searches for one or more repositories by any of its attributes.
func (c *Client) FindRepoNames(criteria map[string]interface{}) ([]string, error) {
resultUnmarshalled, err := c.Call("find_repo", criteria, false, c.Token)
return returnStringSlice(resultUnmarshalled, err)
}
// GetReposSince returns all repositories which were created after the specified date.
func (c *Client) GetReposSince(mtime time.Time) ([]*Repo, error) {
result, err := c.Call("get_repos_since", float64(mtime.Unix()))
if err != nil {
return nil, err
}
return convertRawReposList(result)
}
// RenameRepo renames a repository with a given object id.
func (c *Client) RenameRepo(objectId, newName string) error {
_, err := c.Call("rename_repo", objectId, newName, c.Token)
return err
}
// GetRepoHandle gets the internal ID of a Cobbler item.
func (c *Client) GetRepoHandle(name string) (string, error) {
res, err := c.Call("get_repo_handle", name, c.Token)
return returnString(res, err)
}