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
}
func (container *Container) When() time.Time {
return container.Created
}
func (container *Container) loadUserData() (map[string]string, error) {
jsonData, err := ioutil.ReadFile(path.Join(container.Root, "userdata.json"))
if err != nil {

View file

@ -7,6 +7,7 @@ import (
"log"
"os"
"path"
"github.com/dotcloud/docker/image" // For sorting utility
)
type Docker struct {
@ -16,9 +17,13 @@ type Docker struct {
}
func (docker *Docker) List() []*Container {
containers := []*Container{}
history := new(image.History)
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
}

View file

@ -288,7 +288,8 @@ func (srv *Server) CmdImages(stdin io.ReadCloser, stdout io.Writer, args ...stri
if nameFilter != "" && nameFilter != name {
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 {
break
}

View file

@ -102,7 +102,7 @@ func (index *Index) Find(idOrName string) *Image {
}
// Lookup by name
if history, exists := index.ByName[idOrName]; exists && history.Len() > 0 {
return (*history)[0]
return (*history)[0].(*Image)
}
return nil
}
@ -116,7 +116,7 @@ func (index *Index) Add(name string, image *Image) error {
index.ByName[name] = new(History)
} else {
// 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
}
}
@ -169,7 +169,8 @@ func (index *Index) Rename(oldName, newName string) error {
index.ByName[newName] = index.ByName[oldName]
delete(index.ByName, oldName)
// 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 {
return err
} 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)
type History []*Image
type Event interface {
When() time.Time
}
type History []Event
func (history *History) Len() int {
return len(*history)
}
func (history *History) Less(i, j int) bool {
images := *history
return images[j].Created.Before(images[i].Created)
events := *history
return events[j].When().Before(events[i].When())
}
func (history *History) Swap(i, j int) {
images := *history
tmp := images[i]
images[i] = images[j]
images[j] = tmp
events := *history
tmp := events[i]
events[i] = events[j]
events[j] = tmp
}
func (history *History) Add(image *Image) {
*history = append(*history, image)
func (history *History) Add(event Event) {
*history = append(*history, event)
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 {
Id string // Globally unique identifier
Layers []string // Absolute paths
@ -265,6 +262,10 @@ type Image struct {
Parent string
}
func (image *Image) When() time.Time {
return image.Created
}
func (image *Image) IdParts() (string, string) {
if len(image.Id) < 8 {
return "", image.Id