-
Notifications
You must be signed in to change notification settings - Fork 26
/
registry.go
117 lines (108 loc) · 4.15 KB
/
registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2015 The Serviced Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package facade
import (
"github.com/control-center/serviced/coordinator/client"
"github.com/control-center/serviced/datastore"
"github.com/control-center/serviced/domain/registry"
"github.com/zenoss/glog"
)
// GetRegistryImage returns information about an image that is stored in the
// docker registry index.
// e.g. GetRegistryImage(ctx, "library/reponame:tagname")
func (f *Facade) GetRegistryImage(ctx datastore.Context, image string) (*registry.Image, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.GetRegistryImage"))
rImage, err := f.registryStore.Get(ctx, image)
if err != nil {
return nil, err
}
return rImage, nil
}
// SetRegistryImage creates/updates an image in the docker registry index.
func (f *Facade) SetRegistryImage(ctx datastore.Context, rImage *registry.Image) error {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.SetRegistryImage"))
if err := f.registryStore.Put(ctx, rImage); err != nil {
return err
}
err := f.zzk.SetRegistryImage(rImage)
if err != nil {
return err
}
return nil
}
// DeleteRegistryImage removes an image from the docker registry index.
// e.g. DeleteRegistryImage(ctx, "library/reponame:tagname")
func (f *Facade) DeleteRegistryImage(ctx datastore.Context, image string) error {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.DeleteRegistryImage"))
if err := f.registryStore.Delete(ctx, image); err != nil {
return err
}
if err := f.zzk.DeleteRegistryImage(registry.Key(image).ID()); err != nil {
return err
}
return nil
}
// GetRegistryImages returns all the image that are in the docker registry
// index.
func (f *Facade) GetRegistryImages(ctx datastore.Context) ([]registry.Image, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.GetRegistryImages"))
rImages, err := f.registryStore.GetImages(ctx)
if err != nil {
return nil, err
}
return rImages, nil
}
// SearchRegistryLibrary searches the docker registry index for images at a
// particular library and tag.
// e.g. library/reponame:tagname => SearchRegistryLibrary("library", "tagname")
func (f *Facade) SearchRegistryLibraryByTag(ctx datastore.Context, library, tagname string) ([]registry.Image, error) {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.SearchRegistryLibraryByTag"))
rImages, err := f.registryStore.SearchLibraryByTag(ctx, library, tagname)
if err != nil {
return nil, err
}
return rImages, nil
}
// SyncRegistryImages makes sure images on es are in sync with zk. If force is
// enabled, all images are reset.
func (f *Facade) SyncRegistryImages(ctx datastore.Context, force bool) error {
defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.SyncRegistryImages"))
if err := f.DFSLock(ctx).LockWithTimeout("sync registry images", userLockTimeout); err != nil {
glog.Warningf("Cannot sync registry images: %s", err)
return err
}
defer f.DFSLock(ctx).Unlock()
// get all the images that are currently in the index
rImages, err := f.GetRegistryImages(ctx)
if err != nil {
return err
}
// we aren't going to try to sync deletes because that can get too messy;
// only adds and updates
for _, rImage := range rImages {
img, err := f.zzk.GetRegistryImage(rImage.ID())
if err != client.ErrNoNode && err != nil {
return err
}
// only update the images where the uuid has changed and from the
// upstream only, to make sure we don't override any changes that
// occur out of band from the sync. If force is set, then it is okay
// to blanket reset everything.
if force || img == nil || img.UUID != rImage.UUID {
if err := f.SetRegistryImage(ctx, &rImage); err != nil {
return err
}
}
}
return nil
}