List containers ordered by creation time

This commit is contained in:
Solomon Hykes 2013-01-29 03:24:31 -08:00
parent 4f3362b85a
commit c7a944caf2
4 changed files with 34 additions and 23 deletions

View File

@ -117,6 +117,10 @@ func (container *Container) Cmd() *exec.Cmd {
return container.cmd return container.cmd
} }
func (container *Container) When() time.Time {
return container.Created
}
func (container *Container) loadUserData() (map[string]string, error) { func (container *Container) loadUserData() (map[string]string, error) {
jsonData, err := ioutil.ReadFile(path.Join(container.Root, "userdata.json")) jsonData, err := ioutil.ReadFile(path.Join(container.Root, "userdata.json"))
if err != nil { if err != nil {

View File

@ -7,6 +7,7 @@ import (
"log" "log"
"os" "os"
"path" "path"
"github.com/dotcloud/docker/image" // For sorting utility
) )
type Docker struct { type Docker struct {
@ -16,9 +17,13 @@ type Docker struct {
} }
func (docker *Docker) List() []*Container { func (docker *Docker) List() []*Container {
containers := []*Container{} history := new(image.History)
for e := docker.containers.Front(); e != nil; e = e.Next() { for e := docker.containers.Front(); e != nil; e = e.Next() {
containers = append(containers, e.Value.(*Container)) history.Add(e.Value.(*Container))
}
containers := make([]*Container, len(*history))
for i := range *history {
containers[i] = (*history)[i].(*Container)
} }
return containers return containers
} }

View File

@ -288,7 +288,8 @@ func (srv *Server) CmdImages(stdin io.ReadCloser, stdout io.Writer, args ...stri
if nameFilter != "" && nameFilter != name { if nameFilter != "" && nameFilter != name {
continue continue
} }
for idx, img := range *srv.images.ByName[name] { for idx, evt := range *srv.images.ByName[name] {
img := evt.(*image.Image)
if *limit > 0 && idx >= *limit { if *limit > 0 && idx >= *limit {
break break
} }

View File

@ -102,7 +102,7 @@ func (index *Index) Find(idOrName string) *Image {
} }
// Lookup by name // Lookup by name
if history, exists := index.ByName[idOrName]; exists && history.Len() > 0 { if history, exists := index.ByName[idOrName]; exists && history.Len() > 0 {
return (*history)[0] return (*history)[0].(*Image)
} }
return nil return nil
} }
@ -116,7 +116,7 @@ func (index *Index) Add(name string, image *Image) error {
index.ByName[name] = new(History) index.ByName[name] = new(History)
} else { } else {
// If this image is already the latest version, don't add it. // If this image is already the latest version, don't add it.
if (*index.ByName[name])[0].Id == image.Id { if (*index.ByName[name])[0].(*Image).Id == image.Id {
return nil return nil
} }
} }
@ -169,7 +169,8 @@ func (index *Index) Rename(oldName, newName string) error {
index.ByName[newName] = index.ByName[oldName] index.ByName[newName] = index.ByName[oldName]
delete(index.ByName, oldName) delete(index.ByName, oldName)
// Change the ID of all images, since they include the name // Change the ID of all images, since they include the name
for _, image := range *index.ByName[newName] { for _, event := range *index.ByName[newName] {
image := event.(*Image)
if id, err := generateImageId(newName, image.Layers); err != nil { if id, err := generateImageId(newName, image.Layers); err != nil {
return err return err
} else { } else {
@ -227,37 +228,33 @@ func (index *Index) save() error {
// History wraps an array of images so they can be sorted by date (most recent first) // History wraps an array of images so they can be sorted by date (most recent first)
type History []*Image type Event interface {
When() time.Time
}
type History []Event
func (history *History) Len() int { func (history *History) Len() int {
return len(*history) return len(*history)
} }
func (history *History) Less(i, j int) bool { func (history *History) Less(i, j int) bool {
images := *history events := *history
return images[j].Created.Before(images[i].Created) return events[j].When().Before(events[i].When())
} }
func (history *History) Swap(i, j int) { func (history *History) Swap(i, j int) {
images := *history events := *history
tmp := images[i] tmp := events[i]
images[i] = images[j] events[i] = events[j]
images[j] = tmp events[j] = tmp
} }
func (history *History) Add(image *Image) { func (history *History) Add(event Event) {
*history = append(*history, image) *history = append(*history, event)
sort.Sort(history) sort.Sort(history)
} }
func (history *History) Del(id string) {
for idx, image := range *history {
if image.Id == id {
*history = append((*history)[:idx], (*history)[idx + 1:]...)
}
}
}
type Image struct { type Image struct {
Id string // Globally unique identifier Id string // Globally unique identifier
Layers []string // Absolute paths Layers []string // Absolute paths
@ -265,6 +262,10 @@ type Image struct {
Parent string Parent string
} }
func (image *Image) When() time.Time {
return image.Created
}
func (image *Image) IdParts() (string, string) { func (image *Image) IdParts() (string, string) {
if len(image.Id) < 8 { if len(image.Id) < 8 {
return "", image.Id return "", image.Id