diff --git a/graph/list.go b/graph/list.go new file mode 100644 index 0000000000..0e0e97e447 --- /dev/null +++ b/graph/list.go @@ -0,0 +1,103 @@ +package graph + +import ( + "fmt" + "log" + "path" + "strings" + + "github.com/docker/docker/engine" + "github.com/docker/docker/image" + "github.com/docker/docker/pkg/parsers/filters" +) + +func (s *TagStore) CmdImages(job *engine.Job) engine.Status { + var ( + allImages map[string]*image.Image + err error + filt_tagged = true + ) + + imageFilters, err := filters.FromParam(job.Getenv("filters")) + if err != nil { + return job.Error(err) + } + if i, ok := imageFilters["dangling"]; ok { + for _, value := range i { + if strings.ToLower(value) == "true" { + filt_tagged = false + } + } + } + + 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() + for name, repository := range s.Repositories { + if job.Getenv("filter") != "" { + if match, _ := path.Match(job.Getenv("filter"), name); !match { + continue + } + } + for tag, id := range repository { + image, err := s.graph.Get(id) + if err != nil { + log.Printf("Warning: couldn't load %s from %s/%s: %s", id, name, tag, err) + continue + } + + if out, exists := lookup[id]; exists { + if filt_tagged { + out.SetList("RepoTags", append(out.GetList("RepoTags"), fmt.Sprintf("%s:%s", name, tag))) + } + } else { + // get the boolean list for if only the untagged images are requested + delete(allImages, id) + if filt_tagged { + out := &engine.Env{} + out.Set("ParentId", image.Parent) + out.SetList("RepoTags", []string{fmt.Sprintf("%s:%s", name, tag)}) + out.Set("Id", image.ID) + out.SetInt64("Created", image.Created.Unix()) + out.SetInt64("Size", image.Size) + out.SetInt64("VirtualSize", image.GetParentsSize(0)+image.Size) + 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 + if job.Getenv("filter") == "" { + for _, image := range allImages { + out := &engine.Env{} + out.Set("ParentId", image.Parent) + out.SetList("RepoTags", []string{":"}) + out.Set("Id", image.ID) + out.SetInt64("Created", image.Created.Unix()) + out.SetInt64("Size", image.Size) + out.SetInt64("VirtualSize", image.GetParentsSize(0)+image.Size) + outs.Add(out) + } + } + + outs.ReverseSort() + if _, err := outs.WriteListTo(job.Stdout); err != nil { + return job.Error(err) + } + return engine.StatusOK +} diff --git a/graph/service.go b/graph/service.go index bab02c3981..0577e5d6bb 100644 --- a/graph/service.go +++ b/graph/service.go @@ -17,6 +17,7 @@ func (s *TagStore) Install(eng *engine.Engine) error { eng.Register("image_tarlayer", s.CmdTarLayer) eng.Register("image_export", s.CmdImageExport) eng.Register("history", s.CmdHistory) + eng.Register("images", s.CmdImages) return nil } diff --git a/server/image.go b/server/image.go index 99b8e62e69..010851d12e 100644 --- a/server/image.go +++ b/server/image.go @@ -9,7 +9,6 @@ import ( "fmt" "io" "io/ioutil" - "log" "net" "net/http" "net/url" @@ -25,7 +24,6 @@ import ( "github.com/docker/docker/graph" "github.com/docker/docker/image" "github.com/docker/docker/pkg/parsers" - "github.com/docker/docker/pkg/parsers/filters" "github.com/docker/docker/registry" "github.com/docker/docker/utils" ) @@ -244,97 +242,6 @@ func (srv *Server) ImagesViz(job *engine.Job) engine.Status { return engine.StatusOK } -func (srv *Server) Images(job *engine.Job) engine.Status { - var ( - allImages map[string]*image.Image - err error - filt_tagged = true - ) - - imageFilters, err := filters.FromParam(job.Getenv("filters")) - if err != nil { - return job.Error(err) - } - if i, ok := imageFilters["dangling"]; ok { - for _, value := range i { - if strings.ToLower(value) == "true" { - filt_tagged = false - } - } - } - - if job.GetenvBool("all") && filt_tagged { - allImages, err = srv.daemon.Graph().Map() - } else { - allImages, err = srv.daemon.Graph().Heads() - } - if err != nil { - return job.Error(err) - } - lookup := make(map[string]*engine.Env) - srv.daemon.Repositories().Lock() - for name, repository := range srv.daemon.Repositories().Repositories { - if job.Getenv("filter") != "" { - if match, _ := path.Match(job.Getenv("filter"), name); !match { - continue - } - } - for tag, id := range repository { - image, err := srv.daemon.Graph().Get(id) - if err != nil { - log.Printf("Warning: couldn't load %s from %s/%s: %s", id, name, tag, err) - continue - } - - if out, exists := lookup[id]; exists { - if filt_tagged { - out.SetList("RepoTags", append(out.GetList("RepoTags"), fmt.Sprintf("%s:%s", name, tag))) - } - } else { - // get the boolean list for if only the untagged images are requested - delete(allImages, id) - if filt_tagged { - out := &engine.Env{} - out.Set("ParentId", image.Parent) - out.SetList("RepoTags", []string{fmt.Sprintf("%s:%s", name, tag)}) - out.Set("Id", image.ID) - out.SetInt64("Created", image.Created.Unix()) - out.SetInt64("Size", image.Size) - out.SetInt64("VirtualSize", image.GetParentsSize(0)+image.Size) - lookup[id] = out - } - } - - } - } - srv.daemon.Repositories().Unlock() - - outs := engine.NewTable("Created", len(lookup)) - for _, value := range lookup { - outs.Add(value) - } - - // Display images which aren't part of a repository/tag - if job.Getenv("filter") == "" { - for _, image := range allImages { - out := &engine.Env{} - out.Set("ParentId", image.Parent) - out.SetList("RepoTags", []string{":"}) - out.Set("Id", image.ID) - out.SetInt64("Created", image.Created.Unix()) - out.SetInt64("Size", image.Size) - out.SetInt64("VirtualSize", image.GetParentsSize(0)+image.Size) - outs.Add(out) - } - } - - outs.ReverseSort() - if _, err := outs.WriteListTo(job.Stdout); err != nil { - return job.Error(err) - } - return engine.StatusOK -} - func (srv *Server) ImageTag(job *engine.Job) engine.Status { if len(job.Args) != 2 && len(job.Args) != 3 { return job.Errorf("Usage: %s IMAGE REPOSITORY [TAG]\n", job.Name) diff --git a/server/init.go b/server/init.go index 6ae1329387..8c79afc64d 100644 --- a/server/init.go +++ b/server/init.go @@ -88,7 +88,6 @@ func InitServer(job *engine.Job) engine.Status { for name, handler := range map[string]engine.Handler{ "tag": srv.ImageTag, // FIXME merge with "image_tag" "info": srv.DockerInfo, - "images": srv.Images, "viz": srv.ImagesViz, "log": srv.Log, "load": srv.ImageLoad,