2016-05-21 16:36:11 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-08-17 18:43:36 -04:00
|
|
|
"runtime"
|
2016-05-21 16:36:11 -04:00
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-05-21 16:36:11 -04:00
|
|
|
"github.com/docker/docker/image"
|
2016-10-06 20:12:17 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-05-21 16:36:11 -04:00
|
|
|
)
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
// errImageDoesNotExist is error returned when no image can be found for a reference.
|
|
|
|
type errImageDoesNotExist struct {
|
2017-01-25 19:54:18 -05:00
|
|
|
ref reference.Reference
|
2016-05-21 16:36:11 -04:00
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
func (e errImageDoesNotExist) Error() string {
|
2017-01-25 19:54:18 -05: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 16:36:11 -04:00
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
func (e errImageDoesNotExist) NotFound() {}
|
|
|
|
|
2017-05-16 19:56:56 -04:00
|
|
|
// GetImageIDAndPlatform returns an image ID and platform corresponding to the image referred to by
|
2016-05-21 16:36:11 -04:00
|
|
|
// refOrID.
|
2017-05-16 19:56:56 -04:00
|
|
|
func (daemon *Daemon) GetImageIDAndPlatform(refOrID string) (image.ID, string, error) {
|
2017-01-25 19:54:18 -05:00
|
|
|
ref, err := reference.ParseAnyReference(refOrID)
|
2016-05-21 16:36:11 -04:00
|
|
|
if err != nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
return "", "", validationError{err}
|
2016-05-21 16:36:11 -04:00
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
namedRef, ok := ref.(reference.Named)
|
|
|
|
if !ok {
|
|
|
|
digested, ok := ref.(reference.Digested)
|
|
|
|
if !ok {
|
2017-07-19 10:20:13 -04:00
|
|
|
return "", "", errImageDoesNotExist{ref}
|
2016-05-21 16:36:11 -04:00
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
id := image.IDFromDigest(digested.Digest())
|
2017-05-16 19:56:56 -04:00
|
|
|
for platform := range daemon.stores {
|
|
|
|
if _, err = daemon.stores[platform].imageStore.Get(id); err == nil {
|
|
|
|
return id, platform, nil
|
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
}
|
2017-07-19 10:20:13 -04:00
|
|
|
return "", "", errImageDoesNotExist{ref}
|
2016-05-21 16:36:11 -04:00
|
|
|
}
|
|
|
|
|
2017-08-17 18:43:36 -04:00
|
|
|
if digest, err := daemon.referenceStore.Get(namedRef); err == nil {
|
|
|
|
// Search the image stores to get the platform, defaulting to host OS.
|
|
|
|
imagePlatform := runtime.GOOS
|
|
|
|
id := image.IDFromDigest(digest)
|
|
|
|
for platform := range daemon.stores {
|
|
|
|
if img, err := daemon.stores[platform].imageStore.Get(id); err == nil {
|
|
|
|
imagePlatform = img.Platform()
|
|
|
|
break
|
|
|
|
}
|
2017-05-16 19:56:56 -04:00
|
|
|
}
|
2017-08-17 18:43:36 -04:00
|
|
|
return id, imagePlatform, nil
|
2016-05-21 16:36:11 -04:00
|
|
|
}
|
2016-10-06 20:12:17 -04:00
|
|
|
|
|
|
|
// deprecated: repo:shortid https://github.com/docker/docker/pull/799
|
2017-01-25 19:54:18 -05:00
|
|
|
if tagged, ok := namedRef.(reference.Tagged); ok {
|
2016-10-06 20:12:17 -04:00
|
|
|
if tag := tagged.Tag(); stringid.IsShortID(stringid.TruncateID(tag)) {
|
2017-05-16 19:56:56 -04:00
|
|
|
for platform := range daemon.stores {
|
|
|
|
if id, err := daemon.stores[platform].imageStore.Search(tag); err == nil {
|
2017-08-17 18:43:36 -04:00
|
|
|
for _, storeRef := range daemon.referenceStore.References(id.Digest()) {
|
2017-05-16 19:56:56 -04:00
|
|
|
if storeRef.Name() == namedRef.Name() {
|
|
|
|
return id, platform, nil
|
|
|
|
}
|
2016-10-06 20:12:17 -04:00
|
|
|
}
|
2016-05-21 16:36:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search based on ID
|
2017-05-16 19:56:56 -04:00
|
|
|
for platform := range daemon.stores {
|
|
|
|
if id, err := daemon.stores[platform].imageStore.Search(refOrID); err == nil {
|
|
|
|
return id, platform, nil
|
|
|
|
}
|
2016-05-21 16:36:11 -04:00
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
return "", "", errImageDoesNotExist{ref}
|
2016-05-21 16:36:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetImage returns an image corresponding to the image referred to by refOrID.
|
|
|
|
func (daemon *Daemon) GetImage(refOrID string) (*image.Image, error) {
|
2017-05-16 19:56:56 -04:00
|
|
|
imgID, platform, err := daemon.GetImageIDAndPlatform(refOrID)
|
2016-05-21 16:36:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-16 19:56:56 -04:00
|
|
|
return daemon.stores[platform].imageStore.Get(imgID)
|
2016-05-21 16:36:11 -04:00
|
|
|
}
|