/
buildah_runtime.go
179 lines (151 loc) · 5.19 KB
/
buildah_runtime.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
package container_runtime
import (
"context"
"fmt"
"strings"
"github.com/werf/logboek"
"github.com/werf/werf/pkg/buildah"
"github.com/werf/werf/pkg/image"
)
type BuildahRuntime struct {
buildah buildah.Buildah
}
type BuildahImage struct {
Image LegacyImageInterface
}
func NewBuildahRuntime(buildah buildah.Buildah) *BuildahRuntime {
return &BuildahRuntime{buildah: buildah}
}
// GetImageInfo returns nil, nil if image not found.
func (runtime *BuildahRuntime) 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: %s", 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 *BuildahRuntime) 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 *BuildahRuntime) Pull(ctx context.Context, ref string, opts PullOpts) error {
return runtime.buildah.Pull(ctx, ref, buildah.PullOpts{
CommonOpts: buildah.CommonOpts{
LogWriter: logboek.Context(ctx).OutStream(),
},
})
}
func (runtime *BuildahRuntime) Tag(ctx context.Context, ref, newRef string, opts TagOpts) error {
return runtime.buildah.Tag(ctx, ref, newRef, buildah.TagOpts{
CommonOpts: buildah.CommonOpts{
LogWriter: logboek.Context(ctx).OutStream(),
},
})
}
func (runtime *BuildahRuntime) Push(ctx context.Context, ref string, opts PushOpts) error {
return runtime.buildah.Push(ctx, ref, buildah.PushOpts{
CommonOpts: buildah.CommonOpts{
LogWriter: logboek.Context(ctx).OutStream(),
},
})
}
func (runtime *BuildahRuntime) 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,
})
}
func (runtime *BuildahRuntime) 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 *BuildahRuntime) 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: %s", img.Name(), err)
}
if info, err := runtime.GetImageInfo(ctx, img.Name(), GetImageInfoOpts{}); err != nil {
return fmt.Errorf("unable to get inspect of image %s: %s", img.Name(), err)
} else {
img.SetInfo(info)
}
return nil
}
func (runtime *BuildahRuntime) 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: %s", 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: %s", 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 *BuildahRuntime) 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: %s", img.Name(), err)
}
return nil
}); err != nil {
return err
}
return nil
}
func (runtime *BuildahRuntime) String() string {
return "buildah-runtime"
}