Skip to content

Commit

Permalink
Repeat system pruning until there is nothing removed
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Dec 9, 2020
1 parent dd295f2 commit a59e2a1
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 47 deletions.
2 changes: 1 addition & 1 deletion cmd/podman/containers/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion cmd/podman/images/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ Are you sure you want to continue? [y/N] `)
return err
}

return utils.PrintImagePruneResults(results)
return utils.PrintImagePruneResults(results, false)
}
2 changes: 1 addition & 1 deletion cmd/podman/pods/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
19 changes: 9 additions & 10 deletions cmd/podman/system/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 16 additions & 4 deletions cmd/podman/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/volumes/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions docs/source/markdown/podman-system-prune.1.md
Original file line number Diff line number Diff line change
@@ -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*]
Expand All @@ -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**

Expand Down
2 changes: 1 addition & 1 deletion docs/source/markdown/podman-system.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
76 changes: 50 additions & 26 deletions pkg/domain/infra/abi/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit a59e2a1

Please sign in to comment.