forked from buildpacks/lifecycle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
202 lines (172 loc) · 5.26 KB
/
builder.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
package lifecycle
import (
"fmt"
"io"
"os"
"path/filepath"
"sort"
"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/env"
"github.com/buildpacks/lifecycle/launch"
"github.com/buildpacks/lifecycle/layers"
"github.com/buildpacks/lifecycle/platform"
)
type BuildEnv interface {
AddRootDir(baseDir string) error
AddEnvDir(envDir string, defaultAction env.ActionType) error
WithPlatform(platformDir string) ([]string, error)
List() []string
}
type BuildpackStore interface {
Lookup(bpID, bpVersion string) (buildpack.Buildpack, error)
}
type Buildpack interface {
Build(bpPlan buildpack.Plan, config buildpack.BuildConfig, bpEnv buildpack.BuildEnv) (buildpack.BuildResult, error)
ConfigFile() *buildpack.Descriptor
Detect(config *buildpack.DetectConfig, bpEnv buildpack.BuildEnv) buildpack.DetectRun
}
type Builder struct {
AppDir string
LayersDir string
PlatformDir string
Platform Platform
PlatformAPI *api.Version // TODO: derive from platform
Group buildpack.Group
Plan platform.BuildPlan
Out, Err io.Writer
Logger Logger
BuildpackStore BuildpackStore
}
func (b *Builder) Build() (*platform.BuildMetadata, error) {
b.Logger.Debug("Starting build")
config, err := b.BuildConfig()
if err != nil {
return nil, err
}
processMap := newProcessMap()
plan := b.Plan
var bom []buildpack.BOMEntry
var slices []layers.Slice
var labels []buildpack.Label
bpEnv := env.NewBuildEnv(os.Environ())
for _, bp := range b.Group.Group {
b.Logger.Debugf("Running build for buildpack %s", bp)
b.Logger.Debug("Looking up buildpack")
bpTOML, err := b.BuildpackStore.Lookup(bp.ID, bp.Version)
if err != nil {
return nil, err
}
b.Logger.Debug("Finding plan")
bpPlan := plan.Find(bp.ID)
br, err := bpTOML.Build(bpPlan, config, bpEnv)
if err != nil {
return nil, err
}
b.Logger.Debug("Updating buildpack processes")
updateDefaultProcesses(br.Processes, api.MustParse(bp.API), b.PlatformAPI)
bom = append(bom, br.BOM...)
labels = append(labels, br.Labels...)
plan = plan.Filter(br.MetRequires)
b.Logger.Debug("Updating process list")
warning := processMap.add(br.Processes)
if warning != "" {
b.Logger.Warn(warning)
}
slices = append(slices, br.Slices...)
b.Logger.Debugf("Finished running build for buildpack %s", bp)
}
if b.PlatformAPI.LessThan("0.4") {
config.Logger.Debug("Updating BOM entries")
for i := range bom {
bom[i].ConvertMetadataToVersion()
}
}
b.Logger.Debug("Listing processes")
procList := processMap.list()
b.Logger.Debug("Finished build")
return &platform.BuildMetadata{
BOM: bom,
Buildpacks: b.Group.Group,
Labels: labels,
Processes: procList,
Slices: slices,
BuildpackDefaultProcessType: processMap.defaultType,
}, nil
}
// we set default = true for web processes when platformAPI >= 0.6 and buildpackAPI < 0.6
func updateDefaultProcesses(processes []launch.Process, buildpackAPI *api.Version, platformAPI *api.Version) {
if platformAPI.LessThan("0.6") || buildpackAPI.AtLeast("0.6") {
return
}
for i := range processes {
if processes[i].Type == "web" {
processes[i].Default = true
}
}
}
func (b *Builder) BuildConfig() (buildpack.BuildConfig, error) {
appDir, err := filepath.Abs(b.AppDir)
if err != nil {
return buildpack.BuildConfig{}, err
}
platformDir, err := filepath.Abs(b.PlatformDir)
if err != nil {
return buildpack.BuildConfig{}, err
}
layersDir, err := filepath.Abs(b.LayersDir)
if err != nil {
return buildpack.BuildConfig{}, err
}
return buildpack.BuildConfig{
AppDir: appDir,
PlatformDir: platformDir,
LayersDir: layersDir,
Out: b.Out,
Err: b.Err,
Logger: b.Logger,
}, nil
}
type processMap struct {
typeToProcess map[string]launch.Process
defaultType string
}
func newProcessMap() processMap {
return processMap{
typeToProcess: make(map[string]launch.Process),
defaultType: "",
}
}
// This function adds the processes from listToAdd to processMap
// it sets m.defaultType to the last default process
// if a non-default process overrides a default process, it returns a warning and unset m.defaultType
func (m *processMap) add(listToAdd []launch.Process) string {
warning := ""
for _, procToAdd := range listToAdd {
if procToAdd.Default {
m.defaultType = procToAdd.Type
warning = ""
} else if procToAdd.Type == m.defaultType {
// non-default process overrides a default process
m.defaultType = ""
warning = fmt.Sprintf("Warning: redefining the following default process type with a process not marked as default: %s\n", procToAdd.Type)
}
m.typeToProcess[procToAdd.Type] = procToAdd
}
return warning
}
// list returns a sorted array of processes.
// The array is sorted based on the process types.
// The list is sorted for reproducibility.
func (m processMap) list() []launch.Process {
var keys []string
for proc := range m.typeToProcess {
keys = append(keys, proc)
}
sort.Strings(keys)
result := []launch.Process{}
for _, key := range keys {
result = append(result, m.typeToProcess[key].NoDefault()) // we set the default to false so it won't be part of metadata.toml
}
return result
}