2015-12-04 16:55:15 -05:00
|
|
|
package reference
|
2015-11-18 17:16:21 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-12-07 20:55:35 -05:00
|
|
|
"sort"
|
2015-11-18 17:16:21 -05:00
|
|
|
"sync"
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-04-20 20:08:47 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2017-01-06 20:23:18 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2015-11-18 17:16:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrDoesNotExist is returned if a reference is not found in the
|
|
|
|
// store.
|
|
|
|
ErrDoesNotExist = errors.New("reference does not exist")
|
|
|
|
)
|
|
|
|
|
|
|
|
// An Association is a tuple associating a reference with an image ID.
|
|
|
|
type Association struct {
|
2017-01-25 19:54:18 -05:00
|
|
|
Ref reference.Named
|
2016-09-15 19:37:32 -04:00
|
|
|
ID digest.Digest
|
2015-11-18 17:16:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store provides the set of methods which can operate on a tag store.
|
|
|
|
type Store interface {
|
2017-01-25 19:54:18 -05:00
|
|
|
References(id digest.Digest) []reference.Named
|
|
|
|
ReferencesByName(ref reference.Named) []Association
|
|
|
|
AddTag(ref reference.Named, id digest.Digest, force bool) error
|
|
|
|
AddDigest(ref reference.Canonical, id digest.Digest, force bool) error
|
|
|
|
Delete(ref reference.Named) (bool, error)
|
|
|
|
Get(ref reference.Named) (digest.Digest, error)
|
2015-11-18 17:16:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type store struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
// jsonPath is the path to the file where the serialized tag data is
|
|
|
|
// stored.
|
|
|
|
jsonPath string
|
|
|
|
// Repositories is a map of repositories, indexed by name.
|
|
|
|
Repositories map[string]repository
|
|
|
|
// referencesByIDCache is a cache of references indexed by ID, to speed
|
|
|
|
// up References.
|
2017-01-25 19:54:18 -05:00
|
|
|
referencesByIDCache map[digest.Digest]map[string]reference.Named
|
2017-05-16 19:56:56 -04:00
|
|
|
// platform is the container target platform for this store (which may be
|
|
|
|
// different to the host operating system
|
|
|
|
platform string
|
2015-11-18 17:16:21 -05:00
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
// Repository maps tags to digests. The key is a stringified Reference,
|
2015-11-18 17:16:21 -05:00
|
|
|
// including the repository name.
|
2016-09-15 19:37:32 -04:00
|
|
|
type repository map[string]digest.Digest
|
2015-11-18 17:16:21 -05:00
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
type lexicalRefs []reference.Named
|
2015-12-07 20:55:35 -05:00
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
func (a lexicalRefs) Len() int { return len(a) }
|
|
|
|
func (a lexicalRefs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a lexicalRefs) Less(i, j int) bool {
|
|
|
|
return a[i].String() < a[j].String()
|
|
|
|
}
|
2015-12-07 20:55:35 -05:00
|
|
|
|
|
|
|
type lexicalAssociations []Association
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
func (a lexicalAssociations) Len() int { return len(a) }
|
|
|
|
func (a lexicalAssociations) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a lexicalAssociations) Less(i, j int) bool {
|
|
|
|
return a[i].Ref.String() < a[j].Ref.String()
|
|
|
|
}
|
2015-12-07 20:55:35 -05:00
|
|
|
|
2015-12-04 16:55:15 -05:00
|
|
|
// NewReferenceStore creates a new reference store, tied to a file path where
|
|
|
|
// the set of references are serialized in JSON format.
|
2017-05-16 19:56:56 -04:00
|
|
|
func NewReferenceStore(jsonPath, platform string) (Store, error) {
|
2015-11-18 17:16:21 -05:00
|
|
|
abspath, err := filepath.Abs(jsonPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
store := &store{
|
|
|
|
jsonPath: abspath,
|
|
|
|
Repositories: make(map[string]repository),
|
2017-01-25 19:54:18 -05:00
|
|
|
referencesByIDCache: make(map[digest.Digest]map[string]reference.Named),
|
2017-05-16 19:56:56 -04:00
|
|
|
platform: platform,
|
2015-11-18 17:16:21 -05:00
|
|
|
}
|
|
|
|
// Load the json file if it exists, otherwise create it.
|
|
|
|
if err := store.reload(); os.IsNotExist(err) {
|
|
|
|
if err := store.save(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return store, nil
|
|
|
|
}
|
|
|
|
|
2015-12-04 16:55:15 -05:00
|
|
|
// AddTag adds a tag reference to the store. If force is set to true, existing
|
2015-11-18 17:16:21 -05:00
|
|
|
// references can be overwritten. This only works for tags, not digests.
|
2017-01-25 19:54:18 -05:00
|
|
|
func (store *store) AddTag(ref reference.Named, id digest.Digest, force bool) error {
|
|
|
|
if _, isCanonical := ref.(reference.Canonical); isCanonical {
|
2015-11-25 15:42:40 -05:00
|
|
|
return errors.New("refusing to create a tag with a digest reference")
|
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
return store.addReference(reference.TagNameOnly(ref), id, force)
|
2015-11-25 15:42:40 -05:00
|
|
|
}
|
|
|
|
|
2015-12-04 16:55:15 -05:00
|
|
|
// AddDigest adds a digest reference to the store.
|
2017-01-25 19:54:18 -05:00
|
|
|
func (store *store) AddDigest(ref reference.Canonical, id digest.Digest, force bool) error {
|
2015-11-25 15:42:40 -05:00
|
|
|
return store.addReference(ref, id, force)
|
|
|
|
}
|
|
|
|
|
2017-02-16 04:48:40 -05:00
|
|
|
func favorDigest(originalRef reference.Named) (reference.Named, error) {
|
|
|
|
ref := originalRef
|
2017-02-09 19:30:46 -05:00
|
|
|
// If the reference includes a digest and a tag, we must store only the
|
|
|
|
// digest.
|
2017-02-16 04:48:40 -05:00
|
|
|
canonical, isCanonical := originalRef.(reference.Canonical)
|
|
|
|
_, isNamedTagged := originalRef.(reference.NamedTagged)
|
2017-02-09 19:30:46 -05:00
|
|
|
|
|
|
|
if isCanonical && isNamedTagged {
|
|
|
|
trimmed, err := reference.WithDigest(reference.TrimNamed(canonical), canonical.Digest())
|
|
|
|
if err != nil {
|
|
|
|
// should never happen
|
2017-02-16 04:48:40 -05:00
|
|
|
return originalRef, err
|
2017-02-09 19:30:46 -05:00
|
|
|
}
|
|
|
|
ref = trimmed
|
|
|
|
}
|
2017-02-16 04:48:40 -05:00
|
|
|
return ref, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *store) addReference(ref reference.Named, id digest.Digest, force bool) error {
|
|
|
|
ref, err := favorDigest(ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-09 19:30:46 -05:00
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
refName := reference.FamiliarName(ref)
|
|
|
|
refStr := reference.FamiliarString(ref)
|
|
|
|
|
|
|
|
if refName == string(digest.Canonical) {
|
2015-11-25 15:42:40 -05:00
|
|
|
return errors.New("refusing to create an ambiguous tag using digest algorithm as name")
|
|
|
|
}
|
2015-11-18 17:16:21 -05:00
|
|
|
|
|
|
|
store.mu.Lock()
|
|
|
|
defer store.mu.Unlock()
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
repository, exists := store.Repositories[refName]
|
2015-11-18 17:16:21 -05:00
|
|
|
if !exists || repository == nil {
|
2016-09-15 19:37:32 -04:00
|
|
|
repository = make(map[string]digest.Digest)
|
2017-01-25 19:54:18 -05:00
|
|
|
store.Repositories[refName] = repository
|
2015-11-18 17:16:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
oldID, exists := repository[refStr]
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
// force only works for tags
|
2017-01-25 19:54:18 -05:00
|
|
|
if digested, isDigest := ref.(reference.Canonical); isDigest {
|
2015-11-18 17:16:21 -05:00
|
|
|
return fmt.Errorf("Cannot overwrite digest %s", digested.Digest().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !force {
|
2017-01-25 19:54:18 -05:00
|
|
|
return fmt.Errorf("Conflict: Tag %s is already set to image %s, if you want to replace it, please use -f option", refStr, oldID.String())
|
2015-11-18 17:16:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if store.referencesByIDCache[oldID] != nil {
|
|
|
|
delete(store.referencesByIDCache[oldID], refStr)
|
|
|
|
if len(store.referencesByIDCache[oldID]) == 0 {
|
|
|
|
delete(store.referencesByIDCache, oldID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repository[refStr] = id
|
|
|
|
if store.referencesByIDCache[id] == nil {
|
2017-01-25 19:54:18 -05:00
|
|
|
store.referencesByIDCache[id] = make(map[string]reference.Named)
|
2015-11-18 17:16:21 -05:00
|
|
|
}
|
|
|
|
store.referencesByIDCache[id][refStr] = ref
|
|
|
|
|
|
|
|
return store.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes a reference from the store. It returns true if a deletion
|
|
|
|
// happened, or false otherwise.
|
2017-01-25 19:54:18 -05:00
|
|
|
func (store *store) Delete(ref reference.Named) (bool, error) {
|
2017-02-16 04:48:40 -05:00
|
|
|
ref, err := favorDigest(ref)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
ref = reference.TagNameOnly(ref)
|
|
|
|
|
|
|
|
refName := reference.FamiliarName(ref)
|
|
|
|
refStr := reference.FamiliarString(ref)
|
2015-11-18 17:16:21 -05:00
|
|
|
|
|
|
|
store.mu.Lock()
|
|
|
|
defer store.mu.Unlock()
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
repository, exists := store.Repositories[refName]
|
2015-11-18 17:16:21 -05:00
|
|
|
if !exists {
|
|
|
|
return false, ErrDoesNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
if id, exists := repository[refStr]; exists {
|
|
|
|
delete(repository, refStr)
|
|
|
|
if len(repository) == 0 {
|
2017-01-25 19:54:18 -05:00
|
|
|
delete(store.Repositories, refName)
|
2015-11-18 17:16:21 -05:00
|
|
|
}
|
|
|
|
if store.referencesByIDCache[id] != nil {
|
|
|
|
delete(store.referencesByIDCache[id], refStr)
|
|
|
|
if len(store.referencesByIDCache[id]) == 0 {
|
|
|
|
delete(store.referencesByIDCache, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, store.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, ErrDoesNotExist
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
// Get retrieves an item from the store by reference
|
2017-01-25 19:54:18 -05:00
|
|
|
func (store *store) Get(ref reference.Named) (digest.Digest, error) {
|
|
|
|
if canonical, ok := ref.(reference.Canonical); ok {
|
|
|
|
// If reference contains both tag and digest, only
|
2017-05-21 19:24:07 -04:00
|
|
|
// lookup by digest as it takes precedence over
|
2017-01-25 19:54:18 -05:00
|
|
|
// tag, until tag/digest combos are stored.
|
|
|
|
if _, ok := ref.(reference.Tagged); ok {
|
|
|
|
var err error
|
|
|
|
ref, err = reference.WithDigest(reference.TrimNamed(canonical), canonical.Digest())
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ref = reference.TagNameOnly(ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
refName := reference.FamiliarName(ref)
|
|
|
|
refStr := reference.FamiliarString(ref)
|
2015-11-18 17:16:21 -05:00
|
|
|
|
|
|
|
store.mu.RLock()
|
|
|
|
defer store.mu.RUnlock()
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
repository, exists := store.Repositories[refName]
|
2015-11-18 17:16:21 -05:00
|
|
|
if !exists || repository == nil {
|
|
|
|
return "", ErrDoesNotExist
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
id, exists := repository[refStr]
|
2015-11-18 17:16:21 -05:00
|
|
|
if !exists {
|
|
|
|
return "", ErrDoesNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
// References returns a slice of references to the given ID. The slice
|
|
|
|
// will be nil if there are no references to this ID.
|
2017-01-25 19:54:18 -05:00
|
|
|
func (store *store) References(id digest.Digest) []reference.Named {
|
2015-11-18 17:16:21 -05:00
|
|
|
store.mu.RLock()
|
|
|
|
defer store.mu.RUnlock()
|
|
|
|
|
|
|
|
// Convert the internal map to an array for two reasons:
|
2015-12-04 16:55:15 -05:00
|
|
|
// 1) We must not return a mutable
|
2015-11-18 17:16:21 -05:00
|
|
|
// 2) It would be ugly to expose the extraneous map keys to callers.
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
var references []reference.Named
|
2015-11-18 17:16:21 -05:00
|
|
|
for _, ref := range store.referencesByIDCache[id] {
|
|
|
|
references = append(references, ref)
|
|
|
|
}
|
|
|
|
|
2015-12-07 20:55:35 -05:00
|
|
|
sort.Sort(lexicalRefs(references))
|
|
|
|
|
2015-11-18 17:16:21 -05:00
|
|
|
return references
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReferencesByName returns the references for a given repository name.
|
|
|
|
// If there are no references known for this repository name,
|
|
|
|
// ReferencesByName returns nil.
|
2017-01-25 19:54:18 -05:00
|
|
|
func (store *store) ReferencesByName(ref reference.Named) []Association {
|
|
|
|
refName := reference.FamiliarName(ref)
|
|
|
|
|
2015-11-18 17:16:21 -05:00
|
|
|
store.mu.RLock()
|
|
|
|
defer store.mu.RUnlock()
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
repository, exists := store.Repositories[refName]
|
2015-11-18 17:16:21 -05:00
|
|
|
if !exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var associations []Association
|
|
|
|
for refStr, refID := range repository {
|
2017-01-25 19:54:18 -05:00
|
|
|
ref, err := reference.ParseNormalizedNamed(refStr)
|
2015-11-18 17:16:21 -05:00
|
|
|
if err != nil {
|
|
|
|
// Should never happen
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
associations = append(associations,
|
|
|
|
Association{
|
2016-09-15 19:37:32 -04:00
|
|
|
Ref: ref,
|
|
|
|
ID: refID,
|
2015-11-18 17:16:21 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-07 20:55:35 -05:00
|
|
|
sort.Sort(lexicalAssociations(associations))
|
|
|
|
|
2015-11-18 17:16:21 -05:00
|
|
|
return associations
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *store) save() error {
|
|
|
|
// Store the json
|
|
|
|
jsonData, err := json.Marshal(store)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-20 20:08:47 -04:00
|
|
|
return ioutils.AtomicWriteFile(store.jsonPath, jsonData, 0600)
|
2015-11-18 17:16:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *store) reload() error {
|
|
|
|
f, err := os.Open(store.jsonPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if err := json.NewDecoder(f).Decode(&store); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, repository := range store.Repositories {
|
|
|
|
for refStr, refID := range repository {
|
2017-01-25 19:54:18 -05:00
|
|
|
ref, err := reference.ParseNormalizedNamed(refStr)
|
2015-11-18 17:16:21 -05:00
|
|
|
if err != nil {
|
|
|
|
// Should never happen
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if store.referencesByIDCache[refID] == nil {
|
2017-01-25 19:54:18 -05:00
|
|
|
store.referencesByIDCache[refID] = make(map[string]reference.Named)
|
2015-11-18 17:16:21 -05:00
|
|
|
}
|
|
|
|
store.referencesByIDCache[refID][refStr] = ref
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|