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"
|
2014-08-05 01:02:51 -04:00
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"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-04-07 21:57:54 -04:00
|
|
|
type ImagesConfig struct {
|
|
|
|
Filters string
|
|
|
|
Filter string
|
|
|
|
All bool
|
|
|
|
}
|
|
|
|
|
2015-04-03 20:39:06 -04:00
|
|
|
type ByCreated []*types.Image
|
|
|
|
|
|
|
|
func (r ByCreated) Len() int { return len(r) }
|
|
|
|
func (r ByCreated) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
2015-05-14 20:31:34 -04:00
|
|
|
func (r ByCreated) Less(i, j int) bool { return r[i].Created < r[j].Created }
|
2015-04-03 20:39:06 -04:00
|
|
|
|
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-03-25 21:40:23 -04:00
|
|
|
allImages map[string]*image.Image
|
|
|
|
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 {
|
2014-08-05 01:02:51 -04:00
|
|
|
allImages, err = s.graph.Map()
|
|
|
|
} else {
|
|
|
|
allImages, err = s.graph.Heads()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2015-04-07 21:57:54 -04:00
|
|
|
return nil, err
|
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-04-03 20:39:06 -04:00
|
|
|
sort.Sort(sort.Reverse(ByCreated(images)))
|
|
|
|
|
2015-04-07 21:57:54 -04:00
|
|
|
return images, nil
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|