containerd/cmd/ctr/checkpoint.go
Jacob Wen abbec62620 cmd/ctr: create an image for checkpoint
This allows one to manage the checkpoints by using the `ctr image`
command.

The image is created with label "containerd.io/checkpoint". By
default, it is not included in the output of `ctr images ls`.
We can list the images by using the following command:
$ ctr images ls labels.containerd.\"io/checkpoint\"==true

Fixes #1026

Signed-off-by: Jacob Wen <jian.w.wen@oracle.com>
2017-10-17 11:25:37 -04:00

69 lines
1.4 KiB
Go

package main
import (
"fmt"
"github.com/containerd/containerd"
"github.com/containerd/containerd/images"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
var taskCheckpointCommand = cli.Command{
Name: "checkpoint",
Usage: "checkpoint a container",
ArgsUsage: "CONTAINER",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "exit",
Usage: "stop the container after the checkpoint",
},
},
Action: func(context *cli.Context) error {
var (
ctx, cancel = appContext(context)
id = context.Args().First()
)
defer cancel()
if id == "" {
return errors.New("container id must be provided")
}
client, err := newClient(context)
if err != nil {
return err
}
container, err := client.LoadContainer(ctx, id)
if err != nil {
return err
}
task, err := container.Task(ctx, nil)
if err != nil {
return err
}
var opts []containerd.CheckpointTaskOpts
if context.Bool("exit") {
opts = append(opts, containerd.WithExit)
}
checkpoint, err := task.Checkpoint(ctx, opts...)
if err != nil {
return err
}
labels := map[string]string{
"containerd.io/checkpoint": "true",
}
img := images.Image{
Name: checkpoint.Digest.String(),
Target: checkpoint,
Labels: labels,
}
_, err = client.ImageService().Create(ctx, img)
if err != nil {
return err
}
fmt.Println(checkpoint.Digest.String())
return nil
},
}