forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
359 lines (309 loc) · 9.68 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package pack
import (
"context"
"fmt"
"math/rand"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/buildpack/imgutil"
"github.com/docker/docker/api/types"
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"
"github.com/buildpack/pack/api"
"github.com/buildpack/pack/build"
"github.com/buildpack/pack/builder"
"github.com/buildpack/pack/cmd"
"github.com/buildpack/pack/dist"
"github.com/buildpack/pack/internal/archive"
"github.com/buildpack/pack/internal/paths"
"github.com/buildpack/pack/style"
)
type Lifecycle interface {
Execute(ctx context.Context, opts build.LifecycleOptions) error
}
type BuildOptions struct {
Image string // required
Builder string // required
AppPath string // defaults to current working directory
RunImage string // defaults to the best mirror from the builder metadata or AdditionalMirrors
AdditionalMirrors map[string][]string // only considered if RunImage is not provided
Env map[string]string
Publish bool
NoPull bool
ClearCache bool
Buildpacks []string
ProxyConfig *ProxyConfig // defaults to environment proxy vars
}
type ProxyConfig struct {
HTTPProxy string
HTTPSProxy string
NoProxy string
}
func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
imageRef, err := c.parseTagReference(opts.Image)
if err != nil {
return errors.Wrapf(err, "invalid image name '%s'", opts.Image)
}
appPath, err := c.processAppPath(opts.AppPath)
if err != nil {
return errors.Wrapf(err, "invalid app path '%s'", opts.AppPath)
}
proxyConfig := c.processProxyConfig(opts.ProxyConfig)
builderRef, err := c.processBuilderName(opts.Builder)
if err != nil {
return errors.Wrapf(err, "invalid builder '%s'", opts.Builder)
}
rawBuilderImage, err := c.imageFetcher.Fetch(ctx, builderRef.Name(), true, !opts.NoPull)
if err != nil {
return errors.Wrapf(err, "failed to fetch builder image '%s'", builderRef.Name())
}
bldr, err := c.processBuilderImage(rawBuilderImage)
if err != nil {
return errors.Wrapf(err, "invalid builder '%s'", opts.Builder)
}
runImage := c.resolveRunImage(opts.RunImage, imageRef.Context().RegistryStr(), bldr.GetStackInfo(), opts.AdditionalMirrors)
if _, err := c.validateRunImage(ctx, runImage, opts.NoPull, opts.Publish, bldr.StackID); err != nil {
return errors.Wrapf(err, "invalid run-image '%s'", runImage)
}
fetchedBps, group, err := c.processBuildpacks(ctx, opts.Buildpacks)
if err != nil {
return errors.Wrap(err, "invalid buildpack")
}
ephemeralBuilder, err := c.createEphemeralBuilder(rawBuilderImage, opts.Env, group, fetchedBps)
if err != nil {
return err
}
defer c.docker.ImageRemove(context.Background(), ephemeralBuilder.Name(), types.ImageRemoveOptions{Force: true})
descriptor := ephemeralBuilder.GetLifecycleDescriptor()
if descriptor.Info.Version == nil {
c.logger.Warnf("lifecycle version unknown, assuming %s", style.Symbol(builder.AssumedLifecycleVersion))
} else {
c.logger.Debugf("Executing lifecycle version %s", style.Symbol(descriptor.Info.Version.String()))
}
lcPlatformAPIVersion := api.MustParse(builder.AssumedPlatformAPIVersion)
if descriptor.API.PlatformVersion != nil {
lcPlatformAPIVersion = descriptor.API.PlatformVersion
}
if !api.MustParse(build.PlatformAPIVersion).SupportsVersion(lcPlatformAPIVersion) {
return errors.Errorf(
"pack %s (Platform API version %s) is incompatible with builder %s (Platform API version %s)",
cmd.Version,
build.PlatformAPIVersion,
style.Symbol(opts.Builder),
lcPlatformAPIVersion,
)
}
return c.lifecycle.Execute(ctx, build.LifecycleOptions{
AppPath: appPath,
Image: imageRef,
Builder: ephemeralBuilder,
RunImage: runImage,
ClearCache: opts.ClearCache,
Publish: opts.Publish,
HTTPProxy: proxyConfig.HTTPProxy,
HTTPSProxy: proxyConfig.HTTPSProxy,
NoProxy: proxyConfig.NoProxy,
})
}
func (c *Client) processBuilderName(builderName string) (name.Reference, error) {
if builderName == "" {
return nil, errors.New("builder is a required parameter if the client has no default builder")
}
return name.ParseReference(builderName, name.WeakValidation)
}
func (c *Client) processBuilderImage(img imgutil.Image) (*builder.Builder, error) {
bldr, err := builder.GetBuilder(img)
if err != nil {
return nil, err
}
if bldr.GetStackInfo().RunImage.Image == "" {
return nil, errors.New("builder metadata is missing runImage")
}
return bldr, nil
}
func (c *Client) validateRunImage(context context.Context, name string, noPull bool, publish bool, expectedStack string) (imgutil.Image, error) {
if name == "" {
return nil, errors.New("run image must be specified")
}
img, err := c.imageFetcher.Fetch(context, name, !publish, !noPull)
if err != nil {
return nil, err
}
stackID, err := img.Label("io.buildpacks.stack.id")
if err != nil {
return nil, err
}
if stackID != expectedStack {
return nil, fmt.Errorf("run-image stack id '%s' does not match builder stack '%s'", stackID, expectedStack)
}
return img, nil
}
func (c *Client) processAppPath(appPath string) (string, error) {
var (
resolvedAppPath string
err error
)
if appPath == "" {
if appPath, err = os.Getwd(); err != nil {
return "", errors.Wrap(err, "get working dir")
}
}
if resolvedAppPath, err = filepath.EvalSymlinks(appPath); err != nil {
return "", errors.Wrap(err, "evaluate symlink")
}
if resolvedAppPath, err = filepath.Abs(resolvedAppPath); err != nil {
return "", errors.Wrap(err, "resolve absolute path")
}
fi, err := os.Stat(resolvedAppPath)
if err != nil {
return "", errors.Wrap(err, "stat file")
}
if !fi.IsDir() {
fh, err := os.Open(resolvedAppPath)
if err != nil {
return "", errors.Wrap(err, "read file")
}
defer fh.Close()
isZip, err := archive.IsZip(fh)
if err != nil {
return "", errors.Wrap(err, "check zip")
}
if !isZip {
return "", errors.New("app path must be a directory or zip")
}
}
return resolvedAppPath, nil
}
func (c *Client) processProxyConfig(config *ProxyConfig) ProxyConfig {
var (
httpProxy, httpsProxy, noProxy string
ok bool
)
if config != nil {
return *config
}
if httpProxy, ok = os.LookupEnv("HTTP_PROXY"); !ok {
httpProxy = os.Getenv("http_proxy")
}
if httpsProxy, ok = os.LookupEnv("HTTPS_PROXY"); !ok {
httpsProxy = os.Getenv("https_proxy")
}
if noProxy, ok = os.LookupEnv("NO_PROXY"); !ok {
noProxy = os.Getenv("no_proxy")
}
return ProxyConfig{
HTTPProxy: httpProxy,
HTTPSProxy: httpsProxy,
NoProxy: noProxy,
}
}
func (c *Client) processBuildpacks(ctx context.Context, buildpacks []string) ([]dist.Buildpack, dist.OrderEntry, error) {
group := dist.OrderEntry{Group: []dist.BuildpackRef{}}
var bps []dist.Buildpack
for _, bp := range buildpacks {
if isBuildpackID(bp) {
id, version := c.parseBuildpack(bp)
group.Group = append(group.Group, dist.BuildpackRef{
BuildpackInfo: dist.BuildpackInfo{
ID: id,
Version: version,
},
})
} else {
err := ensureBPSupport(bp)
if err != nil {
return nil, dist.OrderEntry{}, err
}
c.logger.Debugf("fetching buildpack from %s", style.Symbol(bp))
blob, err := c.downloader.Download(ctx, bp)
if err != nil {
return nil, dist.OrderEntry{}, errors.Wrapf(err, "downloading buildpack from %s", style.Symbol(bp))
}
fetchedBP, err := dist.NewBuildpack(blob)
if err != nil {
return nil, dist.OrderEntry{}, errors.Wrapf(err, "creating buildpack from %s", style.Symbol(bp))
}
bps = append(bps, fetchedBP)
group.Group = append(group.Group, dist.BuildpackRef{
BuildpackInfo: fetchedBP.Descriptor().Info,
})
}
}
return bps, group, nil
}
func isBuildpackID(bp string) bool {
if !paths.IsURI(bp) {
if _, err := os.Stat(bp); err != nil {
return true
}
}
return false
}
func ensureBPSupport(bpPath string) (err error) {
p := bpPath
if paths.IsURI(bpPath) {
var u *url.URL
u, err = url.Parse(bpPath)
if err != nil {
return err
}
if u.Scheme == "file" {
p, err = paths.URIToFilePath(bpPath)
if err != nil {
return err
}
}
}
if runtime.GOOS == "windows" && !paths.IsURI(p) {
isDir, err := paths.IsDir(p)
if err != nil {
return err
}
if isDir {
return fmt.Errorf("buildpack %s: directory-based buildpacks are not currently supported on Windows", style.Symbol(bpPath))
}
}
return nil
}
func (c *Client) parseBuildpack(bp string) (string, string) {
parts := strings.Split(bp, "@")
if len(parts) == 2 {
if parts[1] == "latest" {
c.logger.Warn("@latest syntax is deprecated, will not work in future releases")
return parts[0], ""
}
return parts[0], parts[1]
}
return parts[0], ""
}
func (c *Client) createEphemeralBuilder(rawBuilderImage imgutil.Image, env map[string]string, group dist.OrderEntry, buildpacks []dist.Buildpack) (*builder.Builder, error) {
origBuilderName := rawBuilderImage.Name()
bldr, err := builder.New(rawBuilderImage, fmt.Sprintf("pack.local/builder/%x:latest", randString(10)))
if err != nil {
return nil, errors.Wrapf(err, "invalid builder %s", style.Symbol(origBuilderName))
}
bldr.SetEnv(env)
for _, bp := range buildpacks {
bpInfo := bp.Descriptor().Info
c.logger.Debugf("adding buildpack %s version %s to builder", style.Symbol(bpInfo.ID), style.Symbol(bpInfo.Version))
bldr.AddBuildpack(bp)
}
if len(group.Group) > 0 {
c.logger.Debug("setting custom order")
bldr.SetOrder([]dist.OrderEntry{group})
}
if err := bldr.Save(c.logger); err != nil {
return nil, err
}
return bldr, nil
}
func randString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = 'a' + byte(rand.Intn(26))
}
return string(b)
}