Allow providing a custom storage directory for docker checkpoints

Signed-off-by: boucher <rboucher@gmail.com>
This commit is contained in:
boucher 2016-09-19 12:01:16 -04:00
parent 4b65bdd802
commit 5ddcbc3c00
4 changed files with 61 additions and 19 deletions

View File

@ -10,9 +10,10 @@ import (
) )
type createOptions struct { type createOptions struct {
container string container string
checkpoint string checkpoint string
leaveRunning bool checkpointDir string
leaveRunning bool
} }
func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command { func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
@ -31,6 +32,7 @@ func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
flags := cmd.Flags() flags := cmd.Flags()
flags.BoolVar(&opts.leaveRunning, "leave-running", false, "leave the container running after checkpoint") 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")
return cmd return cmd
} }
@ -39,8 +41,9 @@ func runCreate(dockerCli *command.DockerCli, opts createOptions) error {
client := dockerCli.Client() client := dockerCli.Client()
checkpointOpts := types.CheckpointCreateOptions{ checkpointOpts := types.CheckpointCreateOptions{
CheckpointID: opts.checkpoint, CheckpointID: opts.checkpoint,
Exit: !opts.leaveRunning, CheckpointDir: opts.checkpointDir,
Exit: !opts.leaveRunning,
} }
err := client.CheckpointCreate(context.Background(), opts.container, checkpointOpts) err := client.CheckpointCreate(context.Background(), opts.container, checkpointOpts)

View File

@ -6,27 +6,44 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/cli" "github.com/docker/docker/cli"
"github.com/docker/docker/cli/command" "github.com/docker/docker/cli/command"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type listOptions struct {
checkpointDir string
}
func newListCommand(dockerCli *command.DockerCli) *cobra.Command { func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
return &cobra.Command{ var opts listOptions
cmd := &cobra.Command{
Use: "ls CONTAINER", Use: "ls CONTAINER",
Aliases: []string{"list"}, Aliases: []string{"list"},
Short: "List checkpoints for a container", Short: "List checkpoints for a container",
Args: cli.ExactArgs(1), Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return runList(dockerCli, args[0]) return runList(dockerCli, args[0], opts)
}, },
} }
flags := cmd.Flags()
flags.StringVarP(&opts.checkpointDir, "checkpoint-dir", "", "", "use a custom checkpoint storage directory")
return cmd
} }
func runList(dockerCli *command.DockerCli, container string) error { func runList(dockerCli *command.DockerCli, container string, opts listOptions) error {
client := dockerCli.Client() client := dockerCli.Client()
checkpoints, err := client.CheckpointList(context.Background(), container) listOpts := types.CheckpointListOptions{
CheckpointDir: opts.checkpointDir,
}
checkpoints, err := client.CheckpointList(context.Background(), container, listOpts)
if err != nil { if err != nil {
return err return err
} }

View File

@ -3,24 +3,42 @@ package checkpoint
import ( import (
"golang.org/x/net/context" "golang.org/x/net/context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/cli" "github.com/docker/docker/cli"
"github.com/docker/docker/cli/command" "github.com/docker/docker/cli/command"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type removeOptions struct {
checkpointDir string
}
func newRemoveCommand(dockerCli *command.DockerCli) *cobra.Command { func newRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
return &cobra.Command{ var opts removeOptions
cmd := &cobra.Command{
Use: "rm CONTAINER CHECKPOINT", Use: "rm CONTAINER CHECKPOINT",
Aliases: []string{"remove"}, Aliases: []string{"remove"},
Short: "Remove a checkpoint", Short: "Remove a checkpoint",
Args: cli.ExactArgs(2), Args: cli.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return runRemove(dockerCli, args[0], args[1]) return runRemove(dockerCli, args[0], args[1], opts)
}, },
} }
flags := cmd.Flags()
flags.StringVarP(&opts.checkpointDir, "checkpoint-dir", "", "", "use a custom checkpoint storage directory")
return cmd
} }
func runRemove(dockerCli *command.DockerCli, container string, checkpoint string) error { func runRemove(dockerCli *command.DockerCli, container string, checkpoint string, opts removeOptions) error {
client := dockerCli.Client() client := dockerCli.Client()
return client.CheckpointDelete(context.Background(), container, checkpoint)
removeOpts := types.CheckpointDeleteOptions{
CheckpointID: checkpoint,
CheckpointDir: opts.checkpointDir,
}
return client.CheckpointDelete(context.Background(), container, removeOpts)
} }

View File

@ -17,10 +17,11 @@ import (
) )
type startOptions struct { type startOptions struct {
attach bool attach bool
openStdin bool openStdin bool
detachKeys string detachKeys string
checkpoint string checkpoint string
checkpointDir string
containers []string containers []string
} }
@ -46,6 +47,7 @@ func NewStartCommand(dockerCli *command.DockerCli) *cobra.Command {
if dockerCli.HasExperimental() { if dockerCli.HasExperimental() {
flags.StringVar(&opts.checkpoint, "checkpoint", "", "Restore from this checkpoint") flags.StringVar(&opts.checkpoint, "checkpoint", "", "Restore from this checkpoint")
flags.StringVar(&opts.checkpointDir, "checkpoint-dir", "", "Use a custom checkpoint storage directory")
} }
return cmd return cmd
@ -112,7 +114,8 @@ func runStart(dockerCli *command.DockerCli, opts *startOptions) error {
// no matter it's detached, removed on daemon side(--rm) or exit normally. // no matter it's detached, removed on daemon side(--rm) or exit normally.
statusChan := waitExitOrRemoved(dockerCli, ctx, c.ID, c.HostConfig.AutoRemove) statusChan := waitExitOrRemoved(dockerCli, ctx, c.ID, c.HostConfig.AutoRemove)
startOptions := types.ContainerStartOptions{ startOptions := types.ContainerStartOptions{
CheckpointID: opts.checkpoint, CheckpointID: opts.checkpoint,
CheckpointDir: opts.checkpointDir,
} }
// 4. Start the container. // 4. Start the container.
@ -145,7 +148,8 @@ func runStart(dockerCli *command.DockerCli, opts *startOptions) error {
} }
container := opts.containers[0] container := opts.containers[0]
startOptions := types.ContainerStartOptions{ startOptions := types.ContainerStartOptions{
CheckpointID: opts.checkpoint, CheckpointID: opts.checkpoint,
CheckpointDir: opts.checkpointDir,
} }
return dockerCli.Client().ContainerStart(ctx, container, startOptions) return dockerCli.Client().ContainerStart(ctx, container, startOptions)