2015-12-10 01:03:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
"github.com/docker/containerd/api/grpc/types"
|
|
|
|
netcontext "golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-04-13 07:03:53 +00:00
|
|
|
var checkpointSubCmds = []cli.Command{
|
|
|
|
listCheckpointCommand,
|
|
|
|
createCheckpointCommand,
|
|
|
|
deleteCheckpointCommand,
|
|
|
|
}
|
|
|
|
|
2016-01-23 22:40:55 +00:00
|
|
|
var checkpointCommand = cli.Command{
|
2016-04-13 07:03:53 +00:00
|
|
|
Name: "checkpoints",
|
|
|
|
Usage: "list all checkpoints",
|
|
|
|
ArgsUsage: "COMMAND [arguments...]",
|
|
|
|
Subcommands: checkpointSubCmds,
|
|
|
|
Description: func() string {
|
|
|
|
desc := "\n COMMAND:\n"
|
|
|
|
for _, command := range checkpointSubCmds {
|
|
|
|
desc += fmt.Sprintf(" %-10.10s%s\n", command.Name, command.Usage)
|
|
|
|
}
|
|
|
|
return desc
|
|
|
|
}(),
|
2015-12-10 01:03:53 +00:00
|
|
|
Action: listCheckpoints,
|
|
|
|
}
|
|
|
|
|
2016-01-23 22:40:55 +00:00
|
|
|
var listCheckpointCommand = cli.Command{
|
2015-12-10 01:03:53 +00:00
|
|
|
Name: "list",
|
|
|
|
Usage: "list all checkpoints for a container",
|
|
|
|
Action: listCheckpoints,
|
2016-05-26 00:42:37 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "checkpoint-dir",
|
|
|
|
Value: "",
|
|
|
|
Usage: "path to checkpoint directory",
|
|
|
|
},
|
|
|
|
},
|
2015-12-10 01:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func listCheckpoints(context *cli.Context) {
|
|
|
|
var (
|
2015-12-14 23:54:11 +00:00
|
|
|
c = getClient(context)
|
2015-12-10 19:03:36 +00:00
|
|
|
id = context.Args().First()
|
2015-12-10 01:03:53 +00:00
|
|
|
)
|
|
|
|
if id == "" {
|
2016-04-22 15:08:42 +00:00
|
|
|
fatal("container id cannot be empty", ExitStatusMissingArg)
|
2015-12-10 01:03:53 +00:00
|
|
|
}
|
2015-12-10 19:03:36 +00:00
|
|
|
resp, err := c.ListCheckpoint(netcontext.Background(), &types.ListCheckpointRequest{
|
2016-06-07 20:25:54 +00:00
|
|
|
Id: id,
|
2016-05-26 00:42:37 +00:00
|
|
|
CheckpointDir: context.String("checkpoint-dir"),
|
2015-12-10 01:03:53 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
|
|
|
|
fmt.Fprint(w, "NAME\tTCP\tUNIX SOCKETS\tSHELL\n")
|
|
|
|
for _, c := range resp.Checkpoints {
|
|
|
|
fmt.Fprintf(w, "%s\t%v\t%v\t%v\n", c.Name, c.Tcp, c.UnixSockets, c.Shell)
|
|
|
|
}
|
|
|
|
if err := w.Flush(); err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 22:40:55 +00:00
|
|
|
var createCheckpointCommand = cli.Command{
|
2015-12-10 01:03:53 +00:00
|
|
|
Name: "create",
|
|
|
|
Usage: "create a new checkpoint for the container",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "tcp",
|
|
|
|
Usage: "persist open tcp connections",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "unix-sockets",
|
|
|
|
Usage: "perist unix sockets",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "exit",
|
|
|
|
Usage: "exit the container after the checkpoint completes successfully",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "shell",
|
|
|
|
Usage: "checkpoint shell jobs",
|
|
|
|
},
|
2016-05-26 00:42:37 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "checkpoint-dir",
|
|
|
|
Value: "",
|
|
|
|
Usage: "directory to store checkpoints",
|
|
|
|
},
|
2016-06-07 20:25:54 +00:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "empty-ns",
|
|
|
|
Usage: "create a namespace, but don't restore its properties",
|
|
|
|
},
|
2015-12-10 01:03:53 +00:00
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) {
|
|
|
|
var (
|
|
|
|
containerID = context.Args().Get(0)
|
|
|
|
name = context.Args().Get(1)
|
|
|
|
)
|
|
|
|
if containerID == "" {
|
2016-04-22 15:08:42 +00:00
|
|
|
fatal("container id at cannot be empty", ExitStatusMissingArg)
|
2015-12-10 01:03:53 +00:00
|
|
|
}
|
|
|
|
if name == "" {
|
2016-04-22 15:08:42 +00:00
|
|
|
fatal("checkpoint name cannot be empty", ExitStatusMissingArg)
|
2015-12-10 01:03:53 +00:00
|
|
|
}
|
2015-12-14 23:54:11 +00:00
|
|
|
c := getClient(context)
|
2016-06-07 20:25:54 +00:00
|
|
|
checkpoint := types.Checkpoint{
|
|
|
|
Name: name,
|
|
|
|
Exit: context.Bool("exit"),
|
|
|
|
Tcp: context.Bool("tcp"),
|
|
|
|
Shell: context.Bool("shell"),
|
|
|
|
UnixSockets: context.Bool("unix-sockets"),
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyNSes := context.StringSlice("empty-ns")
|
|
|
|
checkpoint.EmptyNS = append(checkpoint.EmptyNS, emptyNSes...)
|
|
|
|
|
2015-12-10 19:03:36 +00:00
|
|
|
if _, err := c.CreateCheckpoint(netcontext.Background(), &types.CreateCheckpointRequest{
|
2016-06-07 20:25:54 +00:00
|
|
|
Id: containerID,
|
2016-05-26 00:42:37 +00:00
|
|
|
CheckpointDir: context.String("checkpoint-dir"),
|
2016-06-07 20:25:54 +00:00
|
|
|
Checkpoint: &checkpoint,
|
2015-12-10 01:03:53 +00:00
|
|
|
}); err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-23 22:40:55 +00:00
|
|
|
var deleteCheckpointCommand = cli.Command{
|
2015-12-10 01:03:53 +00:00
|
|
|
Name: "delete",
|
|
|
|
Usage: "delete a container's checkpoint",
|
2016-05-26 00:42:37 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "checkpoint-dir",
|
|
|
|
Value: "",
|
|
|
|
Usage: "path to checkpoint directory",
|
|
|
|
},
|
|
|
|
},
|
2015-12-10 01:03:53 +00:00
|
|
|
Action: func(context *cli.Context) {
|
|
|
|
var (
|
|
|
|
containerID = context.Args().Get(0)
|
|
|
|
name = context.Args().Get(1)
|
|
|
|
)
|
|
|
|
if containerID == "" {
|
2016-04-22 15:08:42 +00:00
|
|
|
fatal("container id at cannot be empty", ExitStatusMissingArg)
|
2015-12-10 01:03:53 +00:00
|
|
|
}
|
|
|
|
if name == "" {
|
2016-04-22 15:08:42 +00:00
|
|
|
fatal("checkpoint name cannot be empty", ExitStatusMissingArg)
|
2015-12-10 01:03:53 +00:00
|
|
|
}
|
2015-12-14 23:54:11 +00:00
|
|
|
c := getClient(context)
|
2015-12-10 19:03:36 +00:00
|
|
|
if _, err := c.DeleteCheckpoint(netcontext.Background(), &types.DeleteCheckpointRequest{
|
2016-05-26 00:42:37 +00:00
|
|
|
Id: containerID,
|
|
|
|
Name: name,
|
|
|
|
CheckpointDir: context.String("checkpoint-dir"),
|
2015-12-10 01:03:53 +00:00
|
|
|
}); err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|