-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathbuilder.go
387 lines (338 loc) · 9.43 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
/*
* Copyright (c) 2022. Nydus Developers. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
package tool
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var logger = logrus.WithField("module", "builder")
func isSignalKilled(err error) bool {
return strings.Contains(err.Error(), "signal: killed")
}
type PackOption struct {
BuilderPath string
BlobPath string
ExternalBlobPath string
FsVersion string
SourcePath string
ChunkDictPath string
AttributesPath string
PrefetchPatterns string
Compressor string
OCIRef bool
AlignedChunk bool
ChunkSize string
BatchSize string
Encrypt bool
Timeout *time.Duration
Features Features
}
type MergeOption struct {
BuilderPath string
SourceBootstrapPaths []string
RafsBlobDigests []string
RafsBlobTOCDigests []string
RafsBlobSizes []int64
TargetBootstrapPath string
ChunkDictPath string
ParentBootstrapPath string
PrefetchPatterns string
OutputJSONPath string
Timeout *time.Duration
}
type UnpackOption struct {
BuilderPath string
BootstrapPath string
BlobPath string
BackendConfigPath string
TarPath string
Timeout *time.Duration
}
type outputJSON struct {
Blobs []string
}
func buildPackArgs(option PackOption) ([]string, error) {
if option.FsVersion == "" {
option.FsVersion = "6"
}
args := []string{
"create",
"--log-level",
"warn",
"--prefetch-policy",
"fs",
"--blob",
option.BlobPath,
"--whiteout-spec",
"none",
"--fs-version",
option.FsVersion,
}
if option.Features.Contains(FeatureTar2Rafs) {
args = append(
args,
"--blob-inline-meta",
)
info, err := os.Stat(option.SourcePath)
if err != nil {
return nil, err
}
if info.IsDir() {
args = append(
args,
"--type",
"dir-rafs",
)
} else {
args = append(
args,
"--type",
"tar-rafs",
)
}
if option.FsVersion == "6" {
args = append(
args,
"--features",
"blob-toc",
)
}
} else {
args = append(
args,
"--source-type",
"directory",
// Sames with `--blob-inline-meta`, it's used for compatibility
// with the old nydus-image builder.
"--inline-bootstrap",
)
}
if option.ChunkDictPath != "" {
args = append(args, "--chunk-dict", fmt.Sprintf("bootstrap=%s", option.ChunkDictPath))
}
if option.PrefetchPatterns == "" {
option.PrefetchPatterns = "/"
}
if option.Compressor != "" {
args = append(args, "--compressor", option.Compressor)
}
if option.AlignedChunk {
args = append(args, "--aligned-chunk")
}
if option.ChunkSize != "" {
args = append(args, "--chunk-size", option.ChunkSize)
}
if option.Features.Contains(FeatureBatchSize) {
args = append(args, "--batch-size", option.BatchSize)
}
if option.Encrypt {
args = append(args, "--encrypt")
}
if option.AttributesPath != "" {
args = append(args, "--attributes", option.AttributesPath)
}
if option.ExternalBlobPath != "" {
args = append(args, "--external-blob", option.ExternalBlobPath)
}
args = append(args, option.SourcePath)
return args, nil
}
func Pack(option PackOption) error {
if option.OCIRef {
return packRef(option)
}
ctx := context.Background()
var cancel context.CancelFunc
if option.Timeout != nil {
ctx, cancel = context.WithTimeout(ctx, *option.Timeout)
defer cancel()
}
args, err := buildPackArgs(option)
if err != nil {
return err
}
logrus.Debugf("\tCommand: %s %s", option.BuilderPath, strings.Join(args, " "))
cmd := exec.CommandContext(ctx, option.BuilderPath, args...)
cmd.Stdout = logger.Writer()
cmd.Stderr = logger.Writer()
cmd.Stdin = strings.NewReader(option.PrefetchPatterns)
if err := cmd.Run(); err != nil {
if isSignalKilled(err) && option.Timeout != nil {
logrus.WithError(err).Errorf("fail to run %v %+v, possibly due to timeout %v", option.BuilderPath, args, *option.Timeout)
} else {
logrus.WithError(err).Errorf("fail to run %v %+v", option.BuilderPath, args)
}
return err
}
return nil
}
func packRef(option PackOption) error {
args := []string{
"create",
"--log-level",
"warn",
"--type",
"targz-ref",
"--blob-inline-meta",
"--features",
"blob-toc",
"--blob",
option.BlobPath,
}
args = append(args, option.SourcePath)
ctx := context.Background()
var cancel context.CancelFunc
if option.Timeout != nil {
ctx, cancel = context.WithTimeout(ctx, *option.Timeout)
defer cancel()
}
logrus.Debugf("\tCommand: %s %s", option.BuilderPath, strings.Join(args, " "))
cmd := exec.CommandContext(ctx, option.BuilderPath, args...)
cmd.Stdout = logger.Writer()
cmd.Stderr = logger.Writer()
if err := cmd.Run(); err != nil {
if isSignalKilled(err) && option.Timeout != nil {
logrus.WithError(err).Errorf("fail to run %v %+v, possibly due to timeout %v", option.BuilderPath, args, *option.Timeout)
} else {
logrus.WithError(err).Errorf("fail to run %v %+v", option.BuilderPath, args)
}
return err
}
return nil
}
func Merge(option MergeOption) ([]digest.Digest, error) {
args := []string{
"merge",
"--log-level",
"warn",
"--prefetch-policy",
"fs",
"--output-json",
option.OutputJSONPath,
"--bootstrap",
option.TargetBootstrapPath,
}
if option.ChunkDictPath != "" {
args = append(args, "--chunk-dict", fmt.Sprintf("bootstrap=%s", option.ChunkDictPath))
}
if option.ParentBootstrapPath != "" {
args = append(args, "--parent-bootstrap", option.ParentBootstrapPath)
}
if option.PrefetchPatterns == "" {
option.PrefetchPatterns = "/"
}
args = append(args, option.SourceBootstrapPaths...)
if len(option.RafsBlobDigests) > 0 {
args = append(args, "--blob-digests", strings.Join(option.RafsBlobDigests, ","))
}
if len(option.RafsBlobTOCDigests) > 0 {
args = append(args, "--blob-toc-digests", strings.Join(option.RafsBlobTOCDigests, ","))
}
if len(option.RafsBlobSizes) > 0 {
sizes := []string{}
for _, size := range option.RafsBlobSizes {
sizes = append(sizes, fmt.Sprintf("%d", size))
}
args = append(args, "--blob-sizes", strings.Join(sizes, ","))
}
ctx := context.Background()
var cancel context.CancelFunc
if option.Timeout != nil {
ctx, cancel = context.WithTimeout(ctx, *option.Timeout)
defer cancel()
}
logrus.Debugf("\tCommand: %s %s", option.BuilderPath, strings.Join(args, " "))
cmd := exec.CommandContext(ctx, option.BuilderPath, args...)
cmd.Stdout = logger.Writer()
cmd.Stderr = logger.Writer()
cmd.Stdin = strings.NewReader(option.PrefetchPatterns)
if err := cmd.Run(); err != nil {
if isSignalKilled(err) && option.Timeout != nil {
logrus.WithError(err).Errorf("fail to run %v %+v, possibly due to timeout %v", option.BuilderPath, args, *option.Timeout)
} else {
logrus.WithError(err).Errorf("fail to run %v %+v", option.BuilderPath, args)
}
return nil, errors.Wrap(err, "run merge command")
}
outputBytes, err := os.ReadFile(option.OutputJSONPath)
if err != nil {
return nil, errors.Wrapf(err, "read file %s", option.OutputJSONPath)
}
var output outputJSON
err = json.Unmarshal(outputBytes, &output)
if err != nil {
return nil, errors.Wrapf(err, "unmarshal output json file %s", option.OutputJSONPath)
}
blobDigests := []digest.Digest{}
for _, blobID := range output.Blobs {
blobDigests = append(blobDigests, digest.NewDigestFromHex(string(digest.SHA256), blobID))
}
return blobDigests, nil
}
func Unpack(option UnpackOption) error {
args := []string{
"unpack",
"--log-level",
"warn",
"--bootstrap",
option.BootstrapPath,
"--output",
option.TarPath,
}
if option.BackendConfigPath != "" {
configBytes, err := os.ReadFile(option.BackendConfigPath)
if err != nil {
return errors.Wrapf(err, "fail to read backend config file %s", option.BackendConfigPath)
}
var config map[string]interface{}
if err := json.Unmarshal(configBytes, &config); err != nil {
return errors.Wrapf(err, "fail to unmarshal backend config file %s", option.BackendConfigPath)
}
backendConfigType, ok := config["backend"].(map[string]interface{})["type"]
if !ok {
return errors.New("backend config file should contain a valid backend type")
}
backendConfig, ok := config["backend"].(map[string]interface{})[backendConfigType.(string)]
if !ok {
return errors.New("failed to get backend config with type " + backendConfigType.(string))
}
backendConfigBytes, err := json.Marshal(backendConfig)
if err != nil {
return errors.Wrapf(err, "fail to marshal backend config %v", backendConfig)
}
args = append(args, "--backend-type", backendConfigType.(string))
args = append(args, "--backend-config", string(backendConfigBytes))
} else if option.BlobPath != "" {
args = append(args, "--blob", option.BlobPath)
}
ctx := context.Background()
var cancel context.CancelFunc
if option.Timeout != nil {
ctx, cancel = context.WithTimeout(ctx, *option.Timeout)
defer cancel()
}
logrus.Debugf("\tCommand: %s %s", option.BuilderPath, strings.Join(args, " "))
cmd := exec.CommandContext(ctx, option.BuilderPath, args...)
cmd.Stdout = logger.Writer()
cmd.Stderr = logger.Writer()
if err := cmd.Run(); err != nil {
if isSignalKilled(err) && option.Timeout != nil {
logrus.WithError(err).Errorf("fail to run %v %+v, possibly due to timeout %v", option.BuilderPath, args, *option.Timeout)
} else {
logrus.WithError(err).Errorf("fail to run %v %+v", option.BuilderPath, args)
}
return err
}
return nil
}