-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.go
170 lines (135 loc) · 5.26 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
package pipenv
import (
"fmt"
"path/filepath"
"strings"
"time"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/draft"
"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 InstallProcess --output fakes/install_process.go
//go:generate faux --interface SitePackageProcess --output fakes/site_package_process.go
//go:generate faux --interface SBOMGenerator --output fakes/sbom_generator.go
// DependencyManager defines the interface for picking the best matching
// dependency and installing it.
type DependencyManager interface {
Resolve(path, id, version, stack string) (postal.Dependency, error)
GenerateBillOfMaterials(dependencies ...postal.Dependency) []packit.BOMEntry
}
// InstallProcess defines the interface for installing the pipenv dependency into a layer.
type InstallProcess interface {
Execute(version, destLayerPath string) error
}
// SitePackageProcess defines the interface for looking up site packages within a layer.
type SitePackageProcess interface {
Execute(targetLayerPath string) (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 pipenv 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(
dependencyManager DependencyManager,
installProcess InstallProcess,
siteProcess SitePackageProcess,
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)
planner := draft.NewPlanner()
logger.Process("Resolving Pipenv version")
entry, sortedEntries := planner.Resolve(Pipenv, context.Plan.Entries, Priorities)
logger.Candidates(sortedEntries)
version, _ := entry.Metadata["version"].(string)
dependency, err := dependencyManager.Resolve(filepath.Join(context.CNBPath, "buildpack.toml"), entry.Name, version, context.Stack)
if err != nil {
return packit.BuildResult{}, err
}
logger.SelectedDependency(entry, dependency, clock.Now())
legacySBOM := dependencyManager.GenerateBillOfMaterials(dependency)
launch, build := planner.MergeLayerTypes(Pipenv, context.Plan.Entries)
var launchMetadata packit.LaunchMetadata
if launch {
launchMetadata.BOM = legacySBOM
}
var buildMetadata packit.BuildMetadata
if build {
buildMetadata.BOM = legacySBOM
}
pipenvLayer, err := context.Layers.Get(Pipenv)
if err != nil {
return packit.BuildResult{}, err
}
cachedChecksum, ok := pipenvLayer.Metadata[DependencyChecksumKey].(string)
if ok && cachedChecksum == dependency.Checksum {
logger.Process("Reusing cached layer %s", pipenvLayer.Path)
pipenvLayer.Launch, pipenvLayer.Build, pipenvLayer.Cache = launch, build, build
return packit.BuildResult{
Layers: []packit.Layer{pipenvLayer},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
pipenvLayer, err = pipenvLayer.Reset()
if err != nil {
return packit.BuildResult{}, err
}
pipenvLayer.Launch, pipenvLayer.Build, pipenvLayer.Cache = launch, build, build
logger.Process("Executing build process")
logger.Subprocess(fmt.Sprintf("Installing Pipenv %s", dependency.Version))
duration, err := clock.Measure(func() error {
return installProcess.Execute(dependency.Version, pipenvLayer.Path)
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
logger.GeneratingSBOM(pipenvLayer.Path)
var sbomContent sbom.SBOM
duration, err = clock.Measure(func() error {
sbomContent, err = sbomGenerator.GenerateFromDependency(dependency, pipenvLayer.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...)
pipenvLayer.SBOM, err = sbomContent.InFormats(context.BuildpackInfo.SBOMFormats...)
if err != nil {
return packit.BuildResult{}, err
}
pipenvLayer.Metadata = map[string]interface{}{
DependencyChecksumKey: dependency.Checksum,
}
// Look up the site packages path and prepend it onto $PYTHONPATH
sitePackagesPath, err := siteProcess.Execute(pipenvLayer.Path)
if err != nil {
return packit.BuildResult{}, err
}
if sitePackagesPath == "" {
return packit.BuildResult{}, fmt.Errorf("pipenv installation failed: site packages are missing from the pipenv layer")
}
pipenvLayer.SharedEnv.Prepend("PYTHONPATH", strings.TrimRight(sitePackagesPath, "\n"), ":")
logger.EnvironmentVariables(pipenvLayer)
return packit.BuildResult{
Layers: []packit.Layer{pipenvLayer},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
}