2016-05-12 10:52:00 -04:00
|
|
|
package checkpoint
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-12-01 23:28:51 -05:00
|
|
|
"fmt"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-05-12 07:18:48 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2023-08-28 06:06:35 -04:00
|
|
|
"github.com/docker/docker/api/types/checkpoint"
|
2016-05-12 10:52:00 -04:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
2017-05-03 17:58:52 -04:00
|
|
|
func newCreateCommand(dockerCli command.Cli) *cobra.Command {
|
2016-05-12 10:52:00 -04:00
|
|
|
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]
|
2023-09-09 18:27:44 -04:00
|
|
|
return runCreate(cmd.Context(), dockerCli, opts)
|
2016-05-12 10:52:00 -04:00
|
|
|
},
|
2022-05-12 07:18:48 -04:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2016-05-12 10:52:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runCreate(ctx context.Context, dockerCli command.Cli, opts createOptions) error {
|
|
|
|
err := dockerCli.Client().CheckpointCreate(ctx, opts.container, checkpoint.CreateOptions{
|
2016-09-19 12:01:16 -04:00
|
|
|
CheckpointID: opts.checkpoint,
|
|
|
|
CheckpointDir: opts.checkpointDir,
|
|
|
|
Exit: !opts.leaveRunning,
|
2023-08-28 06:06:35 -04:00
|
|
|
})
|
2016-05-12 10:52:00 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-01 23:28:51 -05:00
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s\n", opts.checkpoint)
|
2016-05-12 10:52:00 -04:00
|
|
|
return nil
|
|
|
|
}
|