-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.go
115 lines (94 loc) · 3.34 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
package condaenvupdate
import (
"os"
"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/fs"
"github.com/paketo-buildpacks/packit/v2/sbom"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:generate faux --interface Runner --output fakes/runner.go
//go:generate faux --interface SBOMGenerator --output fakes/sbom_generator.go
// Runner defines the interface for setting up the conda environment.
type Runner interface {
Execute(condaEnvPath string, condaCachePath string, workingDir string) error
ShouldRun(workingDir string, metadata map[string]interface{}) (bool, string, error)
}
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 updates the conda environment and stores the result in a layer. It may
// reuse the environment layer from a previous build, depending on conditions
// determined by the runner.
func Build(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)
condaLayer, err := context.Layers.Get(CondaEnvLayer)
if err != nil {
return packit.BuildResult{}, err
}
condaCacheLayer, err := context.Layers.Get(CondaEnvCache)
if err != nil {
return packit.BuildResult{}, err
}
run, sha, err := runner.ShouldRun(context.WorkingDir, condaLayer.Metadata)
if err != nil {
return packit.BuildResult{}, err
}
if run {
condaLayer, err = condaLayer.Reset()
if err != nil {
return packit.BuildResult{}, err
}
logger.Process("Executing build process")
duration, err := clock.Measure(func() error {
return runner.Execute(condaLayer.Path, condaCacheLayer.Path, context.WorkingDir)
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
logger.GeneratingSBOM(condaLayer.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...)
condaLayer.SBOM, err = sbomContent.InFormats(context.BuildpackInfo.SBOMFormats...)
if err != nil {
return packit.BuildResult{}, err
}
condaLayer.Metadata = map[string]interface{}{
"lockfile-sha": sha,
}
} else {
logger.Process("Reusing cached layer %s", condaLayer.Path)
logger.Break()
}
planner := draft.NewPlanner()
condaLayer.Launch, condaLayer.Build = planner.MergeLayerTypes(CondaEnvPlanEntry, context.Plan.Entries)
condaLayer.Cache = condaLayer.Build
condaCacheLayer.Cache = true
layers := []packit.Layer{condaLayer}
if _, err := os.Stat(condaCacheLayer.Path); err == nil {
if !fs.IsEmptyDir(condaCacheLayer.Path) {
layers = append(layers, condaCacheLayer)
}
}
return packit.BuildResult{
Layers: layers,
}, nil
}
}