forked from go-chef/chef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookbook.go
407 lines (350 loc) · 13.8 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
package chef
import "fmt"
import "os"
import "path/filepath"
import "crypto/md5"
import "encoding/json"
import "encoding/base64"
import "encoding/hex"
import "io"
const filechunk = 8192 // we settle for 8KB
// 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"`
Path string `json:"path"`
Name string `json:"name"`
Checksum string `json:"checksum"`
Specificity string `json:"specificity"`
}
// CookbookItem represents a object of cookbook file data
type CookbookItemPut struct {
Path string `json:"path"`
Name string `json:"name"`
Checksum string `json:"checksum"`
Specificity string `json:"specificity"`
}
// 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
// CookbookVersions is the data container returned from the chef server when listing all cookbooks
type CookbookVersions struct {
Url string `json:"url"`
Versions []CookbookVersion `json:"versions"`
}
// CookbookVersion is the data for a specific cookbook version
type CookbookVersion struct {
Url string `json:"url"`
Version string `json:"version"`
}
// CookbookMeta represents a Golang version of cookbook metadata
type CookbookMeta struct {
Name string `json:"cookbook_name"`
Version string `json:"version"`
Description string `json:"description"`
LongDescription string `json:"long_description"`
Maintainer string `json:"maintainer"`
MaintainerEmail string `json:"maintainer_email"`
License string `json:"license"`
Platforms map[string]string `json:"platforms"`
Depends map[string]string `json:"dependencies"`
Reccomends map[string]string `json:"recommendations"`
Suggests map[string]string `json:"suggestions"`
Conflicts map[string]string `json:"conflicting"`
Provides map[string]string `json:"providing"`
Replaces map[string]string `json:"replacing"`
Attributes map[string]interface{} `json:"attributes"` // 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"` // 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"`
}
// CookbookMeta represents a Golang version of cookbook metadata
type CookbookMetaPut struct {
Name string `json:"cookbook_name"`
Version string `json:"version"`
Description string `json:"description"`
LongDescription string `json:"long_description"`
Maintainer string `json:"maintainer"`
MaintainerEmail string `json:"maintainer_email"`
License string `json:"license"`
Platforms map[string]string `json:"platforms"`
Depends map[string]string `json:"dependencies"`
Reccomends map[string]string `json:"recommendations"`
Suggests map[string]string `json:"suggestions"`
Conflicts map[string]string `json:"conflicting"`
Provides map[string]string `json:"providing"`
Replaces map[string]string `json:"replacing"`
Attributes map[string]interface{} `json:"attributes"` // 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"` // 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"`
}
// 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"`
ChefType string `json:"chef_type"`
Frozen bool `json:"frozen?"`
JsonClass string `json:"json_class"`
Files []CookbookItem `json:"files"`
Templates []CookbookItem `json:"templates"`
Attributes []CookbookItem `json:"attributes"`
Recipes []CookbookItem `json:"recipes"`
Definitions []CookbookItem `json:"definitions"`
Libraries []CookbookItem `json:"libraries"`
Providers []CookbookItem `json:"providers"`
Resources []CookbookItem `json:"resources"`
RootFiles []CookbookItem `json:"root_files"`
Metadata CookbookMeta `json:"metadata"`
}
// Cookbook represents the native Go version of the deserialized api cookbook
type CookbookPut struct {
CookbookName string `json:"cookbook_name"`
Name string `json:"name"`
Version string `json:"version"`
ChefType string `json:"chef_type"`
Frozen bool `json:"frozen?"`
JsonClass string `json:"json_class"`
Files []CookbookItemPut `json:"files"`
Templates []CookbookItemPut `json:"templates"`
Attributes []CookbookItemPut `json:"attributes"`
Recipes []CookbookItemPut `json:"recipes"`
Definitions []CookbookItemPut `json:"definitions"`
Libraries []CookbookItemPut `json:"libraries"`
Providers []CookbookItemPut `json:"providers"`
Resources []CookbookItemPut `json:"resources"`
RootFiles []CookbookItemPut `json:"root_files"`
Metadata CookbookMetaPut `json:"metadata"`
}
// 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
}
// String makes Cookbook implement the string result
func (c Cookbook) String() (out string) {
cc, err := json.MarshalIndent(c,""," ")
if err != nil {
fmt.Println("error:", err)
}
return string(cc)
}
// String makes Cookbook implement the string result
func (c CookbookPut) String() (out string) {
cc, err := json.Marshal(c)
if err != nil {
fmt.Println("error:", err)
}
return string(cc)
}
func (c Cookbook) ToCookbookPut() (cookbookPut CookbookPut) {
cc, err := json.Marshal(c)
if err != nil {
fmt.Println("error:", err)
}
var acookbookPut CookbookPut
err = json.Unmarshal(cc,&acookbookPut)
if err != nil {
fmt.Println("error:", err)
}
// fmt.Println(acookbookPut)
return acookbookPut
}
// 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: http://docs.opscode.com/api_chef_server.html#id5
func (c *CookbookService) GetVersion(name, version string) (data Cookbook, err error) {
url := fmt.Sprintf("cookbooks/%s/%s", name, version)
c.client.magicRequestDecoder("GET", url, nil, &data)
return
}
// ListVersions lists the cookbooks available on the server limited to numVersions
// Chef API docs: http://docs.opscode.com/api_chef_server.html#id2
func (c *CookbookService) ListAvailableVersions(numVersions string) (data CookbookListResult, err error) {
path := versionParams("cookbooks", numVersions)
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", name)
err = c.client.magicRequestDecoder("DELETE", path, nil, nil)
return
}
// Download a version of a cookbook from a server
func (c *CookbookService) Download(name, version, destination string) (err error) {
url := fmt.Sprintf("cookbooks/%s/%s", name, version)
var data Cookbook
err = c.client.magicRequestDecoder("GET", url, nil, &data)
basedir := filepath.Join(destination, name + "-" + version)
//if _, err := os.Stat(basedir); err == nil {
// fmt.Println("There is already a cookbook in the tmp folder for the cookbook: " + name)
// fmt.Println(" Delete the directory " + basedir + " and retry!")
// os.Exit(1)
//}
c.DownloadCookbookItems(data.Attributes,basedir)
c.DownloadCookbookItems(data.Recipes,basedir)
c.DownloadCookbookItems(data.Providers,basedir)
c.DownloadCookbookItems(data.Definitions,basedir)
c.DownloadCookbookItems(data.Libraries,basedir)
c.DownloadCookbookItems(data.Files,basedir)
c.DownloadCookbookItems(data.Templates,basedir)
c.DownloadCookbookItems(data.RootFiles,basedir)
c.DownloadCookbookItems(data.Resources,basedir)
return
}
func (c *CookbookService) DownloadCookbookItems(object []CookbookItem, destination string) (err error){
for _,cbitems := range object{
err = os.MkdirAll(filepath.Dir(filepath.Join(destination, cbitems.Path)),0777)
if err != nil {
return err
}
err := c.client.Download(cbitems.Url, filepath.Join(destination, cbitems.Path))
if err != nil {
return err
}
}
return
}
// Upload a Cookbook
// Chef API docs: https://docs.chef.io/api_chef_server.html#id30
// We should add implementation for chefignore...
// Lazy geneartion of json descriptor of the cookbook ... Use a preprepared one...
func (c *CookbookService) Upload(name, version, source string, cookbookDestriptionJson CookbookPut) (err error) {
fileList := []string{}
err = filepath.Walk(filepath.Join(source,name), func(path string, f os.FileInfo, err error) error {
if !f.IsDir(){
fileList = append(fileList, path)
}
return nil
})
fileChecksums := make(map[string]string)
var checksums []string
for _, file := range fileList {
//fmt.Println(file)
checksum,err := ComputeMd5(file)
if err != nil{
return err
}
fileChecksums[checksum] = file
checksums = append(checksums,checksum)
}
// post the new sums/files to the sandbox
postResp, err := c.client.Sandboxes.Post(checksums)
if err != nil {
fmt.Println("error making request: ", err)
os.Exit(1)
}
for respChecksumID, item := range postResp.Checksums{
if item.Upload {
iofile,err := os.Open(fileChecksums[respChecksumID])
defer iofile.Close()
hexChecksum,_:= hex.DecodeString(respChecksumID)
checksum64 := base64.StdEncoding.EncodeToString(hexChecksum)
req, err := c.client.NewRequest("PUT", item.Url, iofile)
if err !=nil {
fmt.Println(err)
return err
}
req.Header.Set("content-type", "application/x-binary")
req.Header.Set("content-md5", checksum64)
req.Header.Set("accept", "application/json")
_, err = c.client.Do(req, nil)
iofile.Close()
if err !=nil {
fmt.Println("Error on push cookbookitems: " + err.Error())
return err
}
}
}
box , err := c.client.Sandboxes.Put(postResp.ID)
if err != nil {
fmt.Println("Error commiting sandbox: ", err.Error())
fmt.Println(box)
os.Exit(1)
}
c.Put(name,version,cookbookDestriptionJson)
return
}
func ComputeCookbookItemFromFile(filePath, checksum, basePath string)(cookbookitem CookbookItem, err error){
cookbookitem.Checksum = checksum
cookbookitem.Name = filepath.Base(filePath)
if basePath != ""{
relPath,err := filepath.Rel(basePath, filePath)
if err != nil{
fmt.Println("Oups")
return cookbookitem, err
}
cookbookitem.Path = relPath
}else
{
cookbookitem.Path = filePath
}
return cookbookitem ,err
}
func ComputeMd5(filePath string) (string, error) {
var result []byte
file, err := os.Open(filePath)
if err != nil {
return fmt.Sprintf("%x",result), err
}
defer file.Close()
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return fmt.Sprintf("%x",result), err
}
return fmt.Sprintf("%x",hash.Sum(result)), nil
}
// ListVersions lists the cookbooks available on the server limited to numVersions
// Chef API docs: http://docs.opscode.com/api_chef_server.html#id2
func (c *CookbookService) Put(name, version string, cookbook CookbookPut) (err error) {
url := fmt.Sprintf("cookbooks/%s/%s", name, version)
body, err := JSONReader(cookbook)
if err != nil {
fmt.Println("Json Reading err" + err.Error())
return
}
err = c.client.magicRequestDecoder("PUT", url, body, nil)
if err != nil {
fmt.Println("Error on magic decoder" + err.Error())
return
}
return
}