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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/containerd/containerd"
|
"github.com/containerd/containerd"
|
||||||
|
"github.com/containerd/containerd/log"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
var deleteCommand = cli.Command{
|
var containersDeleteCommand = cli.Command{
|
||||||
Name: "delete",
|
Name: "delete",
|
||||||
Usage: "delete an existing container",
|
Usage: "delete an existing container",
|
||||||
ArgsUsage: "CONTAINER",
|
ArgsUsage: "CONTAINER",
|
||||||
|
Aliases: []string{"del", "rm"},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "keep-snapshot",
|
Name: "keep-snapshot",
|
||||||
@ -18,6 +21,7 @@ var deleteCommand = cli.Command{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(context *cli.Context) error {
|
Action: func(context *cli.Context) error {
|
||||||
|
var exitErr error
|
||||||
ctx, cancel := appContext(context)
|
ctx, cancel := appContext(context)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
client, err := newClient(context)
|
client, err := newClient(context)
|
||||||
@ -28,24 +32,39 @@ var deleteCommand = cli.Command{
|
|||||||
if !context.Bool("keep-snapshot") {
|
if !context.Bool("keep-snapshot") {
|
||||||
deleteOpts = append(deleteOpts, containerd.WithSnapshotCleanup)
|
deleteOpts = append(deleteOpts, containerd.WithSnapshotCleanup)
|
||||||
}
|
}
|
||||||
container, err := client.LoadContainer(ctx, context.Args().First())
|
|
||||||
if err != nil {
|
for _, arg := range context.Args() {
|
||||||
return err
|
if err := deleteContainer(ctx, client, arg, deleteOpts...); err != nil {
|
||||||
}
|
if exitErr == nil {
|
||||||
task, err := container.Task(ctx, nil)
|
exitErr = err
|
||||||
if err != nil {
|
}
|
||||||
return container.Delete(ctx, deleteOpts...)
|
log.G(ctx).WithError(err).Errorf("failed to delete container %q", arg)
|
||||||
}
|
|
||||||
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, 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,
|
checkpointCommand,
|
||||||
containersCommand,
|
containersCommand,
|
||||||
contentCommand,
|
contentCommand,
|
||||||
deleteCommand,
|
|
||||||
eventsCommand,
|
eventsCommand,
|
||||||
execCommand,
|
execCommand,
|
||||||
fetchCommand,
|
fetchCommand,
|
||||||
@ -103,6 +102,7 @@ var containersCommand = cli.Command{
|
|||||||
Aliases: []string{"c"},
|
Aliases: []string{"c"},
|
||||||
Subcommands: []cli.Command{
|
Subcommands: []cli.Command{
|
||||||
containersListCommand,
|
containersListCommand,
|
||||||
|
containersDeleteCommand,
|
||||||
containersSetLabelsCommand,
|
containersSetLabelsCommand,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user