forked from paketo-buildpacks/miniconda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
162 lines (132 loc) · 5.03 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
package miniconda
import (
"path/filepath"
"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 Runner --output fakes/runner.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)
Deliver(dependency postal.Dependency, cnbPath, destinationPath, platformPath string) error
GenerateBillOfMaterials(dependencies ...postal.Dependency) []packit.BOMEntry
}
// Runner defines the interface for invoking the miniconda script downloaded as a dependency.
type Runner interface {
Run(runPath, layerPath 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 miniconda dependency to download, download it
// into a layer, run the miniconda script to install conda into a separate
// 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,
runner Runner,
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()
dependency, err := dependencyManager.Resolve(filepath.Join(context.CNBPath, "buildpack.toml"), "miniconda3", "*", context.Stack)
if err != nil {
return packit.BuildResult{}, err
}
legacySBOM := dependencyManager.GenerateBillOfMaterials(dependency)
condaLayer, err := context.Layers.Get("conda")
if err != nil {
return packit.BuildResult{}, err
}
launch, build := planner.MergeLayerTypes("conda", context.Plan.Entries)
var buildMetadata = packit.BuildMetadata{}
var launchMetadata = packit.LaunchMetadata{}
if build {
buildMetadata = packit.BuildMetadata{BOM: legacySBOM}
}
if launch {
launchMetadata = packit.LaunchMetadata{BOM: legacySBOM}
}
cachedSHA, ok := condaLayer.Metadata[DepKey].(string)
if ok && cachedSHA == dependency.SHA256 {
logger.Process("Reusing cached layer %s", condaLayer.Path)
logger.Break()
condaLayer.Launch, condaLayer.Build, condaLayer.Cache = launch, build, build
return packit.BuildResult{
Layers: []packit.Layer{condaLayer},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
condaLayer, err = condaLayer.Reset()
if err != nil {
return packit.BuildResult{}, err
}
condaLayer.Launch, condaLayer.Build, condaLayer.Cache = launch, build, build
// This temporary layer is created because the path to a deterministic and
// easier to make assertions about during testing. Because this layer has
// no type set to true the lifecycle will ensure that this layer is
// removed.
minicondaScriptTempLayer, err := context.Layers.Get("miniconda-script-temp-layer")
if err != nil {
return packit.BuildResult{}, err
}
minicondaScriptTempLayer, err = minicondaScriptTempLayer.Reset()
if err != nil {
return packit.BuildResult{}, err
}
logger.Process("Executing build process")
logger.Subprocess("Installing Miniconda %s", dependency.Version)
duration, err := clock.Measure(func() error {
err := dependencyManager.Deliver(dependency, context.CNBPath, minicondaScriptTempLayer.Path, context.Platform.Path)
if err != nil {
return err
}
scriptPath := filepath.Join(minicondaScriptTempLayer.Path, dependency.Name)
return runner.Run(scriptPath, condaLayer.Path)
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
condaLayer.Metadata = map[string]interface{}{
DepKey: dependency.SHA256,
}
logger.GeneratingSBOM(condaLayer.Path)
var sbomContent sbom.SBOM
duration, err = clock.Measure(func() error {
sbomContent, err = sbomGenerator.GenerateFromDependency(dependency, condaLayer.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...)
condaLayer.SBOM, err = sbomContent.InFormats(context.BuildpackInfo.SBOMFormats...)
if err != nil {
return packit.BuildResult{}, err
}
return packit.BuildResult{
Layers: []packit.Layer{condaLayer},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
}