2015-01-28 08:27:46 +01:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
2015-02-03 22:28:10 +01:00
|
|
|
"net/http"
|
2015-01-28 08:27:46 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.google.com/p/go-uuid/uuid"
|
2015-02-12 01:49:49 +01:00
|
|
|
"github.com/docker/distribution"
|
2015-01-28 08:27:46 +01:00
|
|
|
"github.com/docker/distribution/digest"
|
2015-02-12 01:49:49 +01:00
|
|
|
"github.com/docker/distribution/manifest"
|
2015-01-28 08:27:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type bridge struct {
|
2015-02-03 22:28:10 +01:00
|
|
|
ub URLBuilder
|
|
|
|
actor ActorRecord
|
|
|
|
source SourceRecord
|
|
|
|
request RequestRecord
|
|
|
|
sink Sink
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Listener = &bridge{}
|
|
|
|
|
|
|
|
// URLBuilder defines a subset of url builder to be used by the event listener.
|
|
|
|
type URLBuilder interface {
|
|
|
|
BuildManifestURL(name, tag string) (string, error)
|
|
|
|
BuildBlobURL(name string, dgst digest.Digest) (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBridge returns a notification listener that writes records to sink,
|
|
|
|
// using the actor and source. Any urls populated in the events created by
|
|
|
|
// this bridge will be created using the URLBuilder.
|
2015-02-03 22:28:10 +01:00
|
|
|
// TODO(stevvooe): Update this to simply take a context.Context object.
|
|
|
|
func NewBridge(ub URLBuilder, source SourceRecord, actor ActorRecord, request RequestRecord, sink Sink) Listener {
|
2015-01-28 08:27:46 +01:00
|
|
|
return &bridge{
|
2015-02-03 22:28:10 +01:00
|
|
|
ub: ub,
|
|
|
|
actor: actor,
|
|
|
|
source: source,
|
|
|
|
request: request,
|
|
|
|
sink: sink,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRequestRecord builds a RequestRecord for use in NewBridge from an
|
|
|
|
// http.Request, associating it with a request id.
|
|
|
|
func NewRequestRecord(id string, r *http.Request) RequestRecord {
|
|
|
|
return RequestRecord{
|
|
|
|
ID: id,
|
|
|
|
Addr: r.RemoteAddr,
|
|
|
|
Host: r.Host,
|
|
|
|
Method: r.Method,
|
|
|
|
UserAgent: r.UserAgent(),
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
func (b *bridge) ManifestPushed(repo distribution.Repository, sm *manifest.SignedManifest) error {
|
2015-01-28 08:27:46 +01:00
|
|
|
return b.createManifestEventAndWrite(EventActionPush, repo, sm)
|
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
func (b *bridge) ManifestPulled(repo distribution.Repository, sm *manifest.SignedManifest) error {
|
2015-01-28 08:27:46 +01:00
|
|
|
return b.createManifestEventAndWrite(EventActionPull, repo, sm)
|
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
func (b *bridge) ManifestDeleted(repo distribution.Repository, sm *manifest.SignedManifest) error {
|
2015-01-28 08:27:46 +01:00
|
|
|
return b.createManifestEventAndWrite(EventActionDelete, repo, sm)
|
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
func (b *bridge) LayerPushed(repo distribution.Repository, layer distribution.Layer) error {
|
2015-03-05 05:57:14 +01:00
|
|
|
return b.createLayerEventAndWrite(EventActionPush, repo, layer)
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
func (b *bridge) LayerPulled(repo distribution.Repository, layer distribution.Layer) error {
|
2015-03-05 05:57:14 +01:00
|
|
|
return b.createLayerEventAndWrite(EventActionPull, repo, layer)
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
func (b *bridge) LayerDeleted(repo distribution.Repository, layer distribution.Layer) error {
|
2015-03-05 05:57:14 +01:00
|
|
|
return b.createLayerEventAndWrite(EventActionDelete, repo, layer)
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
func (b *bridge) createManifestEventAndWrite(action string, repo distribution.Repository, sm *manifest.SignedManifest) error {
|
2015-03-05 05:57:14 +01:00
|
|
|
manifestEvent, err := b.createManifestEvent(action, repo, sm)
|
2015-01-28 08:27:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-05 05:57:14 +01:00
|
|
|
return b.sink.Write(*manifestEvent)
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
func (b *bridge) createManifestEvent(action string, repo distribution.Repository, sm *manifest.SignedManifest) (*Event, error) {
|
2015-01-28 08:27:46 +01:00
|
|
|
event := b.createEvent(action)
|
2015-03-05 05:57:14 +01:00
|
|
|
event.Target.MediaType = manifest.ManifestMediaType
|
|
|
|
event.Target.Repository = repo.Name()
|
2015-01-28 08:27:46 +01:00
|
|
|
|
|
|
|
p, err := sm.Payload()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-03-05 05:57:14 +01:00
|
|
|
event.Target.Length = int64(len(p))
|
|
|
|
|
2015-01-28 08:27:46 +01:00
|
|
|
event.Target.Digest, err = digest.FromBytes(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(stevvooe): Currently, the is the "tag" url: once the digest url is
|
|
|
|
// implemented, this should be replaced.
|
|
|
|
event.Target.URL, err = b.ub.BuildManifestURL(sm.Name, sm.Tag)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 05:57:14 +01:00
|
|
|
func (b *bridge) createLayerEventAndWrite(action string, repo distribution.Repository, layer distribution.Layer) error {
|
|
|
|
event, err := b.createLayerEvent(action, repo, layer)
|
2015-01-28 08:27:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.sink.Write(*event)
|
|
|
|
}
|
|
|
|
|
2015-03-05 05:57:14 +01:00
|
|
|
func (b *bridge) createLayerEvent(action string, repo distribution.Repository, layer distribution.Layer) (*Event, error) {
|
2015-01-28 08:27:46 +01:00
|
|
|
event := b.createEvent(action)
|
2015-03-05 05:57:14 +01:00
|
|
|
event.Target.MediaType = layerMediaType
|
|
|
|
event.Target.Repository = repo.Name()
|
|
|
|
|
|
|
|
event.Target.Length = layer.Length()
|
|
|
|
|
|
|
|
dgst := layer.Digest()
|
2015-01-28 08:27:46 +01:00
|
|
|
event.Target.Digest = dgst
|
|
|
|
|
|
|
|
var err error
|
|
|
|
event.Target.URL, err = b.ub.BuildBlobURL(repo.Name(), dgst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createEvent creates an event with actor and source populated.
|
|
|
|
func (b *bridge) createEvent(action string) *Event {
|
|
|
|
event := createEvent(action)
|
|
|
|
event.Source = b.source
|
|
|
|
event.Actor = b.actor
|
2015-02-03 22:28:10 +01:00
|
|
|
event.Request = b.request
|
2015-01-28 08:27:46 +01:00
|
|
|
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
|
|
|
// createEvent returns a new event, timestamped, with the specified action.
|
|
|
|
func createEvent(action string) *Event {
|
|
|
|
return &Event{
|
|
|
|
ID: uuid.New(),
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
Action: action,
|
|
|
|
}
|
|
|
|
}
|