Merge branch 'pr/13' into only_labelled

This commit is contained in:
Nate Jones 2015-11-07 18:31:37 -08:00
commit b86486fdbc
5 changed files with 167 additions and 100 deletions

View file

@ -34,10 +34,20 @@ Image info is visualized with lines indicating parent images:
``` ```
$ dockviz images -d | dot -Tpng -o images.png $ dockviz images -d | dot -Tpng -o images.png
OR
$ dockviz images --dot | dot -Tpng -o images.png
``` ```
![](sample/images.png "Image") ![](sample/images.png "Image")
```
$ dockviz images -d -l | dot -Tpng -o images.png
OR
$ dockviz images --dot -only-labeled | dot -Tpng -o images.png
```
![](sample/images_only_labeled.png "Image")
Or in short form: Or in short form:
``` ```
@ -52,35 +62,47 @@ Or as a tree in the terminal:
``` ```
$ dockviz images -t $ dockviz images -t
└─511136ea3c5a Virtual Size: 0.0 B └─511136ea3c5a Virtual Size: 0.0 B
|─f10ebce2c0e1 Virtual Size: 103.7 MB ─f10ebce2c0e1 Virtual Size: 103.7 MB
| └─82cdea7ab5b5 Virtual Size: 103.9 MB └─82cdea7ab5b5 Virtual Size: 103.9 MB
| └─5dbd9cb5a02f Virtual Size: 103.9 MB └─5dbd9cb5a02f Virtual Size: 103.9 MB
| └─74fe38d11401 Virtual Size: 209.6 MB Tags: ubuntu:12.04, ubuntu:precise └─74fe38d11401 Virtual Size: 209.6 MB Tags: ubuntu:12.04, ubuntu:precise
|─ef519c9ee91a Virtual Size: 100.9 MB ─ef519c9ee91a Virtual Size: 100.9 MB
| └─07302703becc Virtual Size: 101.2 MB └─07302703becc Virtual Size: 101.2 MB
| └─cf8dc907452c Virtual Size: 101.2 MB └─cf8dc907452c Virtual Size: 101.2 MB
| └─a7cf8ae4e998 Virtual Size: 171.3 MB Tags: ubuntu:12.10, ubuntu:quantal └─a7cf8ae4e998 Virtual Size: 171.3 MB Tags: ubuntu:12.10, ubuntu:quantal
| |─e18d8001204e Virtual Size: 171.3 MB │ │─e18d8001204e Virtual Size: 171.3 MB
| | └─d0525208a46c Virtual Size: 171.3 MB │ │ └─d0525208a46c Virtual Size: 171.3 MB
| | └─59dac4bae93b Virtual Size: 242.5 MB │ │ └─59dac4bae93b Virtual Size: 242.5 MB
| | └─89541b3b35f2 Virtual Size: 511.8 MB │ │ └─89541b3b35f2 Virtual Size: 511.8 MB
| | └─7dac4e98548e Virtual Size: 511.8 MB │ │ └─7dac4e98548e Virtual Size: 511.8 MB
| | └─341d0cc3fac8 Virtual Size: 511.8 MB │ │ └─341d0cc3fac8 Virtual Size: 511.8 MB
| | └─2f96171d2098 Virtual Size: 511.8 MB │ │ └─2f96171d2098 Virtual Size: 511.8 MB
| | └─67b8b7262a67 Virtual Size: 513.7 MB │ │ └─67b8b7262a67 Virtual Size: 513.7 MB
| | └─0fe9a2bc50fe Virtual Size: 513.7 MB │ │ └─0fe9a2bc50fe Virtual Size: 513.7 MB
| | └─8c32832f07ba Virtual Size: 513.7 MB │ │ └─8c32832f07ba Virtual Size: 513.7 MB
| | └─cc4e1358bc80 Virtual Size: 513.7 MB │ │ └─cc4e1358bc80 Virtual Size: 513.7 MB
| | └─5c0d04fba9df Virtual Size: 513.7 MB Tags: nate/mongodb:latest │ │ └─5c0d04fba9df Virtual Size: 513.7 MB Tags: nate/mongodb:latest
| └─398d592f2009 Virtual Size: 242.2 MB └─398d592f2009 Virtual Size: 242.2 MB
| └─0cd8e7f50270 Virtual Size: 243.6 MB └─0cd8e7f50270 Virtual Size: 243.6 MB
| └─594b6f8e6f92 Virtual Size: 243.6 MB └─594b6f8e6f92 Virtual Size: 243.6 MB
| └─f832a63e87a4 Virtual Size: 243.6 MB Tags: redis:latest └─f832a63e87a4 Virtual Size: 243.6 MB Tags: redis:latest
└─02dae1c13f51 Virtual Size: 98.3 MB └─02dae1c13f51 Virtual Size: 98.3 MB
└─e7206bfc66aa Virtual Size: 98.5 MB └─e7206bfc66aa Virtual Size: 98.5 MB
└─cb12405ee8fa Virtual Size: 98.5 MB └─cb12405ee8fa Virtual Size: 98.5 MB
└─316b678ddf48 Virtual Size: 169.4 MB Tags: ubuntu:13.04, ubuntu:raring └─316b678ddf48 Virtual Size: 169.4 MB Tags: ubuntu:13.04, ubuntu:raring
``` ```
```
$ dockviz images -t -l
└─511136ea3c5a Virtual Size: 0.0 B
├─f10ebce2c0e1 Virtual Size: 103.7 MB
│ └─74fe38d11401 Virtual Size: 209.6 MB Tags: ubuntu:12.04, ubuntu:precise
├─ef519c9ee91a Virtual Size: 100.9 MB
│ └─a7cf8ae4e998 Virtual Size: 171.3 MB Tags: ubuntu:12.10, ubuntu:quantal
│ ├─5c0d04fba9df Virtual Size: 513.7 MB Tags: nate/mongodb:latest
│ └─f832a63e87a4 Virtual Size: 243.6 MB Tags: redis:latest
└─02dae1c13f51 Virtual Size: 98.3 MB
└─316b678ddf48 Virtual Size: 169.4 MB Tags: ubuntu:13.04, ubuntu:raring
```
# Running # Running

