2014-07-31 11:50:59 +00:00
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
2015-02-09 06:03:42 +00:00
|
|
|
"bytes"
|
2014-07-31 11:50:59 +00:00
|
|
|
"encoding/json"
|
2015-03-25 07:44:12 +00:00
|
|
|
"fmt"
|
2015-02-09 06:03:42 +00:00
|
|
|
"io"
|
2015-02-09 21:02:53 +00:00
|
|
|
"strings"
|
2014-07-31 11:50:59 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/engine"
|
2015-03-18 02:18:41 +00:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2014-11-20 19:46:48 +00:00
|
|
|
"github.com/docker/docker/pkg/parsers/filters"
|
2014-07-31 11:50:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const eventsLimit = 64
|
|
|
|
|
2015-03-18 02:18:41 +00:00
|
|
|
type listener chan<- *jsonmessage.JSONMessage
|
2014-07-31 11:50:59 +00:00
|
|
|
|
|
|
|
type Events struct {
|
|
|
|
mu sync.RWMutex
|
2015-03-18 02:18:41 +00:00
|
|
|
events []*jsonmessage.JSONMessage
|
2014-07-31 11:50:59 +00:00
|
|
|
subscribers []listener
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Events {
|
|
|
|
return &Events{
|
2015-03-18 02:18:41 +00:00
|
|
|
events: make([]*jsonmessage.JSONMessage, 0, eventsLimit),
|
2014-07-31 11:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install installs events public api in docker engine
|
|
|
|
func (e *Events) Install(eng *engine.Engine) error {
|
|
|
|
// Here you should describe public interface
|
|
|
|
jobs := map[string]engine.Handler{
|
|
|
|
"events": e.Get,
|
2014-08-06 09:28:30 +00:00
|
|
|
"log": e.Log,
|
2014-07-31 11:50:59 +00:00
|
|
|
"subscribers_count": e.SubscribersCount,
|
|
|
|
}
|
|
|
|
for name, job := range jobs {
|
|
|
|
if err := eng.Register(name, job); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-25 07:44:12 +00:00
|
|
|
func (e *Events) Get(job *engine.Job) error {
|
2014-07-31 11:50:59 +00:00
|
|
|
var (
|
|
|
|
since = job.GetenvInt64("since")
|
|
|
|
until = job.GetenvInt64("until")
|
|
|
|
timeout = time.NewTimer(time.Unix(until, 0).Sub(time.Now()))
|
|
|
|
)
|
|
|
|
|
2014-11-20 19:46:48 +00:00
|
|
|
eventFilters, err := filters.FromParam(job.Getenv("filters"))
|
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-11-20 19:46:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 11:50:59 +00:00
|
|
|
// If no until, disable timeout
|
|
|
|
if until == 0 {
|
|
|
|
timeout.Stop()
|
|
|
|
}
|
|
|
|
|
2015-03-18 02:18:41 +00:00
|
|
|
listener := make(chan *jsonmessage.JSONMessage)
|
2014-07-31 11:50:59 +00:00
|
|
|
e.subscribe(listener)
|
|
|
|
defer e.unsubscribe(listener)
|
|
|
|
|
|
|
|
job.Stdout.Write(nil)
|
|
|
|
|
|
|
|
// Resend every event in the [since, until] time interval.
|
|
|
|
if since != 0 {
|
2014-11-20 19:46:48 +00:00
|
|
|
if err := e.writeCurrent(job, since, until, eventFilters); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-07-31 11:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event, ok := <-listener:
|
|
|
|
if !ok {
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-07-31 11:50:59 +00:00
|
|
|
}
|
2014-11-20 19:46:48 +00:00
|
|
|
if err := writeEvent(job, event, eventFilters); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-07-31 11:50:59 +00:00
|
|
|
}
|
|
|
|
case <-timeout.C:
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-07-31 11:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 07:44:12 +00:00
|
|
|
func (e *Events) Log(job *engine.Job) error {
|
2014-07-31 11:50:59 +00:00
|
|
|
if len(job.Args) != 3 {
|
2015-03-25 07:44:12 +00:00
|
|
|
return fmt.Errorf("usage: %s ACTION ID FROM", job.Name)
|
2014-07-31 11:50:59 +00:00
|
|
|
}
|
|
|
|
// not waiting for receivers
|
|
|
|
go e.log(job.Args[0], job.Args[1], job.Args[2])
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-07-31 11:50:59 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 07:44:12 +00:00
|
|
|
func (e *Events) SubscribersCount(job *engine.Job) error {
|
2014-07-31 11:50:59 +00:00
|
|
|
ret := &engine.Env{}
|
|
|
|
ret.SetInt("count", e.subscribersCount())
|
|
|
|
ret.WriteTo(job.Stdout)
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-07-31 11:50:59 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 02:18:41 +00:00
|
|
|
func writeEvent(job *engine.Job, event *jsonmessage.JSONMessage, eventFilters filters.Args) error {
|
2014-11-20 19:46:48 +00:00
|
|
|
isFiltered := func(field string, filter []string) bool {
|
|
|
|
if len(filter) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, v := range filter {
|
|
|
|
if v == field {
|
|
|
|
return false
|
|
|
|
}
|
2015-02-09 21:02:53 +00:00
|
|
|
if strings.Contains(field, ":") {
|
|
|
|
image := strings.Split(field, ":")
|
|
|
|
if image[0] == v {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2014-11-20 19:46:48 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-02-09 06:03:42 +00:00
|
|
|
//incoming container filter can be name,id or partial id, convert and replace as a full container id
|
|
|
|
for i, cn := range eventFilters["container"] {
|
|
|
|
eventFilters["container"][i] = GetContainerId(job.Eng, cn)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isFiltered(event.Status, eventFilters["event"]) || isFiltered(event.From, eventFilters["image"]) ||
|
|
|
|
isFiltered(event.ID, eventFilters["container"]) {
|
2014-11-20 19:46:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 11:50:59 +00:00
|
|
|
// When sending an event JSON serialization errors are ignored, but all
|
|
|
|
// other errors lead to the eviction of the listener.
|
|
|
|
if b, err := json.Marshal(event); err == nil {
|
|
|
|
if _, err = job.Stdout.Write(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-20 19:46:48 +00:00
|
|
|
func (e *Events) writeCurrent(job *engine.Job, since, until int64, eventFilters filters.Args) error {
|
2014-07-31 11:50:59 +00:00
|
|
|
e.mu.RLock()
|
|
|
|
for _, event := range e.events {
|
|
|
|
if event.Time >= since && (event.Time <= until || until == 0) {
|
2014-11-20 19:46:48 +00:00
|
|
|
if err := writeEvent(job, event, eventFilters); err != nil {
|
2014-07-31 11:50:59 +00:00
|
|
|
e.mu.RUnlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.mu.RUnlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Events) subscribersCount() int {
|
|
|
|
e.mu.RLock()
|
|
|
|
c := len(e.subscribers)
|
|
|
|
e.mu.RUnlock()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Events) log(action, id, from string) {
|
|
|
|
e.mu.Lock()
|
|
|
|
now := time.Now().UTC().Unix()
|
2015-03-18 02:18:41 +00:00
|
|
|
jm := &jsonmessage.JSONMessage{Status: action, ID: id, From: from, Time: now}
|
2014-07-31 11:50:59 +00:00
|
|
|
if len(e.events) == cap(e.events) {
|
|
|
|
// discard oldest event
|
|
|
|
copy(e.events, e.events[1:])
|
|
|
|
e.events[len(e.events)-1] = jm
|
|
|
|
} else {
|
|
|
|
e.events = append(e.events, jm)
|
|
|
|
}
|
|
|
|
for _, s := range e.subscribers {
|
|
|
|
// We give each subscriber a 100ms time window to receive the event,
|
|
|
|
// after which we move to the next.
|
|
|
|
select {
|
|
|
|
case s <- jm:
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Events) subscribe(l listener) {
|
|
|
|
e.mu.Lock()
|
|
|
|
e.subscribers = append(e.subscribers, l)
|
|
|
|
e.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// unsubscribe closes and removes the specified listener from the list of
|
|
|
|
// previously registed ones.
|
|
|
|
// It returns a boolean value indicating if the listener was successfully
|
|
|
|
// found, closed and unregistered.
|
|
|
|
func (e *Events) unsubscribe(l listener) bool {
|
|
|
|
e.mu.Lock()
|
|
|
|
for i, subscriber := range e.subscribers {
|
|
|
|
if subscriber == l {
|
|
|
|
close(l)
|
|
|
|
e.subscribers = append(e.subscribers[:i], e.subscribers[i+1:]...)
|
|
|
|
e.mu.Unlock()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.mu.Unlock()
|
|
|
|
return false
|
|
|
|
}
|
2015-02-09 06:03:42 +00:00
|
|
|
|
|
|
|
func GetContainerId(eng *engine.Engine, name string) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
job := eng.Job("container_inspect", name)
|
|
|
|
|
|
|
|
var outStream io.Writer
|
|
|
|
|
|
|
|
outStream = &buf
|
|
|
|
job.Stdout.Set(outStream)
|
|
|
|
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var out struct{ ID string }
|
|
|
|
json.NewDecoder(&buf).Decode(&out)
|
|
|
|
return out.ID
|
|
|
|
}
|