From 11c27935d0d17dc2a3c5ac45d746c6507a067fc6 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Wed, 9 Dec 2015 15:37:04 -0800 Subject: [PATCH] Replace api cli with grpc cli Signed-off-by: Michael Crosby --- api/grpc/gctr/container.go | 121 ------------------------------------- api/grpc/gctr/main.go | 53 ---------------- ctr/container.go | 36 ++++++++--- 3 files changed, 26 insertions(+), 184 deletions(-) delete mode 100644 api/grpc/gctr/container.go delete mode 100644 api/grpc/gctr/main.go diff --git a/api/grpc/gctr/container.go b/api/grpc/gctr/container.go deleted file mode 100644 index 87b9650..0000000 --- a/api/grpc/gctr/container.go +++ /dev/null @@ -1,121 +0,0 @@ -package main - -import ( - "fmt" - "os" - "text/tabwriter" - - "google.golang.org/grpc" - - "github.com/Sirupsen/logrus" - "github.com/codegangsta/cli" - "github.com/docker/containerd/api/grpc/types" - netcontext "golang.org/x/net/context" -) - -// TODO: parse flags and pass opts -func getClient() types.APIClient { - conn, err := grpc.Dial("localhost:8888", grpc.WithInsecure()) - if err != nil { - fatal(err.Error(), 1) - } - return types.NewAPIClient(conn) -} - -var ContainersCommand = cli.Command{ - Name: "containers", - Usage: "interact with running containers", - Subcommands: []cli.Command{ - StartCommand, - ListCommand, - KillCommand, - }, - Action: listContainers, -} - -var ListCommand = cli.Command{ - Name: "list", - Usage: "list all running containers", - Action: listContainers, -} - -func listContainers(context *cli.Context) { - cli := getClient() - resp, err := cli.State(netcontext.Background(), &types.StateRequest{}) - if err != nil { - fatal(err.Error(), 1) - } - w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0) - fmt.Fprint(w, "ID\tPATH\tSTATUS\n") - for _, c := range resp.Containers { - fmt.Fprintf(w, "%s\t%s\t%s\n", c.Id, c.BundlePath, c.Status) - } - if err := w.Flush(); err != nil { - logrus.Fatal(err) - } -} - -var StartCommand = cli.Command{ - Name: "start", - Usage: "start a container", - Flags: []cli.Flag{ - cli.StringFlag{ - Name: "id", - 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() - if path == "" { - fatal("bundle path cannot be empty", 1) - } - id := context.String("id") - if id == "" { - fatal("container id cannot be empty", 1) - } - c := getClient() - if _, err := c.CreateContainer(netcontext.Background(), &types.CreateContainerRequest{ - Id: id, - BundlePath: path, - Checkpoint: context.String("checkpoint"), - }); err != nil { - fatal(err.Error(), 1) - } - }, -} - -var KillCommand = cli.Command{ - Name: "kill", - Usage: "send a signal to a container or it's processes", - Flags: []cli.Flag{ - cli.IntFlag{ - Name: "pid,p", - Usage: "pid of the process to signal within the container", - }, - cli.IntFlag{ - Name: "signal,s", - Value: 15, - Usage: "signal to send to the container", - }, - }, - Action: func(context *cli.Context) { - id := context.Args().First() - if id == "" { - fatal("container id cannot be empty", 1) - } - c := getClient() - if _, err := c.Signal(netcontext.Background(), &types.SignalRequest{ - Id: id, - Pid: uint32(context.Int("pid")), - Signal: uint32(context.Int("signal")), - }); err != nil { - fatal(err.Error(), 1) - } - }, -} diff --git a/api/grpc/gctr/main.go b/api/grpc/gctr/main.go deleted file mode 100644 index a189d2c..0000000 --- a/api/grpc/gctr/main.go +++ /dev/null @@ -1,53 +0,0 @@ -package main - -import ( - "fmt" - "os" - - "github.com/Sirupsen/logrus" - "github.com/codegangsta/cli" - "github.com/docker/containerd" -) - -const Usage = `High performance conatiner daemon controller` - -func main() { - app := cli.NewApp() - app.Name = "ctr" - app.Version = containerd.Version - app.Usage = Usage - app.Authors = []cli.Author{ - { - Name: "@crosbymichael", - Email: "crosbymichael@gmail.com", - }, - } - app.Flags = []cli.Flag{ - cli.BoolFlag{ - Name: "debug", - Usage: "enable debug output in the logs", - }, - cli.StringFlag{ - Name: "addr", - Value: "http://localhost:8888", - Usage: "address to the containerd api", - }, - } - app.Commands = []cli.Command{ - ContainersCommand, - } - app.Before = func(context *cli.Context) error { - if context.GlobalBool("debug") { - logrus.SetLevel(logrus.DebugLevel) - } - return nil - } - if err := app.Run(os.Args); err != nil { - logrus.Fatal(err) - } -} - -func fatal(err string, code int) { - fmt.Fprintf(os.Stderr, "[ctr] %s", err) - os.Exit(code) -} diff --git a/ctr/container.go b/ctr/container.go index fd9fdcd..9197a3d 100644 --- a/ctr/container.go +++ b/ctr/container.go @@ -7,9 +7,20 @@ import ( "github.com/Sirupsen/logrus" "github.com/codegangsta/cli" - "github.com/docker/containerd/api/v1" + "github.com/docker/containerd/api/grpc/types" + netcontext "golang.org/x/net/context" + "google.golang.org/grpc" ) +// TODO: parse flags and pass opts +func getClient() types.APIClient { + conn, err := grpc.Dial("localhost:8888", grpc.WithInsecure()) + if err != nil { + fatal(err.Error(), 1) + } + return types.NewAPIClient(conn) +} + var ContainersCommand = cli.Command{ Name: "containers", Usage: "interact with running containers", @@ -28,15 +39,15 @@ var ListCommand = cli.Command{ } func listContainers(context *cli.Context) { - c := v1.NewClient(context.GlobalString("addr")) - containers, err := c.State() + cli := getClient() + resp, err := cli.State(netcontext.Background(), &types.StateRequest{}) if err != nil { fatal(err.Error(), 1) } w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0) fmt.Fprint(w, "ID\tPATH\tSTATUS\n") - for _, c := range containers { - fmt.Fprintf(w, "%s\t%s\t%s\n", c.ID, c.BundlePath, c.State.Status) + for _, c := range resp.Containers { + fmt.Fprintf(w, "%s\t%s\t%s\n", c.Id, c.BundlePath, c.Status) } if err := w.Flush(); err != nil { logrus.Fatal(err) @@ -67,9 +78,10 @@ var StartCommand = cli.Command{ if id == "" { fatal("container id cannot be empty", 1) } - c := v1.NewClient(context.GlobalString("addr")) - if err := c.Start(id, v1.StartOpts{ - Path: path, + c := getClient() + if _, err := c.CreateContainer(netcontext.Background(), &types.CreateContainerRequest{ + Id: id, + BundlePath: path, Checkpoint: context.String("checkpoint"), }); err != nil { fatal(err.Error(), 1) @@ -96,8 +108,12 @@ var KillCommand = cli.Command{ if id == "" { fatal("container id cannot be empty", 1) } - c := v1.NewClient(context.GlobalString("addr")) - if err := c.SignalProcess(id, context.Int("pid"), context.Int("signal")); err != nil { + c := getClient() + if _, err := c.Signal(netcontext.Background(), &types.SignalRequest{ + Id: id, + Pid: uint32(context.Int("pid")), + Signal: uint32(context.Int("signal")), + }); err != nil { fatal(err.Error(), 1) } },