github.com/ijc/containerd@v0.2.5/ctr/checkpoint.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "text/tabwriter" 7 8 "github.com/codegangsta/cli" 9 "github.com/docker/containerd/api/grpc/types" 10 netcontext "golang.org/x/net/context" 11 ) 12 13 var checkpointSubCmds = []cli.Command{ 14 listCheckpointCommand, 15 createCheckpointCommand, 16 deleteCheckpointCommand, 17 } 18 19 var checkpointCommand = cli.Command{ 20 Name: "checkpoints", 21 Usage: "list all checkpoints", 22 ArgsUsage: "COMMAND [arguments...]", 23 Subcommands: checkpointSubCmds, 24 Description: func() string { 25 desc := "\n COMMAND:\n" 26 for _, command := range checkpointSubCmds { 27 desc += fmt.Sprintf(" %-10.10s%s\n", command.Name, command.Usage) 28 } 29 return desc 30 }(), 31 Action: listCheckpoints, 32 } 33 34 var listCheckpointCommand = cli.Command{ 35 Name: "list", 36 Usage: "list all checkpoints for a container", 37 Action: listCheckpoints, 38 Flags: []cli.Flag{ 39 cli.StringFlag{ 40 Name: "checkpoint-dir", 41 Value: "", 42 Usage: "path to checkpoint directory", 43 }, 44 }, 45 } 46 47 func listCheckpoints(context *cli.Context) { 48 var ( 49 c = getClient(context) 50 id = context.Args().First() 51 ) 52 if id == "" { 53 fatal("container id cannot be empty", ExitStatusMissingArg) 54 } 55 resp, err := c.ListCheckpoint(netcontext.Background(), &types.ListCheckpointRequest{ 56 Id: id, 57 CheckpointDir: context.String("checkpoint-dir"), 58 }) 59 if err != nil { 60 fatal(err.Error(), 1) 61 } 62 w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0) 63 fmt.Fprint(w, "NAME\tTCP\tUNIX SOCKETS\tSHELL\n") 64 for _, c := range resp.Checkpoints { 65 fmt.Fprintf(w, "%s\t%v\t%v\t%v\n", c.Name, c.Tcp, c.UnixSockets, c.Shell) 66 } 67 if err := w.Flush(); err != nil { 68 fatal(err.Error(), 1) 69 } 70 } 71 72 var createCheckpointCommand = cli.Command{ 73 Name: "create", 74 Usage: "create a new checkpoint for the container", 75 Flags: []cli.Flag{ 76 cli.BoolFlag{ 77 Name: "tcp", 78 Usage: "persist open tcp connections", 79 }, 80 cli.BoolFlag{ 81 Name: "unix-sockets", 82 Usage: "persist unix sockets", 83 }, 84 cli.BoolFlag{ 85 Name: "exit", 86 Usage: "exit the container after the checkpoint completes successfully", 87 }, 88 cli.BoolFlag{ 89 Name: "shell", 90 Usage: "checkpoint shell jobs", 91 }, 92 cli.StringFlag{ 93 Name: "checkpoint-dir", 94 Value: "", 95 Usage: "directory to store checkpoints", 96 }, 97 cli.StringSliceFlag{ 98 Name: "empty-ns", 99 Usage: "create a namespace, but don't restore its properties", 100 }, 101 }, 102 Action: func(context *cli.Context) { 103 var ( 104 containerID = context.Args().Get(0) 105 name = context.Args().Get(1) 106 ) 107 if containerID == "" { 108 fatal("container id at cannot be empty", ExitStatusMissingArg) 109 } 110 if name == "" { 111 fatal("checkpoint name cannot be empty", ExitStatusMissingArg) 112 } 113 c := getClient(context) 114 checkpoint := types.Checkpoint{ 115 Name: name, 116 Exit: context.Bool("exit"), 117 Tcp: context.Bool("tcp"), 118 Shell: context.Bool("shell"), 119 UnixSockets: context.Bool("unix-sockets"), 120 } 121 122 emptyNSes := context.StringSlice("empty-ns") 123 checkpoint.EmptyNS = append(checkpoint.EmptyNS, emptyNSes...) 124 125 if _, err := c.CreateCheckpoint(netcontext.Background(), &types.CreateCheckpointRequest{ 126 Id: containerID, 127 CheckpointDir: context.String("checkpoint-dir"), 128 Checkpoint: &checkpoint, 129 }); err != nil { 130 fatal(err.Error(), 1) 131 } 132 }, 133 } 134 135 var deleteCheckpointCommand = cli.Command{ 136 Name: "delete", 137 Usage: "delete a container's checkpoint", 138 Flags: []cli.Flag{ 139 cli.StringFlag{ 140 Name: "checkpoint-dir", 141 Value: "", 142 Usage: "path to checkpoint directory", 143 }, 144 }, 145 Action: func(context *cli.Context) { 146 var ( 147 containerID = context.Args().Get(0) 148 name = context.Args().Get(1) 149 ) 150 if containerID == "" { 151 fatal("container id at cannot be empty", ExitStatusMissingArg) 152 } 153 if name == "" { 154 fatal("checkpoint name cannot be empty", ExitStatusMissingArg) 155 } 156 c := getClient(context) 157 if _, err := c.DeleteCheckpoint(netcontext.Background(), &types.DeleteCheckpointRequest{ 158 Id: containerID, 159 Name: name, 160 CheckpointDir: context.String("checkpoint-dir"), 161 }); err != nil { 162 fatal(err.Error(), 1) 163 } 164 }, 165 }