Skip to content

Commit d99e29c

Browse files
committed
Move NerdctlArchiveCache function to cacheutil
Signed-off-by: Anders F Björklund <[email protected]>
1 parent ca2c432 commit d99e29c

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

Diff for: pkg/cacheutil/cacheutil.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cacheutil
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/lima-vm/lima/pkg/downloader"
8+
"github.com/lima-vm/lima/pkg/fileutils"
9+
"github.com/lima-vm/lima/pkg/limayaml"
10+
)
11+
12+
// EnsureNerdctlArchiveCache prefetches the archive into the cache.
13+
//
14+
// EnsureNerdctlArchiveCache prefetches the nerdctl-full-VERSION-GOOS-GOARCH.tar.gz archive
15+
// into the cache before launching the hostagent process, so that we can show the progress in tty.
16+
// https://github.com/lima-vm/lima/issues/326
17+
func EnsureNerdctlArchiveCache(ctx context.Context, y *limayaml.LimaYAML, created bool) (string, error) {
18+
if !*y.Containerd.System && !*y.Containerd.User {
19+
// nerdctl archive is not needed
20+
return "", nil
21+
}
22+
23+
errs := make([]error, len(y.Containerd.Archives))
24+
for i, f := range y.Containerd.Archives {
25+
// Skip downloading again if the file is already in the cache
26+
if created && f.Arch == *y.Arch && !downloader.IsLocal(f.Location) {
27+
path, err := fileutils.CachedFile(f)
28+
if err == nil {
29+
return path, nil
30+
}
31+
}
32+
path, err := fileutils.DownloadFile(ctx, "", f, false, "the nerdctl archive", *y.Arch)
33+
if err != nil {
34+
errs[i] = err
35+
continue
36+
}
37+
if path == "" {
38+
if downloader.IsLocal(f.Location) {
39+
return f.Location, nil
40+
}
41+
return "", fmt.Errorf("cache did not contain %q", f.Location)
42+
}
43+
return path, nil
44+
}
45+
46+
return "", fileutils.Errors(errs)
47+
}

Diff for: pkg/instance/start.go

+2-38
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
"github.com/coreos/go-semver/semver"
18+
"github.com/lima-vm/lima/pkg/cacheutil"
1819
"github.com/lima-vm/lima/pkg/driver"
1920
"github.com/lima-vm/lima/pkg/driverutil"
2021
"github.com/lima-vm/lima/pkg/executil"
@@ -23,8 +24,6 @@ import (
2324
"github.com/lima-vm/lima/pkg/qemu/entitlementutil"
2425
"github.com/mattn/go-isatty"
2526

26-
"github.com/lima-vm/lima/pkg/downloader"
27-
"github.com/lima-vm/lima/pkg/fileutils"
2827
hostagentevents "github.com/lima-vm/lima/pkg/hostagent/events"
2928
"github.com/lima-vm/lima/pkg/limayaml"
3029
"github.com/lima-vm/lima/pkg/store"
@@ -36,41 +35,6 @@ import (
3635
// to be running before timing out.
3736
const DefaultWatchHostAgentEventsTimeout = 10 * time.Minute
3837

39-
// ensureNerdctlArchiveCache prefetches the nerdctl-full-VERSION-GOOS-GOARCH.tar.gz archive
40-
// into the cache before launching the hostagent process, so that we can show the progress in tty.
41-
// https://github.com/lima-vm/lima/issues/326
42-
func ensureNerdctlArchiveCache(ctx context.Context, y *limayaml.LimaYAML, created bool) (string, error) {
43-
if !*y.Containerd.System && !*y.Containerd.User {
44-
// nerdctl archive is not needed
45-
return "", nil
46-
}
47-
48-
errs := make([]error, len(y.Containerd.Archives))
49-
for i, f := range y.Containerd.Archives {
50-
// Skip downloading again if the file is already in the cache
51-
if created && f.Arch == *y.Arch && !downloader.IsLocal(f.Location) {
52-
path, err := fileutils.CachedFile(f)
53-
if err == nil {
54-
return path, nil
55-
}
56-
}
57-
path, err := fileutils.DownloadFile(ctx, "", f, false, "the nerdctl archive", *y.Arch)
58-
if err != nil {
59-
errs[i] = err
60-
continue
61-
}
62-
if path == "" {
63-
if downloader.IsLocal(f.Location) {
64-
return f.Location, nil
65-
}
66-
return "", fmt.Errorf("cache did not contain %q", f.Location)
67-
}
68-
return path, nil
69-
}
70-
71-
return "", fileutils.Errors(errs)
72-
}
73-
7438
type Prepared struct {
7539
Driver driver.Driver
7640
NerdctlArchiveCache string
@@ -98,7 +62,7 @@ func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
9862
if err := limaDriver.CreateDisk(ctx); err != nil {
9963
return nil, err
10064
}
101-
nerdctlArchiveCache, err := ensureNerdctlArchiveCache(ctx, inst.Config, created)
65+
nerdctlArchiveCache, err := cacheutil.EnsureNerdctlArchiveCache(ctx, inst.Config, created)
10266
if err != nil {
10367
return nil, err
10468
}

0 commit comments

Comments
 (0)