-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.go
279 lines (238 loc) · 9.08 KB
/
build.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
package bundleinstall
import (
"os"
"path/filepath"
"time"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/fs"
"github.com/paketo-buildpacks/packit/v2/sbom"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:generate faux --interface InstallProcess --output fakes/install_process.go
//go:generate faux --interface EntryResolver --output fakes/entry_resolver.go
//go:generate faux --interface SBOMGenerator --output fakes/sbom_generator.go
// InstallProcess defines the interface for executing the "bundle install"
// build process.
type InstallProcess interface {
ShouldRun(metadata map[string]interface{}, workingDir string) (should bool, checksum string, rubyVersion string, err error)
Execute(workingDir, layerPath string, config map[string]string, keepBuildFiles bool) error
}
// EntryResolver defines the interface for determining what phases of the
// lifecycle will require gems.
type EntryResolver interface {
MergeLayerTypes(string, []packit.BuildpackPlanEntry) (launch, build bool)
}
type SBOMGenerator interface {
Generate(dir string) (sbom.SBOM, error)
}
// Build will return a packit.BuildFunc that will be invoked during the build
// phase of the buildpack lifecycle.
//
// Build will execute the installation process to install gems that can be used
// in the build or launch phases of the buildpack lifecycle. Specifically,
// Build will provide different sets of gems as discrete layers depending upon
// their requirement in either the build or launch phase.
//
// If gems are required during the build phase, Build will ensure that all
// gems, including those in the "development" and "test" groups are installed
// into a layer that is made available during the remainder of the build phase.
//
// If gems are required during the launch phase, Build will ensure that only
// those gems that are not in the "development" or "test" groups are installed
// into a layer that is made available during the launch phase.
//
// If gems are required during both the build and launch phases, Build will
// provide both of the above layers with their sets of gems. These layers
// operate mutually exclusively as only one is available in each of the build
// or launch phase.
//
// To improve performance when installing gems for use in both the build and
// launch phases, Build will copy the contents of the build layer into the
// launch layer before executing the launch layer installation process. This
// will result in the launch layer installation process performing an effective
// "no-op" as all of the gems that it requires should already be copied into
// the layer. The launch layer installation process will however perform a
// "bundle clean" to remove any extra gems, including those from the
// "development" and "test" groups that may have been copied from the build
// layer.
//
// Finally, upon completing the installation process, Build will remove any
// local bundler configuration files such that the Bundler CLI will only follow
// configuration from the global location, which will be configured to point to
// a file that is maintained in each of the build and launch layers
// respectively.
func Build(
entries EntryResolver,
installProcess InstallProcess,
sbomGenerator SBOMGenerator,
logger scribe.Emitter,
clock chronos.Clock,
environment Environment,
) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
launch, build := entries.MergeLayerTypes("gems", context.Plan.Entries)
var layers []packit.Layer
if build {
logger.Debug.Process("Getting the layer associated with %s", LayerNameBuildGems)
layer, err := context.Layers.Get(LayerNameBuildGems)
if err != nil {
return packit.BuildResult{}, err
}
logger.Debug.Subprocess(layer.Path)
logger.Debug.Break()
layer.Build = true
layer.Cache = true
logger.Debug.Process("Checking if the build environment install process should run")
logger.Debug.Break()
should, checksum, rubyVersion, err := installProcess.ShouldRun(layer.Metadata, context.WorkingDir)
if err != nil {
return packit.BuildResult{}, err
}
stack, ok := layer.Metadata["stack"]
if ok && stack.(string) != context.Stack {
logger.Process("Stack upgraded from %s to %s, clearing cached gems", stack.(string), context.Stack)
layer, err = layer.Reset()
if err != nil {
return packit.BuildResult{}, err
}
should = true
layer.Build = true
layer.Cache = true
}
if should {
logger.Process("Executing build environment install process")
duration, err := clock.Measure(func() error {
return installProcess.Execute(context.WorkingDir, layer.Path, map[string]string{
"path": layer.Path,
"clean": "true",
}, environment.KeepGemExtensionBuildFiles)
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
layer.BuildEnv.Default("BUNDLE_USER_CONFIG", filepath.Join(layer.Path, "config"))
layer.Metadata = map[string]interface{}{
"stack": context.Stack,
"cache_sha": checksum,
"ruby_version": rubyVersion,
}
logger.GeneratingSBOM(layer.Path)
var sbomContent sbom.SBOM
duration, err = clock.Measure(func() error {
sbomContent, err = sbomGenerator.Generate(context.WorkingDir)
return err
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
logger.FormattingSBOM(context.BuildpackInfo.SBOMFormats...)
layer.SBOM, err = sbomContent.InFormats(context.BuildpackInfo.SBOMFormats...)
if err != nil {
return packit.BuildResult{}, err
}
} else {
logger.Process("Reusing cached layer %s", layer.Path)
logger.Break()
}
layers = append(layers, layer)
}
if launch {
logger.Debug.Process("Getting the layer associated with %s", LayerNameLaunchGems)
layer, err := context.Layers.Get(LayerNameLaunchGems)
if err != nil {
return packit.BuildResult{}, err
}
logger.Debug.Subprocess(layer.Path)
logger.Debug.Break()
layer.Launch = true
logger.Debug.Process("Checking if the launch environment install process should run")
logger.Debug.Break()
should, checksum, rubyVersion, err := installProcess.ShouldRun(layer.Metadata, context.WorkingDir)
if err != nil {
return packit.BuildResult{}, err
}
stack, ok := layer.Metadata["stack"]
if ok && stack.(string) != context.Stack {
logger.Process("Stack upgraded from %s to %s, clearing cached gems", stack.(string), context.Stack)
layer, err = layer.Reset()
if err != nil {
return packit.BuildResult{}, err
}
should = true
layer.Launch = true
}
if should {
logger.Process("Executing launch environment install process")
duration, err := clock.Measure(func() error {
if build {
buildLayer, err := context.Layers.Get(LayerNameBuildGems)
if err != nil {
return err
}
err = fs.Copy(filepath.Join(buildLayer.Path), filepath.Join(layer.Path))
if err != nil {
return err
}
}
return installProcess.Execute(context.WorkingDir, layer.Path, map[string]string{
"path": layer.Path,
"without": "development:test",
"clean": "true",
}, environment.KeepGemExtensionBuildFiles)
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
layer.LaunchEnv.Default("BUNDLE_USER_CONFIG", filepath.Join(layer.Path, "config"))
layer.Metadata = map[string]interface{}{
"stack": context.Stack,
"cache_sha": checksum,
"ruby_version": rubyVersion,
}
logger.GeneratingSBOM(layer.Path)
var sbomContent sbom.SBOM
duration, err = clock.Measure(func() error {
sbomContent, err = sbomGenerator.Generate(context.WorkingDir)
return err
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
logger.FormattingSBOM(context.BuildpackInfo.SBOMFormats...)
layer.SBOM, err = sbomContent.InFormats(context.BuildpackInfo.SBOMFormats...)
if err != nil {
return packit.BuildResult{}, err
}
} else {
logger.Process("Reusing cached layer %s", layer.Path)
logger.Break()
}
layers = append(layers, layer)
}
for _, layer := range layers {
logger.EnvironmentVariables(layer)
}
logger.Debug.Process("Cleaning up %s/.bundle/config", context.WorkingDir)
err := os.RemoveAll(filepath.Join(context.WorkingDir, ".bundle", "config"))
if err != nil {
return packit.BuildResult{}, err
}
logger.Debug.Process("Cleaning up %s/.bundle/config.bak", context.WorkingDir)
logger.Debug.Break()
err = os.RemoveAll(filepath.Join(context.WorkingDir, ".bundle", "config.bak"))
if err != nil {
return packit.BuildResult{}, err
}
return packit.BuildResult{Layers: layers}, nil
}
}