-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move NerdctlArchiveCache function to cacheutil
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
- Loading branch information
1 parent
09dc821
commit ac70e20
Showing
2 changed files
with
47 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cacheutil | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/lima-vm/lima/pkg/downloader" | ||
"github.com/lima-vm/lima/pkg/fileutils" | ||
"github.com/lima-vm/lima/pkg/limayaml" | ||
) | ||
|
||
// 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters