This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
/
engine_chef.go
259 lines (216 loc) · 8.3 KB
/
engine_chef.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
package engine
import (
"capsulecd/pkg/config"
"capsulecd/pkg/errors"
"capsulecd/pkg/pipeline"
"capsulecd/pkg/scm"
"capsulecd/pkg/utils"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
)
type chefMetadata struct {
Version string `json:"version"`
Name string `json:"name"`
}
type engineChef struct {
engineBase
Scm scm.Interface //Interface
CurrentMetadata *chefMetadata
NextMetadata *chefMetadata
}
func (g *engineChef) Init(pipelineData *pipeline.Data, configData config.Interface, sourceScm scm.Interface) error {
g.Config = configData
g.Scm = sourceScm
g.PipelineData = pipelineData
g.CurrentMetadata = new(chefMetadata)
g.NextMetadata = new(chefMetadata)
//set command defaults (can be overridden by repo/system configuration)
g.Config.SetDefault("chef_supermarket_type", "Other")
g.Config.SetDefault("engine_cmd_compile", "echo 'skipping compile'")
g.Config.SetDefault("engine_cmd_lint", "foodcritic .")
g.Config.SetDefault("engine_cmd_fmt", "foodcritic .")
g.Config.SetDefault("engine_cmd_test", "rake test")
g.Config.SetDefault("engine_cmd_security_check", "bundle audit check --update")
return nil
}
func (g *engineChef) ValidateTools() error {
//a chefdk like environment needs to be available for this Engine
if _, kerr := exec.LookPath("knife"); kerr != nil {
return errors.EngineValidateToolError("knife binary is missing")
}
if _, berr := exec.LookPath("berks"); berr != nil {
return errors.EngineValidateToolError("berkshelf binary is missing")
}
//TODO: figure out how to validate that "bundle audit" command exists.
if _, berr := exec.LookPath("bundle"); berr != nil {
return errors.EngineValidateToolError("bundler binary is missing")
}
if _, berr := exec.LookPath("foodcritic"); berr != nil && !g.Config.GetBool("engine_disable_lint") {
return errors.EngineValidateToolError("foodcritic binary is missing")
}
return nil
}
func (g *engineChef) AssembleStep() error {
//validate that the chef metadata.rb file exists
if !utils.FileExists(path.Join(g.PipelineData.GitLocalPath, "metadata.rb")) {
return errors.EngineBuildPackageInvalid("metadata.rb file is required to process Chef cookbook")
}
// bump up the chef cookbook version
if merr := g.retrieveCurrentMetadata(g.PipelineData.GitLocalPath); merr != nil {
return merr
}
if perr := g.populateNextMetadata(); perr != nil {
return perr
}
if nerr := g.writeNextMetadata(g.PipelineData.GitLocalPath); nerr != nil {
return nerr
}
// TODO: check if this cookbook name and version already exist.
// check for/create any required missing folders/files
// Berksfile.lock and Gemfile.lock are not required to be committed, but they should be.
rakefilePath := path.Join(g.PipelineData.GitLocalPath, "Rakefile")
if !utils.FileExists(rakefilePath) {
ioutil.WriteFile(rakefilePath, []byte("task :test"), 0644)
}
berksfilePath := path.Join(g.PipelineData.GitLocalPath, "Berksfile")
if !utils.FileExists(berksfilePath) {
ioutil.WriteFile(berksfilePath, []byte(utils.StripIndent(
`source "https://supermarket.chef.io"
metadata
`)), 0644)
}
gemfilePath := path.Join(g.PipelineData.GitLocalPath, "Gemfile")
if !utils.FileExists(gemfilePath) {
ioutil.WriteFile(gemfilePath, []byte(`source "https://rubygems.org"`), 0644)
}
specPath := path.Join(g.PipelineData.GitLocalPath, "spec")
if !utils.FileExists(specPath) {
os.MkdirAll(specPath, 0777)
}
gitignorePath := path.Join(g.PipelineData.GitLocalPath, ".gitignore")
if !utils.FileExists(gitignorePath) {
if err := utils.GitGenerateGitIgnore(g.PipelineData.GitLocalPath, "ChefCookbook"); err != nil {
log.Print("Generate error")
return err
}
}
return nil
}
func (g *engineChef) DependenciesStep() error {
// the cookbook has already been downloaded. lets make sure all its dependencies are available.
if cerr := utils.BashCmdExec("berks install", g.PipelineData.GitLocalPath, nil, ""); cerr != nil {
return errors.EngineTestDependenciesError("berks install failed. Check cookbook dependencies")
}
//download all its gem dependencies
if berr := utils.BashCmdExec("bundle install", g.PipelineData.GitLocalPath, nil, ""); berr != nil {
return errors.EngineTestDependenciesError("bundle install failed. Check Gem dependencies")
}
return nil
}
// Use default compile step..
// func (g *engineChef) CompileStep() error {}
// Use default test step.
// func (g *engineChef) TestStep() error {}
func (g *engineChef) PackageStep() error {
if !g.Config.GetBool("engine_package_keep_lock_file") {
os.Remove(path.Join(g.PipelineData.GitLocalPath, "Berksfile.lock"))
os.Remove(path.Join(g.PipelineData.GitLocalPath, "Gemfile.lock"))
}
if cerr := utils.GitCommit(g.PipelineData.GitLocalPath, fmt.Sprintf("(v%s) Automated packaging of release by CapsuleCD", g.NextMetadata.Version)); cerr != nil {
return cerr
}
tagCommit, terr := utils.GitTag(g.PipelineData.GitLocalPath, fmt.Sprintf("v%s", g.NextMetadata.Version))
if terr != nil {
return terr
}
g.PipelineData.ReleaseCommit = tagCommit
g.PipelineData.ReleaseVersion = g.NextMetadata.Version
return nil
}
func (g *engineChef) DistStep() error {
if !g.Config.IsSet("chef_supermarket_username") || !g.Config.IsSet("chef_supermarket_key") {
return errors.EngineDistCredentialsMissing("Cannot deploy cookbook to supermarket, credentials missing")
}
// knife is really sensitive to folder names. The cookbook name MUST match the folder name otherwise knife throws up
// when doing a knife cookbook share. So we're going to make a new tmp directory, create a subdirectory with the EXACT
// cookbook name, and then copy the cookbook contents into it. Yeah yeah, its pretty nasty, but blame Chef.
tmpParentPath, terr := ioutil.TempDir("", "")
if terr != nil {
return terr
}
defer os.RemoveAll(tmpParentPath)
tmpLocalPath := path.Join(tmpParentPath, g.NextMetadata.Name)
if cerr := utils.CopyDir(g.PipelineData.GitLocalPath, tmpLocalPath); cerr != nil {
return cerr
}
pemFile, _ := ioutil.TempFile("", "client.pem")
defer os.Remove(pemFile.Name())
knifeFile, _ := ioutil.TempFile("", "knife.rb")
defer os.Remove(knifeFile.Name())
// write the knife.rb config jfile.
knifeContent := fmt.Sprintf(utils.StripIndent(
`node_name "%s" # Replace with the login name you use to login to the Supermarket.
client_key "%s" # Define the path to wherever your client.pem file lives. This is the key you generated when you signed up for a Chef account.
cookbook_path [ '%s' ] # Directory where the cookbook you're uploading resides.
`),
g.Config.GetString("chef_supermarket_username"),
pemFile.Name(),
tmpParentPath,
)
_, kerr := knifeFile.Write([]byte(knifeContent))
if kerr != nil {
return kerr
}
chefKey, berr := g.Config.GetBase64Decoded("chef_supermarket_key")
if berr != nil {
return berr
}
_, perr := pemFile.Write([]byte(chefKey))
if perr != nil {
return perr
}
cookbookDistCmd := fmt.Sprintf("knife cookbook site share %s %s -c %s",
g.NextMetadata.Name,
g.Config.GetString("chef_supermarket_type"),
knifeFile.Name(),
)
if derr := utils.BashCmdExec(cookbookDistCmd, "", nil, ""); derr != nil {
return errors.EngineDistPackageError("knife cookbook upload to supermarket failed")
}
return nil
}
//private Helpers
func (g *engineChef) retrieveCurrentMetadata(gitLocalPath string) error {
//dat, err := ioutil.ReadFile(path.Join(gitLocalPath, "metadata.rb"))
//knife cookbook metadata -o ../ chef-mycookbook -- will generate a metadata.json file.
if cerr := utils.BashCmdExec(fmt.Sprintf("knife cookbook metadata -o ../ %s", path.Base(gitLocalPath)), gitLocalPath, nil, ""); cerr != nil {
return cerr
}
defer os.Remove(path.Join(gitLocalPath, "metadata.json"))
//read metadata.json file.
metadataContent, rerr := ioutil.ReadFile(path.Join(gitLocalPath, "metadata.json"))
if rerr != nil {
return rerr
}
if uerr := json.Unmarshal(metadataContent, g.CurrentMetadata); uerr != nil {
return uerr
}
return nil
}
func (g *engineChef) populateNextMetadata() error {
nextVersion, err := g.BumpVersion(g.CurrentMetadata.Version)
if err != nil {
return err
}
g.NextMetadata.Version = nextVersion
g.NextMetadata.Name = g.CurrentMetadata.Name
return nil
}
func (g *engineChef) writeNextMetadata(gitLocalPath string) error {
return utils.BashCmdExec(fmt.Sprintf("knife spork bump %s manual %s -o ../", path.Base(gitLocalPath), g.NextMetadata.Version), gitLocalPath, nil, "")
}