forked from chef/chef-load
/
cookbooks.go
77 lines (70 loc) · 2.24 KB
/
cookbooks.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
package main
import (
"encoding/json"
"fmt"
"github.com/go-chef/chef"
)
func downloadCookbooks(nodeClient *chef.Client, cookbooks map[string]json.RawMessage) {
for _, v := range cookbooks {
var cookbook cookbook
json.Unmarshal(v, &cookbook)
for _, cookbookFile := range cookbook.Attributes {
getCookbookFile(nodeClient, cookbookFile.URL)
}
for _, cookbookFile := range cookbook.Definitions {
getCookbookFile(nodeClient, cookbookFile.URL)
}
for _, cookbookFile := range cookbook.Files {
getCookbookFile(nodeClient, cookbookFile.URL)
}
for _, cookbookFile := range cookbook.Libraries {
getCookbookFile(nodeClient, cookbookFile.URL)
}
for _, cookbookFile := range cookbook.Providers {
getCookbookFile(nodeClient, cookbookFile.URL)
}
for _, cookbookFile := range cookbook.Recipes {
getCookbookFile(nodeClient, cookbookFile.URL)
}
for _, cookbookFile := range cookbook.Resources {
getCookbookFile(nodeClient, cookbookFile.URL)
}
for _, cookbookFile := range cookbook.RootFiles {
getCookbookFile(nodeClient, cookbookFile.URL)
}
for _, cookbookFile := range cookbook.Templates {
getCookbookFile(nodeClient, cookbookFile.URL)
}
}
}
type cookbook struct {
CookbookName string `json:"cookbook_name"`
Name string `json:"name"`
Version string `json:"version"`
Attributes []cookbookFile `json:"attributes"`
Definitions []cookbookFile `json:"definitions"`
Files []cookbookFile `json:"files"`
Libraries []cookbookFile `json:"libraries"`
Providers []cookbookFile `json:"providers"`
Recipes []cookbookFile `json:"recipes"`
Resources []cookbookFile `json:"resources"`
RootFiles []cookbookFile `json:"root_files"`
Templates []cookbookFile `json:"templates"`
}
type cookbookFile struct {
Checksum string `json:"checksum"`
Name string `json:"name"`
Path string `json:"path"`
Specificity string `json:"specificity"`
URL string `json:"url"`
}
func getCookbookFile(nodeClient *chef.Client, cookbookFile string) {
req, err := nodeClient.NewRequest("GET", cookbookFile, nil)
res, err := nodeClient.Do(req, nil)
if err != nil {
fmt.Println(err)
// TODO: need to handle errors better
return
}
defer res.Body.Close()
}