-
Notifications
You must be signed in to change notification settings - Fork 9
/
images.go
295 lines (237 loc) · 7.45 KB
/
images.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cmd
import (
"archive/tar"
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/docker/go-units"
"github.com/dustin/go-humanize"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/daemon"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/spf13/cobra"
"k8s.io/utils/pointer"
"k8s.io/utils/strings/slices"
"namespacelabs.dev/foundation/internal/artifacts"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/build"
"namespacelabs.dev/foundation/internal/build/binary"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/internal/runtime/docker"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/pkggraph"
"namespacelabs.dev/foundation/std/tasks"
)
func NewImagesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "images",
Short: "Image related functionality.",
Hidden: true,
}
cmd.AddCommand(unpack())
cmd.AddCommand(flatten())
cmd.AddCommand(makeFSImage())
return cmd
}
func unpack() *cobra.Command {
unpack := &cobra.Command{
Use: "unpack <image-ref> --target <path/to/dir>",
Short: "Unpack an image to the local filesystem.",
}
image := imageFromArgs(unpack)
target := unpack.Flags().String("target", "", "Where the image should be unpacked to.")
extract := unpack.Flags().StringArray("extract", nil, "If set, limits the paths being exported to the specified list.")
_ = unpack.MarkFlagRequired("target")
return fncobra.With(unpack, func(ctx context.Context) error {
dst := fnfs.ReadWriteLocalFS(*target, fnfs.AnnounceWrites(console.Stdout(ctx)))
if err := dst.MkdirAll(".", 0700); err != nil {
return err
}
tr := tar.NewReader(mutate.Extract(*image))
for {
h, err := tr.Next()
if err == io.EOF {
break
} else if err != nil {
return fnerrors.BadInputError("unexpected error: %v", err)
}
clean := filepath.Clean(h.Name)
if !matchAny(*extract, clean) {
continue
}
switch h.Typeflag {
case tar.TypeDir:
if err := dst.MkdirAll(clean, 0700); err != nil {
return err
}
case tar.TypeReg:
if err := dst.MkdirAll(filepath.Dir(clean), 0700); err != nil {
return err
}
var writeErr error
if h.FileInfo().Size() > 1024*1024 {
writeErr = tasks.Action("extract").Arg("path", clean).Arg("size", humanize.Bytes(uint64(h.FileInfo().Size()))).
Run(ctx, func(ctx context.Context) error {
return writeFile(ctx, dst, clean, tr, h, true)
})
} else {
writeErr = writeFile(ctx, dst, clean, tr, h, false)
}
if writeErr != nil {
return writeErr
}
default:
fmt.Fprintf(console.Warnings(ctx), "ignoring %q (%v)\n", clean, h.Typeflag)
}
}
return nil
})
}
func writeFile(ctx context.Context, dst fnfs.WriteFS, clean string, tr *tar.Reader, h *tar.Header, progress bool) error {
w, err := dst.OpenWrite(clean, h.FileInfo().Mode().Perm())
if err != nil {
return err
}
var r io.Reader
if progress {
x := artifacts.NewProgressReader(io.NopCloser(tr), uint64(h.FileInfo().Size()))
r = x
tasks.Attachments(ctx).SetProgress(x)
} else {
r = tr
}
_, copyErr := io.Copy(w, r)
closeErr := w.Close()
if copyErr == nil {
copyErr = closeErr
}
if copyErr != nil {
return copyErr
}
return nil
}
func flatten() *cobra.Command {
flatten := &cobra.Command{
Use: "flatten <image-ref> --target <path/to/file>",
Short: "Flatten an image to a tar file in the filesystem.",
}
image := imageFromArgs(flatten)
target := flatten.Flags().String("target", "", "Where the image should be unpacked to.")
_ = flatten.MarkFlagRequired("target")
return fncobra.With(flatten, func(ctx context.Context) error {
f, err := os.Create(*target)
if err != nil {
return err
}
r := mutate.Extract(*image)
defer r.Close()
if _, err := io.Copy(f, r); err != nil {
return err
}
return f.Close()
})
}
func makeFSImage() *cobra.Command {
cmd := &cobra.Command{
Use: "mkfsimage <image-ref> --kind ext4|squashfs --target <path/to/file>",
Short: "Flatten an image to a filesystem in the filesystem.",
}
image := imageFromArgs(cmd)
kind := cmd.Flags().String("kind", "ext4", "The filesystem kind.")
target := cmd.Flags().String("target", "", "Where the image should be unpacked to.")
size := cmd.Flags().String("size", "0", "The size of the resulting image.")
_ = cmd.MarkFlagRequired("target")
return fncobra.With(cmd, func(ctx context.Context) error {
sz, err := units.FromHumanSize(*size)
if err != nil {
return err
}
switch strings.ToLower(*kind) {
case "squashfs", "squash":
return binary.ToLocalSquashFS(ctx, *image, *target)
case "ext4fs", "ext4":
dir, err := os.MkdirTemp("", "ext4")
if err != nil {
return err
}
defer os.RemoveAll(dir)
return binary.MakeExt4Image(ctx, *image, dir, *target, sz)
}
return fnerrors.BadInputError("make_fs_image: unsupported filesystem %q", *kind)
})
}
func resolveImage(ctx context.Context, image string, env cfg.Context, pl *parsing.PackageLoader) (oci.Image, error) {
if strings.HasPrefix(image, "tar:") {
return tarball.ImageFromPath(strings.TrimPrefix(image, "tar:"), nil)
}
if strings.HasPrefix(image, "docker:") {
n, err := name.ParseReference(strings.TrimPrefix(image, "docker:"))
if err != nil {
return nil, err
}
return daemon.Image(n, daemon.WithContext(ctx), daemon.WithBufferedOpener())
}
if strings.HasPrefix(image, "binary:") {
ref, err := schema.StrictParsePackageRef(strings.TrimPrefix(image, "binary:"))
if err != nil {
return nil, err
}
prepared, err := binary.Load(ctx, pl, env, ref, binary.BuildImageOpts{})
if err != nil {
return nil, err
}
sealedCtx := pkggraph.MakeSealedContext(env, pl.Seal())
deferred, err := prepared.Image(ctx, sealedCtx)
if err != nil {
return nil, err
}
resolvable, err := compute.GetValue(ctx, deferred)
if err != nil {
return nil, err
}
return resolvable.Image()
}
insecure := false
if strings.HasPrefix(image, "insecure:") {
insecure = true
image = strings.TrimPrefix(image, "insecure:")
}
platform := docker.HostPlatform()
return compute.GetValue(ctx, oci.ImageP(image, &platform, oci.RegistryAccess{InsecureRegistry: insecure}))
}
func matchAny(files []string, path string) bool {
if len(files) == 0 {
return true
}
return slices.Contains(files, path)
}
func imageFromArgs(cmd *cobra.Command) *oci.Image {
env := fncobra.EnvFromValue(cmd, pointer.String("dev"))
targetImage := new(oci.Image)
cmd.Flags().Var(build.BuildPlatformsVar{}, "build_platforms", "Which platforms to build the binary for.")
fncobra.PushParse(cmd, func(ctx context.Context, args []string) error {
if len(args) != 1 {
return fnerrors.New("expected a single argument, with an image reference")
}
image, err := resolveImage(ctx, args[0], *env, parsing.NewPackageLoader(*env))
if err != nil {
return err
}
*targetImage = image
return nil
})
return targetImage
}