Add basic frame and ctr command line client

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2015-12-02 14:41:49 -08:00
parent ba46df11b4
commit 847e5bc7ee
8 changed files with 227 additions and 5 deletions

2
ctr/Makefile Normal file
View file

@ -0,0 +1,2 @@
all:
go build

BIN
ctr/ctr Executable file

Binary file not shown.

53
ctr/main.go Normal file
View file

@ -0,0 +1,53 @@
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)
}

96
ctr/start.go Normal file
View file

@ -0,0 +1,96 @@
package main
import (
"fmt"
"os"
"text/tabwriter"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/docker/containerd/api/v1"
)
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) {
c := v1.NewClient(context.GlobalString("addr"))
containers, err := c.Containers()
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)
}
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",
},
},
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 := v1.NewClient(context.GlobalString("addr"))
if err := c.Start(id, path); 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 := v1.NewClient(context.GlobalString("addr"))
if err := c.Signal(id, context.Int("pid"), context.Int("signal")); err != nil {
fatal(err.Error(), 1)
}
},
}