/
buildah_backend.go
446 lines (375 loc) · 14.2 KB
/
buildah_backend.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package container_backend
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/google/uuid"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/otiai10/copy"
"github.com/werf/logboek"
"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/image"
)
type BuildahBackend struct {
buildah buildah.Buildah
}
type BuildahImage struct {
Image LegacyImageInterface
}
func NewBuildahBackend(buildah buildah.Buildah) *BuildahBackend {
return &BuildahBackend{buildah: buildah}
}
func (runtime *BuildahBackend) HasStapelBuildSupport() bool {
return true
}
func (runtime *BuildahBackend) getBuildahCommonOpts(ctx context.Context, suppressLog bool) (opts buildah.CommonOpts) {
if !suppressLog {
opts.LogWriter = logboek.Context(ctx).OutStream()
}
return
}
type containerDesc struct {
ImageName string
Name string
RootMount string
}
func (runtime *BuildahBackend) createContainers(ctx context.Context, images []string) ([]*containerDesc, error) {
var res []*containerDesc
for _, img := range images {
containerID := fmt.Sprintf("werf-stage-build-%s", uuid.New().String())
_, err := runtime.buildah.FromCommand(ctx, containerID, img, buildah.FromCommandOpts(runtime.getBuildahCommonOpts(ctx, true)))
if err != nil {
return nil, fmt.Errorf("unable to create container using base image %q: %w", img, err)
}
res = append(res, &containerDesc{ImageName: img, Name: containerID})
}
return res, nil
}
func (runtime *BuildahBackend) removeContainers(ctx context.Context, containers []*containerDesc) error {
for _, cont := range containers {
if err := runtime.buildah.Rm(ctx, cont.Name, buildah.RmOpts(runtime.getBuildahCommonOpts(ctx, true))); err != nil {
return fmt.Errorf("unable to remove container %q: %w", cont.Name, err)
}
}
return nil
}
func (runtime *BuildahBackend) mountContainers(ctx context.Context, containers []*containerDesc) error {
for _, cont := range containers {
containerRoot, err := runtime.buildah.Mount(ctx, cont.Name, buildah.MountOpts(runtime.getBuildahCommonOpts(ctx, true)))
if err != nil {
return fmt.Errorf("unable to mount container %q root dir: %w", cont.Name, err)
}
cont.RootMount = containerRoot
}
return nil
}
func (runtime *BuildahBackend) unmountContainers(ctx context.Context, containers []*containerDesc) error {
for _, cont := range containers {
if err := runtime.buildah.Umount(ctx, cont.Name, buildah.UmountOpts(runtime.getBuildahCommonOpts(ctx, true))); err != nil {
return fmt.Errorf("container %q: %w", cont.Name, err)
}
}
return nil
}
func (runtime *BuildahBackend) cleanupMountpoints(ctx context.Context, container *containerDesc, buildVolumes []string) error {
var mountpoints []string
for _, volume := range buildVolumes {
volumeParts := strings.SplitN(volume, ":", 2)
mountpoints = append(mountpoints, volumeParts[1])
}
if err := runtime.mountContainers(ctx, []*containerDesc{container}); err != nil {
return err
}
defer func() {
if err := runtime.unmountContainers(ctx, []*containerDesc{container}); err != nil {
logboek.Context(ctx).Warn().LogF("ERROR: unable to unmount containers: %s\n", err)
}
}()
for _, mountpoint := range mountpoints {
if err := os.RemoveAll(filepath.Join(container.RootMount, mountpoint)); err != nil {
return fmt.Errorf("unable to remove mountpoint %q in container %s: %w", mountpoint, container.Name, err)
}
}
return nil
}
func (runtime *BuildahBackend) applyUserCommands(ctx context.Context, container *containerDesc, opts BuildStapelStageOptions) error {
for _, cmd := range opts.Commands {
var mounts []specs.Mount
mounts, err := makeBuildahMounts(opts.BuildVolumes)
if err != nil {
return err
}
// TODO(stapel-to-buildah): Consider support for shell script instead of separate run commands to allow shared
// usage of shell variables and functions between multiple commands.
// Maybe there is no need of such function, instead provide options to select shell in the werf.yaml.
// Is it important to provide compatibility between docker-server-based werf.yaml and buildah-based?
if err := runtime.buildah.RunCommand(ctx, container.Name, []string{"sh", "-c", cmd}, buildah.RunCommandOpts{
CommonOpts: runtime.getBuildahCommonOpts(ctx, false),
Mounts: mounts,
}); err != nil {
return fmt.Errorf("unable to run %q: %w", cmd, err)
}
}
return nil
}
type dependencyContainer struct {
Container *containerDesc
Import DependencyImport
}
func (runtime *BuildahBackend) applyDependencies(ctx context.Context, container *containerDesc, opts BuildStapelStageOptions) error {
var dependencies []*dependencyContainer
var dependenciesImages []string
for _, imp := range opts.Imports {
dependenciesImages = append(dependenciesImages, imp.ImageName)
}
logboek.Context(ctx).Debug().LogF("Creating containers for dependencies images %v\n", dependenciesImages)
dependenciesContainers, err := runtime.createContainers(ctx, dependenciesImages)
if err != nil {
return fmt.Errorf("unable to create dependencies containers: %w", err)
}
defer func() {
if err := runtime.removeContainers(ctx, dependenciesContainers); err != nil {
logboek.Context(ctx).Error().LogF("ERROR: unable to remove temporal dependencies containers: %s\n", err)
}
}()
for _, cont := range dependenciesContainers {
FindImport:
for _, imp := range opts.Imports {
if imp.ImageName == cont.ImageName {
dependencies = append(dependencies, &dependencyContainer{
Container: cont,
Import: imp,
})
break FindImport
}
}
}
logboek.Context(ctx).Debug().LogF("Mounting dependencies containers %v\n", dependenciesContainers)
if err := runtime.mountContainers(ctx, append(dependenciesContainers, container)); err != nil {
return fmt.Errorf("unable to mount containers: %w", err)
}
defer func() {
logboek.Context(ctx).Debug().LogF("Unmounting dependencies containers %v\n", dependenciesContainers)
if err := runtime.unmountContainers(ctx, append(dependenciesContainers, container)); err != nil {
logboek.Context(ctx).Error().LogF("ERROR: unable to unmount containers: %s\n", err)
}
}()
for _, dep := range dependencies {
copyFrom := filepath.Join(dep.Container.RootMount, dep.Import.FromPath)
copyTo := filepath.Join(container.RootMount, dep.Import.ToPath)
fmt.Printf("copying from %q to %q\n", copyFrom, copyTo)
logboek.Context(ctx).Debug().LogF("Copying dependency %v from %q to %q\n", dep.Import, copyFrom, copyTo)
if err := copy.Copy(copyFrom, copyTo); err != nil {
return fmt.Errorf("unable to copy %s to %s: %w", copyFrom, copyTo, err)
}
}
return nil
}
func (runtime *BuildahBackend) BuildStapelStage(ctx context.Context, stageType StapelStageType, opts BuildStapelStageOptions) (string, error) {
var container *containerDesc
if c, err := runtime.createContainers(ctx, []string{opts.BaseImage}); err != nil {
return "", err
} else {
container = c[0]
}
defer func() {
if err := runtime.removeContainers(ctx, []*containerDesc{container}); err != nil {
logboek.Context(ctx).Error().LogF("ERROR: unable to remove temporal build container: %s\n", err)
}
}()
// TODO(stapel-to-buildah): cleanup orphan build containers in werf-host-cleanup procedure
switch stageType {
case FromStage:
if len(opts.BuildVolumes) > 0 {
if err := runtime.cleanupMountpoints(ctx, container, opts.BuildVolumes); err != nil {
return "", err
}
}
case UserCommandsStage:
if err := runtime.applyUserCommands(ctx, container, opts); err != nil {
return "", err
}
case DockerInstructionsStage:
case DependenciesStage:
if err := runtime.applyDependencies(ctx, container, opts); err != nil {
return "", err
}
default:
return "", fmt.Errorf("unsupported stage type %q", stageType.String())
}
logboek.Context(ctx).Debug().LogF("Setting config for build container %q\n", container.Name)
if err := runtime.buildah.Config(ctx, container.Name, buildah.ConfigOpts{
CommonOpts: runtime.getBuildahCommonOpts(ctx, true),
Labels: opts.Labels,
Volumes: opts.Volumes,
Expose: opts.Expose,
Envs: opts.Envs,
Cmd: opts.Cmd,
Entrypoint: opts.Entrypoint,
User: opts.User,
Workdir: opts.Workdir,
Healthcheck: opts.Healthcheck,
}); err != nil {
return "", fmt.Errorf("unable to set container %q config: %w", container.Name, err)
}
// TODO(stapel-to-buildah): Save container name as builtID. There is no need to commit an image here,
// because buildah allows to commit and push directly container, which would happen later.
logboek.Context(ctx).Debug().LogF("committing container %q\n", container.Name)
imgID, err := runtime.buildah.Commit(ctx, container.Name, buildah.CommitOpts{CommonOpts: runtime.getBuildahCommonOpts(ctx, true)})
if err != nil {
return "", fmt.Errorf("unable to commit container %q: %w", container.Name, err)
}
return imgID, nil
}
// GetImageInfo returns nil, nil if image not found.
func (runtime *BuildahBackend) GetImageInfo(ctx context.Context, ref string, opts GetImageInfoOpts) (*image.Info, error) {
inspect, err := runtime.buildah.Inspect(ctx, ref)
if err != nil {
return nil, fmt.Errorf("error getting buildah inspect of %q: %w", ref, err)
}
if inspect == nil {
return nil, nil
}
repository, tag := image.ParseRepositoryAndTag(ref)
return &image.Info{
Name: ref,
Repository: repository,
Tag: tag,
Labels: inspect.Docker.Config.Labels,
CreatedAtUnixNano: inspect.Docker.Created.UnixNano(),
// RepoDigest: repoDigest, // FIXME
OnBuild: inspect.Docker.Config.OnBuild,
ID: inspect.Docker.ID,
ParentID: inspect.Docker.Config.Image,
Size: inspect.Docker.Size,
}, nil
}
func (runtime *BuildahBackend) Rmi(ctx context.Context, ref string, opts RmiOpts) error {
return runtime.buildah.Rmi(ctx, ref, buildah.RmiOpts{
Force: true,
CommonOpts: buildah.CommonOpts{
LogWriter: logboek.Context(ctx).OutStream(),
},
})
}
func (runtime *BuildahBackend) Pull(ctx context.Context, ref string, opts PullOpts) error {
return runtime.buildah.Pull(ctx, ref, buildah.PullOpts{
LogWriter: logboek.Context(ctx).OutStream(),
})
}
func (runtime *BuildahBackend) Tag(ctx context.Context, ref, newRef string, opts TagOpts) error {
return runtime.buildah.Tag(ctx, ref, newRef, buildah.TagOpts{
LogWriter: logboek.Context(ctx).OutStream(),
})
}
func (runtime *BuildahBackend) Push(ctx context.Context, ref string, opts PushOpts) error {
return runtime.buildah.Push(ctx, ref, buildah.PushOpts{
LogWriter: logboek.Context(ctx).OutStream(),
})
}
func (runtime *BuildahBackend) BuildDockerfile(ctx context.Context, dockerfile []byte, opts BuildDockerfileOpts) (string, error) {
buildArgs := make(map[string]string)
for _, argStr := range opts.BuildArgs {
argParts := strings.SplitN(argStr, "=", 2)
if len(argParts) < 2 {
return "", fmt.Errorf("invalid build argument %q given, expected string in the key=value format", argStr)
}
buildArgs[argParts[0]] = argParts[1]
}
return runtime.buildah.BuildFromDockerfile(ctx, dockerfile, buildah.BuildFromDockerfileOpts{
CommonOpts: buildah.CommonOpts{
LogWriter: logboek.Context(ctx).OutStream(),
},
ContextTar: opts.ContextTar,
BuildArgs: buildArgs,
Target: opts.Target,
})
}
func (runtime *BuildahBackend) RefreshImageObject(ctx context.Context, img LegacyImageInterface) error {
if info, err := runtime.GetImageInfo(ctx, img.Name(), GetImageInfoOpts{}); err != nil {
return err
} else {
img.SetInfo(info)
}
return nil
}
func (runtime *BuildahBackend) PullImageFromRegistry(ctx context.Context, img LegacyImageInterface) error {
if err := runtime.Pull(ctx, img.Name(), PullOpts{}); err != nil {
return fmt.Errorf("unable to pull image %s: %w", img.Name(), err)
}
if info, err := runtime.GetImageInfo(ctx, img.Name(), GetImageInfoOpts{}); err != nil {
return fmt.Errorf("unable to get inspect of image %s: %w", img.Name(), err)
} else {
img.SetInfo(info)
}
return nil
}
func (runtime *BuildahBackend) RenameImage(ctx context.Context, img LegacyImageInterface, newImageName string, removeOldName bool) error {
if err := logboek.Context(ctx).Info().LogProcess(fmt.Sprintf("Tagging image %s by name %s", img.Name(), newImageName)).DoError(func() error {
if err := runtime.Tag(ctx, img.Name(), newImageName, TagOpts{}); err != nil {
return fmt.Errorf("unable to tag image %s by name %s: %w", img.Name(), newImageName, err)
}
return nil
}); err != nil {
return err
}
if removeOldName {
if err := logboek.Context(ctx).Info().LogProcess(fmt.Sprintf("Removing old image tag %s", img.Name())).DoError(func() error {
if err := runtime.Rmi(ctx, img.Name(), RmiOpts{}); err != nil {
return fmt.Errorf("unable to remove image %q: %w", img.Name(), err)
}
return nil
}); err != nil {
return err
}
}
img.SetName(newImageName)
if info, err := runtime.GetImageInfo(ctx, img.Name(), GetImageInfoOpts{}); err != nil {
return err
} else {
img.SetInfo(info)
}
desc := img.GetStageDescription()
repository, tag := image.ParseRepositoryAndTag(newImageName)
desc.Info.Repository = repository
desc.Info.Tag = tag
return nil
}
func (runtime *BuildahBackend) RemoveImage(ctx context.Context, img LegacyImageInterface) error {
if err := logboek.Context(ctx).Info().LogProcess(fmt.Sprintf("Removing image tag %s", img.Name())).DoError(func() error {
if err := runtime.Rmi(ctx, img.Name(), RmiOpts{}); err != nil {
return fmt.Errorf("unable to remove image %q: %w", img.Name(), err)
}
return nil
}); err != nil {
return err
}
return nil
}
func (runtime *BuildahBackend) String() string {
return "buildah-runtime"
}
func parseVolume(volume string) (string, string, error) {
volumeParts := strings.SplitN(volume, ":", 2)
if len(volumeParts) != 2 {
return "", "", fmt.Errorf("expected SOURCE:DESTINATION format")
}
return volumeParts[0], volumeParts[1], nil
}
func makeBuildahMounts(volumes []string) ([]specs.Mount, error) {
var mounts []specs.Mount
for _, volume := range volumes {
from, to, err := parseVolume(volume)
if err != nil {
return nil, fmt.Errorf("invalid volume %q: %w", volume, err)
}
mounts = append(mounts, specs.Mount{
Type: "bind",
Source: from,
Destination: to,
})
}
return mounts, nil
}