forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_builder.go
226 lines (186 loc) · 5.91 KB
/
create_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
package pack
import (
"context"
"fmt"
"github.com/Masterminds/semver"
"github.com/buildpacks/imgutil"
"github.com/pkg/errors"
pubbldr "github.com/buildpacks/pack/builder"
"github.com/buildpacks/pack/internal/builder"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/image"
"github.com/buildpacks/pack/internal/style"
)
type CreateBuilderOptions struct {
BuilderName string
Config pubbldr.Config
Publish bool
NoPull bool
}
func (c *Client) CreateBuilder(ctx context.Context, opts CreateBuilderOptions) error {
if err := validateBuilderConfig(opts.Config); err != nil {
return errors.Wrap(err, "invalid builder config")
}
if err := c.validateRunImageConfig(ctx, opts); err != nil {
return err
}
baseImage, err := c.imageFetcher.Fetch(ctx, opts.Config.Stack.BuildImage, !opts.Publish, !opts.NoPull)
if err != nil {
return errors.Wrap(err, "fetch build image")
}
c.logger.Debugf("Creating builder %s from build-image %s", style.Symbol(opts.BuilderName), style.Symbol(baseImage.Name()))
bldr, err := builder.New(baseImage, opts.BuilderName)
if err != nil {
return errors.Wrap(err, "invalid build-image")
}
bldr.SetDescription(opts.Config.Description)
if bldr.StackID != opts.Config.Stack.ID {
return fmt.Errorf(
"stack %s from builder config is incompatible with stack %s from build image",
style.Symbol(opts.Config.Stack.ID),
style.Symbol(bldr.StackID),
)
}
lifecycle, err := c.fetchLifecycle(ctx, opts.Config.Lifecycle)
if err != nil {
return errors.Wrap(err, "fetch lifecycle")
}
if err := bldr.SetLifecycle(lifecycle); err != nil {
return errors.Wrap(err, "setting lifecycle")
}
for _, b := range opts.Config.Buildpacks.Buildpacks() {
err := ensureBPSupport(b.URI)
if err != nil {
return err
}
blob, err := c.downloader.Download(ctx, b.URI)
if err != nil {
return errors.Wrapf(err, "downloading buildpack from %s", style.Symbol(b.URI))
}
fetchedBp, err := dist.BuildpackFromRootBlob(blob)
if err != nil {
return errors.Wrapf(err, "creating buildpack from %s", style.Symbol(b.URI))
}
err = validateBuildpack(fetchedBp, b.URI, b.ID, b.Version)
if err != nil {
return errors.Wrap(err, "invalid buildpack")
}
bldr.AddBuildpack(fetchedBp)
}
for _, pkg := range opts.Config.Buildpacks.Packages() {
mainBP, depBPs, err := extractPackagedBuildpacks(ctx, pkg.ImageName, c.imageFetcher, opts.Publish, opts.NoPull)
if err != nil {
return err
}
for _, bp := range append([]dist.Buildpack{mainBP}, depBPs...) {
bldr.AddBuildpack(bp)
}
}
bldr.SetOrder(opts.Config.Order)
bldr.SetStack(opts.Config.Stack)
return bldr.Save(c.logger)
}
func validateBuildpack(bp dist.Buildpack, source, expectedID, expectedBPVersion string) error {
if expectedID != "" && bp.Descriptor().Info.ID != expectedID {
return fmt.Errorf(
"buildpack from URI %s has ID %s which does not match ID %s from builder config",
style.Symbol(source),
style.Symbol(bp.Descriptor().Info.ID),
style.Symbol(expectedID),
)
}
if expectedBPVersion != "" && bp.Descriptor().Info.Version != expectedBPVersion {
return fmt.Errorf(
"buildpack from URI %s has version %s which does not match version %s from builder config",
style.Symbol(source),
style.Symbol(bp.Descriptor().Info.Version),
style.Symbol(expectedBPVersion),
)
}
return nil
}
func (c *Client) fetchLifecycle(ctx context.Context, config pubbldr.LifecycleConfig) (builder.Lifecycle, error) {
if config.Version != "" && config.URI != "" {
return nil, errors.Errorf(
"%s can only declare %s or %s, not both",
style.Symbol("lifecycle"), style.Symbol("version"), style.Symbol("uri"),
)
}
var uri string
switch {
case config.Version != "":
v, err := semver.NewVersion(config.Version)
if err != nil {
return nil, errors.Wrapf(err, "%s must be a valid semver", style.Symbol("lifecycle.version"))
}
uri = uriFromLifecycleVersion(*v)
case config.URI != "":
uri = config.URI
default:
uri = uriFromLifecycleVersion(*semver.MustParse(builder.DefaultLifecycleVersion))
}
b, err := c.downloader.Download(ctx, uri)
if err != nil {
return nil, errors.Wrap(err, "downloading lifecycle")
}
lifecycle, err := builder.NewLifecycle(b)
if err != nil {
return nil, errors.Wrap(err, "invalid lifecycle")
}
return lifecycle, nil
}
func uriFromLifecycleVersion(version semver.Version) string {
return fmt.Sprintf("https://github.com/buildpacks/lifecycle/releases/download/v%s/lifecycle-v%s+linux.x86-64.tgz", version.String(), version.String())
}
func validateBuilderConfig(conf pubbldr.Config) error {
if conf.Stack.ID == "" {
return errors.New("stack.id is required")
}
if conf.Stack.BuildImage == "" {
return errors.New("stack.build-image is required")
}
if conf.Stack.RunImage == "" {
return errors.New("stack.run-image is required")
}
return nil
}
func (c *Client) validateRunImageConfig(ctx context.Context, opts CreateBuilderOptions) error {
var runImages []imgutil.Image
for _, i := range append([]string{opts.Config.Stack.RunImage}, opts.Config.Stack.RunImageMirrors...) {
if !opts.Publish {
img, err := c.imageFetcher.Fetch(ctx, i, true, false)
if err != nil {
if errors.Cause(err) != image.ErrNotFound {
return err
}
} else {
runImages = append(runImages, img)
continue
}
}
img, err := c.imageFetcher.Fetch(ctx, i, false, false)
if err != nil {
if errors.Cause(err) != image.ErrNotFound {
return err
}
c.logger.Warnf("run image %s is not accessible", style.Symbol(i))
} else {
runImages = append(runImages, img)
}
}
for _, img := range runImages {
stackID, err := img.Label("io.buildpacks.stack.id")
if err != nil {
return err
}
if stackID != opts.Config.Stack.ID {
return fmt.Errorf(
"stack %s from builder config is incompatible with stack %s from run image %s",
style.Symbol(opts.Config.Stack.ID),
style.Symbol(stackID),
style.Symbol(img.Name()),
)
}
}
return nil
}