|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package checkpoint |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" |
| 21 | + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" |
| 22 | + "github.com/containerd/nerdctl/v2/pkg/api/types" |
| 23 | + "github.com/containerd/nerdctl/v2/pkg/clientutil" |
| 24 | + "github.com/containerd/nerdctl/v2/pkg/cmd/checkpoint" |
| 25 | + "github.com/spf13/cobra" |
| 26 | +) |
| 27 | + |
| 28 | +func CreateCommand() *cobra.Command { |
| 29 | + var cmd = &cobra.Command{ |
| 30 | + Use: "create [OPTIONS] CONTAINER CHECKPOINT", |
| 31 | + Short: "Create a checkpoint from a running container", |
| 32 | + Args: cobra.ExactArgs(2), |
| 33 | + RunE: createAction, |
| 34 | + ValidArgsFunction: createShellComplete, |
| 35 | + SilenceUsage: true, |
| 36 | + SilenceErrors: true, |
| 37 | + } |
| 38 | + cmd.Flags().Bool("leave-running", false, "Leave the container running after checkpointing") |
| 39 | + return cmd |
| 40 | +} |
| 41 | + |
| 42 | +func processCreateFlags(cmd *cobra.Command) (types.CheckpointCreateOptions, error) { |
| 43 | + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) |
| 44 | + if err != nil { |
| 45 | + return types.CheckpointCreateOptions{}, err |
| 46 | + } |
| 47 | + |
| 48 | + leaveRunning, err := cmd.Flags().GetBool("leave-running") |
| 49 | + |
| 50 | + if err != nil { |
| 51 | + return types.CheckpointCreateOptions{}, err |
| 52 | + } |
| 53 | + |
| 54 | + return types.CheckpointCreateOptions{ |
| 55 | + Stdout: cmd.OutOrStdout(), |
| 56 | + GOptions: globalOptions, |
| 57 | + LeaveRunning: leaveRunning, |
| 58 | + }, nil |
| 59 | +} |
| 60 | + |
| 61 | +func createAction(cmd *cobra.Command, args []string) error { |
| 62 | + createOptions, err := processCreateFlags(cmd) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), createOptions.GOptions.Namespace, createOptions.GOptions.Address) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + defer cancel() |
| 71 | + |
| 72 | + err = checkpoint.Create(ctx, client, args[0], args[1], createOptions) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func createShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 81 | + return completion.ImageNames(cmd) |
| 82 | +} |
0 commit comments