forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package_buildpack.go
164 lines (132 loc) · 4.71 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
package pack
import (
"context"
"github.com/pkg/errors"
pubbldpkg "github.com/buildpacks/pack/buildpackage"
"github.com/buildpacks/pack/config"
"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/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"
)
// PackageBuildpackOptions is a configuration object used to define
// the behavior of PackageBuildpack.
type PackageBuildpackOptions struct {
// 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
}
// 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")
}
blob, err := c.downloader.Download(ctx, bpURI)
if err != nil {
return errors.Wrapf(err, "downloading buildpack from %s", style.Symbol(bpURI))
}
bp, err := dist.BuildpackFromRootBlob(blob, 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.URI != "" {
if buildpack.HasDockerLocator(dep.URI) {
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...)
} else {
blob, err := c.downloader.Download(ctx, dep.URI)
if err != nil {
return errors.Wrapf(err, "downloading buildpack from %s", style.Symbol(dep.URI))
}
isOCILayout, err := buildpackage.IsOCILayoutBlob(blob)
if err != nil {
return errors.Wrap(err, "inspecting buildpack blob")
}
if isOCILayout {
mainBP, deps, err := buildpackage.BuildpacksFromOCILayoutBlob(blob)
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(blob, writerFactory)
if err != nil {
return errors.Wrapf(err, "creating buildpack from %s", style.Symbol(dep.URI))
}
depBPs = []dist.Buildpack{depBP}
}
}
} else 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...)
}
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) 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
}