From a59e2a1a114c039e1780aa2b08b9452dc569cdf4 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Fri, 4 Dec 2020 14:06:52 -0500 Subject: Repeat system pruning until there is nothing removed Signed-off-by: Daniel J Walsh --- cmd/podman/containers/prune.go | 2 +- cmd/podman/images/prune.go | 2 +- cmd/podman/pods/prune.go | 2 +- cmd/podman/system/prune.go | 19 ++++--- cmd/podman/utils/utils.go | 20 +++++-- cmd/podman/volumes/prune.go | 2 +- docs/source/markdown/podman-system-prune.1.md | 4 +- docs/source/markdown/podman-system.1.md | 2 +- pkg/domain/infra/abi/system.go | 76 ++++++++++++++++++--------- 9 files changed, 82 insertions(+), 47 deletions(-) diff --git a/cmd/podman/containers/prune.go b/cmd/podman/containers/prune.go index 9ac529b1c..d3842778b 100644 --- a/cmd/podman/containers/prune.go +++ b/cmd/podman/containers/prune.go @@ -78,5 +78,5 @@ func prune(cmd *cobra.Command, args []string) error { if err != nil { return err } - return utils.PrintContainerPruneResults(responses) + return utils.PrintContainerPruneResults(responses, false) } diff --git a/cmd/podman/images/prune.go b/cmd/podman/images/prune.go index e68fe5f40..268a68681 100644 --- a/cmd/podman/images/prune.go +++ b/cmd/podman/images/prune.go @@ -71,5 +71,5 @@ Are you sure you want to continue? [y/N] `) return err } - return utils.PrintImagePruneResults(results) + return utils.PrintImagePruneResults(results, false) } diff --git a/cmd/podman/pods/prune.go b/cmd/podman/pods/prune.go index 444b0f5e0..e069c9b7f 100644 --- a/cmd/podman/pods/prune.go +++ b/cmd/podman/pods/prune.go @@ -60,5 +60,5 @@ func prune(cmd *cobra.Command, args []string) error { if err != nil { return err } - return utils.PrintPodPruneResults(responses) + return utils.PrintPodPruneResults(responses, false) } diff --git a/cmd/podman/system/prune.go b/cmd/podman/system/prune.go index f2b9a3db5..f57689584 100644 --- a/cmd/podman/system/prune.go +++ b/cmd/podman/system/prune.go @@ -73,32 +73,31 @@ Are you sure you want to continue? [y/N] `, volumeString) return nil } } + // TODO: support for filters in system prune response, err := registry.ContainerEngine().SystemPrune(context.Background(), pruneOptions) if err != nil { return err } - // Print pod prune results - fmt.Println("Deleted Pods") - err = utils.PrintPodPruneResults(response.PodPruneReport) + // Print container prune results + err = utils.PrintContainerPruneResults(response.ContainerPruneReport, true) if err != nil { return err } - // Print container prune results - fmt.Println("Deleted Containers") - err = utils.PrintContainerPruneResults(response.ContainerPruneReport) + // Print pod prune results + err = utils.PrintPodPruneResults(response.PodPruneReport, true) if err != nil { return err } // Print Volume prune results if pruneOptions.Volume { - fmt.Println("Deleted Volumes") - err = utils.PrintVolumePruneResults(response.VolumePruneReport) + err = utils.PrintVolumePruneResults(response.VolumePruneReport, true) if err != nil { return err } } // Print Images prune results - fmt.Println("Deleted Images") - return utils.PrintImagePruneResults(response.ImagePruneReport) + utils.PrintImagePruneResults(response.ImagePruneReport, true) + + return nil } diff --git a/cmd/podman/utils/utils.go b/cmd/podman/utils/utils.go index 1c9e4d786..2ca2c4c92 100644 --- a/cmd/podman/utils/utils.go +++ b/cmd/podman/utils/utils.go @@ -26,8 +26,11 @@ func FileExists(path string) bool { return !file.IsDir() } -func PrintPodPruneResults(podPruneReports []*entities.PodPruneReport) error { +func PrintPodPruneResults(podPruneReports []*entities.PodPruneReport, heading bool) error { var errs OutputErrors + if heading && len(podPruneReports) > 0 { + fmt.Println("Deleted Pods") + } for _, r := range podPruneReports { if r.Err == nil { fmt.Println(r.Id) @@ -38,8 +41,11 @@ func PrintPodPruneResults(podPruneReports []*entities.PodPruneReport) error { return errs.PrintErrors() } -func PrintContainerPruneResults(containerPruneReport *entities.ContainerPruneReport) error { +func PrintContainerPruneResults(containerPruneReport *entities.ContainerPruneReport, heading bool) error { var errs OutputErrors + if heading && (len(containerPruneReport.ID) > 0 || len(containerPruneReport.Err) > 0) { + fmt.Println("Deleted Containers") + } for k := range containerPruneReport.ID { fmt.Println(k) } @@ -49,8 +55,11 @@ func PrintContainerPruneResults(containerPruneReport *entities.ContainerPruneRep return errs.PrintErrors() } -func PrintVolumePruneResults(volumePruneReport []*entities.VolumePruneReport) error { +func PrintVolumePruneResults(volumePruneReport []*entities.VolumePruneReport, heading bool) error { var errs OutputErrors + if heading && len(volumePruneReport) > 0 { + fmt.Println("Deleted Volumes") + } for _, r := range volumePruneReport { if r.Err == nil { fmt.Println(r.Id) @@ -61,7 +70,10 @@ func PrintVolumePruneResults(volumePruneReport []*entities.VolumePruneReport) er return errs.PrintErrors() } -func PrintImagePruneResults(imagePruneReport *entities.ImagePruneReport) error { +func PrintImagePruneResults(imagePruneReport *entities.ImagePruneReport, heading bool) error { + if heading && (len(imagePruneReport.Report.Id) > 0 || len(imagePruneReport.Report.Err) > 0) { + fmt.Println("Deleted Images") + } for _, i := range imagePruneReport.Report.Id { fmt.Println(i) } diff --git a/cmd/podman/volumes/prune.go b/cmd/podman/volumes/prune.go index 4c2136dcf..d1370120b 100644 --- a/cmd/podman/volumes/prune.go +++ b/cmd/podman/volumes/prune.go @@ -62,5 +62,5 @@ func prune(cmd *cobra.Command, args []string) error { if err != nil { return err } - return utils.PrintVolumePruneResults(responses) + return utils.PrintVolumePruneResults(responses, false) } diff --git a/docs/source/markdown/podman-system-prune.1.md b/docs/source/markdown/podman-system-prune.1.md index 9a078648b..431a11267 100644 --- a/docs/source/markdown/podman-system-prune.1.md +++ b/docs/source/markdown/podman-system-prune.1.md @@ -1,7 +1,7 @@ % podman-system-prune(1) ## NAME -podman\-system\-prune - Remove all unused container, image and volume data +podman\-system\-prune - Remove all unused pod, container, image and volume data ## SYNOPSIS **podman system prune** [*options*] @@ -16,7 +16,7 @@ By default, volumes are not removed to prevent important data from being deleted ## OPTIONS #### **--all**, **-a** -Remove all unused images not just dangling ones. +Recursively remove all unused pod, container, image and volume data (Maximum 50 iterations.) #### **--force**, **-f** diff --git a/docs/source/markdown/podman-system.1.md b/docs/source/markdown/podman-system.1.md index 9ac73237e..7b9081b84 100644 --- a/docs/source/markdown/podman-system.1.md +++ b/docs/source/markdown/podman-system.1.md @@ -17,7 +17,7 @@ The system command allows you to manage the podman systems | df | [podman-system-df(1)](podman-system-df.1.md) | Show podman disk usage. | | info | [podman-system-info(1)](podman-info.1.md) | Displays Podman related system information. | | migrate | [podman-system-migrate(1)](podman-system-migrate.1.md) | Migrate existing containers to a new podman version. | -| prune | [podman-system-prune(1)](podman-system-prune.1.md) | Remove all unused container, image and volume data. | +| prune | [podman-system-prune(1)](podman-system-prune.1.md) | Remove all unused pod, container, image and volume data. | | renumber | [podman-system-renumber(1)](podman-system-renumber.1.md) | Migrate lock numbers to handle a change in maximum number of locks. | | reset | [podman-system-reset(1)](podman-system-reset.1.md) | Reset storage back to initial state. | | service | [podman-system-service(1)](podman-system-service.1.md) | Run an API service | diff --git a/pkg/domain/infra/abi/system.go b/pkg/domain/infra/abi/system.go index ec2532bea..7ed58092b 100644 --- a/pkg/domain/infra/abi/system.go +++ b/pkg/domain/infra/abi/system.go @@ -168,37 +168,61 @@ func checkInput() error { // nolint:deadcode,unused // SystemPrune removes unused data from the system. Pruning pods, containers, volumes and images. func (ic *ContainerEngine) SystemPrune(ctx context.Context, options entities.SystemPruneOptions) (*entities.SystemPruneReport, error) { var systemPruneReport = new(entities.SystemPruneReport) - podPruneReport, err := ic.prunePodHelper(ctx) - if err != nil { - return nil, err - } - systemPruneReport.PodPruneReport = podPruneReport - - containerPruneReport, err := ic.pruneContainersHelper(nil) - if err != nil { - return nil, err - } - systemPruneReport.ContainerPruneReport = containerPruneReport - - results, err := ic.Libpod.ImageRuntime().PruneImages(ctx, options.All, nil) - if err != nil { - return nil, err - } - report := entities.ImagePruneReport{ - Report: entities.Report{ - Id: results, - Err: nil, - }, - } + found := true + for found { + found = false + podPruneReport, err := ic.prunePodHelper(ctx) + if err != nil { + return nil, err + } + if len(podPruneReport) > 0 { + found = true + } + systemPruneReport.PodPruneReport = append(systemPruneReport.PodPruneReport, podPruneReport...) + containerPruneReport, err := ic.pruneContainersHelper(nil) + if err != nil { + return nil, err + } + if len(containerPruneReport.ID) > 0 { + found = true + } + if systemPruneReport.ContainerPruneReport == nil { + systemPruneReport.ContainerPruneReport = containerPruneReport + } else { + for name, val := range containerPruneReport.ID { + systemPruneReport.ContainerPruneReport.ID[name] = val + } + } - systemPruneReport.ImagePruneReport = &report + results, err := ic.Libpod.ImageRuntime().PruneImages(ctx, options.All, nil) - if options.Volume { - volumePruneReport, err := ic.pruneVolumesHelper(ctx) if err != nil { return nil, err } - systemPruneReport.VolumePruneReport = volumePruneReport + if len(results) > 0 { + found = true + } + + if systemPruneReport.ImagePruneReport == nil { + systemPruneReport.ImagePruneReport = &entities.ImagePruneReport{ + Report: entities.Report{ + Id: results, + Err: nil, + }, + } + } else { + systemPruneReport.ImagePruneReport.Report.Id = append(systemPruneReport.ImagePruneReport.Report.Id, results...) + } + if options.Volume { + volumePruneReport, err := ic.pruneVolumesHelper(ctx) + if err != nil { + return nil, err + } + if len(volumePruneReport) > 0 { + found = true + } + systemPruneReport.VolumePruneReport = append(systemPruneReport.VolumePruneReport, volumePruneReport...) + } } return systemPruneReport, nil } -- cgit v1.2.3-54-g00ecf