2015-01-28 07:27:46 +00:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/Sirupsen/logrus"
|
2015-02-12 00:49:49 +00:00
|
|
|
"github.com/docker/distribution"
|
2015-01-28 07:27:46 +00:00
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
"github.com/docker/distribution/manifest"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ManifestListener describes a set of methods for listening to events related to manifests.
|
|
|
|
type ManifestListener interface {
|
2015-02-12 00:49:49 +00:00
|
|
|
ManifestPushed(repo distribution.Repository, sm *manifest.SignedManifest) error
|
|
|
|
ManifestPulled(repo distribution.Repository, sm *manifest.SignedManifest) error
|
2015-01-28 07:27:46 +00:00
|
|
|
|
|
|
|
// TODO(stevvooe): Please note that delete support is still a little shaky
|
|
|
|
// and we'll need to propagate these in the future.
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
ManifestDeleted(repo distribution.Repository, sm *manifest.SignedManifest) error
|
2015-01-28 07:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LayerListener describes a listener that can respond to layer related events.
|
|
|
|
type LayerListener interface {
|
2015-02-12 00:49:49 +00:00
|
|
|
LayerPushed(repo distribution.Repository, layer distribution.Layer) error
|
|
|
|
LayerPulled(repo distribution.Repository, layer distribution.Layer) error
|
2015-01-28 07:27:46 +00:00
|
|
|
|
|
|
|
// TODO(stevvooe): Please note that delete support is still a little shaky
|
|
|
|
// and we'll need to propagate these in the future.
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
LayerDeleted(repo distribution.Repository, layer distribution.Layer) error
|
2015-01-28 07:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Listener combines all repository events into a single interface.
|
|
|
|
type Listener interface {
|
|
|
|
ManifestListener
|
|
|
|
LayerListener
|
|
|
|
}
|
|
|
|
|
|
|
|
type repositoryListener struct {
|
2015-02-12 00:49:49 +00:00
|
|
|
distribution.Repository
|
2015-01-28 07:27:46 +00:00
|
|
|
listener Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen dispatches events on the repository to the listener.
|
2015-02-12 00:49:49 +00:00
|
|
|
func Listen(repo distribution.Repository, listener Listener) distribution.Repository {
|
2015-01-28 07:27:46 +00:00
|
|
|
return &repositoryListener{
|
|
|
|
Repository: repo,
|
|
|
|
listener: listener,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
func (rl *repositoryListener) Manifests() distribution.ManifestService {
|
2015-01-28 07:27:46 +00:00
|
|
|
return &manifestServiceListener{
|
|
|
|
ManifestService: rl.Repository.Manifests(),
|
|
|
|
parent: rl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
func (rl *repositoryListener) Layers() distribution.LayerService {
|
2015-01-28 07:27:46 +00:00
|
|
|
return &layerServiceListener{
|
|
|
|
LayerService: rl.Repository.Layers(),
|
|
|
|
parent: rl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type manifestServiceListener struct {
|
2015-02-12 00:49:49 +00:00
|
|
|
distribution.ManifestService
|
2015-01-28 07:27:46 +00:00
|
|
|
parent *repositoryListener
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msl *manifestServiceListener) Get(tag string) (*manifest.SignedManifest, error) {
|
|
|
|
sm, err := msl.ManifestService.Get(tag)
|
|
|
|
if err == nil {
|
|
|
|
if err := msl.parent.listener.ManifestPulled(msl.parent.Repository, sm); err != nil {
|
|
|
|
logrus.Errorf("error dispatching manifest pull to listener: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sm, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msl *manifestServiceListener) Put(tag string, sm *manifest.SignedManifest) error {
|
|
|
|
err := msl.ManifestService.Put(tag, sm)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if err := msl.parent.listener.ManifestPushed(msl.parent.Repository, sm); err != nil {
|
|
|
|
logrus.Errorf("error dispatching manifest push to listener: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type layerServiceListener struct {
|
2015-02-12 00:49:49 +00:00
|
|
|
distribution.LayerService
|
2015-01-28 07:27:46 +00:00
|
|
|
parent *repositoryListener
|
|
|
|
}
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
func (lsl *layerServiceListener) Fetch(dgst digest.Digest) (distribution.Layer, error) {
|
2015-01-28 07:27:46 +00:00
|
|
|
layer, err := lsl.LayerService.Fetch(dgst)
|
|
|
|
if err == nil {
|
|
|
|
if err := lsl.parent.listener.LayerPulled(lsl.parent.Repository, layer); err != nil {
|
|
|
|
logrus.Errorf("error dispatching layer pull to listener: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return layer, err
|
|
|
|
}
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
func (lsl *layerServiceListener) Upload() (distribution.LayerUpload, error) {
|
2015-01-28 07:27:46 +00:00
|
|
|
lu, err := lsl.LayerService.Upload()
|
|
|
|
return lsl.decorateUpload(lu), err
|
|
|
|
}
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
func (lsl *layerServiceListener) Resume(uuid string) (distribution.LayerUpload, error) {
|
2015-01-28 07:27:46 +00:00
|
|
|
lu, err := lsl.LayerService.Resume(uuid)
|
|
|
|
return lsl.decorateUpload(lu), err
|
|
|
|
}
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
func (lsl *layerServiceListener) decorateUpload(lu distribution.LayerUpload) distribution.LayerUpload {
|
2015-01-28 07:27:46 +00:00
|
|
|
return &layerUploadListener{
|
|
|
|
LayerUpload: lu,
|
|
|
|
parent: lsl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type layerUploadListener struct {
|
2015-02-12 00:49:49 +00:00
|
|
|
distribution.LayerUpload
|
2015-01-28 07:27:46 +00:00
|
|
|
parent *layerServiceListener
|
|
|
|
}
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
func (lul *layerUploadListener) Finish(dgst digest.Digest) (distribution.Layer, error) {
|
2015-01-28 07:27:46 +00:00
|
|
|
layer, err := lul.LayerUpload.Finish(dgst)
|
|
|
|
if err == nil {
|
|
|
|
if err := lul.parent.parent.listener.LayerPushed(lul.parent.parent.Repository, layer); err != nil {
|
|
|
|
logrus.Errorf("error dispatching layer push to listener: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return layer, err
|
|
|
|
}
|