2015-11-02 23:25:26 +00:00
|
|
|
package daemon
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
import (
|
2015-12-21 22:55:23 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/events"
|
2015-11-12 19:55:17 +00:00
|
|
|
"github.com/docker/docker/container"
|
2015-12-21 22:55:23 +00:00
|
|
|
"github.com/docker/libnetwork"
|
2015-11-12 19:55:17 +00:00
|
|
|
)
|
|
|
|
|
2015-11-03 17:33:13 +00:00
|
|
|
// LogContainerEvent generates an event related to a container.
|
2015-11-12 19:55:17 +00:00
|
|
|
func (daemon *Daemon) LogContainerEvent(container *container.Container, action string) {
|
2015-12-21 22:55:23 +00:00
|
|
|
attributes := copyAttributes(container.Config.Labels)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogImageEvent generates an event related to a container.
|
|
|
|
func (daemon *Daemon) LogImageEvent(imageID, refName, action string) {
|
|
|
|
attributes := map[string]string{}
|
|
|
|
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`.
|
|
|
|
attributes = copyAttributes(img.Config.Labels)
|
|
|
|
}
|
|
|
|
if refName != "" {
|
|
|
|
attributes["name"] = refName
|
|
|
|
}
|
|
|
|
actor := events.Actor{
|
|
|
|
ID: imageID,
|
|
|
|
Attributes: attributes,
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon.EventsService.Log(action, events.ImageEventType, actor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2015-12-22 00:45:31 +00:00
|
|
|
daemon.logNetworkEventWithID(nw.ID(), action, attributes)
|
|
|
|
}
|
2015-12-21 22:55:23 +00:00
|
|
|
|
2015-12-22 00:45:31 +00:00
|
|
|
func (daemon *Daemon) logNetworkEventWithID(id, action string, attributes map[string]string) {
|
2015-12-21 22:55:23 +00:00
|
|
|
actor := events.Actor{
|
2015-12-22 00:45:31 +00:00
|
|
|
ID: id,
|
2015-12-21 22:55:23 +00:00
|
|
|
Attributes: attributes,
|
|
|
|
}
|
|
|
|
daemon.EventsService.Log(action, events.NetworkEventType, actor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyAttributes guarantees that labels are not mutated by event triggers.
|
|
|
|
func copyAttributes(labels map[string]string) map[string]string {
|
|
|
|
attributes := map[string]string{}
|
|
|
|
if labels == nil {
|
|
|
|
return attributes
|
|
|
|
}
|
|
|
|
for k, v := range labels {
|
|
|
|
attributes[k] = v
|
|
|
|
}
|
|
|
|
return attributes
|
2015-11-02 23:25:26 +00:00
|
|
|
}
|