2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-12-21 17:55:23 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-01-26 21:48:14 -05:00
|
|
|
"sort"
|
2015-12-21 17:55:23 -05:00
|
|
|
"strings"
|
2016-02-29 14:24:51 -05:00
|
|
|
"sync"
|
2015-12-21 17:55:23 -05:00
|
|
|
"time"
|
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-02-29 14:24:51 -05:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2015-12-21 17:55:23 -05:00
|
|
|
"github.com/docker/docker/pkg/jsonlog"
|
2015-03-24 23:57:23 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
eventtypes "github.com/docker/engine-api/types/events"
|
|
|
|
"github.com/docker/engine-api/types/filters"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdEvents prints a live stream of real time events from the server.
|
|
|
|
//
|
|
|
|
// Usage: docker events [OPTIONS]
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdEvents(args ...string) error {
|
2015-10-08 08:46:21 -04:00
|
|
|
cmd := Cli.Subcmd("events", nil, Cli.DockerCommands["events"].Description, true)
|
2015-11-09 09:37:24 -05:00
|
|
|
since := cmd.String([]string{"-since"}, "", "Show all events created since timestamp")
|
2015-03-24 23:57:23 -04:00
|
|
|
until := cmd.String([]string{"-until"}, "", "Stream events until this timestamp")
|
|
|
|
flFilter := opts.NewListOpts(nil)
|
|
|
|
cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
|
|
|
|
cmd.Require(flag.Exact, 0)
|
|
|
|
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-03 14:34:35 -05:00
|
|
|
eventFilterArgs := filters.NewArgs()
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
// Consolidate all filter flags, and sanity check them early.
|
|
|
|
// They'll get process in the daemon/server.
|
|
|
|
for _, f := range flFilter.GetAll() {
|
|
|
|
var err error
|
|
|
|
eventFilterArgs, err = filters.ParseFlag(f, eventFilterArgs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-12-03 14:34:35 -05:00
|
|
|
|
2015-12-04 17:02:06 -05:00
|
|
|
options := types.EventsOptions{
|
2015-12-03 14:34:35 -05:00
|
|
|
Since: *since,
|
|
|
|
Until: *until,
|
|
|
|
Filters: eventFilterArgs,
|
2015-05-01 14:23:44 -04:00
|
|
|
}
|
2015-12-03 14:34:35 -05:00
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
responseBody, err := cli.client.Events(context.Background(), options)
|
2015-12-03 14:34:35 -05:00
|
|
|
if err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-12-03 14:34:35 -05:00
|
|
|
defer responseBody.Close()
|
|
|
|
|
2015-12-21 17:55:23 -05:00
|
|
|
return streamEvents(responseBody, cli.out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// streamEvents decodes prints the incoming events in the provided output.
|
|
|
|
func streamEvents(input io.Reader, output io.Writer) error {
|
2015-12-28 15:15:34 -05:00
|
|
|
return decodeEvents(input, func(event eventtypes.Message, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
printOutput(event, output)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type eventProcessor func(event eventtypes.Message, err error) error
|
|
|
|
|
|
|
|
func decodeEvents(input io.Reader, ep eventProcessor) error {
|
2015-12-21 17:55:23 -05:00
|
|
|
dec := json.NewDecoder(input)
|
|
|
|
for {
|
|
|
|
var event eventtypes.Message
|
2015-12-28 15:15:34 -05:00
|
|
|
err := dec.Decode(&event)
|
|
|
|
if err != nil && err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if procErr := ep(event, err); procErr != nil {
|
|
|
|
return procErr
|
2015-12-21 17:55:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// printOutput prints all types of event information.
|
|
|
|
// Each output includes the event type, actor id, name and action.
|
|
|
|
// Actor attributes are printed at the end if the actor has any.
|
|
|
|
func printOutput(event eventtypes.Message, output io.Writer) {
|
|
|
|
if event.TimeNano != 0 {
|
|
|
|
fmt.Fprintf(output, "%s ", time.Unix(0, event.TimeNano).Format(jsonlog.RFC3339NanoFixed))
|
|
|
|
} else if event.Time != 0 {
|
|
|
|
fmt.Fprintf(output, "%s ", time.Unix(event.Time, 0).Format(jsonlog.RFC3339NanoFixed))
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(output, "%s %s %s", event.Type, event.Action, event.Actor.ID)
|
|
|
|
|
|
|
|
if len(event.Actor.Attributes) > 0 {
|
|
|
|
var attrs []string
|
2016-01-26 21:48:14 -05:00
|
|
|
var keys []string
|
|
|
|
for k := range event.Actor.Attributes {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, k := range keys {
|
|
|
|
v := event.Actor.Attributes[k]
|
2015-12-21 17:55:23 -05:00
|
|
|
attrs = append(attrs, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
fmt.Fprintf(output, " (%s)", strings.Join(attrs, ", "))
|
|
|
|
}
|
|
|
|
fmt.Fprint(output, "\n")
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2016-02-29 14:24:51 -05:00
|
|
|
|
|
|
|
type eventHandler struct {
|
|
|
|
handlers map[string]func(eventtypes.Message)
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *eventHandler) Handle(action string, h func(eventtypes.Message)) {
|
|
|
|
w.mu.Lock()
|
|
|
|
w.handlers[action] = h
|
|
|
|
w.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch ranges over the passed in event chan and processes the events based on the
|
|
|
|
// handlers created for a given action.
|
|
|
|
// To stop watching, close the event chan.
|
|
|
|
func (w *eventHandler) Watch(c <-chan eventtypes.Message) {
|
|
|
|
for e := range c {
|
|
|
|
w.mu.Lock()
|
|
|
|
h, exists := w.handlers[e.Action]
|
|
|
|
w.mu.Unlock()
|
|
|
|
if !exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logrus.Debugf("event handler: received event: %v", e)
|
|
|
|
go h(e)
|
|
|
|
}
|
|
|
|
}
|