Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/cacheutil/cacheutil.go
2611 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package cacheutil
5
6
import (
7
"context"
8
"fmt"
9
"path"
10
11
"github.com/lima-vm/lima/v2/pkg/downloader"
12
"github.com/lima-vm/lima/v2/pkg/fileutils"
13
"github.com/lima-vm/lima/v2/pkg/limatype"
14
)
15
16
// NerdctlArchive returns the basename of the archive.
17
func NerdctlArchive(y *limatype.LimaYAML) string {
18
if *y.Containerd.System || *y.Containerd.User {
19
for _, f := range y.Containerd.Archives {
20
if f.Arch == *y.Arch {
21
return path.Base(f.Location)
22
}
23
}
24
}
25
return ""
26
}
27
28
// EnsureNerdctlArchiveCache prefetches the nerdctl-full-VERSION-GOOS-GOARCH.tar.gz archive
29
// into the cache before launching the hostagent process, so that we can show the progress in tty.
30
// https://github.com/lima-vm/lima/issues/326
31
func EnsureNerdctlArchiveCache(ctx context.Context, y *limatype.LimaYAML, created bool) (string, error) {
32
if !*y.Containerd.System && !*y.Containerd.User {
33
// nerdctl archive is not needed
34
return "", nil
35
}
36
37
errs := make([]error, len(y.Containerd.Archives))
38
for i, f := range y.Containerd.Archives {
39
// Skip downloading again if the file is already in the cache
40
if created && f.Arch == *y.Arch && !downloader.IsLocal(f.Location) {
41
path, err := fileutils.CachedFile(f)
42
if err == nil {
43
return path, nil
44
}
45
}
46
path, err := fileutils.DownloadFile(ctx, "", f, false, "the nerdctl archive", *y.Arch)
47
if err != nil {
48
errs[i] = err
49
continue
50
}
51
if path == "" {
52
if downloader.IsLocal(f.Location) {
53
return f.Location, nil
54
}
55
return "", fmt.Errorf("cache did not contain %q", f.Location)
56
}
57
return path, nil
58
}
59
60
return "", fileutils.Errors(errs)
61
}
62
63