Skip to content

Commit 48b26ab

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

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

pkg/cacheutil/cacheutil.go

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

pkg/start/start.go

+2-38
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ 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/osutil"
2122
"github.com/lima-vm/lima/pkg/qemu"
2223
"github.com/lima-vm/lima/pkg/qemu/entitlementutil"
2324
"github.com/mattn/go-isatty"
2425

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

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

0 commit comments

Comments
 (0)