Skip to content

Commit ca07442

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

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

Diff for: pkg/cacheutil/cacheutil.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cacheutil
2+
3+
import (
4+
"fmt"
5+
"path"
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(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("", 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+
}

Diff for: pkg/start/start.go

+2-38
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ import (
1414
"time"
1515

1616
"github.com/coreos/go-semver/semver"
17+
"github.com/lima-vm/lima/pkg/cacheutil"
1718
"github.com/lima-vm/lima/pkg/driver"
1819
"github.com/lima-vm/lima/pkg/driverutil"
1920
"github.com/lima-vm/lima/pkg/osutil"
2021
"github.com/lima-vm/lima/pkg/qemu"
2122
"github.com/lima-vm/lima/pkg/qemu/entitlementutil"
2223

23-
"github.com/lima-vm/lima/pkg/downloader"
24-
"github.com/lima-vm/lima/pkg/fileutils"
2524
hostagentevents "github.com/lima-vm/lima/pkg/hostagent/events"
2625
"github.com/lima-vm/lima/pkg/limayaml"
2726
"github.com/lima-vm/lima/pkg/store"
@@ -33,41 +32,6 @@ import (
3332
// to be running before timing out.
3433
const DefaultWatchHostAgentEventsTimeout = 10 * time.Minute
3534

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

0 commit comments

Comments
 (0)