-
Notifications
You must be signed in to change notification settings - Fork 653
/
Copy pathcacheutil.go
61 lines (54 loc) · 1.65 KB
/
cacheutil.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
// SPDX-FileCopyrightText: Copyright The Lima Authors
// SPDX-License-Identifier: Apache-2.0
package cacheutil
import (
"context"
"fmt"
"path"
"github.com/lima-vm/lima/pkg/downloader"
"github.com/lima-vm/lima/pkg/fileutils"
"github.com/lima-vm/lima/pkg/limayaml"
)
// NerdctlArchive returns the basename of the archive.
func NerdctlArchive(y *limayaml.LimaYAML) string {
if *y.Containerd.System || *y.Containerd.User {
for _, f := range y.Containerd.Archives {
if f.Arch == *y.Arch {
return path.Base(f.Location)
}
}
}
return ""
}
// EnsureNerdctlArchiveCache prefetches the nerdctl-full-VERSION-GOOS-GOARCH.tar.gz archive
// into the cache before launching the hostagent process, so that we can show the progress in tty.
// https://github.com/lima-vm/lima/issues/326
func EnsureNerdctlArchiveCache(ctx context.Context, y *limayaml.LimaYAML, created bool) (string, error) {
if !*y.Containerd.System && !*y.Containerd.User {
// nerdctl archive is not needed
return "", nil
}
errs := make([]error, len(y.Containerd.Archives))
for i, f := range y.Containerd.Archives {
// Skip downloading again if the file is already in the cache
if created && f.Arch == *y.Arch && !downloader.IsLocal(f.Location) {
path, err := fileutils.CachedFile(f)
if err == nil {
return path, nil
}
}
path, err := fileutils.DownloadFile(ctx, "", f, false, "the nerdctl archive", *y.Arch)
if err != nil {
errs[i] = err
continue
}
if path == "" {
if downloader.IsLocal(f.Location) {
return f.Location, nil
}
return "", fmt.Errorf("cache did not contain %q", f.Location)
}
return path, nil
}
return "", fileutils.Errors(errs)
}