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"
|
|
|
|
)
|
|
|
|
|
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-08-08 15:43:48 -04:00
|
|
|
// GetImageIDAndOS returns an image ID and operating system corresponding to the image referred to by
|
2016-05-21 16:36:11 -04:00
|
|
|
// refOrID.
|
2017-08-08 15:43:48 -04:00
|
|
|
func (daemon *Daemon) GetImageIDAndOS(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 {
|
2017-08-08 15:43:48 -04:00
|
|
|
// Search the image stores to get the operating system, defaulting to host OS.
|
|
|
|
imageOS := runtime.GOOS
|
2017-08-17 18:43:36 -04:00
|
|
|
id := image.IDFromDigest(digest)
|
2017-08-08 15:43:48 -04:00
|
|
|
for os := range daemon.stores {
|
|
|
|
if img, err := daemon.stores[os].imageStore.Get(id); err == nil {
|
|
|
|
imageOS = img.OperatingSystem()
|
2017-08-17 18:43:36 -04:00
|
|
|
break
|
|
|
|
}
|
2017-05-16 19:56:56 -04:00
|
|
|
}
|
2017-08-08 15:43:48 -04:00
|
|
|
return id, imageOS, nil
|
2016-05-21 16:36:11 -04:00
|
|
|
}
|
2016-10-06 20:12:17 -04:00
|
|
|
|
2016-05-21 16:36:11 -04:00
|
|
|
// Search based on ID
|
2017-08-08 15:43:48 -04:00
|
|
|
for os := range daemon.stores {
|
|
|
|
if id, err := daemon.stores[os].imageStore.Search(refOrID); err == nil {
|
|
|
|
return id, os, nil
|
2017-05-16 19:56:56 -04:00
|
|
|
}
|
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-08-08 15:43:48 -04:00
|
|
|
imgID, os, err := daemon.GetImageIDAndOS(refOrID)
|
2016-05-21 16:36:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-08 15:43:48 -04:00
|
|
|
return daemon.stores[os].imageStore.Get(imgID)
|
2016-05-21 16:36:11 -04:00
|
|
|
}
|