2014-08-05 01:02:51 -04:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/engine"
|
|
|
|
"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
|
|
|
|
2014-08-05 01:02:51 -04:00
|
|
|
func (s *TagStore) CmdImages(job *engine.Job) engine.Status {
|
|
|
|
var (
|
|
|
|
allImages map[string]*image.Image
|
|
|
|
err error
|
|
|
|
filt_tagged = true
|
2015-01-06 19:04:10 -05:00
|
|
|
filt_label = false
|
2014-08-05 01:02:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
imageFilters, err := filters.FromParam(job.Getenv("filters"))
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
2015-01-16 16:49:46 -05:00
|
|
|
for name := range imageFilters {
|
|
|
|
if _, ok := acceptedImageFilterTags[name]; !ok {
|
|
|
|
return job.Errorf("Invalid filter '%s'", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 01:02:51 -04:00
|
|
|
if i, ok := imageFilters["dangling"]; ok {
|
|
|
|
for _, value := range i {
|
|
|
|
if strings.ToLower(value) == "true" {
|
|
|
|
filt_tagged = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 19:04:10 -05:00
|
|
|
_, filt_label = imageFilters["label"]
|
|
|
|
|
2014-08-05 01:02:51 -04:00
|
|
|
if job.GetenvBool("all") && filt_tagged {
|
|
|
|
allImages, err = s.graph.Map()
|
|
|
|
} else {
|
|
|
|
allImages, err = s.graph.Heads()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
lookup := make(map[string]*engine.Env)
|
|
|
|
s.Lock()
|
2015-02-26 21:23:50 -05:00
|
|
|
for repoName, repository := range s.Repositories {
|
2014-08-05 01:02:51 -04:00
|
|
|
if job.Getenv("filter") != "" {
|
2015-02-26 21:23:50 -05:00
|
|
|
if match, _ := path.Match(job.Getenv("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-02-26 21:23:50 -05:00
|
|
|
log.Printf("Warning: couldn't load %s from %s: %s", id, imgRef, err)
|
2014-08-05 01:02:51 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if out, exists := lookup[id]; exists {
|
|
|
|
if filt_tagged {
|
2015-02-26 21:23:50 -05:00
|
|
|
if utils.DigestReference(ref) {
|
|
|
|
out.SetList("RepoDigests", append(out.GetList("RepoDigests"), imgRef))
|
|
|
|
} else { // Tag Ref.
|
|
|
|
out.SetList("RepoTags", append(out.GetList("RepoTags"), imgRef))
|
|
|
|
}
|
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
|
|
|
|
}
|
2014-08-05 01:02:51 -04:00
|
|
|
if filt_tagged {
|
|
|
|
out := &engine.Env{}
|
2014-12-11 07:56:21 -05:00
|
|
|
out.SetJson("ParentId", image.Parent)
|
|
|
|
out.SetJson("Id", image.ID)
|
2014-08-05 01:02:51 -04:00
|
|
|
out.SetInt64("Created", image.Created.Unix())
|
|
|
|
out.SetInt64("Size", image.Size)
|
|
|
|
out.SetInt64("VirtualSize", image.GetParentsSize(0)+image.Size)
|
2015-01-06 19:04:10 -05:00
|
|
|
out.SetJson("Labels", image.ContainerConfig.Labels)
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
if utils.DigestReference(ref) {
|
|
|
|
out.SetList("RepoTags", []string{})
|
|
|
|
out.SetList("RepoDigests", []string{imgRef})
|
|
|
|
} else {
|
|
|
|
out.SetList("RepoTags", []string{imgRef})
|
|
|
|
out.SetList("RepoDigests", []string{})
|
|
|
|
}
|
|
|
|
|
2014-08-05 01:02:51 -04:00
|
|
|
lookup[id] = out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Unlock()
|
|
|
|
|
|
|
|
outs := engine.NewTable("Created", len(lookup))
|
|
|
|
for _, value := range lookup {
|
|
|
|
outs.Add(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display images which aren't part of a repository/tag
|
2015-01-06 19:04:10 -05:00
|
|
|
if job.Getenv("filter") == "" || filt_label {
|
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
|
|
|
|
}
|
2014-08-05 01:02:51 -04:00
|
|
|
out := &engine.Env{}
|
2014-12-11 07:56:21 -05:00
|
|
|
out.SetJson("ParentId", image.Parent)
|
2014-08-05 01:02:51 -04:00
|
|
|
out.SetList("RepoTags", []string{"<none>:<none>"})
|
2015-02-26 21:23:50 -05:00
|
|
|
out.SetList("RepoDigests", []string{"<none>@<none>"})
|
2014-12-11 07:56:21 -05:00
|
|
|
out.SetJson("Id", image.ID)
|
2014-08-05 01:02:51 -04:00
|
|
|
out.SetInt64("Created", image.Created.Unix())
|
|
|
|
out.SetInt64("Size", image.Size)
|
|
|
|
out.SetInt64("VirtualSize", image.GetParentsSize(0)+image.Size)
|
2015-01-06 19:04:10 -05:00
|
|
|
out.SetJson("Labels", image.ContainerConfig.Labels)
|
2014-08-05 01:02:51 -04:00
|
|
|
outs.Add(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outs.ReverseSort()
|
|
|
|
if _, err := outs.WriteListTo(job.Stdout); err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
return engine.StatusOK
|
|
|
|
}
|