Port ctr to use client
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
|
||||
containersapi "github.com/containerd/containerd/api/services/containers"
|
||||
"github.com/containerd/containerd/api/services/execution"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@@ -19,49 +13,17 @@ var deleteCommand = cli.Command{
|
||||
Action: func(context *cli.Context) error {
|
||||
ctx, cancel := appContext(context)
|
||||
defer cancel()
|
||||
|
||||
containers, err := getContainersService(context)
|
||||
client, err := newClient(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tasks, err := getTasksService(context)
|
||||
container, err := client.LoadContainer(ctx, context.Args().First())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
snapshotter, err := getSnapshotter(context)
|
||||
if err != nil {
|
||||
return err
|
||||
if _, err := container.Task(ctx, nil); err == nil {
|
||||
return fmt.Errorf("cannot delete a container with a running task")
|
||||
}
|
||||
id := context.Args().First()
|
||||
if id == "" {
|
||||
return errors.New("container id must be provided")
|
||||
}
|
||||
|
||||
_, err = containers.Delete(ctx, &containersapi.DeleteContainerRequest{
|
||||
ID: id,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to delete container")
|
||||
}
|
||||
|
||||
_, err = tasks.Delete(ctx, &execution.DeleteRequest{
|
||||
ContainerID: id,
|
||||
})
|
||||
if err != nil {
|
||||
// Ignore error if task has already been removed, task is
|
||||
// removed by default after run
|
||||
if grpc.Code(errors.Cause(err)) != codes.NotFound {
|
||||
return errors.Wrap(err, "failed to task container")
|
||||
}
|
||||
}
|
||||
|
||||
if runtime.GOOS != "windows" {
|
||||
if err := snapshotter.Remove(ctx, id); err != nil {
|
||||
return errors.Wrapf(err, "failed to remove snapshot %q", id)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return container.Delete(ctx)
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user