2016-05-21 23:36:11 +03:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-01-26 03:54:18 +03:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-05-21 23:36:11 +03:00
|
|
|
"github.com/docker/docker/builder"
|
|
|
|
"github.com/docker/docker/image"
|
2016-10-07 03:12:17 +03:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-05-21 23:36:11 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrImageDoesNotExist is error returned when no image can be found for a reference.
|
|
|
|
type ErrImageDoesNotExist struct {
|
2017-01-26 03:54:18 +03:00
|
|
|
ref reference.Reference
|
2016-05-21 23:36:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrImageDoesNotExist) Error() string {
|
2017-01-26 03:54:18 +03:00
|
|
|
ref := e.ref
|
|
|
|
if named, ok := ref.(reference.Named); ok {
|
|
|
|
ref = reference.TagNameOnly(named)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("No such image: %s", reference.FamiliarString(ref))
|
2016-05-21 23:36:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetImageID returns an image ID corresponding to the image referred to by
|
|
|
|
// refOrID.
|
|
|
|
func (daemon *Daemon) GetImageID(refOrID string) (image.ID, error) {
|
2017-01-26 03:54:18 +03:00
|
|
|
ref, err := reference.ParseAnyReference(refOrID)
|
2016-05-21 23:36:11 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-01-26 03:54:18 +03:00
|
|
|
namedRef, ok := ref.(reference.Named)
|
|
|
|
if !ok {
|
|
|
|
digested, ok := ref.(reference.Digested)
|
|
|
|
if !ok {
|
|
|
|
return "", ErrImageDoesNotExist{ref}
|
2016-05-21 23:36:11 +03:00
|
|
|
}
|
2017-01-26 03:54:18 +03:00
|
|
|
id := image.IDFromDigest(digested.Digest())
|
|
|
|
if _, err := daemon.imageStore.Get(id); err != nil {
|
|
|
|
return "", ErrImageDoesNotExist{ref}
|
|
|
|
}
|
|
|
|
return id, nil
|
2016-05-21 23:36:11 +03:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:54:18 +03:00
|
|
|
if id, err := daemon.referenceStore.Get(namedRef); err == nil {
|
2016-09-16 02:37:32 +03:00
|
|
|
return image.IDFromDigest(id), nil
|
2016-05-21 23:36:11 +03:00
|
|
|
}
|
2016-10-07 03:12:17 +03:00
|
|
|
|
|
|
|
// deprecated: repo:shortid https://github.com/docker/docker/pull/799
|
2017-01-26 03:54:18 +03:00
|
|
|
if tagged, ok := namedRef.(reference.Tagged); ok {
|
2016-10-07 03:12:17 +03:00
|
|
|
if tag := tagged.Tag(); stringid.IsShortID(stringid.TruncateID(tag)) {
|
|
|
|
if id, err := daemon.imageStore.Search(tag); err == nil {
|
2017-01-26 03:54:18 +03:00
|
|
|
for _, storeRef := range daemon.referenceStore.References(id.Digest()) {
|
|
|
|
if storeRef.Name() == namedRef.Name() {
|
2016-10-07 03:12:17 +03:00
|
|
|
return id, nil
|
|
|
|
}
|
2016-05-21 23:36:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search based on ID
|
|
|
|
if id, err := daemon.imageStore.Search(refOrID); err == nil {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:54:18 +03:00
|
|
|
return "", ErrImageDoesNotExist{ref}
|
2016-05-21 23:36:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetImage returns an image corresponding to the image referred to by refOrID.
|
|
|
|
func (daemon *Daemon) GetImage(refOrID string) (*image.Image, error) {
|
|
|
|
imgID, err := daemon.GetImageID(refOrID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return daemon.imageStore.Get(imgID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetImageOnBuild looks up a Docker image referenced by `name`.
|
|
|
|
func (daemon *Daemon) GetImageOnBuild(name string) (builder.Image, error) {
|
|
|
|
img, err := daemon.GetImage(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return img, nil
|
|
|
|
}
|