Add more client methods
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
		
							
								
								
									
										10
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								Makefile
									
									
									
									
									
								
							| @@ -4,3 +4,13 @@ all: | ||||
| 	cd containerd && go build -tags libcontainer -o ../bin/containerd | ||||
| 	cd ctr && go build -o ../bin/ctr | ||||
|  | ||||
| client: | ||||
| 	mkdir -p bin/ | ||||
| 	cd ctr && go build -o ../bin/ctr | ||||
|  | ||||
| daemon: | ||||
| 	mkdir -p bin/ | ||||
| 	cd containerd && go build -tags libcontainer -o ../bin/containerd | ||||
|  | ||||
| install: | ||||
| 	cp bin/* /usr/local/bin/ | ||||
|   | ||||
| @@ -18,12 +18,17 @@ type Client struct { | ||||
| 	addr string | ||||
| } | ||||
|  | ||||
| type StartOpts struct { | ||||
| 	Path       string | ||||
| 	Checkpoint string | ||||
| } | ||||
|  | ||||
| // Start starts a container with the specified id and path to the container's | ||||
| // bundle on the system. | ||||
| func (c *Client) Start(id, path, checkpoint string) error { | ||||
| func (c *Client) Start(id string, opts StartOpts) error { | ||||
| 	container := Container{ | ||||
| 		BundlePath: path, | ||||
| 		Checkpoint: checkpoint, | ||||
| 		BundlePath: opts.Path, | ||||
| 		Checkpoint: opts.Checkpoint, | ||||
| 	} | ||||
| 	buf := bytes.NewBuffer(nil) | ||||
| 	if err := json.NewEncoder(buf).Encode(container); err != nil { | ||||
| @@ -40,9 +45,8 @@ func (c *Client) Start(id, path, checkpoint string) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // Containers returns all running containers within containerd. | ||||
| func (c *Client) Containers() ([]Container, error) { | ||||
| 	r, err := http.Get(c.addr + "/containers") | ||||
| func (c *Client) State() ([]Container, error) { | ||||
| 	r, err := http.Get(c.addr + "/state") | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| @@ -69,3 +73,29 @@ func (c *Client) SignalProcess(id string, pid, signal int) error { | ||||
| 	r.Body.Close() | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (c *Client) Checkpoints(id string) ([]Checkpoint, error) { | ||||
| 	r, err := http.Get(c.addr + "/containers/" + id + "/checkpoint") | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer r.Body.Close() | ||||
| 	var checkpoints []Checkpoint | ||||
| 	if err := json.NewDecoder(r.Body).Decode(&checkpoints); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return checkpoints, nil | ||||
| } | ||||
|  | ||||
| func (c *Client) CreateCheckpoint(id, name string, cp Checkpoint) error { | ||||
| 	buf := bytes.NewBuffer(nil) | ||||
| 	if err := json.NewEncoder(buf).Encode(cp); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	r, err := http.Post(c.addr+"/containers/"+id+"/checkpoint", "application/json", buf) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	r.Body.Close() | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
| @@ -29,7 +29,7 @@ var ListCommand = cli.Command{ | ||||
| 
 | ||||
| func listContainers(context *cli.Context) { | ||||
| 	c := v1.NewClient(context.GlobalString("addr")) | ||||
| 	containers, err := c.Containers() | ||||
| 	containers, err := c.State() | ||||
| 	if err != nil { | ||||
| 		fatal(err.Error(), 1) | ||||
| 	} | ||||
| @@ -52,6 +52,11 @@ var StartCommand = cli.Command{ | ||||
| 			Value: "", | ||||
| 			Usage: "id of the container", | ||||
| 		}, | ||||
| 		cli.StringFlag{ | ||||
| 			Name:  "checkpoint,c", | ||||
| 			Value: "", | ||||
| 			Usage: "checkpoint to start the container from", | ||||
| 		}, | ||||
| 	}, | ||||
| 	Action: func(context *cli.Context) { | ||||
| 		path := context.Args().First() | ||||
| @@ -63,7 +68,10 @@ var StartCommand = cli.Command{ | ||||
| 			fatal("container id cannot be empty", 1) | ||||
| 		} | ||||
| 		c := v1.NewClient(context.GlobalString("addr")) | ||||
| 		if err := c.Start(id, path, ""); err != nil { | ||||
| 		if err := c.Start(id, v1.StartOpts{ | ||||
| 			Path:       path, | ||||
| 			Checkpoint: context.String("checkpoint"), | ||||
| 		}); err != nil { | ||||
| 			fatal(err.Error(), 1) | ||||
| 		} | ||||
| 	}, | ||||
		Reference in New Issue
	
	Block a user
	 Michael Crosby
					Michael Crosby