github.com/kim0/docker@v0.6.2-0.20161130212042-4addda3f07e7/cli/command/checkpoint/create.go (about)

     1  package checkpoint
     2  
     3  import (
     4  	"golang.org/x/net/context"
     5  
     6  	"github.com/docker/docker/api/types"
     7  	"github.com/docker/docker/cli"
     8  	"github.com/docker/docker/cli/command"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  type createOptions struct {
    13  	container     string
    14  	checkpoint    string
    15  	checkpointDir string
    16  	leaveRunning  bool
    17  }
    18  
    19  func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
    20  	var opts createOptions
    21  
    22  	cmd := &cobra.Command{
    23  		Use:   "create CONTAINER CHECKPOINT",
    24  		Short: "Create a checkpoint from a running container",
    25  		Args:  cli.ExactArgs(2),
    26  		RunE: func(cmd *cobra.Command, args []string) error {
    27  			opts.container = args[0]
    28  			opts.checkpoint = args[1]
    29  			return runCreate(dockerCli, opts)
    30  		},
    31  	}
    32  
    33  	flags := cmd.Flags()
    34  	flags.BoolVar(&opts.leaveRunning, "leave-running", false, "leave the container running after checkpoint")
    35  	flags.StringVarP(&opts.checkpointDir, "checkpoint-dir", "", "", "use a custom checkpoint storage directory")
    36  
    37  	return cmd
    38  }
    39  
    40  func runCreate(dockerCli *command.DockerCli, opts createOptions) error {
    41  	client := dockerCli.Client()
    42  
    43  	checkpointOpts := types.CheckpointCreateOptions{
    44  		CheckpointID:  opts.checkpoint,
    45  		CheckpointDir: opts.checkpointDir,
    46  		Exit:          !opts.leaveRunning,
    47  	}
    48  
    49  	err := client.CheckpointCreate(context.Background(), opts.container, checkpointOpts)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	return nil
    55  }