193
images.go
View file

@ -21,16 +21,17 @@ type Image struct {
} }
type ImagesCommand struct { type ImagesCommand struct {
Dot bool `short:"d" long:"dot" description:"Show image information as Graphviz dot."` Dot bool `short:"d" long:"dot" description:"Show image information as Graphviz dot. You can add a start image id or name -d/--dot [id/name]"`
Tree bool `short:"t" long:"tree" description:"Show image information as tree."` Tree bool `short:"t" long:"tree" description:"Show image information as tree. You can add a start image id or name -t/--tree [id/name]"`
Short bool `short:"s" long:"short" description:"Show short summary of images (repo name and list of tags)."` Short bool `short:"s" long:"short" description:"Show short summary of images (repo name and list of tags)."`
NoTruncate bool `short:"n" long:"no-trunc" description:"Don't truncate the image IDs."` NoTruncate bool `short:"n" long:"no-trunc" description:"Don't truncate the image IDs."`
OnlyLabelled bool `short:"l" long:"only-labelled" description:"Print only labelled images/containers."`
} }
var imagesCommand ImagesCommand var imagesCommand ImagesCommand
func (x *ImagesCommand) Execute(args []string) error {
func (x *ImagesCommand) Execute(args []string) error {
var images *[]Image var images *[]Image
stat, err := os.Stdin.Stat() stat, err := os.Stdin.Stat()
@ -82,50 +83,76 @@ func (x *ImagesCommand) Execute(args []string) error {
images = &ims images = &ims
} }
if imagesCommand.Dot { if imagesCommand.Tree || imagesCommand.Dot {
fmt.Printf(jsonToDot(images)) var startImage *Image
} else if imagesCommand.Tree {
var startImage = ""
if len(args) > 0 { if len(args) > 0 {
// attempt to find the start image, which can be specified as an // attempt to find the start image, which can be specified as an
// image ID or a repository name // image ID or a repository name
startImageArg := args[0] startImageArg := args[0]
startImageRepo := args[0] startImageRepo := args[0]
// in case a repo name was specified, append ":latest" if it isn't // if tag is not defined, find by :latest tag
// already there if strings.Index(startImageRepo, ":") == -1 {
if !strings.HasSuffix(startImageRepo, ":latest") {
startImageRepo = fmt.Sprintf("%s:latest", startImageRepo) startImageRepo = fmt.Sprintf("%s:latest", startImageRepo)
} }
IMAGES: IMAGES:
for _, image := range *images { for _, image := range *images {
// check if the start image arg matches an image id // find by image id
if strings.Index(image.Id, startImageArg) == 0 { if strings.Index(image.Id, startImageArg) == 0 {
startImage = image.Id startImage = &image
break IMAGES break IMAGES
} }
// check if the start image arg matches an repository name // find by image name (name and tag)
if image.RepoTags[0] != "<none>:<none>" { for _, repotag := range image.RepoTags {
for _, repotag := range image.RepoTags { if repotag == startImageRepo {
if repotag == startImageRepo { startImage = &image
startImage = image.Id break IMAGES
break IMAGES
}
} }
} }
} }
if startImage == "" { if startImage == nil {
return fmt.Errorf("Unable to find image %s.", startImageArg) return fmt.Errorf("Unable to find image %s = %s.", startImageArg, startImageRepo)
} }
} }
fmt.Printf(jsonToTree(images, startImage, imagesCommand.NoTruncate)) // select the start image of the tree
var roots []Image
if startImage == nil {
roots = collectRoots(images)
} else {
startImage.ParentId = ""
roots = []Image{*startImage}
}
// build helper map (image -> children)
var imagesByParent = make(map[string][]Image)
imagesByParent = collectChildren(images);
// image ids truncate
// initialize image informations
// filter images
if imagesCommand.OnlyLabelled{
*images, imagesByParent = filterImages(images, &imagesByParent)
}
var buffer bytes.Buffer
if imagesCommand.Tree {
jsonToText(&buffer, imagesCommand.NoTruncate, roots, imagesByParent, "")
}
if imagesCommand.Dot {
buffer.WriteString("digraph docker {\n")
imagesToDot(&buffer, roots, imagesByParent)
buffer.WriteString(" base [style=invisible]\n}\n")
buffer.String()
}
fmt.Print(buffer.String())
} else if imagesCommand.Short { } else if imagesCommand.Short {
fmt.Printf(jsonToShort(images)) fmt.Printf(jsonToShort(images))
} else { } else {
@ -135,72 +162,90 @@ func (x *ImagesCommand) Execute(args []string) error {
return nil return nil
} }
func jsonToTree(images *[]Image, startImageArg string, noTrunc bool) string {
var buffer bytes.Buffer
var startImage Image func collectChildren(images *[]Image) map[string][]Image {
var imagesByParent = make(map[string][]Image)
for _, image := range *images {
if children, exists := imagesByParent[image.ParentId]; exists {
imagesByParent[image.ParentId] = append(children, image)
} else {
imagesByParent[image.ParentId] = []Image{image}
}
}
return imagesByParent
}
func collectRoots(images *[]Image) []Image {
var roots []Image var roots []Image
var byParent = make(map[string][]Image)
for _, image := range *images { for _, image := range *images {
if image.ParentId == "" { if image.ParentId == "" {
roots = append(roots, image) roots = append(roots, image)
} else {
if children, exists := byParent[image.ParentId]; exists {
byParent[image.ParentId] = append(children, image)
} else {
byParent[image.ParentId] = []Image{image}
}
}
if startImageArg != "" {
if startImageArg == image.Id || startImageArg == truncate(image.Id) {
startImage = image
}
for _, repotag := range image.RepoTags {
if repotag == startImageArg {
startImage = image
}
}
} }
} }
if startImageArg != "" { return roots
WalkTree(&buffer, noTrunc, []Image{startImage}, byParent, "")
} else {
WalkTree(&buffer, noTrunc, roots, byParent, "")
}
return buffer.String()
} }
func WalkTree(buffer *bytes.Buffer, noTrunc bool, images []Image, byParent map[string][]Image, prefix string) {
if len(images) > 1 { func filterImages (images *[]Image, byParent *map[string][]Image) (filteredImages []Image, filteredChildren map[string][]Image) {
length := len(images) for i := 0; i<len(*images); i++ {
// image is visible
// 1. it has a label
// 2. it is root
// 3. it is a node
var visible bool = (*images)[i].RepoTags[0] != "<none>:<none>" || (*images)[i].ParentId == "" || len((*byParent)[(*images)[i].Id]) > 1
if visible {
filteredImages = append(filteredImages, (*images)[i])
} else {
// change childs parent id
// if items are filtered with only one child
for j := 0; j<len(filteredImages); j++ {
if filteredImages[j].ParentId == (*images)[i].Id {
filteredImages[j].ParentId = (*images)[i].ParentId
}
}
for j := 0; j<len(*images); j++ {
if (*images)[j].ParentId == (*images)[i].Id {
(*images)[j].ParentId = (*images)[i].ParentId
}
}
}
}
filteredChildren = collectChildren(&filteredImages)
return filteredImages, filteredChildren
}
func jsonToText(buffer *bytes.Buffer, noTrunc bool, images []Image, byParent map[string][]Image, prefix string) {
var length = len(images)
if length > 1 {
for index, image := range images { for index, image := range images {
var nextPrefix string = ""
if index+1 == length { if index+1 == length {
PrintTreeNode(buffer, noTrunc, image, prefix+"└─") PrintTreeNode(buffer, noTrunc, image, prefix+"└─")
if subimages, exists := byParent[image.Id]; exists { nextPrefix = " "
WalkTree(buffer, noTrunc, subimages, byParent, prefix+" ")
}
} else { } else {
PrintTreeNode(buffer, noTrunc, image, prefix+"├─") PrintTreeNode(buffer, noTrunc, image, prefix+"├─")
if subimages, exists := byParent[image.Id]; exists { nextPrefix = "│ "
WalkTree(buffer, noTrunc, subimages, byParent, prefix+"│ ") }
} if subimages, exists := byParent[image.Id]; exists {
jsonToText(buffer, noTrunc, subimages, byParent, prefix+nextPrefix)
} }
} }
} else { } else {
for _, image := range images { for _, image := range images {
PrintTreeNode(buffer, noTrunc, image, prefix+"└─") PrintTreeNode(buffer, noTrunc, image, prefix+"└─")
if subimages, exists := byParent[image.Id]; exists { if subimages, exists := byParent[image.Id]; exists {
WalkTree(buffer, noTrunc, subimages, byParent, prefix+" ") jsonToText(buffer, noTrunc, subimages, byParent, prefix+" ")
} }
} }
} }
} }
func PrintTreeNode(buffer *bytes.Buffer, noTrunc bool, image Image, prefix string) { func PrintTreeNode(buffer *bytes.Buffer, noTrunc bool, image Image, prefix string) {
var imageID string var imageID string
if noTrunc { if noTrunc {
@ -235,10 +280,12 @@ func humanSize(raw int64) string {
return fmt.Sprintf("%.01f %s", rawFloat, sizes[ind]) return fmt.Sprintf("%.01f %s", rawFloat, sizes[ind])
} }
func truncate(id string) string { func truncate(id string) string {
return id[0:12] return id[0:12]
} }
func parseImagesJSON(rawJSON []byte) (*[]Image, error) { func parseImagesJSON(rawJSON []byte) (*[]Image, error) {
var images []Image var images []Image
@ -251,12 +298,9 @@ func parseImagesJSON(rawJSON []byte) (*[]Image, error) {
return &images, nil return &images, nil
} }
func jsonToDot(images *[]Image) string {
var buffer bytes.Buffer func imagesToDot(buffer *bytes.Buffer, images []Image, byParent map[string][]Image) {
buffer.WriteString("digraph docker {\n") for _, image := range images {
for _, image := range *images {
if image.ParentId == "" { if image.ParentId == "" {
buffer.WriteString(fmt.Sprintf(" base -> \"%s\" [style=invis]\n", truncate(image.Id))) buffer.WriteString(fmt.Sprintf(" base -> \"%s\" [style=invis]\n", truncate(image.Id)))
} else { } else {
@ -265,13 +309,13 @@ func jsonToDot(images *[]Image) string {
if image.RepoTags[0] != "<none>:<none>" { if image.RepoTags[0] != "<none>:<none>" {
buffer.WriteString(fmt.Sprintf(" \"%s\" [label=\"%s\\n%s\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n", truncate(image.Id), truncate(image.Id), strings.Join(image.RepoTags, "\\n"))) buffer.WriteString(fmt.Sprintf(" \"%s\" [label=\"%s\\n%s\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n", truncate(image.Id), truncate(image.Id), strings.Join(image.RepoTags, "\\n")))
} }
if subimages, exists := byParent[image.Id]; exists {
imagesToDot(buffer, subimages, byParent)
}
} }
buffer.WriteString(" base [style=invisible]\n}\n")
return buffer.String()
} }
func jsonToShort(images *[]Image) string { func jsonToShort(images *[]Image) string {
var buffer bytes.Buffer var buffer bytes.Buffer
@ -303,6 +347,7 @@ func jsonToShort(images *[]Image) string {
return buffer.String() return buffer.String()
} }
func init() { func init() {
parser.AddCommand("images", parser.AddCommand("images",
"Visualize docker images.", "Visualize docker images.",

BIN
images_only_labeled.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 157 KiB

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB