Skip to content

Commit

Permalink
Move NerdctlArchiveCache function to cacheutil
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
  • Loading branch information
afbjorklund committed Mar 18, 2024
1 parent 09dc821 commit ac70e20
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 38 deletions.
45 changes: 45 additions & 0 deletions pkg/cacheutil/cacheutil.go
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)
}
40 changes: 2 additions & 38 deletions pkg/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ import (
"time"

"github.com/coreos/go-semver/semver"
"github.com/lima-vm/lima/pkg/cacheutil"
"github.com/lima-vm/lima/pkg/driver"
"github.com/lima-vm/lima/pkg/driverutil"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/qemu"
"github.com/lima-vm/lima/pkg/qemu/entitlementutil"
"github.com/mattn/go-isatty"

"github.com/lima-vm/lima/pkg/downloader"
"github.com/lima-vm/lima/pkg/fileutils"
hostagentevents "github.com/lima-vm/lima/pkg/hostagent/events"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/store"
Expand All @@ -35,41 +34,6 @@ import (
// to be running before timing out.
const DefaultWatchHostAgentEventsTimeout = 10 * time.Minute

// 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)
}

type Prepared struct {
Driver driver.Driver
NerdctlArchiveCache string
Expand Down Expand Up @@ -104,7 +68,7 @@ func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
if err := limaDriver.CreateDisk(ctx); err != nil {
return nil, err
}
nerdctlArchiveCache, err := ensureNerdctlArchiveCache(ctx, y, created)
nerdctlArchiveCache, err := cacheutil.EnsureNerdctlArchiveCache(ctx, y, created)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ac70e20

Please sign in to comment.