Merge pull request #1255 from stevvooe/multi-delete
cmd/ctr: allow deleting multiple containers at once
This commit is contained in:
commit
afe0ce3543
@ -1,16 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/containerd/containerd"
|
||||
"github.com/containerd/containerd/log"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var deleteCommand = cli.Command{
|
||||
var containersDeleteCommand = cli.Command{
|
||||
Name: "delete",
|
||||
Usage: "delete an existing container",
|
||||
ArgsUsage: "CONTAINER",
|
||||
Aliases: []string{"del", "rm"},
|
||||
Flags: []cli.Flag{
|
||||
cli.BoolFlag{
|
||||
Name: "keep-snapshot",
|
||||
@ -18,6 +21,7 @@ var deleteCommand = cli.Command{
|
||||
},
|
||||
},
|
||||
Action: func(context *cli.Context) error {
|
||||
var exitErr error
|
||||
ctx, cancel := appContext(context)
|
||||
defer cancel()
|
||||
client, err := newClient(context)
|
||||
@ -28,24 +32,39 @@ var deleteCommand = cli.Command{
|
||||
if !context.Bool("keep-snapshot") {
|
||||
deleteOpts = append(deleteOpts, containerd.WithSnapshotCleanup)
|
||||
}
|
||||
container, err := client.LoadContainer(ctx, context.Args().First())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
task, err := container.Task(ctx, nil)
|
||||
if err != nil {
|
||||
return container.Delete(ctx, deleteOpts...)
|
||||
}
|
||||
status, err := task.Status(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if status == containerd.Stopped || status == containerd.Created {
|
||||
if _, err := task.Delete(ctx); err != nil {
|
||||
return err
|
||||
|
||||
for _, arg := range context.Args() {
|
||||
if err := deleteContainer(ctx, client, arg, deleteOpts...); err != nil {
|
||||
if exitErr == nil {
|
||||
exitErr = err
|
||||
}
|
||||
log.G(ctx).WithError(err).Errorf("failed to delete container %q", arg)
|
||||
}
|
||||
return container.Delete(ctx, deleteOpts...)
|
||||
}
|
||||
return fmt.Errorf("cannot delete a non stopped container: %v", status)
|
||||
|
||||
return exitErr
|
||||
},
|
||||
}
|
||||
|
||||
func deleteContainer(ctx context.Context, client *containerd.Client, id string, opts ...containerd.DeleteOpts) error {
|
||||
container, err := client.LoadContainer(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
task, err := container.Task(ctx, nil)
|
||||
if err != nil {
|
||||
return container.Delete(ctx, opts...)
|
||||
}
|
||||
status, err := task.Status(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if status == containerd.Stopped || status == containerd.Created {
|
||||
if _, err := task.Delete(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return container.Delete(ctx, opts...)
|
||||
}
|
||||
return fmt.Errorf("cannot delete a non stopped container: %v", status)
|
||||
|
||||
}
|
||||
|
@ -63,7 +63,6 @@ containerd CLI
|
||||
checkpointCommand,
|
||||
containersCommand,
|
||||
contentCommand,
|
||||
deleteCommand,
|
||||
eventsCommand,
|
||||
execCommand,
|
||||
fetchCommand,
|
||||
@ -103,6 +102,7 @@ var containersCommand = cli.Command{
|
||||
Aliases: []string{"c"},
|
||||
Subcommands: []cli.Command{
|
||||
containersListCommand,
|
||||
containersDeleteCommand,
|
||||
containersSetLabelsCommand,
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user