2015-11-02 18:25:26 -05:00
|
|
|
package daemon
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
import (
|
2015-12-21 17:55:23 -05:00
|
|
|
"strings"
|
2016-04-11 14:52:34 -04:00
|
|
|
"time"
|
2015-12-21 17:55:23 -05:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/events"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2016-04-11 14:52:34 -04:00
|
|
|
daemonevents "github.com/docker/docker/daemon/events"
|
2015-12-21 17:55:23 -05:00
|
|
|
"github.com/docker/libnetwork"
|
2015-11-12 14:55:17 -05:00
|
|
|
)
|
|
|
|
|
2016-01-07 17:14:05 -05:00
|
|
|
// LogContainerEvent generates an event related to a container with only the default attributes.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) LogContainerEvent(container *container.Container, action string) {
|
2016-01-07 17:14:05 -05:00
|
|
|
daemon.LogContainerEventWithAttributes(container, action, map[string]string{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogContainerEventWithAttributes generates an event related to a container with specific given attributes.
|
|
|
|
func (daemon *Daemon) LogContainerEventWithAttributes(container *container.Container, action string, attributes map[string]string) {
|
|
|
|
copyAttributes(attributes, container.Config.Labels)
|
2015-12-21 17:55:23 -05:00
|
|
|
if container.Config.Image != "" {
|
|
|
|
attributes["image"] = container.Config.Image
|
|
|
|
}
|
|
|
|
attributes["name"] = strings.TrimLeft(container.Name, "/")
|
|
|
|
|
|
|
|
actor := events.Actor{
|
|
|
|
ID: container.ID,
|
|
|
|
Attributes: attributes,
|
|
|
|
}
|
|
|
|
daemon.EventsService.Log(action, events.ContainerEventType, actor)
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:36:14 -04:00
|
|
|
// LogImageEvent generates an event related to an image with only the default attributes.
|
2015-12-21 17:55:23 -05:00
|
|
|
func (daemon *Daemon) LogImageEvent(imageID, refName, action string) {
|
2016-01-07 17:14:05 -05:00
|
|
|
daemon.LogImageEventWithAttributes(imageID, refName, action, map[string]string{})
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:36:14 -04:00
|
|
|
// LogImageEventWithAttributes generates an event related to an image with specific given attributes.
|
2016-01-07 17:14:05 -05:00
|
|
|
func (daemon *Daemon) LogImageEventWithAttributes(imageID, refName, action string, attributes map[string]string) {
|
2015-12-21 17:55:23 -05:00
|
|
|
img, err := daemon.GetImage(imageID)
|
|
|
|
if err == nil && img.Config != nil {
|
|
|
|
// image has not been removed yet.
|
|
|
|
// it could be missing if the event is `delete`.
|
2016-01-07 17:14:05 -05:00
|
|
|
copyAttributes(attributes, img.Config.Labels)
|
2015-12-21 17:55:23 -05:00
|
|
|
}
|
|
|
|
if refName != "" {
|
|
|
|
attributes["name"] = refName
|
|
|
|
}
|
|
|
|
actor := events.Actor{
|
|
|
|
ID: imageID,
|
|
|
|
Attributes: attributes,
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon.EventsService.Log(action, events.ImageEventType, actor)
|
|
|
|
}
|
|
|
|
|
2016-07-18 11:02:12 -04:00
|
|
|
// LogPluginEvent generates an event related to a plugin with only the default attributes.
|
|
|
|
func (daemon *Daemon) LogPluginEvent(pluginID, refName, action string) {
|
|
|
|
daemon.LogPluginEventWithAttributes(pluginID, refName, action, map[string]string{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogPluginEventWithAttributes generates an event related to a plugin with specific given attributes.
|
|
|
|
func (daemon *Daemon) LogPluginEventWithAttributes(pluginID, refName, action string, attributes map[string]string) {
|
|
|
|
attributes["name"] = refName
|
|
|
|
actor := events.Actor{
|
|
|
|
ID: pluginID,
|
|
|
|
Attributes: attributes,
|
|
|
|
}
|
|
|
|
daemon.EventsService.Log(action, events.PluginEventType, actor)
|
|
|
|
}
|
|
|
|
|
2015-12-21 17:55:23 -05:00
|
|
|
// LogVolumeEvent generates an event related to a volume.
|
|
|
|
func (daemon *Daemon) LogVolumeEvent(volumeID, action string, attributes map[string]string) {
|
|
|
|
actor := events.Actor{
|
|
|
|
ID: volumeID,
|
|
|
|
Attributes: attributes,
|
|
|
|
}
|
|
|
|
daemon.EventsService.Log(action, events.VolumeEventType, actor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogNetworkEvent generates an event related to a network with only the default attributes.
|
|
|
|
func (daemon *Daemon) LogNetworkEvent(nw libnetwork.Network, action string) {
|
|
|
|
daemon.LogNetworkEventWithAttributes(nw, action, map[string]string{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogNetworkEventWithAttributes generates an event related to a network with specific given attributes.
|
|
|
|
func (daemon *Daemon) LogNetworkEventWithAttributes(nw libnetwork.Network, action string, attributes map[string]string) {
|
|
|
|
attributes["name"] = nw.Name()
|
|
|
|
attributes["type"] = nw.Type()
|
|
|
|
actor := events.Actor{
|
2015-12-21 23:35:30 -05:00
|
|
|
ID: nw.ID(),
|
2015-12-21 17:55:23 -05:00
|
|
|
Attributes: attributes,
|
|
|
|
}
|
|
|
|
daemon.EventsService.Log(action, events.NetworkEventType, actor)
|
|
|
|
}
|
|
|
|
|
2016-05-08 19:11:34 -04:00
|
|
|
// LogDaemonEventWithAttributes generates an event related to the daemon itself with specific given attributes.
|
|
|
|
func (daemon *Daemon) LogDaemonEventWithAttributes(action string, attributes map[string]string) {
|
|
|
|
if daemon.EventsService != nil {
|
2016-05-08 19:15:33 -04:00
|
|
|
if info, err := daemon.SystemInfo(); err == nil && info.Name != "" {
|
|
|
|
attributes["name"] = info.Name
|
|
|
|
}
|
2016-05-08 19:11:34 -04:00
|
|
|
actor := events.Actor{
|
|
|
|
ID: daemon.ID,
|
|
|
|
Attributes: attributes,
|
|
|
|
}
|
|
|
|
daemon.EventsService.Log(action, events.DaemonEventType, actor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
// SubscribeToEvents returns the currently record of events, a channel to stream new events from, and a function to cancel the stream of events.
|
|
|
|
func (daemon *Daemon) SubscribeToEvents(since, until time.Time, filter filters.Args) ([]events.Message, chan interface{}) {
|
|
|
|
ef := daemonevents.NewFilter(filter)
|
|
|
|
return daemon.EventsService.SubscribeTopic(since, until, ef)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnsubscribeFromEvents stops the event subscription for a client by closing the
|
|
|
|
// channel where the daemon sends events to.
|
|
|
|
func (daemon *Daemon) UnsubscribeFromEvents(listener chan interface{}) {
|
|
|
|
daemon.EventsService.Evict(listener)
|
|
|
|
}
|
|
|
|
|
2015-12-21 17:55:23 -05:00
|
|
|
// copyAttributes guarantees that labels are not mutated by event triggers.
|
2016-01-07 17:14:05 -05:00
|
|
|
func copyAttributes(attributes, labels map[string]string) {
|
2015-12-21 17:55:23 -05:00
|
|
|
if labels == nil {
|
2016-01-07 17:14:05 -05:00
|
|
|
return
|
2015-12-21 17:55:23 -05:00
|
|
|
}
|
|
|
|
for k, v := range labels {
|
|
|
|
attributes[k] = v
|
|
|
|
}
|
2015-11-02 18:25:26 -05:00
|
|
|
}
|