2014-08-05 01:02:51 -04:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
2015-03-25 03:44:12 -04:00
|
|
|
"fmt"
|
2014-08-05 01:02:51 -04:00
|
|
|
"path"
|
2015-04-03 20:39:06 -04:00
|
|
|
"sort"
|
2014-08-05 01:02:51 -04:00
|
|
|
"strings"
|
|
|
|
|
2015-07-02 10:11:52 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-04-03 20:39:06 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-07-20 13:57:15 -04:00
|
|
|
"github.com/docker/docker/image"
|
2014-08-05 01:02:51 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers/filters"
|
2015-02-26 21:23:50 -05:00
|
|
|
"github.com/docker/docker/utils"
|
2014-08-05 01:02:51 -04:00
|
|
|
)
|
|
|
|
|
2015-01-06 19:04:10 -05:00
|
|
|
var acceptedImageFilterTags = map[string]struct{}{
|
|
|
|
"dangling": {},
|
|
|
|
"label": {},
|
|
|
|
}
|
2015-01-16 16:49:46 -05:00
|
|
|
|
2015-07-21 12:21:45 -04:00
|
|
|
// ImagesConfig defines the criteria to obtain a list of images.
|
2015-04-07 21:57:54 -04:00
|
|
|
type ImagesConfig struct {
|
2015-07-21 12:21:45 -04:00
|
|
|
// Filters is supported list of filters used to get list of images.
|
2015-04-07 21:57:54 -04:00
|
|
|
Filters string
|
2015-07-21 12:21:45 -04:00
|
|
|
// Filter the list of images by name.
|
|
|
|
Filter string
|
|
|
|
// All inditest that all the images will be returned in the list, if set to true.
|
|
|
|
All bool
|
2015-04-07 21:57:54 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 12:21:45 -04:00
|
|
|
// byCreated is a temporary type used to sort list of images on their field 'Created'.
|
|
|
|
type byCreated []*types.Image
|
2015-04-03 20:39:06 -04:00
|
|
|
|
2015-07-21 12:21:45 -04:00
|
|
|
func (r byCreated) Len() int { return len(r) }
|
|
|
|
func (r byCreated) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
|
|
func (r byCreated) Less(i, j int) bool { return r[i].Created < r[j].Created }
|
2015-04-03 20:39:06 -04:00
|
|
|
|
2015-07-21 12:21:45 -04:00
|
|
|
// Images provide list of images based on selection criteria.
|
2015-04-07 21:57:54 -04:00
|
|
|
func (s *TagStore) Images(config *ImagesConfig) ([]*types.Image, error) {
|
2014-08-05 01:02:51 -04:00
|
|
|
var (
|
2015-07-20 13:57:15 -04:00
|
|
|
allImages map[string]*image.Image
|
2015-03-25 21:40:23 -04:00
|
|
|
err error
|
|
|
|
filtTagged = true
|
|
|
|
filtLabel = false
|
2014-08-05 01:02:51 -04:00
|
|
|
)
|
|
|
|
|
2015-04-07 21:57:54 -04:00
|
|
|
imageFilters, err := filters.FromParam(config.Filters)
|
2014-08-05 01:02:51 -04:00
|
|
|
if err != nil {
|
2015-04-07 21:57:54 -04:00
|
|
|
return nil, err
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
2015-01-16 16:49:46 -05:00
|
|
|
for name := range imageFilters {
|
|
|
|
if _, ok := acceptedImageFilterTags[name]; !ok {
|
2015-04-07 21:57:54 -04:00
|
|
|
return nil, fmt.Errorf("Invalid filter '%s'", name)
|
2015-01-16 16:49:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 01:02:51 -04:00
|
|
|
if i, ok := imageFilters["dangling"]; ok {
|
|
|
|
for _, value := range i {
|
|
|
|
if strings.ToLower(value) == "true" {
|
2015-03-25 21:40:23 -04:00
|
|
|
filtTagged = false
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 21:40:23 -04:00
|
|
|
_, filtLabel = imageFilters["label"]
|
2015-01-06 19:04:10 -05:00
|
|
|
|
2015-04-07 21:57:54 -04:00
|
|
|
if config.All && filtTagged {
|
2015-06-19 11:01:39 -04:00
|
|
|
allImages = s.graph.Map()
|
2014-08-05 01:02:51 -04:00
|
|
|
} else {
|
2015-06-19 11:01:39 -04:00
|
|
|
allImages = s.graph.Heads()
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
2015-04-03 20:39:06 -04:00
|
|
|
|
|
|
|
lookup := make(map[string]*types.Image)
|
2014-08-05 01:02:51 -04:00
|
|
|
s.Lock()
|
2015-02-26 21:23:50 -05:00
|
|
|
for repoName, repository := range s.Repositories {
|
2015-04-07 21:57:54 -04:00
|
|
|
if config.Filter != "" {
|
|
|
|
if match, _ := path.Match(config.Filter, repoName); !match {
|
2014-08-05 01:02:51 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2015-02-26 21:23:50 -05:00
|
|
|
for ref, id := range repository {
|
|
|
|
imgRef := utils.ImageReference(repoName, ref)
|
2014-08-05 01:02:51 -04:00
|
|
|
image, err := s.graph.Get(id)
|
|
|
|
if err != nil {
|
2015-07-02 10:11:52 -04:00
|
|
|
logrus.Warnf("couldn't load %s from %s: %s", id, imgRef, err)
|
2014-08-05 01:02:51 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-04-03 20:39:06 -04:00
|
|
|
if lImage, exists := lookup[id]; exists {
|
2015-03-25 21:40:23 -04:00
|
|
|
if filtTagged {
|
2015-02-26 21:23:50 -05:00
|
|
|
if utils.DigestReference(ref) {
|
2015-04-03 20:39:06 -04:00
|
|
|
lImage.RepoDigests = append(lImage.RepoDigests, imgRef)
|
2015-02-26 21:23:50 -05:00
|
|
|
} else { // Tag Ref.
|
2015-04-03 20:39:06 -04:00
|
|
|
lImage.RepoTags = append(lImage.RepoTags, imgRef)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// get the boolean list for if only the untagged images are requested
|
|
|
|
delete(allImages, id)
|
2015-01-06 19:04:10 -05:00
|
|
|
if !imageFilters.MatchKVList("label", image.ContainerConfig.Labels) {
|
|
|
|
continue
|
|
|
|
}
|
2015-03-25 21:40:23 -04:00
|
|
|
if filtTagged {
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage := new(types.Image)
|
|
|
|
newImage.ParentId = image.Parent
|
|
|
|
newImage.ID = image.ID
|
2015-05-14 20:31:34 -04:00
|
|
|
newImage.Created = int(image.Created.Unix())
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage.Size = int(image.Size)
|
2015-06-05 18:31:10 -04:00
|
|
|
newImage.VirtualSize = int(s.graph.GetParentsSize(image, 0) + image.Size)
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage.Labels = image.ContainerConfig.Labels
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
if utils.DigestReference(ref) {
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage.RepoTags = []string{}
|
|
|
|
newImage.RepoDigests = []string{imgRef}
|
2015-02-26 21:23:50 -05:00
|
|
|
} else {
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage.RepoTags = []string{imgRef}
|
|
|
|
newImage.RepoDigests = []string{}
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
2015-04-03 20:39:06 -04:00
|
|
|
lookup[id] = newImage
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Unlock()
|
|
|
|
|
2015-04-03 20:39:06 -04:00
|
|
|
images := []*types.Image{}
|
2014-08-05 01:02:51 -04:00
|
|
|
for _, value := range lookup {
|
2015-04-03 20:39:06 -04:00
|
|
|
images = append(images, value)
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Display images which aren't part of a repository/tag
|
2015-04-07 21:57:54 -04:00
|
|
|
if config.Filter == "" || filtLabel {
|
2014-08-05 01:02:51 -04:00
|
|
|
for _, image := range allImages {
|
2015-01-06 19:04:10 -05:00
|
|
|
if !imageFilters.MatchKVList("label", image.ContainerConfig.Labels) {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage := new(types.Image)
|
|
|
|
newImage.ParentId = image.Parent
|
|
|
|
newImage.RepoTags = []string{"<none>:<none>"}
|
|
|
|
newImage.RepoDigests = []string{"<none>@<none>"}
|
|
|
|
newImage.ID = image.ID
|
2015-05-14 20:31:34 -04:00
|
|
|
newImage.Created = int(image.Created.Unix())
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage.Size = int(image.Size)
|
2015-06-05 18:31:10 -04:00
|
|
|
newImage.VirtualSize = int(s.graph.GetParentsSize(image, 0) + image.Size)
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage.Labels = image.ContainerConfig.Labels
|
|
|
|
|
|
|
|
images = append(images, newImage)
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 12:21:45 -04:00
|
|
|
sort.Sort(sort.Reverse(byCreated(images)))
|
2015-04-03 20:39:06 -04:00
|
|
|
|
2015-04-07 21:57:54 -04:00
|
|
|
return images, nil
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|