From e87f8336c4603f8bc5a3fd26764aa86a1b41e413 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 8 Dec 2015 13:31:20 -0800 Subject: [PATCH] Add more client methods Signed-off-by: Michael Crosby --- Makefile | 10 ++++++++ api/v1/client.go | 42 +++++++++++++++++++++++++++++----- ctr/{start.go => container.go} | 12 ++++++++-- 3 files changed, 56 insertions(+), 8 deletions(-) rename ctr/{start.go => container.go} (88%) diff --git a/Makefile b/Makefile index f32d40b..af68092 100644 --- a/Makefile +++ b/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/ diff --git a/api/v1/client.go b/api/v1/client.go index e7b72f7..46a7d67 100644 --- a/api/v1/client.go +++ b/api/v1/client.go @@ -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 +} diff --git a/ctr/start.go b/ctr/container.go similarity index 88% rename from ctr/start.go rename to ctr/container.go index 4ded310..fd9fdcd 100644 --- a/ctr/start.go +++ b/ctr/container.go @@ -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) } },