2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2016-05-21 16:36:11 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
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-11-28 23:09:37 -05:00
|
|
|
return "", "", errdefs.InvalidParameter(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-08-24 14:48:16 -04:00
|
|
|
if img, err := daemon.imageStore.Get(id); err == nil {
|
|
|
|
return id, img.OperatingSystem(), 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.
|
2017-08-17 18:43:36 -04:00
|
|
|
id := image.IDFromDigest(digest)
|
2017-08-24 14:48:16 -04:00
|
|
|
if img, err := daemon.imageStore.Get(id); err == nil {
|
|
|
|
return id, img.OperatingSystem(), nil
|
2017-05-16 19:56:56 -04:00
|
|
|
}
|
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-24 14:48:16 -04: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 19:56:56 -04:00
|
|
|
}
|
2017-09-19 15:14:46 -04:00
|
|
|
return id, img.OperatingSystem(), 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-08-24 14:48:16 -04:00
|
|
|
imgID, _, err := daemon.GetImageIDAndOS(refOrID)
|
2016-05-21 16:36:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-24 14:48:16 -04:00
|
|
|
return daemon.imageStore.Get(imgID)
|
2016-05-21 16:36:11 -04:00
|
|
|
}
|