-
Notifications
You must be signed in to change notification settings - Fork 16
/
build.go
253 lines (205 loc) · 7.84 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
package cpython
import (
"os"
"path/filepath"
"time"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/cargo"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/draft"
"github.com/paketo-buildpacks/packit/v2/fs"
"github.com/paketo-buildpacks/packit/v2/postal"
"github.com/paketo-buildpacks/packit/v2/sbom"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:generate faux --interface DependencyManager --output fakes/dependency_manager.go
//go:generate faux --interface PythonInstaller --output fakes/installer.go
//go:generate faux --interface PythonPipCleanup --output fakes/pip_cleanup.go
//go:generate faux --interface SBOMGenerator --output fakes/sbom_generator.go
// DependencyManager defines the interface for picking the best matching
// dependency installing it, and generating a BOM.
type DependencyManager interface {
Resolve(path, id, version, stack string) (postal.Dependency, error)
Deliver(dependency postal.Dependency, cnbPath, destinationPath, platformPath string) error
GenerateBillOfMaterials(dependencies ...postal.Dependency) []packit.BOMEntry
}
// PythonInstaller defines the interface for installing python from source
type PythonInstaller interface {
Install(
sourcePath string,
workingDir string,
entry packit.BuildpackPlanEntry,
dependency postal.Dependency,
layerPath string,
) error
}
// PythonPipCleanup defines the interface for cleaning up pip after a python installation
type PythonPipCleanup interface {
Cleanup(packages []string, targetLayer string) error
}
type SBOMGenerator interface {
GenerateFromDependency(dependency postal.Dependency, 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 find the right cpython dependency to install, install it in a
// layer, and generate Bill-of-Materials. It also makes use of the checksum of
// the dependency to reuse the layer when possible.
func Build(
dependencies DependencyManager,
pythonInstaller PythonInstaller,
pipCleanup PythonPipCleanup,
sbomGenerator SBOMGenerator,
logger scribe.Emitter,
clock chronos.Clock,
) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
logger.Process("Resolving CPython version")
planner := draft.NewPlanner()
entry, sortedEntries := planner.Resolve(Cpython, context.Plan.Entries, Priorities)
logger.Candidates(sortedEntries)
entryVersion, _ := entry.Metadata["version"].(string)
// This is done because the core dependencies pipeline provides the cpython
// dependency under the name python.
entry.Name = "python"
dependency, err := dependencies.Resolve(filepath.Join(context.CNBPath, "buildpack.toml"), entry.Name, entryVersion, context.Stack)
if err != nil {
return packit.BuildResult{}, err
}
// This is done because the core dependencies pipeline provides the cpython
// dependency under the name python.
dependency.ID = "cpython"
dependency.Name = "CPython"
logger.SelectedDependency(entry, dependency, clock.Now())
legacySBOM := dependencies.GenerateBillOfMaterials(dependency)
launch, build := planner.MergeLayerTypes(Cpython, context.Plan.Entries)
var launchMetadata packit.LaunchMetadata
if launch {
launchMetadata.BOM = legacySBOM
}
var buildMetadata packit.BuildMetadata
if build {
buildMetadata.BOM = legacySBOM
}
cpythonLayer, err := context.Layers.Get(Cpython)
if err != nil {
return packit.BuildResult{}, err
}
cpythonLayer.Launch, cpythonLayer.Build, cpythonLayer.Cache = launch, build, build
cachedChecksum, ok := cpythonLayer.Metadata[DepKey].(string)
dependencyChecksum := dependency.Checksum
if dependencyChecksum == "" {
//nolint:staticcheck // SHA256 is only a fallback in case Checksum is not present
dependencyChecksum = dependency.SHA256
}
if ok && cachedChecksum != "" && cargo.Checksum(cachedChecksum).MatchString(dependencyChecksum) {
logger.Process("Reusing cached layer %s", cpythonLayer.Path)
logger.Break()
return packit.BuildResult{
Layers: []packit.Layer{cpythonLayer},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
logger.Process("Executing build process")
cpythonLayer, err = cpythonLayer.Reset()
if err != nil {
return packit.BuildResult{}, err
}
cpythonLayer.Launch, cpythonLayer.Build, cpythonLayer.Cache = launch, build, build
logger.Subprocess("Installing CPython %s", dependency.Version)
var duration time.Duration
// Install python from source when URI and Source match
if dependency.URI == dependency.Source {
sourcePath := filepath.Join(cpythonLayer.Path, "python-source")
if dependency.StripComponents == 0 {
// CPython distributions have one layer of directory prefix, so strip that when unpacking
dependency.StripComponents = 1
}
err = os.Mkdir(sourcePath, 0755)
if err != nil {
// untested - hard to force a test failure
return packit.BuildResult{}, err
}
downloadDuration, err := clock.Measure(func() error {
return dependencies.Deliver(dependency, context.CNBPath, sourcePath, context.Platform.Path)
})
if err != nil {
return packit.BuildResult{}, err
}
installDuration, err := clock.Measure(func() error {
return pythonInstaller.Install(sourcePath, context.WorkingDir, entry, dependency, cpythonLayer.Path)
})
if err != nil {
return packit.BuildResult{}, err
}
duration = downloadDuration + installDuration
err = os.RemoveAll(sourcePath)
if err != nil {
return packit.BuildResult{}, err
}
} else {
// Otherwise extract context from URI into layer
downloadDuration, err := clock.Measure(func() error {
return dependencies.Deliver(dependency, context.CNBPath, cpythonLayer.Path, context.Platform.Path)
})
if err != nil {
return packit.BuildResult{}, err
}
duration = downloadDuration
}
pipCleanupDuration, err := clock.Measure(func() error {
if _, ok := os.LookupEnv("BP_CPYTHON_RM_SETUPTOOLS"); ok {
return pipCleanup.Cleanup(pipPackagesToBeUninstalled(), cpythonLayer.Path)
}
return nil
})
if err != nil {
return packit.BuildResult{}, err
}
duration += pipCleanupDuration
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
logger.GeneratingSBOM(cpythonLayer.Path)
var sbomContent sbom.SBOM
duration, err = clock.Measure(func() error {
sbomContent, err = sbomGenerator.GenerateFromDependency(dependency, cpythonLayer.Path)
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...)
cpythonLayer.SBOM, err = sbomContent.InFormats(context.BuildpackInfo.SBOMFormats...)
if err != nil {
return packit.BuildResult{}, err
}
cpythonLayer.Metadata = map[string]interface{}{
DepKey: dependencyChecksum,
}
cpythonLayer.BuildEnv.Default("PYTHONPYCACHEPREFIX", "/tmp")
cpythonLayer.SharedEnv.Default("PYTHONPATH", cpythonLayer.Path)
cpythonLayer.ExecD = []string{filepath.Join(context.CNBPath, "bin", "env")}
if exists, err := fs.Exists(filepath.Join(cpythonLayer.Path, "bin", "python")); err != nil {
return packit.BuildResult{}, err
} else if exists {
logger.Debug.Detail("bin/python already exists")
} else {
logger.Debug.Detail("Writing symlink bin/python")
if err := os.Symlink(filepath.Join(cpythonLayer.Path, "bin", "python3"), filepath.Join(cpythonLayer.Path, "bin", "python")); err != nil {
return packit.BuildResult{}, err
}
}
logger.Break()
logger.EnvironmentVariables(cpythonLayer)
return packit.BuildResult{
Layers: []packit.Layer{cpythonLayer},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
}