Skip to content

Commit

Permalink
Implement lima prune --dry-run
Browse files Browse the repository at this point in the history
  • Loading branch information
jay7x committed May 26, 2023
1 parent 6a9c606 commit 1917f8b
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions cmd/limactl/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,30 @@ func newPruneCommand() *cobra.Command {
ValidArgsFunction: cobra.NoFileCompletions,
}

pruneCommand.Flags().Bool("dry-run", false, "Show which images would be deleted, but don't really delete them")
pruneCommand.Flags().Bool("no-digest-only", false, "Only prune images without a digest specified (\"fallback\" images usually)")
pruneCommand.Flags().Bool("unreferenced-only", false, "Only prune downloads not referenced in any VM")
return pruneCommand
}

func deletePathMaybe(path string, isDryRun bool) error {
if isDryRun {
return nil
}
err := os.RemoveAll(path)
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"path": path,
}).Errorf("Cannot delete directory. Skipping...")
}
return err
}

func pruneAction(cmd *cobra.Command, args []string) error {
pruneDryRun, err := cmd.Flags().GetBool("dry-run")
if err != nil {
return err
}
pruneWithoutDigest, err := cmd.Flags().GetBool("no-digest-only")
if err != nil {
return err
Expand Down Expand Up @@ -55,34 +73,28 @@ func pruneAction(cmd *cobra.Command, args []string) error {

logrus.WithFields(entryFields).Debug("cache entry")

if pruneDryRun {
entryFields["is_dry_run"] = pruneDryRun
}

// check if the cache entry is referenced
if refFile, refFound := files[entry.ID]; refFound {
if refFile.Location != entry.Location { // sanity check
logrus.WithFields(logrus.Fields{
"id": entry.ID,
"location": entry.Location,
"referenced_location": refFile.Location,
}).Warnf("Sanity check failed! URL mismatch")
ef := entryFields
ef["referenced_location"] = refFile.Location
logrus.WithFields(ef).Warnf("Sanity check failed! URL mismatch")
}

if pruneWithoutDigest && refFile.Digest == "" {
// delete the fallback image entry (entry w/o digest) even if referenced
logrus.WithFields(entryFields).Infof("Deleting fallback entry")
if err := os.RemoveAll(entry.Path); err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"path": entry.Path,
}).Errorf("Cannot delete directory. Skipping...")
}
deletePathMaybe(entry.Path, pruneDryRun)
}
} else {
if pruneUnreferenced {
// delete the unreferenced cached entry
logrus.WithFields(entryFields).Infof("Deleting unreferenced entry")
if err := os.RemoveAll(entry.Path); err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"path": entry.Path,
}).Errorf("Cannot delete directory. Skipping...")
}
deletePathMaybe(entry.Path, pruneDryRun)
}
}
}
Expand All @@ -95,8 +107,11 @@ func pruneAction(cmd *cobra.Command, args []string) error {
return err
}
cacheDir := filepath.Join(ucd, "lima")
logrus.Infof("Pruning everything in %q", cacheDir)
return os.RemoveAll(cacheDir)
logrus.WithFields(logrus.Fields{
"is_dry_run": pruneDryRun,
"location": cacheDir,
}).Infof("Pruning entire cache")
return deletePathMaybe(cacheDir, pruneDryRun)
}

// Collect all downloads referenced in VM definitions and templates
Expand Down

0 comments on commit 1917f8b

Please sign in to comment.