github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/daemon/events.go (about) 1 package daemon 2 3 import ( 4 "strings" 5 "time" 6 7 "github.com/docker/docker/container" 8 daemonevents "github.com/docker/docker/daemon/events" 9 "github.com/docker/engine-api/types/events" 10 "github.com/docker/engine-api/types/filters" 11 "github.com/docker/libnetwork" 12 ) 13 14 // LogContainerEvent generates an event related to a container with only the default attributes. 15 func (daemon *Daemon) LogContainerEvent(container *container.Container, action string) { 16 daemon.LogContainerEventWithAttributes(container, action, map[string]string{}) 17 } 18 19 // LogContainerEventWithAttributes generates an event related to a container with specific given attributes. 20 func (daemon *Daemon) LogContainerEventWithAttributes(container *container.Container, action string, attributes map[string]string) { 21 copyAttributes(attributes, container.Config.Labels) 22 if container.Config.Image != "" { 23 attributes["image"] = container.Config.Image 24 } 25 attributes["name"] = strings.TrimLeft(container.Name, "/") 26 27 actor := events.Actor{ 28 ID: container.ID, 29 Attributes: attributes, 30 } 31 daemon.EventsService.Log(action, events.ContainerEventType, actor) 32 } 33 34 // LogImageEvent generates an event related to an image with only the default attributes. 35 func (daemon *Daemon) LogImageEvent(imageID, refName, action string) { 36 daemon.LogImageEventWithAttributes(imageID, refName, action, map[string]string{}) 37 } 38 39 // LogImageEventWithAttributes generates an event related to an image with specific given attributes. 40 func (daemon *Daemon) LogImageEventWithAttributes(imageID, refName, action string, attributes map[string]string) { 41 img, err := daemon.GetImage(imageID) 42 if err == nil && img.Config != nil { 43 // image has not been removed yet. 44 // it could be missing if the event is `delete`. 45 copyAttributes(attributes, img.Config.Labels) 46 } 47 if refName != "" { 48 attributes["name"] = refName 49 } 50 actor := events.Actor{ 51 ID: imageID, 52 Attributes: attributes, 53 } 54 55 daemon.EventsService.Log(action, events.ImageEventType, actor) 56 } 57 58 // LogVolumeEvent generates an event related to a volume. 59 func (daemon *Daemon) LogVolumeEvent(volumeID, action string, attributes map[string]string) { 60 actor := events.Actor{ 61 ID: volumeID, 62 Attributes: attributes, 63 } 64 daemon.EventsService.Log(action, events.VolumeEventType, actor) 65 } 66 67 // LogNetworkEvent generates an event related to a network with only the default attributes. 68 func (daemon *Daemon) LogNetworkEvent(nw libnetwork.Network, action string) { 69 daemon.LogNetworkEventWithAttributes(nw, action, map[string]string{}) 70 } 71 72 // LogNetworkEventWithAttributes generates an event related to a network with specific given attributes. 73 func (daemon *Daemon) LogNetworkEventWithAttributes(nw libnetwork.Network, action string, attributes map[string]string) { 74 attributes["name"] = nw.Name() 75 attributes["type"] = nw.Type() 76 actor := events.Actor{ 77 ID: nw.ID(), 78 Attributes: attributes, 79 } 80 daemon.EventsService.Log(action, events.NetworkEventType, actor) 81 } 82 83 // LogDaemonEventWithAttributes generates an event related to the daemon itself with specific given attributes. 84 func (daemon *Daemon) LogDaemonEventWithAttributes(action string, attributes map[string]string) { 85 if daemon.EventsService != nil { 86 if info, err := daemon.SystemInfo(); err == nil && info.Name != "" { 87 attributes["name"] = info.Name 88 } 89 actor := events.Actor{ 90 ID: daemon.ID, 91 Attributes: attributes, 92 } 93 daemon.EventsService.Log(action, events.DaemonEventType, actor) 94 } 95 } 96 97 // SubscribeToEvents returns the currently record of events, a channel to stream new events from, and a function to cancel the stream of events. 98 func (daemon *Daemon) SubscribeToEvents(since, until time.Time, filter filters.Args) ([]events.Message, chan interface{}) { 99 ef := daemonevents.NewFilter(filter) 100 return daemon.EventsService.SubscribeTopic(since, until, ef) 101 } 102 103 // UnsubscribeFromEvents stops the event subscription for a client by closing the 104 // channel where the daemon sends events to. 105 func (daemon *Daemon) UnsubscribeFromEvents(listener chan interface{}) { 106 daemon.EventsService.Evict(listener) 107 } 108 109 // copyAttributes guarantees that labels are not mutated by event triggers. 110 func copyAttributes(attributes, labels map[string]string) { 111 if labels == nil { 112 return 113 } 114 for k, v := range labels { 115 attributes[k] = v 116 } 117 }