/
docker_with_fuse.go
302 lines (251 loc) · 9.34 KB
/
docker_with_fuse.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
package buildah
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"github.com/werf/lockgate"
"github.com/werf/logboek"
"github.com/werf/werf/pkg/buildah/thirdparty"
"github.com/werf/werf/pkg/docker"
"github.com/werf/werf/pkg/werf"
)
type DockerWithFuseBuildah struct {
BaseBuildah
commonBuildahCliArgs []string
}
type StoreOptions struct {
GraphDriverName string
GraphDriverOptions []string
}
func NewDockerWithFuseBuildah(commonOpts CommonBuildahOpts, opts DockerWithFuseModeOpts) (*DockerWithFuseBuildah, error) {
b := &DockerWithFuseBuildah{}
baseBuildah, err := NewBaseBuildah(commonOpts.TmpDir, BaseBuildahOpts{
Isolation: *commonOpts.Isolation,
Insecure: commonOpts.Insecure,
})
if err != nil {
return nil, fmt.Errorf("unable to create BaseBuildah: %s", err)
}
b.BaseBuildah = *baseBuildah
// TODO: remove these strings and mount previously generated config files inside of a docker-with-fuse container
b.SignaturePolicyPath = "/etc/containers/policy.json"
b.RegistriesConfigPath = "/etc/containers/registries.conf"
b.RegistriesConfigDirPath = "/etc/containers/registries.conf.d"
b.commonBuildahCliArgs, err = GetBasicBuildahCliArgs(*commonOpts.StorageDriver)
if err != nil {
return nil, fmt.Errorf("unable to get common Buildah cli args: %s", err)
}
b.commonBuildahCliArgs = append(
b.commonBuildahCliArgs, "--registries-conf", b.RegistriesConfigPath,
"--registries-conf-dir", b.RegistriesConfigDirPath,
)
return b, nil
}
func (b *DockerWithFuseBuildah) Tag(ctx context.Context, ref, newRef string, opts TagOpts) error {
_, _, err := b.runBuildah(ctx, []string{}, []string{"tag", ref, newRef}, opts.LogWriter)
return err
}
func (b *DockerWithFuseBuildah) Push(ctx context.Context, ref string, opts PushOpts) error {
args := []string{
"push", fmt.Sprintf("--tls-verify=%s", strconv.FormatBool(!b.Insecure)),
"--signature-policy", b.SignaturePolicyPath, ref, fmt.Sprintf("docker://%s", ref),
}
_, _, err := b.runBuildah(ctx, []string{}, args, opts.LogWriter)
return err
}
func (b *DockerWithFuseBuildah) Mount(ctx context.Context, container string, opts MountOpts) (string, error) {
panic("not implemented")
}
func (b *DockerWithFuseBuildah) Umount(ctx context.Context, container string, opts UmountOpts) error {
panic("not implemented")
}
func (b *DockerWithFuseBuildah) BuildFromDockerfile(ctx context.Context, dockerfile []byte, opts BuildFromDockerfileOpts) (string, error) {
sessionTmpDir, _, _, err := b.prepareBuildFromDockerfile(dockerfile, opts.ContextTar)
if err != nil {
return "", fmt.Errorf("error preparing for build from dockerfile: %s", err)
}
defer func() {
if debug() {
return
}
if err = os.RemoveAll(sessionTmpDir); err != nil {
logboek.Warn().LogF("unable to remove session tmp dir %q: %s\n", sessionTmpDir, err)
}
}()
buildArgs := []string{
"bud", "--isolation", b.Isolation.String(), "--format", "docker",
fmt.Sprintf("--tls-verify=%s", strconv.FormatBool(!b.Insecure)), "--signature-policy", b.SignaturePolicyPath,
}
for k, v := range opts.BuildArgs {
buildArgs = append(buildArgs, "--build-arg", fmt.Sprintf("%s=%s", k, v))
}
buildArgs = append(buildArgs, "-f", "/.werf/buildah/tmp/Dockerfile")
// NOTE: it is principal to use cli option --tls-verify=true|false form with equality sign, instead of separate arguments (--tls-verify true|false), because --tls-verify is by itself a boolean argument
output, _, err := b.runBuildah(
ctx, []string{
"--volume", fmt.Sprintf("%s:/.werf/buildah/tmp", sessionTmpDir),
"--workdir", "/.werf/buildah/tmp/context",
},
buildArgs, opts.LogWriter,
)
if err != nil {
return "", err
}
outputLines := scanLines(output)
return outputLines[len(outputLines)-1], nil
}
func (b *DockerWithFuseBuildah) RunCommand(ctx context.Context, container string, command []string, opts RunCommandOpts) error {
_, _, err := b.runBuildah(ctx, []string{}, append([]string{"run", container}, command...), opts.LogWriter)
return err
}
func (b *DockerWithFuseBuildah) FromCommand(ctx context.Context, container string, image string, opts FromCommandOpts) (string, error) {
_, _, err := b.runBuildah(ctx, []string{}, []string{
"from", fmt.Sprintf("--tls-verify=%s", strconv.FormatBool(!b.Insecure)), "--name", container,
"--signature-policy", b.SignaturePolicyPath, "--isolation", b.Isolation.String(), "--format", "docker", image,
}, opts.LogWriter)
// FIXME: return container name
return "", err
}
// TODO: make it more generic to handle not only images
func (b *DockerWithFuseBuildah) Inspect(ctx context.Context, ref string) (*thirdparty.BuilderInfo, error) {
stdout, stderr, err := b.runBuildah(ctx, []string{}, []string{"inspect", "--type", "image", ref}, nil)
if err != nil {
if strings.Contains(stderr, "image not known") {
return nil, nil
}
return nil, err
}
var res thirdparty.BuilderInfo
if err := json.Unmarshal([]byte(stdout), &res); err != nil {
return nil, fmt.Errorf("unable to unmarshal buildah inspect json output: %s", err)
}
return &res, nil
}
func (b *DockerWithFuseBuildah) Pull(ctx context.Context, ref string, opts PullOpts) error {
args := []string{
"pull", fmt.Sprintf("--tls-verify=%s", strconv.FormatBool(!b.Insecure)),
"--signature-policy", b.SignaturePolicyPath, ref,
}
_, _, err := b.runBuildah(ctx, []string{}, args, opts.LogWriter)
return err
}
func (b *DockerWithFuseBuildah) Rmi(ctx context.Context, ref string, opts RmiOpts) error {
args := []string{"rmi"}
if opts.Force {
args = append(args, "-f")
}
args = append(args, ref)
_, _, err := b.runBuildah(ctx, []string{}, args, opts.LogWriter)
return err
}
func (b *DockerWithFuseBuildah) runBuildah(ctx context.Context, dockerArgs []string, buildahArgs []string, logWriter io.Writer) (string, string, error) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
var stdoutWriter io.Writer
var stderrWriter io.Writer
if logWriter != nil {
stdoutWriter = io.MultiWriter(stdout, logWriter)
stderrWriter = io.MultiWriter(stderr, logWriter)
} else {
stdoutWriter = stdout
stderrWriter = stderr
}
if err := runStorageContainer(ctx, BuildahStorageContainerName, BuildahImage); err != nil {
return "", "", fmt.Errorf("unable to run werf buildah storage container: %s", err)
}
args := []string{"--rm"}
args = append(args, dockerArgs...)
args = append(args, BuildahWithFuseDockerArgs(BuildahStorageContainerName, docker.DockerConfigDir)...)
args = append(args, b.commonBuildahCliArgs...)
args = append(args, buildahArgs...)
if debug() {
fmt.Printf("DEBUG CMD: docker run -ti %s\n", strings.Join(args, " "))
}
err := docker.CliRun_ProvidedOutput(ctx, stdoutWriter, stderrWriter, args...)
return stdout.String(), stderr.String(), err
}
func BuildahWithFuseDockerArgs(storageContainerName, dockerConfigDir string) []string {
return []string{
"--user", "1000",
"--device", "/dev/fuse",
"--security-opt", "seccomp=unconfined",
"--security-opt", "apparmor=unconfined",
"--volume", fmt.Sprintf("%s:%s", dockerConfigDir, "/home/build/.docker"),
"--volumes-from", storageContainerName,
BuildahImage, "buildah",
}
}
func GetBasicBuildahCliArgs(driver StorageDriver) ([]string, error) {
var result []string
cliStoreOpts, err := newBuildahCliStoreOptions(driver)
if err != nil {
return result, fmt.Errorf("unable to get buildah cli store options: %s", err)
}
if cliStoreOpts.GraphDriverName != "" {
result = append(result, "--storage-driver", cliStoreOpts.GraphDriverName)
}
if len(cliStoreOpts.GraphDriverOptions) > 0 {
result = append(result, "--storage-opt", strings.Join(cliStoreOpts.GraphDriverOptions, ","))
}
return result, nil
}
func runStorageContainer(ctx context.Context, name, image string) error {
exist, err := docker.ContainerExist(ctx, name)
if err != nil {
return fmt.Errorf("unable to check existence of docker container %q: %s", name, err)
}
if exist {
return nil
}
return werf.WithHostLock(ctx, fmt.Sprintf("buildah.container.%s", name), lockgate.AcquireOptions{Timeout: time.Second * 600}, func() error {
return logboek.Context(ctx).LogProcess("Creating container %s using image %s", name, image).DoError(func() error {
exist, err := docker.ContainerExist(ctx, name)
if err != nil {
return fmt.Errorf("unable to check existence of docker container %q: %s", name, err)
}
if exist {
return nil
}
imageExist, err := docker.ImageExist(ctx, image)
if err != nil {
return fmt.Errorf("unable to check existence of docker image %q: %s", image, err)
}
if !imageExist {
if err := docker.CliPullWithRetries(ctx, image); err != nil {
return err
}
}
return docker.CliCreate(ctx, "--name", name, image)
})
})
}
func newBuildahCliStoreOptions(driver StorageDriver) (*StoreOptions, error) {
var graphDriverOptions []string
if driver == StorageDriverOverlay {
fuseOpts, err := GetFuseOverlayfsOptions()
if err != nil {
return nil, fmt.Errorf("unable to get overlay options: %s", err)
}
graphDriverOptions = append(graphDriverOptions, fuseOpts...)
graphDriverOptions = append(graphDriverOptions, fmt.Sprintf("%s.ignore_chown_errors=true", StorageDriverOverlay))
}
return &StoreOptions{
GraphDriverName: string(driver),
GraphDriverOptions: graphDriverOptions,
}, nil
}
func scanLines(data string) []string {
var lines []string
s := bufio.NewScanner(strings.NewReader(data))
for s.Scan() {
lines = append(lines, s.Text())
}
return lines
}