Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 7 additions & 32 deletions cmd/podman/cleanup.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package main

import (
"fmt"
"os"

"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/libpodruntime"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -49,38 +46,16 @@ func init() {
}

func cleanupCmd(c *cliconfig.CleanupValues) error {
runtime, err := libpodruntime.GetRuntime(getContext(), &c.PodmanCommand)
runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
if err != nil {
return errors.Wrapf(err, "could not get runtime")
}
defer runtime.Shutdown(false)

cleanupContainers, lastError := getAllOrLatestContainers(&c.PodmanCommand, runtime, -1, "all")

ctx := getContext()

for _, ctr := range cleanupContainers {
hadError := false
if c.Remove {
if err := runtime.RemoveContainer(ctx, ctr, false, true); err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = errors.Wrapf(err, "failed to cleanup and remove container %v", ctr.ID())
hadError = true
}
} else {
if err := ctr.Cleanup(ctx); err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = errors.Wrapf(err, "failed to cleanup container %v", ctr.ID())
hadError = true
}
}
if !hadError {
fmt.Println(ctr.ID())
}
ok, failures, err := runtime.CleanupContainers(getContext(), c)
if err != nil {
return err
}
return lastError

return printCmdResults(ok, failures)
}
42 changes: 42 additions & 0 deletions pkg/adapter/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,3 +834,45 @@ func (r *LocalRuntime) Prune(ctx context.Context, maxWorkers int, force bool) ([
}
return pool.Run()
}

// CleanupContainers any leftovers bits of stopped containers
func (r *LocalRuntime) CleanupContainers(ctx context.Context, cli *cliconfig.CleanupValues) ([]string, map[string]error, error) {
var (
ok = []string{}
failures = map[string]error{}
)

ctrs, err := shortcuts.GetContainersByContext(cli.All, cli.Latest, cli.InputArgs, r.Runtime)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't know why we actually support --all and --latest for cleanup... but since it made it into the interface, we can't really remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just remove the documentation and hide these fields. I don't believe that anyone actually ever runs container cleanup directly. We could probably hide the entire implementation from users.

if err != nil {
return ok, failures, err
}

for _, ctr := range ctrs {
if cli.Remove {
err = removeContainer(ctx, ctr, r)
} else {
err = cleanupContainer(ctx, ctr, r)
}

if err == nil {
ok = append(ok, ctr.ID())
} else {
failures[ctr.ID()] = err
}
}
return ok, failures, nil
}

func removeContainer(ctx context.Context, ctr *libpod.Container, runtime *LocalRuntime) error {
if err := runtime.RemoveContainer(ctx, ctr, false, true); err != nil {
return errors.Wrapf(err, "failed to cleanup and remove container %v", ctr.ID())
}
return nil
}

func cleanupContainer(ctx context.Context, ctr *libpod.Container, runtime *LocalRuntime) error {
if err := ctr.Cleanup(ctx); err != nil {
return errors.Wrapf(err, "failed to cleanup container %v", ctr.ID())
}
return nil
}
5 changes: 5 additions & 0 deletions pkg/adapter/containers_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,3 +880,8 @@ func (r *LocalRuntime) Prune(ctx context.Context, maxWorkers int, force bool) ([
}
return ok, failures, nil
}

// Cleanup any leftovers bits of stopped containers
func (r *LocalRuntime) CleanupContainers(ctx context.Context, cli *cliconfig.CleanupValues) ([]string, map[string]error, error) {
return nil, nil, errors.New("container cleanup not supported for remote clients")
}