-
Notifications
You must be signed in to change notification settings - Fork 30
/
buildpacks.go
391 lines (348 loc) · 12 KB
/
buildpacks.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
package importcmd
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
v1 "github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x-plugins/jx-gitops/pkg/apis/gitops/v1alpha1"
"github.com/jenkins-x-plugins/jx-project/pkg/config"
"github.com/jenkins-x-plugins/jx-project/pkg/gitresolver"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
"github.com/jenkins-x/jx-helpers/v3/pkg/yamls"
"github.com/pkg/errors"
jxdraft "github.com/jenkins-x-plugins/jx-project/pkg/draft"
"github.com/jenkins-x-plugins/jx-project/pkg/jenkinsfile"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
)
// InvokeDraftPack used to pass arguments into the draft pack invocation
type InvokeDraftPack struct {
Dir string
DevEnvCloneDir string
CustomDraftPack string
Jenkinsfile string
InitialisedGit bool
DisableAddFiles bool
UseNextGenPipeline bool
CreateJenkinsxYamlIfMissing bool
ProjectConfig *config.ProjectConfig
}
// InitBuildPacks initialise the build packs
func (o *ImportOptions) InitBuildPacks(i *InvokeDraftPack) (string, *v1.TeamSettings, error) {
bp, settings, err := o.PickPipelineCatalog(i)
if err != nil {
return "", settings, err
}
if o.PipelineCatalogDir != "" {
log.Logger().Infof("using the pipeline catalog dir %s", termcolor.ColorInfo(o.PipelineCatalogDir))
return o.PipelineCatalogDir, settings, err
}
dir, err := gitresolver.InitBuildPack(o.Git(), bp.GitURL, bp.GitRef)
return dir, settings, err
}
// CloneDevEnvironment clones the development environment to a directory
func (o *ImportOptions) CloneDevEnvironment() (string, error) {
if o.DevEnv == nil {
return "", errors.Errorf("no Dev Environment")
}
devEnvGitURL := o.DevEnv.Spec.Source.URL
if devEnvGitURL == "" {
return "", errors.Errorf("no spec.source.url for dev environment so cannot clone the version stream")
}
devEnvCloneDir, err := gitclient.CloneToDir(o.Git(), devEnvGitURL, "")
if err != nil {
return "", errors.Wrapf(err, "failed to clone dev environment git repository %s", devEnvGitURL)
}
return devEnvCloneDir, nil
}
// PickPipelineCatalog lets you pick a build pack
func (o *ImportOptions) PickPipelineCatalog(i *InvokeDraftPack) (*v1alpha1.PipelineCatalogSource, *v1.TeamSettings, error) {
if o.DevEnv == nil {
return nil, nil, errors.Errorf("no Dev Environment")
}
devEnvCloneDir := i.DevEnvCloneDir
if devEnvCloneDir == "" {
return nil, nil, errors.Errorf("no Dev Environment git clone dir")
}
settings := &o.DevEnv.Spec.TeamSettings
pipelineCatalogsFile := filepath.Join(devEnvCloneDir, "extensions", v1alpha1.PipelineCatalogFileName)
exists, err := files.FileExists(pipelineCatalogsFile)
if err != nil {
return nil, settings, errors.Wrapf(err, "failed to check if file exists %s", pipelineCatalogsFile)
}
pipelineCatalog := &v1alpha1.PipelineCatalog{}
if exists {
err = yamls.LoadFile(pipelineCatalogsFile, pipelineCatalog)
if err != nil {
return nil, settings, errors.Wrapf(err, "failed to load PipelineCatalog file %s", pipelineCatalogsFile)
}
}
if len(pipelineCatalog.Spec.Repositories) == 0 {
// lets add the default pipeline catalog
defaultCatalog := v1alpha1.PipelineCatalogSource{
ID: "default-pipeline-catalog",
Label: "Cluster Pipeline Catalog",
GitURL: "",
GitRef: "",
}
if defaultCatalog.GitURL == "" {
defaultCatalog.GitURL = "https://github.com/jenkins-x/jx3-pipeline-catalog"
}
pipelineCatalog.Spec.Repositories = append(pipelineCatalog.Spec.Repositories, defaultCatalog)
}
m := map[string]*v1alpha1.PipelineCatalogSource{}
names := []string{}
defaultValue := ""
for i := range pipelineCatalog.Spec.Repositories {
pc := &pipelineCatalog.Spec.Repositories[i]
if pc.Label == "" {
pc.Label = pc.ID
if pc.Label == "" {
pc.Label = pc.GitURL
}
}
label := pc.Label
if defaultValue == "" {
defaultValue = label
}
names = append(names, label)
m[label] = pc
}
sort.Strings(names)
if o.BatchMode {
pc := &pipelineCatalog.Spec.Repositories[0]
return pc, settings, nil
}
name, err := o.Input.PickNameWithDefault(names, "Pick the pipeline catalog you would like to use", defaultValue,
"the pipeline catalog folder contains the tekton pipelines and associated files")
if err != nil {
return nil, settings, errors.Wrap(err, "failed to pick the build pack name")
}
return m[name], settings, err
}
// InvokeDraftPack invokes a draft pack copying in a Jenkinsfile if required
func (o *ImportOptions) InvokeDraftPack(i *InvokeDraftPack) (string, error) {
packsDir, _, err := o.InitBuildPacks(i)
if err != nil {
return "", err
}
// lets assume Jenkins X import mode
//
// was:
// lets configure the draft pack mode based on the team settings
// if settings.GetImportMode() != v1.ImportModeTypeJenkinsfile {
i.UseNextGenPipeline = true
i.CreateJenkinsxYamlIfMissing = true
dir := i.Dir
customDraftPack := i.CustomDraftPack
pomName := filepath.Join(dir, "pom.xml")
gradleName := filepath.Join(dir, "build.gradle")
jenkinsPluginsName := filepath.Join(dir, "plugins.txt")
packagerConfigName := filepath.Join(dir, "packager-config.yml")
jenkinsxYaml := filepath.Join(dir, config.ProjectConfigFileName)
envChart := filepath.Join(dir, "env/Chart.yaml")
lpack := ""
if len(customDraftPack) == 0 {
if i.ProjectConfig == nil {
i.ProjectConfig, _, err = config.LoadProjectConfig(dir)
if err != nil {
return "", err
}
}
customDraftPack = i.ProjectConfig.BuildPack
}
if len(customDraftPack) > 0 {
log.Logger().Infof("trying to use draft pack: %s", customDraftPack)
lpack = filepath.Join(packsDir, customDraftPack)
f, err := files.DirExists(lpack)
if err != nil {
log.Logger().Error(err.Error())
return "", err
}
if !f {
log.Logger().Error("Could not find pack: " + customDraftPack + " going to try detect which pack to use")
lpack = ""
}
}
if len(lpack) == 0 {
if exists, err := files.FileExists(pomName); err == nil && exists {
pack, err := PomFlavour(pomName)
if err != nil {
return "", err
}
lpack = filepath.Join(packsDir, pack)
exists, _ = files.DirExists(lpack)
if !exists {
log.Logger().Warn("defaulting to maven pack")
lpack = filepath.Join(packsDir, "maven")
}
} else if exists, err := files.FileExists(gradleName); err == nil && exists {
lpack = filepath.Join(packsDir, "gradle")
} else if exists, err := files.FileExists(jenkinsPluginsName); err == nil && exists {
lpack = filepath.Join(packsDir, "jenkins")
} else if exists, err := files.FileExists(packagerConfigName); err == nil && exists {
lpack = filepath.Join(packsDir, "cwp")
} else if exists, err := files.FileExists(envChart); err == nil && exists {
lpack = filepath.Join(packsDir, "environment")
} else {
// pack detection time
lpack, err = jxdraft.DoPackDetectionForBuildPack(os.Stdout, dir, packsDir)
if err != nil {
if lpack == "" {
// lets detect docker and/or helm
// TODO one day when our pipelines can include steps conditional on the presence of a file glob
// we can just use a single docker/helm package that does docker and/or helm
// but for now we've 3 separate packs for docker, docker-helm and helm
hasDocker := false
hasHelm := false
if exists, err2 := files.FileExists(filepath.Join(dir, "Dockerfile")); err2 == nil && exists {
hasDocker = true
}
// lets check for a helm pack
files, err2 := filepath.Glob(filepath.Join(dir, "charts/*/Chart.yaml"))
if err2 != nil {
return "", errors.Wrapf(err, "failed to detect if there was a chart file in dir %s", dir)
}
if len(files) == 0 {
files, err2 = filepath.Glob(filepath.Join(dir, "*/Chart.yaml"))
if err2 != nil {
return "", errors.Wrapf(err, "failed to detect if there was a chart file in dir %s", dir)
}
}
if len(files) > 0 {
hasHelm = true
}
if hasDocker {
if hasHelm {
lpack = filepath.Join(packsDir, "docker-helm")
err = nil
} else {
lpack = filepath.Join(packsDir, "docker")
err = nil
}
} else if hasHelm {
lpack = filepath.Join(packsDir, "helm")
err = nil
}
}
if lpack == "" {
exists, err2 := files.FileExists(filepath.Join(dir, jenkinsfile.Name))
if exists && err2 == nil {
lpack = filepath.Join(packsDir, "custom-jenkins")
err = nil
}
}
if err != nil {
return "", err
}
}
}
}
pack := filepath.Base(lpack)
pack, err = o.PickCatalogFolderName(i, packsDir, pack)
if err != nil {
return "", err
}
lpack = filepath.Join(packsDir, pack)
log.Logger().Infof("selected catalog folder: %s", termcolor.ColorInfo(pack))
i.CustomDraftPack = pack
if i.DisableAddFiles {
return pack, nil
}
chartsDir := filepath.Join(dir, "charts")
jenkinsxYamlExists, err := files.FileExists(jenkinsxYaml)
if err != nil {
return pack, err
}
err = copyBuildPack(dir, lpack, o.PackFilter)
if err != nil {
log.Logger().Warnf("Failed to apply the build pack in %s due to %s", dir, err)
}
// lets delete empty charts dir if a draft pack created one
exists, err := files.DirExists(chartsDir)
if err == nil && exists {
files, err := ioutil.ReadDir(chartsDir)
if err != nil {
return pack, errors.Wrapf(err, "failed to read charts dir %s", chartsDir)
}
if len(files) == 0 {
err = os.Remove(chartsDir)
if err != nil {
return pack, errors.Wrapf(err, "failed to remove empty charts dir %s", chartsDir)
}
}
}
if !jenkinsxYamlExists && i.CreateJenkinsxYamlIfMissing {
// lets check if we have a lighthouse trigger
g := filepath.Join(dir, ".lighthouse", "*", "triggers.yaml")
matches, err := filepath.Glob(g)
if err != nil {
return pack, errors.Wrapf(err, "failed to evaluate glob %s", g)
}
if len(matches) == 0 {
pipelineConfig, err := config.LoadProjectConfigFile(jenkinsxYaml)
if err != nil {
return pack, err
}
// only update the build pack if its not currently set to none so that build packs can
// use a custom pipeline plugin mechanism
if pipelineConfig.BuildPack != pack && pipelineConfig.BuildPack != "none" {
pipelineConfig.BuildPack = pack
err = pipelineConfig.SaveConfig(jenkinsxYaml)
if err != nil {
return pack, err
}
}
}
}
lighthouseDir := filepath.Join(packsDir, pack, ".lighthouse")
exists, err = files.DirExists(lighthouseDir)
if err != nil {
return pack, errors.Wrapf(err, "failed to detect lighthouse dir %s", lighthouseDir)
}
if exists {
err = o.createMissingLighthouseKptFiles(lighthouseDir, pack)
if err != nil {
return pack, errors.Wrapf(err, "failed to add missing Kptfiles for pipeline catalog")
}
}
return pack, nil
}
// DiscoverBuildPack discovers the build pack given the build pack configuration
func (o *ImportOptions) DiscoverBuildPack(dir string, projectConfig *config.ProjectConfig, packConfig string) (string, error) {
if packConfig != "" {
return packConfig, nil
}
args := &InvokeDraftPack{
Dir: dir,
CustomDraftPack: packConfig,
ProjectConfig: projectConfig,
DisableAddFiles: true,
}
pack, err := o.InvokeDraftPack(args)
if err != nil {
return pack, errors.Wrapf(err, "failed to discover task pack in dir %s", dir)
}
return pack, nil
}
// Refactor: taken from jx so we can also bring in the draft pack and not fail when copying buildpacks without a charts dir
// CopyBuildPack copies the build pack from the source dir to the destination dir
func copyBuildPack(dest, src string, filter func(*Pack)) error {
// first do some validation that we are copying from a valid pack directory
p, err := FromDir(src)
if err != nil {
return fmt.Errorf("could not load %s: %s", src, err)
}
// lets remove any files we think should be zapped
for _, file := range []string{jenkinsfile.PipelineConfigFileName, jenkinsfile.PipelineTemplateFileName} {
delete(p.Files, file)
}
if filter != nil {
filter(p)
}
_, packName := filepath.Split(src)
return p.SaveDir(dest, packName)
}