Merge pull request #2995 from jwhonce/wip/cleanup

Refactor container cleanup to use latest functions
This commit is contained in:
OpenShift Merge Robot 2019-04-30 20:10:27 +02:00 committed by GitHub
commit e509eb25e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 32 deletions

View File

@ -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"
)
@ -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)
}

View File

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

View File

@ -883,3 +883,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")
}