2016-09-08 13:11:39 -04:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-09-02 03:40:06 -04:00
|
|
|
"io/ioutil"
|
2016-09-08 13:11:39 -04:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2016-09-02 03:40:06 -04:00
|
|
|
"text/template"
|
2016-09-08 13:11:39 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
eventtypes "github.com/docker/docker/api/types/events"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
2016-09-13 14:53:11 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonlog"
|
2016-09-02 03:40:06 -04:00
|
|
|
"github.com/docker/docker/utils/templates"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type eventsOptions struct {
|
|
|
|
since string
|
|
|
|
until string
|
2016-09-13 14:53:11 -04:00
|
|
|
filter opts.FilterOpt
|
2016-09-02 03:40:06 -04:00
|
|
|
format string
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEventsCommand creates a new cobra.Command for `docker events`
|
|
|
|
func NewEventsCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-09-13 14:53:11 -04:00
|
|
|
opts := eventsOptions{filter: opts.NewFilterOpt()}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "events [OPTIONS]",
|
|
|
|
Short: "Get real time events from the server",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runEvents(dockerCli, &opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.StringVar(&opts.since, "since", "", "Show all events created since timestamp")
|
|
|
|
flags.StringVar(&opts.until, "until", "", "Stream events until this timestamp")
|
2016-09-13 14:53:11 -04:00
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
2016-09-02 03:40:06 -04:00
|
|
|
flags.StringVar(&opts.format, "format", "", "Format the output using the given go template")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runEvents(dockerCli *command.DockerCli, opts *eventsOptions) error {
|
2016-09-02 03:40:06 -04:00
|
|
|
tmpl, err := makeTemplate(opts.format)
|
|
|
|
if err != nil {
|
|
|
|
return cli.StatusError{
|
|
|
|
StatusCode: 64,
|
|
|
|
Status: "Error parsing format: " + err.Error()}
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
options := types.EventsOptions{
|
|
|
|
Since: opts.since,
|
|
|
|
Until: opts.until,
|
2016-09-13 14:53:11 -04:00
|
|
|
Filters: opts.filter.Value(),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
responseBody, err := dockerCli.Client().Events(context.Background(), options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer responseBody.Close()
|
|
|
|
|
2016-09-02 03:40:06 -04:00
|
|
|
return streamEvents(dockerCli.Out(), responseBody, tmpl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeTemplate(format string) (*template.Template, error) {
|
|
|
|
if format == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
tmpl, err := templates.Parse(format)
|
|
|
|
if err != nil {
|
|
|
|
return tmpl, err
|
|
|
|
}
|
|
|
|
// we execute the template for an empty message, so as to validate
|
|
|
|
// a bad template like "{{.badFieldString}}"
|
|
|
|
return tmpl, tmpl.Execute(ioutil.Discard, &eventtypes.Message{})
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// streamEvents decodes prints the incoming events in the provided output.
|
2016-09-02 03:40:06 -04:00
|
|
|
func streamEvents(out io.Writer, input io.Reader, tmpl *template.Template) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
return DecodeEvents(input, func(event eventtypes.Message, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-02 03:40:06 -04:00
|
|
|
if tmpl == nil {
|
|
|
|
return prettyPrintEvent(out, event)
|
|
|
|
}
|
|
|
|
return formatEvent(out, event, tmpl)
|
2016-09-08 13:11:39 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type eventProcessor func(event eventtypes.Message, err error) error
|
|
|
|
|
2016-09-02 03:40:06 -04:00
|
|
|
// prettyPrintEvent prints all types of event information.
|
2016-09-08 13:11:39 -04:00
|
|
|
// Each output includes the event type, actor id, name and action.
|
|
|
|
// Actor attributes are printed at the end if the actor has any.
|
2016-09-02 03:40:06 -04:00
|
|
|
func prettyPrintEvent(out io.Writer, event eventtypes.Message) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
if event.TimeNano != 0 {
|
2016-09-02 03:40:06 -04:00
|
|
|
fmt.Fprintf(out, "%s ", time.Unix(0, event.TimeNano).Format(jsonlog.RFC3339NanoFixed))
|
2016-09-08 13:11:39 -04:00
|
|
|
} else if event.Time != 0 {
|
2016-09-02 03:40:06 -04:00
|
|
|
fmt.Fprintf(out, "%s ", time.Unix(event.Time, 0).Format(jsonlog.RFC3339NanoFixed))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-09-02 03:40:06 -04:00
|
|
|
fmt.Fprintf(out, "%s %s %s", event.Type, event.Action, event.Actor.ID)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
if len(event.Actor.Attributes) > 0 {
|
|
|
|
var attrs []string
|
|
|
|
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]
|
|
|
|
attrs = append(attrs, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
2016-09-02 03:40:06 -04:00
|
|
|
fmt.Fprintf(out, " (%s)", strings.Join(attrs, ", "))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-02 03:40:06 -04:00
|
|
|
fmt.Fprint(out, "\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatEvent(out io.Writer, event eventtypes.Message, tmpl *template.Template) error {
|
|
|
|
defer out.Write([]byte{'\n'})
|
|
|
|
return tmpl.Execute(out, event)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|