-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.go
115 lines (98 loc) · 3.48 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 passenger
import (
"fmt"
"path/filepath"
"time"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"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 PassengerfileConfigParser --output fakes/passengerfile_parser.go
//go:generate faux --interface SBOMGenerator --output fakes/sbom_generator.go
type DependencyManager interface {
Resolve(path, id, version, stack string) (postal.Dependency, error)
Deliver(dependency postal.Dependency, cnbPath, layerPath, platformPath string) error
}
type PassengerfileConfigParser interface {
ParsePort(path string, defaultPort int) (int, error)
}
type SBOMGenerator interface {
GenerateFromDependency(dependency postal.Dependency, dir string) (sbom.SBOM, error)
}
func Build(
dependencyManager DependencyManager,
passengerfileParser PassengerfileConfigParser,
sbomGenerator SBOMGenerator,
clock chronos.Clock,
logger scribe.Emitter,
) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
logger.Process("Executing build process")
dependency, err := dependencyManager.Resolve(filepath.Join(context.CNBPath, "buildpack.toml"), "curl", "*", context.Stack)
if err != nil {
return packit.BuildResult{}, err
}
logger.Debug.Process("Getting the layer associated with curl:")
curlLayer, err := context.Layers.Get("curl")
if err != nil {
return packit.BuildResult{}, err
}
logger.Debug.Subprocess(curlLayer.Path)
logger.Debug.Break()
curlLayer.Launch = true
logger.Subprocess("Installing %s %s", dependency.Name, dependency.Version)
duration, err := clock.Measure(func() error {
logger.Debug.Subprocess("Installation path: %s", curlLayer.Path)
logger.Debug.Subprocess("Dependency URI: %s", dependency.URI)
return dependencyManager.Deliver(dependency, context.CNBPath, curlLayer.Path, context.Platform.Path)
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
logger.GeneratingSBOM(curlLayer.Path)
var sbomContent sbom.SBOM
duration, err = clock.Measure(func() error {
sbomContent, err = sbomGenerator.GenerateFromDependency(dependency, curlLayer.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...)
curlLayer.SBOM, err = sbomContent.InFormats(context.BuildpackInfo.SBOMFormats...)
if err != nil {
return packit.BuildResult{}, err
}
passengerfilePath := filepath.Join(context.WorkingDir, "Passengerfile.json")
defaultPort := 3000
port, err := passengerfileParser.ParsePort(passengerfilePath, defaultPort)
if err != nil {
return packit.BuildResult{}, err
}
args := fmt.Sprintf(`bundle exec passenger start --port ${PORT:-%d}`, port)
processes := []packit.Process{
{
Type: "web",
Command: "bash",
Args: []string{"-c", args},
Default: true,
Direct: true,
},
}
logger.LaunchProcesses(processes)
return packit.BuildResult{
Layers: []packit.Layer{curlLayer},
Launch: packit.LaunchMetadata{
Processes: processes,
},
}, nil
}
}