2016-05-21 20:36:11 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-08-17 22:43:36 +00:00
|
|
|
"runtime"
|
2016-05-21 20:36:11 +00:00
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2018-01-11 19:53:06 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-05-21 20:36:11 +00:00
|
|
|
"github.com/docker/docker/image"
|
|
|
|
)
|
|
|
|
|
2017-07-19 14:20:13 +00:00
|
|
|
// errImageDoesNotExist is error returned when no image can be found for a reference.
|
|
|
|
type errImageDoesNotExist struct {
|
2017-01-26 00:54:18 +00:00
|
|
|
ref reference.Reference
|
2016-05-21 20:36:11 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 14:20:13 +00:00
|
|
|
func (e errImageDoesNotExist) Error() string {
|
2017-01-26 00:54:18 +00: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 20:36:11 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 14:20:13 +00:00
|
|
|
func (e errImageDoesNotExist) NotFound() {}
|
|
|
|
|
2017-08-08 19:43:48 +00:00
|
|
|
// GetImageIDAndOS returns an image ID and operating system corresponding to the image referred to by
|
2016-05-21 20:36:11 +00:00
|
|
|
// refOrID.
|
2017-08-08 19:43:48 +00:00
|
|
|
func (daemon *Daemon) GetImageIDAndOS(refOrID string) (image.ID, string, error) {
|
2017-01-26 00:54:18 +00:00
|
|
|
ref, err := reference.ParseAnyReference(refOrID)
|
2016-05-21 20:36:11 +00:00
|
|
|
if err != nil {
|
2017-11-29 04:09:37 +00:00
|
|
|
return "", "", errdefs.InvalidParameter(err)
|
2016-05-21 20:36:11 +00:00
|
|
|
}
|
2017-01-26 00:54:18 +00:00
|
|
|
namedRef, ok := ref.(reference.Named)
|
|
|
|
if !ok {
|
|
|
|
digested, ok := ref.(reference.Digested)
|
|
|
|
if !ok {
|
2017-07-19 14:20:13 +00:00
|
|
|
return "", "", errImageDoesNotExist{ref}
|
2016-05-21 20:36:11 +00:00
|
|
|
}
|
2017-01-26 00:54:18 +00:00
|
|
|
id := image.IDFromDigest(digested.Digest())
|
2017-08-24 18:48:16 +00:00
|
|
|
if img, err := daemon.imageStore.Get(id); err == nil {
|
|
|
|
return id, img.OperatingSystem(), nil
|
2017-01-26 00:54:18 +00:00
|
|
|
}
|
2017-07-19 14:20:13 +00:00
|
|
|
return "", "", errImageDoesNotExist{ref}
|
2016-05-21 20:36:11 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 22:43:36 +00:00
|
|
|
if digest, err := daemon.referenceStore.Get(namedRef); err == nil {
|
2017-08-08 19:43:48 +00:00
|
|
|
// Search the image stores to get the operating system, defaulting to host OS.
|
2017-08-17 22:43:36 +00:00
|
|
|
id := image.IDFromDigest(digest)
|
2017-08-24 18:48:16 +00:00
|
|
|
if img, err := daemon.imageStore.Get(id); err == nil {
|
|
|
|
return id, img.OperatingSystem(), nil
|
2017-05-16 23:56:56 +00:00
|
|
|
}
|
2016-05-21 20:36:11 +00:00
|
|
|
}
|
2016-10-07 00:12:17 +00:00
|
|
|
|
2016-05-21 20:36:11 +00:00
|
|
|
// Search based on ID
|
2017-08-24 18:48:16 +00:00
|
|
|
if id, err := daemon.imageStore.Search(refOrID); err == nil {
|
|
|
|
img, err := daemon.imageStore.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", errImageDoesNotExist{ref}
|
2017-05-16 23:56:56 +00:00
|
|
|
}
|
2017-08-24 18:48:16 +00:00
|
|
|
imageOS := img.OperatingSystem()
|
|
|
|
if imageOS == "" {
|
|
|
|
imageOS = runtime.GOOS
|
|
|
|
}
|
|
|
|
return id, imageOS, nil
|
2016-05-21 20:36:11 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 14:20:13 +00:00
|
|
|
return "", "", errImageDoesNotExist{ref}
|
2016-05-21 20:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetImage returns an image corresponding to the image referred to by refOrID.
|
|
|
|
func (daemon *Daemon) GetImage(refOrID string) (*image.Image, error) {
|
2017-08-24 18:48:16 +00:00
|
|
|
imgID, _, err := daemon.GetImageIDAndOS(refOrID)
|
2016-05-21 20:36:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-24 18:48:16 +00:00
|
|
|
return daemon.imageStore.Get(imgID)
|
2016-05-21 20:36:11 +00:00
|
|
|
}
|