-
Notifications
You must be signed in to change notification settings - Fork 64
/
cookbook.go
407 lines (372 loc) · 13.1 KB
/
cookbook.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
package chef
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
const metaRbName = "metadata.rb"
const metaJsonName = "metadata.json"
type metaFunc func(s []string, m *CookbookMeta) error
var metaRegistry map[string]metaFunc
// CookbookService is the service for interacting with chef server cookbooks endpoint
type CookbookService struct {
client *Client
}
// CookbookItem represents a object of cookbook file data
type CookbookItem struct {
Url string `json:"url,omitempty"`
Path string `json:"path,omitempty"`
Name string `json:"name,omitempty"`
Checksum string `json:"checksum,omitempty"`
Specificity string `json:"specificity,omitempty"`
}
// CookbookListResult is the summary info returned by chef-api when listing
// http://docs.opscode.com/api_chef_server.html#cookbooks
type CookbookListResult map[string]CookbookVersions
// CookbookRecipesResult is the summary info returned by chef-api when listing
// http://docs.opscode.com/api_chef_server.html#cookbooks-recipes
type CookbookRecipesResult []string
// CookbookVersions is the data container returned from the chef server when listing all cookbooks
type CookbookVersions struct {
Url string `json:"url,omitempty"`
Versions []CookbookVersion `json:"versions,omitempty"`
}
// CookbookVersion is the data for a specific cookbook version
type CookbookVersion struct {
Url string `json:"url,omitempty"`
Version string `json:"version,omitempty"`
}
// CookbookMeta represents a Golang version of cookbook metadata
type CookbookMeta struct {
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
Description string `json:"description,omitempty"`
LongDescription string `json:"long_description,omitempty"`
Maintainer string `json:"maintainer,omitempty"`
MaintainerEmail string `json:"maintainer_email,omitempty"`
License string `json:"license,omitempty"`
Platforms map[string]interface{} `json:"platforms,omitempty"`
Depends map[string]string `json:"dependencies,omitempty"`
Reccomends map[string]string `json:"recommendations,omitempty"`
Suggests map[string]string `json:"suggestions,omitempty"`
Conflicts map[string]string `json:"conflicting,omitempty"`
Provides map[string]interface{} `json:"providing,omitempty"`
Replaces map[string]string `json:"replacing,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"` // this has a format as well that could be typed, but blargh https://github.com/lob/chef/blob/master/cookbooks/apache2/metadata.json
Groupings map[string]interface{} `json:"groupings,omitempty"` // never actually seen this used.. looks like it should be map[string]map[string]string, but not sure http://docs.opscode.com/essentials_cookbook_metadata.html
Recipes map[string]string `json:"recipes,omitempty"`
SourceUrl string `json:"source_url"`
IssueUrl string `json:"issues_url"`
ChefVersion string
OhaiVersion string
Gems [][]string `json:"gems"`
EagerLoadLibraries bool `json:"eager_load_libraries"`
Privacy bool `json:"privacy"`
}
// CookbookAccess represents the permissions on a Cookbook
type CookbookAccess struct {
Read bool `json:"read,omitempty"`
Create bool `json:"create,omitempty"`
Grant bool `json:"grant,omitempty"`
Update bool `json:"update,omitempty"`
Delete bool `json:"delete,omitempty"`
}
// Cookbook represents the native Go version of the deserialized api cookbook
type Cookbook struct {
CookbookName string `json:"cookbook_name"`
Name string `json:"name"`
Version string `json:"version,omitempty"`
ChefType string `json:"chef_type,omitempty"`
Frozen bool `json:"frozen?,omitempty"`
JsonClass string `json:"json_class,omitempty"`
Files []CookbookItem `json:"files,omitempty"`
Templates []CookbookItem `json:"templates,omitempty"`
Attributes []CookbookItem `json:"attributes,omitempty"`
Recipes []CookbookItem `json:"recipes,omitempty"`
Definitions []CookbookItem `json:"definitions,omitempty"`
Libraries []CookbookItem `json:"libraries,omitempty"`
Providers []CookbookItem `json:"providers,omitempty"`
Resources []CookbookItem `json:"resources,omitempty"`
RootFiles []CookbookItem `json:"root_files,omitempty"`
Metadata CookbookMeta `json:"metadata,omitempty"`
Access CookbookAccess `json:"access,omitempty"`
}
// String makes CookbookListResult implement the string result
func (c CookbookListResult) String() (out string) {
for k, v := range c {
out += fmt.Sprintf("%s => %s\n", k, v.Url)
for _, i := range v.Versions {
out += fmt.Sprintf(" * %s\n", i.Version)
}
}
return out
}
// versionParams assembles a querystring for the chef api's num_versions
// This is used to restrict the number of versions returned in the reponse
func versionParams(path, numVersions string) string {
if numVersions == "0" {
numVersions = "all"
}
// need to optionally add numVersion args to the request
if len(numVersions) > 0 {
path = fmt.Sprintf("%s?num_versions=%s", path, numVersions)
}
return path
}
// Get retruns a CookbookVersion for a specific cookbook
//
// GET /cookbooks/name
func (c *CookbookService) Get(name string) (data CookbookVersion, err error) {
path := fmt.Sprintf("cookbooks/%s", name)
err = c.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// GetAvailable returns the versions of a coookbook available on a server
func (c *CookbookService) GetAvailableVersions(name, numVersions string) (data CookbookListResult, err error) {
path := versionParams(fmt.Sprintf("cookbooks/%s", name), numVersions)
err = c.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// GetVersion fetches a specific version of a cookbooks data from the server api
//
// GET /cookbook/foo/1.2.3
// GET /cookbook/foo/_latest
// Chef API docs: https://docs.chef.io/api_chef_server.html#cookbooks-name-version
func (c *CookbookService) GetVersion(name, version string) (data Cookbook, err error) {
url := fmt.Sprintf("cookbooks/%s/%s", name, version)
err = c.client.magicRequestDecoder("GET", url, nil, &data)
return
}
// ListVersions lists the cookbooks available on the server limited to numVersions
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#cookbooks-name
func (c *CookbookService) ListAvailableVersions(numVersions string) (data CookbookListResult, err error) {
path := versionParams("cookbooks", numVersions)
err = c.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// ListAllRecipes lists the names of all recipes in the most recent cookbook versions
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#cookbooks-recipes
func (c *CookbookService) ListAllRecipes() (data CookbookRecipesResult, err error) {
path := "cookbooks/_recipes"
err = c.client.magicRequestDecoder("GET", path, nil, &data)
return
}
// List returns a CookbookListResult with the latest versions of cookbooks available on the server
func (c *CookbookService) List() (CookbookListResult, error) {
return c.ListAvailableVersions("")
}
// DeleteVersion removes a version of a cook from a server
func (c *CookbookService) Delete(name, version string) (err error) {
path := fmt.Sprintf("cookbooks/%s/%s", name, version)
err = c.client.magicRequestDecoder("DELETE", path, nil, nil)
return
}
func ReadMetaData(path string) (m CookbookMeta, err error) {
fileName := filepath.Join(path, metaJsonName)
jsonType := true
if !isFileExists(fileName) {
jsonType = false
fileName = filepath.Join(path, metaRbName)
}
file, err := os.ReadFile(fileName)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if jsonType {
return NewMetaDataFromJson(file)
} else {
return NewMetaData(string(file))
}
}
func trimQuotes(s string) string {
if len(s) >= 2 {
if c := s[len(s)-1]; s[0] == c && (c == '"' || c == '\'') {
return s[1 : len(s)-1]
}
}
return s
}
func getKeyValue(str string) (string, []string) {
c := strings.Split(str, " ")
if len(c) == 0 {
return "", nil
}
return strings.TrimSpace(c[0]), c[1:]
}
func isFileExists(name string) bool {
if _, err := os.Stat(name); errors.Is(err, os.ErrNotExist) {
return false
}
return true
}
func clearWhiteSpace(s []string) (result []string) {
for _, i := range s {
if len(i) > 0 {
result = append(result, i)
}
}
return result
}
func NewMetaData(data string) (m CookbookMeta, err error) {
linesData := strings.Split(data, "\n")
if len(linesData) < 3 {
return m, errors.New("not much info")
}
m.Depends = make(map[string]string, 1)
m.Platforms = make(map[string]interface{}, 1)
for _, i := range linesData {
key, value := getKeyValue(strings.TrimSpace(i))
if fn, ok := metaRegistry[key]; ok {
err = fn(value, &m)
if err != nil {
return
}
}
}
return m, err
}
func NewMetaDataFromJson(data []byte) (m CookbookMeta, err error) {
err = json.Unmarshal(data, &m)
return m, err
}
func StringParserForMeta(s []string) string {
str := strings.Join(s, " ")
return trimQuotes(strings.TrimSpace(str))
}
func metaNameParser(s []string, m *CookbookMeta) error {
m.Name = StringParserForMeta(s)
return nil
}
func metaMaintainerParser(s []string, m *CookbookMeta) error {
m.Maintainer = StringParserForMeta(s)
return nil
}
func metaMaintainerMailParser(s []string, m *CookbookMeta) error {
m.MaintainerEmail = StringParserForMeta(s)
return nil
}
func metaLicenseParser(s []string, m *CookbookMeta) error {
m.License = StringParserForMeta(s)
return nil
}
func metaDescriptionParser(s []string, m *CookbookMeta) error {
m.Description = StringParserForMeta(s)
return nil
}
func metaLongDescriptionParser(s []string, m *CookbookMeta) error {
m.LongDescription = StringParserForMeta(s)
return nil
}
func metaIssueUrlParser(s []string, m *CookbookMeta) error {
m.IssueUrl = StringParserForMeta(s)
return nil
}
func metaSourceUrlParser(s []string, m *CookbookMeta) error {
m.SourceUrl = StringParserForMeta(s)
return nil
}
func metaGemParser(s []string, m *CookbookMeta) error {
m.Gems = append(m.Gems, s)
return nil
}
func metaVersionParser(s []string, m *CookbookMeta) error {
m.Version = StringParserForMeta(s)
return nil
}
func metaOhaiVersionParser(s []string, m *CookbookMeta) error {
m.OhaiVersion = StringParserForMeta(s)
return nil
}
func metaChefVersionParser(s []string, m *CookbookMeta) error {
m.ChefVersion = StringParserForMeta(s)
return nil
}
func metaPrivacyParser(s []string, m *CookbookMeta) error {
if s[0] == "true" {
m.Privacy = true
}
return nil
}
func metaSupportsParser(s []string, m *CookbookMeta) error {
s = clearWhiteSpace(s)
switch len(s) {
case 1:
if s[0] != "os" {
m.Platforms[strings.TrimSpace(s[0])] = ">= 0.0.0"
}
case 2:
m.Platforms[strings.TrimSpace(s[0])] = s[1]
case 3:
v := trimQuotes(s[1] + " " + s[2])
m.Platforms[strings.TrimSpace(s[0])] = v
}
if len(s) > 3 {
return errors.New(`<<~OBSOLETED
The dependency specification syntax you are using is no longer valid. You may not
specify more than one version constraint for a particular cookbook.
Consult https://docs.chef.io/config_rb_metadata/ for the updated syntax.`)
}
return nil
}
func metaDependsParser(s []string, m *CookbookMeta) error {
s = clearWhiteSpace(s)
switch len(s) {
case 1:
m.Depends[strings.TrimSpace(s[0])] = ">= 0.0.0"
case 2:
m.Depends[strings.TrimSpace(s[0])] = s[1]
case 3:
v := trimQuotes(s[1] + " " + s[2])
m.Depends[strings.TrimSpace(s[0])] = v
}
if len(s) > 3 {
return errors.New(`<<~OBSOLETED
The dependency specification syntax you are using is no longer valid. You may not
specify more than one version constraint for a particular cookbook.
Consult https://docs.chef.io/config_rb_metadata/ for the updated syntax.`)
}
return nil
}
func metaSupportsRubyParser(s []string, m *CookbookMeta) error {
if len(s) > 1 {
for _, i := range s {
switch i {
case ").each":
continue
case "do":
continue
case "|os|":
continue
default:
m.Platforms[strings.TrimSpace(s[0])] = ">= 0.0.0"
}
}
}
return nil
}
func init() {
metaRegistry = make(map[string]metaFunc, 15)
metaRegistry["name"] = metaNameParser
metaRegistry["maintainer"] = metaMaintainerParser
metaRegistry["maintainer_email"] = metaMaintainerMailParser
metaRegistry["license"] = metaLicenseParser
metaRegistry["description"] = metaDescriptionParser
metaRegistry["long_description"] = metaLongDescriptionParser
metaRegistry["source_url"] = metaSourceUrlParser
metaRegistry["issues_url"] = metaIssueUrlParser
metaRegistry["platforms"] = metaSupportsParser
metaRegistry["supports"] = metaSupportsParser
metaRegistry["%w("] = metaSupportsRubyParser
metaRegistry["privacy"] = metaPrivacyParser
metaRegistry["depends"] = metaDependsParser
metaRegistry["version"] = metaVersionParser
metaRegistry["chef_version"] = metaChefVersionParser
metaRegistry["ohai_version"] = metaOhaiVersionParser
metaRegistry["gem"] = metaGemParser
}