This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathbuilder.go
392 lines (320 loc) · 11.7 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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package pack
import (
"bytes"
"context"
"fmt"
"strings"
"github.com/buildpacks/pack"
"github.com/buildpacks/pack/logging"
"github.com/buildpacks/pack/project"
"github.com/docker/docker/client"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/waypoint-plugin-sdk/component"
"github.com/hashicorp/waypoint-plugin-sdk/docs"
"github.com/hashicorp/waypoint-plugin-sdk/terminal"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
wpdockerclient "github.com/hashicorp/waypoint/builtin/docker/client"
"github.com/hashicorp/waypoint/internal/assets"
"github.com/hashicorp/waypoint/internal/pkg/epinject"
)
// Builder uses `pack` -- the frontend for CloudNative Buildpacks -- to build
// an artifact from source.
type Builder struct {
config BuilderConfig
}
// BuildFunc implements component.Builder
func (b *Builder) BuildFunc() interface{} {
return b.Build
}
// Config is the configuration structure for the registry.
type BuilderConfig struct {
// Control whether or not to inject the entrypoint binary into the resulting image
DisableCEB bool `hcl:"disable_entrypoint,optional"`
// The Buildpack builder image to use, defaults to the standard heroku one.
Builder string `hcl:"builder,optional"`
// The exact buildpacks to use.
Buildpacks []string `hcl:"buildpacks,optional"`
// Environment variables that are meant to configure the application in a static
// way. This might be control an image that has mulitple modes of operation,
// selected via environment variable. Most configuration should use the waypoint
// config commands.
StaticEnvVars map[string]string `hcl:"static_environment,optional"`
// Files patterns to prevent from being pulled into the build.
Ignore []string `hcl:"ignore,optional"`
// Process type that will be used when setting container start command.
ProcessType string `hcl:"process_type,optional" default:"web"`
}
const DefaultBuilder = "heroku/buildpacks:18"
// Config implements Configurable
func (b *Builder) Config() (interface{}, error) {
return &b.config, nil
}
var skipBuildPacks = map[string]struct{}{
"heroku/procfile": {},
}
// Build
func (b *Builder) Build(
ctx context.Context,
ui terminal.UI,
jobInfo *component.JobInfo,
src *component.Source,
log hclog.Logger,
) (*DockerImage, error) {
builder := b.config.Builder
if builder == "" {
builder = DefaultBuilder
}
dockerClient, err := wpdockerclient.NewClientWithOpts(client.FromEnv)
if err != nil {
return nil, err
}
dockerClient.NegotiateAPIVersion(ctx)
// We now test if Docker is actually functional. Pack requires a Docker
// daemon and we can't fallback to "img" or any other Dockerless solution.
log.Debug("testing if Docker is available")
if fallback, err := wpdockerclient.Fallback(ctx, log, dockerClient); err != nil {
log.Warn("error during check if we should use Docker fallback", "err", err)
return nil, status.Errorf(codes.Internal,
"error validating Docker connection: %s", err)
} else if fallback {
ui.Output(
`WARNING: `+
`Docker daemon appears unavailable. The 'pack' builder requires access `+
`to a Docker daemon. Pack does not support dockerless builds. We will `+
`still attempt to run the build but it will likely fail. If you are `+
`running this build locally, please install Docker. If you are running `+
`this build remotely (in a Waypoint runner), the runner must be configured `+
`to have access to the Docker daemon.`+"\n",
terminal.WithWarningStyle(),
)
} else {
log.Debug("Docker appears available")
}
ui.Output("Creating new buildpack-based image using builder: %s", builder)
sg := ui.StepGroup()
step := sg.Add("Creating pack client")
defer step.Abort()
build := sg.Add("Building image")
defer build.Abort()
client, err := pack.NewClient(
pack.WithLogger(logging.New(build.TermOutput())),
pack.WithDockerClient(dockerClient),
)
if err != nil {
return nil, err
}
step.Done()
bo := pack.BuildOptions{
Image: src.App,
Builder: builder,
AppPath: src.Path,
Env: b.config.StaticEnvVars,
Buildpacks: b.config.Buildpacks,
ProjectDescriptor: project.Descriptor{
Build: project.Build{
Exclude: b.config.Ignore,
},
},
DefaultProcessType: b.config.ProcessType,
}
err = client.Build(ctx, bo)
if err != nil {
return nil, err
}
build.Done()
info, err := client.InspectImage(src.App, true)
if err != nil {
return nil, err
}
labels := map[string]string{}
var languages []string
for _, bp := range info.Buildpacks {
if _, ok := skipBuildPacks[bp.ID]; ok {
continue
}
idx := strings.IndexByte(bp.ID, '/')
if idx != -1 {
languages = append(languages, bp.ID[idx+1:])
} else {
languages = append(languages, bp.ID)
}
}
labels["common/languages"] = strings.Join(languages, ",")
labels["common/buildpack-stack"] = info.StackID
proc := info.Processes.DefaultProcess
if proc != nil {
cmd := proc.Command
if len(proc.Args) > 0 {
if len(cmd) > 0 {
cmd = fmt.Sprintf("%s %s", cmd, strings.Join(proc.Args, " "))
} else {
cmd = strings.Join(proc.Args, " ")
}
}
if cmd != "" {
labels["common/command"] = cmd
if proc.Type != "" {
labels["common/command-type"] = proc.Type
}
}
}
if !b.config.DisableCEB {
inject := sg.Add("Injecting entrypoint binary to image")
defer inject.Abort()
asset, err := assets.Asset("ceb/ceb")
if err != nil {
return nil, status.Errorf(codes.Internal, "unable to restore custom entry point binary: %s", err)
}
assetInfo, err := assets.AssetInfo("ceb/ceb")
if err != nil {
return nil, status.Errorf(codes.Internal, "unable to restore custom entry point binary: %s", err)
}
imageId, err := epinject.AlterEntrypoint(ctx, src.App+":latest", func(cur []string) (*epinject.NewEntrypoint, error) {
ep := &epinject.NewEntrypoint{
Entrypoint: append([]string{"/waypoint-entrypoint"}, cur...),
InjectFiles: map[string]epinject.InjectFile{
"/waypoint-entrypoint": {
Reader: bytes.NewReader(asset),
Info: assetInfo,
},
},
}
return ep, nil
})
if err != nil {
return nil, err
}
labels["common/image-id"] = imageId
inject.Done()
}
sg.Wait()
ui.Output("")
ui.Output("Generated new Docker image: %s:latest", src.App)
// We don't even need to inspect Docker to verify we have the image.
// If `pack` succeeded we can assume that it created an image for us.
return &DockerImage{
Image: src.App,
Tag: "latest", // It always tags latest
BuildLabels: labels,
}, nil
}
func (b *Builder) Documentation() (*docs.Documentation, error) {
doc, err := docs.New(docs.FromConfig(&BuilderConfig{}), docs.FromFunc(b.BuildFunc()))
if err != nil {
return nil, err
}
doc.Description(`
Create a Docker image using CloudNative Buildpacks.
**Pack requires access to a Docker daemon.** For remote builds, such as those
triggered by [Git polling](/docs/projects/git), the
[runner](/docs/runner) needs to have access to a Docker daemon such
as exposing the Docker socket, enabling Docker-in-Docker, etc. Unfortunately,
pack doesn't support dockerless builds. Configuring Docker access within
a Docker container is outside the scope of these docs, please search the
internet for "Docker in Docker" or other terms for more information.
`)
doc.Example(`
build {
use "pack" {
builder = "heroku/buildpacks:18"
disable_entrypoint = false
}
}
`)
doc.Input("component.Source")
doc.Output("pack.Image")
doc.AddMapper(
"pack.Image",
"docker.Image",
"Allow pack images to be used as normal docker images",
)
doc.SetField(
"disable_entrypoint",
"if set, the entrypoint binary won't be injected into the image",
docs.Summary(
"The entrypoint binary is what provides extended functionality",
"such as logs and exec. If it is not injected at build time",
"the expectation is that the image already contains it",
),
)
doc.SetField(
"builder",
"The buildpack builder image to use",
docs.Default(DefaultBuilder),
)
doc.SetField(
"buildpacks",
"The exact buildpacks to use",
docs.Summary(
"If set, the builder will run these buildpacks in the specified order.\n\n",
"They can be listed using several [URI formats](https://buildpacks.io/docs/app-developer-guide/specific-buildpacks).",
),
)
doc.SetField(
"static_environment",
"environment variables to expose to the buildpack",
docs.Summary(
"these environment variables should not be run of the mill",
"configuration variables, use waypoint config for that.",
"These variables are used to control over all container modes,",
"such as configuring it to start a web app vs a background worker",
),
)
doc.SetField(
"ignore",
"file patterns to match files which will not be included in the build",
docs.Summary(
`Each pattern follows the semantics of .gitignore. This is a summarized version:
1. A blank line matches no files, so it can serve as a separator
for readability.
2. A line starting with # serves as a comment. Put a backslash ("\")
in front of the first hash for patterns that begin with a hash.
3. Trailing spaces are ignored unless they are quoted with backslash ("\").
4. An optional prefix "!" which negates the pattern; any matching file
excluded by a previous pattern will become included again. It is not
possible to re-include a file if a parent directory of that file is
excluded. Git doesn’t list excluded directories for performance reasons,
so any patterns on contained files have no effect, no matter where they
are defined. Put a backslash ("\") in front of the first "!" for
patterns that begin with a literal "!", for example, "\!important!.txt".
5. If the pattern ends with a slash, it is removed for the purpose of the
following description, but it would only find a match with a directory.
In other words, foo/ will match a directory foo and paths underneath it,
but will not match a regular file or a symbolic link foo (this is
consistent with the way how pathspec works in general in Git).
6. If the pattern does not contain a slash /, Git treats it as a shell glob
pattern and checks for a match against the pathname relative to the
location of the .gitignore file (relative to the top level of the work
tree if not from a .gitignore file).
7. Otherwise, Git treats the pattern as a shell glob suitable for
consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the
pattern will not match a / in the pathname. For example,
"Documentation/*.html" matches "Documentation/git.html" but not
"Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".
8. A leading slash matches the beginning of the pathname. For example,
"/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
9. Two consecutive asterisks ("**") in patterns matched against full
pathname may have special meaning:
i. A leading "**" followed by a slash means match in all directories.
For example, "** /foo" matches file or directory "foo" anywhere,
the same as pattern "foo". "** /foo/bar" matches file or directory
"bar" anywhere that is directly under directory "foo".
ii. A trailing "/**" matches everything inside. For example, "abc/**"
matches all files inside directory "abc", relative to the location
of the .gitignore file, with infinite depth.
iii. A slash followed by two consecutive asterisks then a slash matches
zero or more directories. For example, "a/** /b" matches "a/b",
"a/x/b", "a/x/y/b" and so on.
iv. Other consecutive asterisks are considered invalid.`),
)
doc.SetField(
"process_type",
"The process type to use from your Procfile. if not set, defaults to `web`",
docs.Summary(
"The process type is used to control over all container modes,",
"such as configuring it to start a web app vs a background worker",
),
)
return doc, nil
}