ctr: add events command

This simply print all events generated by containerd

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
Kenfe-Mickael Laventure 2016-12-14 09:15:27 -08:00
parent 286ea04591
commit 13399c1330
3 changed files with 67 additions and 0 deletions

61
cmd/ctr/events.go Normal file
View file

@ -0,0 +1,61 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/nats-io/go-nats"
"github.com/urfave/cli"
)
var eventsCommand = cli.Command{
Name: "events",
Usage: "display containerd events",
Flags: []cli.Flag{
cli.StringFlag{
Name: "subject, s",
Usage: "subjects filter",
Value: "containerd.>",
},
},
Action: func(context *cli.Context) error {
nc, err := nats.Connect(nats.DefaultURL)
if err != nil {
return err
}
nec, err := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
if err != nil {
nc.Close()
return err
}
defer nec.Close()
evCh := make(chan *nats.Msg, 64)
sub, err := nec.Subscribe(context.String("subject"), func(e *nats.Msg) {
evCh <- e
})
if err != nil {
return err
}
defer sub.Unsubscribe()
for {
e, more := <-evCh
if !more {
break
}
var prettyJSON bytes.Buffer
err := json.Indent(&prettyJSON, e.Data, "", "\t")
if err != nil {
fmt.Println(string(e.Data))
} else {
fmt.Println(prettyJSON.String())
}
}
return nil
},
}

View file

@ -18,6 +18,10 @@ var execCommand = cli.Command{
Name: "id, i", Name: "id, i",
Usage: "target container id", Usage: "target container id",
}, },
cli.StringFlag{
Name: "pid, p",
Usage: "new process id",
},
cli.StringFlag{ cli.StringFlag{
Name: "cwd, c", Name: "cwd, c",
Usage: "current working directory for the process", Usage: "current working directory for the process",
@ -48,6 +52,7 @@ var execCommand = cli.Command{
sOpts := &execution.StartProcessRequest{ sOpts := &execution.StartProcessRequest{
ContainerID: id, ContainerID: id,
Process: &execution.Process{ Process: &execution.Process{
ID: context.String("pid"),
Cwd: context.String("cwd"), Cwd: context.String("cwd"),
Terminal: context.Bool("tty"), Terminal: context.Bool("tty"),
Args: context.Args(), Args: context.Args(),

View file

@ -36,6 +36,7 @@ containerd client
app.Commands = []cli.Command{ app.Commands = []cli.Command{
runCommand, runCommand,
execCommand, execCommand,
eventsCommand,
} }
app.Before = func(context *cli.Context) error { app.Before = func(context *cli.Context) error {
if context.GlobalBool("debug") { if context.GlobalBool("debug") {