Skip to content

Commit

Permalink
Merge pull request moby#3575 from shykes/engine-get-images
Browse files Browse the repository at this point in the history
move `docker images` to a job
  • Loading branch information
vieux committed Jan 14, 2014
2 parents 6652416 + 16ca6a1 commit 9792df2
Show file tree
Hide file tree
Showing 16 changed files with 395 additions and 322 deletions.
57 changes: 43 additions & 14 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"github.com/dotcloud/docker/archive"
"github.com/dotcloud/docker/auth"
"github.com/dotcloud/docker/engine"
"github.com/dotcloud/docker/pkg/systemd"
"github.com/dotcloud/docker/utils"
"github.com/gorilla/mux"
Expand All @@ -28,7 +29,7 @@ import (
)

const (
APIVERSION = 1.8
APIVERSION = 1.9
DEFAULTHTTPHOST = "127.0.0.1"
DEFAULTHTTPPORT = 4243
DEFAULTUNIXSOCKET = "/var/run/docker.sock"
Expand Down Expand Up @@ -182,26 +183,54 @@ func getImagesJSON(srv *Server, version float64, w http.ResponseWriter, r *http.
return err
}

all, err := getBoolParam(r.Form.Get("all"))
if err != nil {
return err
var (
buffer *bytes.Buffer
job = srv.Eng.Job("images")
)

job.Setenv("filter", r.Form.Get("filter"))
job.Setenv("all", r.Form.Get("all"))

if version >= 1.9 {
job.Stdout.Add(w)
} else {
buffer = bytes.NewBuffer(nil)
job.Stdout.Add(buffer)
}
filter := r.Form.Get("filter")

outs, err := srv.Images(all, filter)
if err != nil {
if err := job.Run(); err != nil {
return err
}

if version < 1.7 {
outs2 := []APIImagesOld{}
for _, ctnr := range outs {
outs2 = append(outs2, ctnr.ToLegacy()...)
if version < 1.9 { // Send as a valide JSON array
outs := engine.NewTable("Created", 0)
if _, err := outs.ReadFrom(buffer); err != nil {
return err
}
if version < 1.8 { // Convert to legacy format
outsLegacy := engine.NewTable("Created", 0)
for _, out := range outs.Data {
for _, repoTag := range out.GetList("RepoTags") {
parts := strings.Split(repoTag, ":")
outLegacy := &engine.Env{}
outLegacy.Set("Repository", parts[0])
outLegacy.Set("Tag", parts[1])
outLegacy.Set("ID", out.Get("ID"))
outLegacy.SetInt64("Created", out.GetInt64("Created"))
outLegacy.SetInt64("Size", out.GetInt64("Size"))
outLegacy.SetInt64("VirtualSize", out.GetInt64("VirtualSize"))
outsLegacy.Add(outLegacy)
}
}
if _, err := outsLegacy.WriteListTo(w); err != nil {
return err
}
} else if _, err := outs.WriteListTo(w); err != nil {
return err
}

return writeJSON(w, http.StatusOK, outs2)
}
return writeJSON(w, http.StatusOK, outs)

return nil
}

func getImagesViz(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
Expand Down
36 changes: 0 additions & 36 deletions api_params.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package docker

import "strings"

type (
APIHistory struct {
ID string `json:"Id"`
Expand All @@ -11,24 +9,6 @@ type (
Size int64
}

APIImages struct {
ID string `json:"Id"`
RepoTags []string `json:",omitempty"`
Created int64
Size int64
VirtualSize int64
ParentId string `json:",omitempty"`
}

APIImagesOld struct {
Repository string `json:",omitempty"`
Tag string `json:",omitempty"`
ID string `json:"Id"`
Created int64
Size int64
VirtualSize int64
}

APITop struct {
Titles []string
Processes [][]string
Expand Down Expand Up @@ -101,22 +81,6 @@ type (
}
)

func (api APIImages) ToLegacy() []APIImagesOld {
outs := []APIImagesOld{}
for _, repotag := range api.RepoTags {
components := strings.SplitN(repotag, ":", 2)
outs = append(outs, APIImagesOld{
ID: api.ID,
Repository: components[0],
Tag: components[1],
Created: api.Created,
Size: api.Size,
VirtualSize: api.VirtualSize,
})
}
return outs
}

func (api APIContainers) ToLegacy() *APIContainersOld {
return &APIContainersOld{
ID: api.ID,
Expand Down
105 changes: 54 additions & 51 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1137,36 +1137,38 @@ func (cli *DockerCli) CmdImages(args ...string) error {
return err
}

var outs []APIImages
if err := json.Unmarshal(body, &outs); err != nil {
outs := engine.NewTable("Created", 0)

if _, err := outs.ReadFrom(bytes.NewReader(body)); err != nil {
return err
}

var (
printNode func(cli *DockerCli, noTrunc bool, image APIImages, prefix string)
startImage APIImages
printNode func(cli *DockerCli, noTrunc bool, image *engine.Env, prefix string)
startImage *engine.Env

roots []APIImages
byParent = make(map[string][]APIImages)
roots = engine.NewTable("Created", outs.Len())
byParent = make(map[string]*engine.Table)
)

for _, image := range outs {
if image.ParentId == "" {
roots = append(roots, image)
for _, image := range outs.Data {
if image.Get("ParentId") == "" {
roots.Add(image)
} else {
if children, exists := byParent[image.ParentId]; exists {
byParent[image.ParentId] = append(children, image)
if children, exists := byParent[image.Get("ParentId")]; exists {
children.Add(image)
} else {
byParent[image.ParentId] = []APIImages{image}
byParent[image.Get("ParentId")] = engine.NewTable("Created", 1)
byParent[image.Get("ParentId")].Add(image)
}
}

if filter != "" {
if filter == image.ID || filter == utils.TruncateID(image.ID) {
if filter == image.Get("ID") || filter == utils.TruncateID(image.Get("ID")) {
startImage = image
}

for _, repotag := range image.RepoTags {
for _, repotag := range image.GetList("RepoTags") {
if repotag == filter {
startImage = image
}
Expand All @@ -1181,10 +1183,12 @@ func (cli *DockerCli) CmdImages(args ...string) error {
printNode = (*DockerCli).printTreeNode
}

if startImage.ID != "" {
cli.WalkTree(*noTrunc, &[]APIImages{startImage}, byParent, "", printNode)
if startImage != nil {
root := engine.NewTable("Created", 1)
root.Add(startImage)
cli.WalkTree(*noTrunc, root, byParent, "", printNode)
} else if filter == "" {
cli.WalkTree(*noTrunc, &roots, byParent, "", printNode)
cli.WalkTree(*noTrunc, roots, byParent, "", printNode)
}
if *flViz {
fmt.Fprintf(cli.out, " base [style=invisible]\n}\n")
Expand All @@ -1203,9 +1207,8 @@ func (cli *DockerCli) CmdImages(args ...string) error {
return err
}

var outs []APIImages
err = json.Unmarshal(body, &outs)
if err != nil {
outs := engine.NewTable("Created", 0)
if _, err := outs.ReadFrom(bytes.NewReader(body)); err != nil {
return err
}

Expand All @@ -1214,19 +1217,19 @@ func (cli *DockerCli) CmdImages(args ...string) error {
fmt.Fprintln(w, "REPOSITORY\tTAG\tIMAGE ID\tCREATED\tVIRTUAL SIZE")
}

for _, out := range outs {
for _, repotag := range out.RepoTags {
for _, out := range outs.Data {
for _, repotag := range out.GetList("RepoTags") {

repo, tag := utils.ParseRepositoryTag(repotag)

outID := out.Get("ID")
if !*noTrunc {
out.ID = utils.TruncateID(out.ID)
outID = utils.TruncateID(outID)
}

if !*quiet {
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\n", repo, tag, out.ID, utils.HumanDuration(time.Now().UTC().Sub(time.Unix(out.Created, 0))), utils.HumanSize(out.VirtualSize))
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\n", repo, tag, outID, utils.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))), utils.HumanSize(out.GetInt64("VirtualSize")))
} else {
fmt.Fprintln(w, out.ID)
fmt.Fprintln(w, outID)
}
}
}
Expand All @@ -1238,66 +1241,66 @@ func (cli *DockerCli) CmdImages(args ...string) error {
return nil
}

func (cli *DockerCli) WalkTree(noTrunc bool, images *[]APIImages, byParent map[string][]APIImages, prefix string, printNode func(cli *DockerCli, noTrunc bool, image APIImages, prefix string)) {
length := len(*images)
func (cli *DockerCli) WalkTree(noTrunc bool, images *engine.Table, byParent map[string]*engine.Table, prefix string, printNode func(cli *DockerCli, noTrunc bool, image *engine.Env, prefix string)) {
length := images.Len()
if length > 1 {
for index, image := range *images {
for index, image := range images.Data {
if index+1 == length {
printNode(cli, noTrunc, image, prefix+"└─")
if subimages, exists := byParent[image.ID]; exists {
cli.WalkTree(noTrunc, &subimages, byParent, prefix+" ", printNode)
if subimages, exists := byParent[image.Get("ID")]; exists {
cli.WalkTree(noTrunc, subimages, byParent, prefix+" ", printNode)
}
} else {
printNode(cli, noTrunc, image, prefix+"─")
if subimages, exists := byParent[image.ID]; exists {
cli.WalkTree(noTrunc, &subimages, byParent, prefix+" ", printNode)
printNode(cli, noTrunc, image, prefix+"\u251C─")
if subimages, exists := byParent[image.Get("ID")]; exists {
cli.WalkTree(noTrunc, subimages, byParent, prefix+"\u2502 ", printNode)
}
}
}
} else {
for _, image := range *images {
for _, image := range images.Data {
printNode(cli, noTrunc, image, prefix+"└─")
if subimages, exists := byParent[image.ID]; exists {
cli.WalkTree(noTrunc, &subimages, byParent, prefix+" ", printNode)
if subimages, exists := byParent[image.Get("ID")]; exists {
cli.WalkTree(noTrunc, subimages, byParent, prefix+" ", printNode)
}
}
}
}

func (cli *DockerCli) printVizNode(noTrunc bool, image APIImages, prefix string) {
func (cli *DockerCli) printVizNode(noTrunc bool, image *engine.Env, prefix string) {
var (
imageID string
parentID string
)
if noTrunc {
imageID = image.ID
parentID = image.ParentId
imageID = image.Get("ID")
parentID = image.Get("ParentId")
} else {
imageID = utils.TruncateID(image.ID)
parentID = utils.TruncateID(image.ParentId)
imageID = utils.TruncateID(image.Get("ID"))
parentID = utils.TruncateID(image.Get("ParentId"))
}
if image.ParentId == "" {
if parentID == "" {
fmt.Fprintf(cli.out, " base -> \"%s\" [style=invis]\n", imageID)
} else {
fmt.Fprintf(cli.out, " \"%s\" -> \"%s\"\n", parentID, imageID)
}
if image.RepoTags[0] != "<none>:<none>" {
if image.GetList("RepoTags")[0] != "<none>:<none>" {
fmt.Fprintf(cli.out, " \"%s\" [label=\"%s\\n%s\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n",
imageID, imageID, strings.Join(image.RepoTags, "\\n"))
imageID, imageID, strings.Join(image.GetList("RepoTags"), "\\n"))
}
}

func (cli *DockerCli) printTreeNode(noTrunc bool, image APIImages, prefix string) {
func (cli *DockerCli) printTreeNode(noTrunc bool, image *engine.Env, prefix string) {
var imageID string
if noTrunc {
imageID = image.ID
imageID = image.Get("ID")
} else {
imageID = utils.TruncateID(image.ID)
imageID = utils.TruncateID(image.Get("ID"))
}

fmt.Fprintf(cli.out, "%s%s Virtual Size: %s", prefix, imageID, utils.HumanSize(image.VirtualSize))
if image.RepoTags[0] != "<none>:<none>" {
fmt.Fprintf(cli.out, " Tags: %s\n", strings.Join(image.RepoTags, ", "))
fmt.Fprintf(cli.out, "%s%s Virtual Size: %s", prefix, imageID, utils.HumanSize(image.GetInt64("VirtualSize")))
if image.GetList("RepoTags")[0] != "<none>:<none>" {
fmt.Fprintf(cli.out, " Tags: %s\n", strings.Join(image.GetList("RepoTags"), ", "))
} else {
fmt.Fprint(cli.out, "\n")
}
Expand Down
Loading

0 comments on commit 9792df2

Please sign in to comment.