2016-05-12 10:52:00 -04:00
|
|
|
package checkpoint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type createOptions struct {
|
2016-09-19 12:01:16 -04:00
|
|
|
container string
|
|
|
|
checkpoint string
|
|
|
|
checkpointDir string
|
|
|
|
leaveRunning bool
|
2016-05-12 10:52:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
var opts createOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-11-08 03:15:09 -05:00
|
|
|
Use: "create [OPTIONS] CONTAINER CHECKPOINT",
|
2016-05-12 10:52:00 -04:00
|
|
|
Short: "Create a checkpoint from a running container",
|
|
|
|
Args: cli.ExactArgs(2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.container = args[0]
|
|
|
|
opts.checkpoint = args[1]
|
|
|
|
return runCreate(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2016-11-08 03:15:09 -05:00
|
|
|
flags.BoolVar(&opts.leaveRunning, "leave-running", false, "Leave the container running after checkpoint")
|
|
|
|
flags.StringVarP(&opts.checkpointDir, "checkpoint-dir", "", "", "Use a custom checkpoint storage directory")
|
2016-05-12 10:52:00 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCreate(dockerCli *command.DockerCli, opts createOptions) error {
|
|
|
|
client := dockerCli.Client()
|
|
|
|
|
|
|
|
checkpointOpts := types.CheckpointCreateOptions{
|
2016-09-19 12:01:16 -04:00
|
|
|
CheckpointID: opts.checkpoint,
|
|
|
|
CheckpointDir: opts.checkpointDir,
|
|
|
|
Exit: !opts.leaveRunning,
|
2016-05-12 10:52:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err := client.CheckpointCreate(context.Background(), opts.container, checkpointOpts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|