2016-09-08 13:11:39 -04:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2016-09-02 03:40:06 -04:00
|
|
|
"text/template"
|
2016-09-08 13:11:39 -04:00
|
|
|
"time"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2017-08-08 11:26:24 -04:00
|
|
|
"github.com/docker/cli/templates"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
eventtypes "github.com/docker/docker/api/types/events"
|
|
|
|
"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`
|
2017-10-11 12:18:27 -04:00
|
|
|
func NewEventsCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := 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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
return runEvents(dockerCli, &options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"aliases": "docker system events, docker events",
|
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.StringVar(&options.since, "since", "", "Show all events created since timestamp")
|
|
|
|
flags.StringVar(&options.until, "until", "", "Stream events until this timestamp")
|
|
|
|
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
|
|
|
|
flags.StringVar(&options.format, "format", "", "Format the output using the given Go template")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:18:27 -04:00
|
|
|
func runEvents(dockerCli command.Cli, options *eventsOptions) error {
|
2017-05-15 08:45:19 -04:00
|
|
|
tmpl, err := makeTemplate(options.format)
|
2016-09-02 03:40:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return cli.StatusError{
|
|
|
|
StatusCode: 64,
|
2022-09-29 11:21:51 -04:00
|
|
|
Status: "Error parsing format: " + err.Error(),
|
|
|
|
}
|
2016-09-02 03:40:06 -04:00
|
|
|
}
|
2017-05-15 08:45:19 -04:00
|
|
|
eventOptions := types.EventsOptions{
|
|
|
|
Since: options.since,
|
|
|
|
Until: options.until,
|
|
|
|
Filters: options.filter.Value(),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-08-09 16:34:07 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-05-15 08:45:19 -04:00
|
|
|
events, errs := dockerCli.Client().Events(ctx, eventOptions)
|
2016-08-09 16:34:07 -04:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
out := dockerCli.Out()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-events:
|
|
|
|
if err := handleEvent(out, event, tmpl); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case err := <-errs:
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleEvent(out io.Writer, event eventtypes.Message, tmpl *template.Template) error {
|
|
|
|
if tmpl == nil {
|
|
|
|
return prettyPrintEvent(out, event)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-08-09 16:34:07 -04:00
|
|
|
return formatEvent(out, event, tmpl)
|
2016-09-02 03:40:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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}}"
|
2022-02-25 08:32:43 -05:00
|
|
|
return tmpl, tmpl.Execute(io.Discard, &eventtypes.Message{})
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-09-22 17:59:46 -04:00
|
|
|
// rfc3339NanoFixed is similar to time.RFC3339Nano, except it pads nanoseconds
|
|
|
|
// zeros to maintain a fixed number of characters
|
|
|
|
const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
|
|
|
|
|
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 {
|
2017-09-22 17:59:46 -04:00
|
|
|
fmt.Fprintf(out, "%s ", time.Unix(0, event.TimeNano).Format(rfc3339NanoFixed))
|
2016-09-08 13:11:39 -04:00
|
|
|
} else if event.Time != 0 {
|
2017-09-22 17:59:46 -04:00
|
|
|
fmt.Fprintf(out, "%s ", time.Unix(event.Time, 0).Format(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]
|
2023-06-12 12:19:50 -04:00
|
|
|
attrs = append(attrs, k+"="+v)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
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
|
|
|
}
|