forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_buildpack.go
219 lines (177 loc) · 6.52 KB
/
package_buildpack.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
package pack
import (
"context"
"github.com/pkg/errors"
pubbldpkg "github.com/buildpacks/pack/buildpackage"
"github.com/buildpacks/pack/config"
"github.com/buildpacks/pack/internal/blob"
"github.com/buildpacks/pack/internal/buildpack"
"github.com/buildpacks/pack/internal/buildpackage"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/layer"
"github.com/buildpacks/pack/internal/paths"
"github.com/buildpacks/pack/internal/style"
)
const (
// Packaging indicator that format of inputs/outputs will be an OCI image on the registry.
FormatImage = "image"
// Packaging indicator that format of output will be a file on the host filesystem.
FormatFile = "file"
// CNBExtension is the file extension for a cloud native buildpack tar archive
CNBExtension = ".cnb"
)
// PackageBuildpackOptions is a configuration object used to define
// the behavior of PackageBuildpack.
type PackageBuildpackOptions struct {
// The base director to resolve relative assest from
RelativeBaseDir string
// The name of the output buildpack artifact.
Name string
// Type of output format, The options are the either the const FormatImage, or FormatFile.
Format string
// Defines the Buildpacks configuration.
Config pubbldpkg.Config
// Push resulting builder image up to a registry
// specified in the Name variable.
Publish bool
// Strategy for updating images before packaging.
PullPolicy config.PullPolicy
// Name of the buildpack registry. Used to
// add buildpacks to a package.
Registry string
}
// PackageBuildpack packages buildpack(s) into either an image or file.
func (c *Client) PackageBuildpack(ctx context.Context, opts PackageBuildpackOptions) error {
if opts.Format == "" {
opts.Format = FormatImage
}
if opts.Config.Platform.OS == "windows" && !c.experimental {
return NewExperimentError("Windows buildpackage support is currently experimental.")
}
err := c.validateOSPlatform(ctx, opts.Config.Platform.OS, opts.Publish, opts.Format)
if err != nil {
return err
}
writerFactory, err := layer.NewWriterFactory(opts.Config.Platform.OS)
if err != nil {
return errors.Wrap(err, "creating layer writer factory")
}
packageBuilder := buildpackage.NewBuilder(c.imageFactory)
bpURI := opts.Config.Buildpack.URI
if bpURI == "" {
return errors.New("buildpack URI must be provided")
}
mainBlob, err := c.downloadBuildpackFromURI(ctx, bpURI, opts.RelativeBaseDir)
if err != nil {
return err
}
bp, err := dist.BuildpackFromRootBlob(mainBlob, writerFactory)
if err != nil {
return errors.Wrapf(err, "creating buildpack from %s", style.Symbol(bpURI))
}
packageBuilder.SetBuildpack(bp)
for _, dep := range opts.Config.Dependencies {
var depBPs []dist.Buildpack
if dep.ImageName != "" {
c.logger.Warn("The 'image' key is deprecated. Use 'uri=\"docker://...\"' instead.")
mainBP, deps, err := extractPackagedBuildpacks(ctx, dep.ImageName, c.imageFetcher, opts.Publish, opts.PullPolicy)
if err != nil {
return err
}
depBPs = append([]dist.Buildpack{mainBP}, deps...)
} else if dep.URI != "" {
locatorType, err := buildpack.GetLocatorType(dep.URI, opts.RelativeBaseDir, nil)
if err != nil {
return err
}
switch locatorType {
case buildpack.URILocator:
depBlob, err := c.downloadBuildpackFromURI(ctx, dep.URI, opts.RelativeBaseDir)
if err != nil {
return err
}
isOCILayout, err := buildpackage.IsOCILayoutBlob(depBlob)
if err != nil {
return errors.Wrap(err, "inspecting buildpack blob")
}
if isOCILayout {
mainBP, deps, err := buildpackage.BuildpacksFromOCILayoutBlob(depBlob)
if err != nil {
return errors.Wrapf(err, "extracting buildpacks from %s", style.Symbol(dep.URI))
}
depBPs = append([]dist.Buildpack{mainBP}, deps...)
} else {
depBP, err := dist.BuildpackFromRootBlob(depBlob, writerFactory)
if err != nil {
return errors.Wrapf(err, "creating buildpack from %s", style.Symbol(dep.URI))
}
depBPs = []dist.Buildpack{depBP}
}
case buildpack.PackageLocator:
imageName := buildpack.ParsePackageLocator(dep.URI)
c.logger.Debugf("Downloading buildpack from image: %s", style.Symbol(imageName))
mainBP, deps, err := extractPackagedBuildpacks(ctx, imageName, c.imageFetcher, opts.Publish, opts.PullPolicy)
if err != nil {
return err
}
depBPs = append([]dist.Buildpack{mainBP}, deps...)
case buildpack.RegistryLocator:
registryCache, err := c.getRegistry(c.logger, opts.Registry)
if err != nil {
return errors.Wrapf(err, "invalid registry '%s'", opts.Registry)
}
registryBp, err := registryCache.LocateBuildpack(dep.URI)
if err != nil {
return errors.Wrapf(err, "locating in registry %s", style.Symbol(dep.URI))
}
mainBP, deps, err := extractPackagedBuildpacks(ctx, registryBp.Address, c.imageFetcher, opts.Publish, opts.PullPolicy)
if err != nil {
return errors.Wrapf(err, "extracting from registry %s", style.Symbol(dep.URI))
}
depBPs = append([]dist.Buildpack{mainBP}, deps...)
case buildpack.InvalidLocator:
return errors.Errorf("invalid locator %s", style.Symbol(dep.URI))
default:
return errors.Errorf("unsupported locator type %s", style.Symbol(locatorType.String()))
}
}
for _, depBP := range depBPs {
packageBuilder.AddDependency(depBP)
}
}
switch opts.Format {
case FormatFile:
return packageBuilder.SaveAsFile(opts.Name, opts.Config.Platform.OS)
case FormatImage:
_, err = packageBuilder.SaveAsImage(opts.Name, opts.Publish, opts.Config.Platform.OS)
return errors.Wrapf(err, "saving image")
default:
return errors.Errorf("unknown format: %s", style.Symbol(opts.Format))
}
}
func (c *Client) downloadBuildpackFromURI(ctx context.Context, uri, relativeBaseDir string) (blob.Blob, error) {
absPath, err := paths.FilePathToURI(uri, relativeBaseDir)
if err != nil {
return nil, errors.Wrapf(err, "making absolute: %s", style.Symbol(uri))
}
uri = absPath
c.logger.Debugf("Downloading buildpack from URI: %s", style.Symbol(uri))
blob, err := c.downloader.Download(ctx, uri)
if err != nil {
return nil, errors.Wrapf(err, "downloading buildpack from %s", style.Symbol(uri))
}
return blob, nil
}
func (c *Client) validateOSPlatform(ctx context.Context, os string, publish bool, format string) error {
if publish || format == FormatFile {
return nil
}
info, err := c.docker.Info(ctx)
if err != nil {
return err
}
if info.OSType != os {
return errors.Errorf("invalid %s specified: DOCKER_OS is %s", style.Symbol("platform.os"), style.Symbol(info.OSType))
}
return nil
}