forked from buildpacks/lifecycle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
253 lines (225 loc) · 5.27 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package lifecycle
import (
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"github.com/BurntSushi/toml"
"github.com/buildpacks/lifecycle/launch"
)
type Builder struct {
AppDir string
LayersDir string
PlatformDir string
BuildpacksDir string
Env BuildEnv
Group BuildpackGroup
Plan BuildPlan
Out, Err *log.Logger
}
type BuildEnv interface {
AddRootDir(baseDir string) error
AddEnvDir(envDir string) error
WithPlatform(platformDir string) ([]string, error)
List() []string
}
type Slice struct {
Paths []string `tom:"paths"`
}
type LaunchTOML struct {
Processes []launch.Process `toml:"processes"`
Slices []Slice `toml:"slices"`
}
type BOMEntry struct {
Require
Buildpack Buildpack `toml:"buildpack" json:"buildpack"`
}
type buildpackPlan struct {
Entries []Require `toml:"entries"`
}
func (b *Builder) Build() (*BuildMetadata, error) {
platformDir, err := filepath.Abs(b.PlatformDir)
if err != nil {
return nil, err
}
layersDir, err := filepath.Abs(b.LayersDir)
if err != nil {
return nil, err
}
appDir, err := filepath.Abs(b.AppDir)
if err != nil {
return nil, err
}
planDir, err := ioutil.TempDir("", "plan.")
if err != nil {
return nil, err
}
defer os.RemoveAll(planDir)
procMap := processMap{}
plan := b.Plan
var bom []BOMEntry
var slices []Slice
for _, bp := range b.Group.Group {
bpInfo, err := bp.lookup(b.BuildpacksDir)
if err != nil {
return nil, err
}
bpDirName := launch.EscapeID(bp.ID)
bpLayersDir := filepath.Join(layersDir, bpDirName)
bpPlanDir := filepath.Join(planDir, bpDirName)
if err := os.MkdirAll(bpLayersDir, 0777); err != nil {
return nil, err
}
if err := os.MkdirAll(bpPlanDir, 0777); err != nil {
return nil, err
}
bpPlanPath := filepath.Join(bpPlanDir, "plan.toml")
if err := WriteTOML(bpPlanPath, plan.find(bp)); err != nil {
return nil, err
}
cmd := exec.Command(
filepath.Join(bpInfo.Path, "bin", "build"),
bpLayersDir,
platformDir,
bpPlanPath,
)
cmd.Dir = appDir
cmd.Stdout = b.Out.Writer()
cmd.Stderr = b.Err.Writer()
if bpInfo.Buildpack.ClearEnv {
cmd.Env = b.Env.List()
} else {
cmd.Env, err = b.Env.WithPlatform(platformDir)
if err != nil {
return nil, err
}
}
if err := cmd.Run(); err != nil {
return nil, err
}
if err := setupEnv(b.Env, bpLayersDir); err != nil {
return nil, err
}
var bpPlanOut buildpackPlan
if _, err := toml.DecodeFile(bpPlanPath, &bpPlanOut); err != nil {
return nil, err
}
var bpBOM []BOMEntry
plan, bpBOM = plan.filter(bp, bpPlanOut)
bom = append(bom, bpBOM...)
var launch LaunchTOML
tomlPath := filepath.Join(bpLayersDir, "launch.toml")
if _, err := toml.DecodeFile(tomlPath, &launch); os.IsNotExist(err) {
continue
} else if err != nil {
return nil, err
}
procMap.add(launch.Processes)
slices = append(slices, launch.Slices...)
}
return &BuildMetadata{
Processes: procMap.list(),
Buildpacks: b.Group.Group,
BOM: bom,
Slices: slices,
}, nil
}
func (p BuildPlan) find(bp Buildpack) buildpackPlan {
var out []Require
for _, entry := range p.Entries {
for _, provider := range entry.Providers {
if provider == bp {
out = append(out, entry.Requires...)
break
}
}
}
return buildpackPlan{Entries: out}
}
// TODO: ensure at least one claimed entry of each name is provided by the BP
func (p BuildPlan) filter(bp Buildpack, plan buildpackPlan) (BuildPlan, []BOMEntry) {
var out []BuildPlanEntry
for _, entry := range p.Entries {
if !plan.has(entry) {
out = append(out, entry)
}
}
var bom []BOMEntry
for _, entry := range plan.Entries {
bom = append(bom, BOMEntry{Require: entry, Buildpack: bp})
}
return BuildPlan{Entries: out}, bom
}
func (p buildpackPlan) has(entry BuildPlanEntry) bool {
for _, buildEntry := range p.Entries {
for _, req := range entry.Requires {
if req.Name == buildEntry.Name {
return true
}
}
}
return false
}
func setupEnv(env BuildEnv, layersDir string) error {
if err := eachDir(layersDir, func(path string) error {
if !isBuild(path + ".toml") {
return nil
}
return env.AddRootDir(path)
}); err != nil {
return err
}
return eachDir(layersDir, func(path string) error {
if !isBuild(path + ".toml") {
return nil
}
if err := env.AddEnvDir(filepath.Join(path, "env")); err != nil {
return err
}
return env.AddEnvDir(filepath.Join(path, "env.build"))
})
}
func eachDir(dir string, fn func(path string) error) error {
files, err := ioutil.ReadDir(dir)
if os.IsNotExist(err) {
return nil
} else if err != nil {
return err
}
for _, f := range files {
if !f.IsDir() {
continue
}
if err := fn(filepath.Join(dir, f.Name())); err != nil {
return err
}
}
return nil
}
func isBuild(path string) bool {
var layerTOML struct {
Build bool `toml:"build"`
}
_, err := toml.DecodeFile(path, &layerTOML)
return err == nil && layerTOML.Build
}
type processMap map[string]launch.Process
func (m processMap) add(l []launch.Process) {
for _, proc := range l {
m[proc.Type] = proc
}
}
func (m processMap) list() []launch.Process {
var keys []string
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
procs := []launch.Process{}
for _, key := range keys {
procs = append(procs, m[key])
}
return procs
}