1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Remove Job from PS API

Signed-off-by: Doug Davis <dug@us.ibm.com>
This commit is contained in:
Doug Davis 2015-04-07 12:34:30 -07:00
parent e011119a49
commit 1bfa80bdd9
6 changed files with 72 additions and 166 deletions

View file

@ -153,7 +153,7 @@ func (cli *DockerCli) CmdPs(args ...string) error {
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\t%s\t", ID, image, command, fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\t%s\t", ID, image, command,
units.HumanDuration(time.Now().UTC().Sub(time.Unix(int64(container.Created), 0))), units.HumanDuration(time.Now().UTC().Sub(time.Unix(int64(container.Created), 0))),
container.Status, api.NewDisplayablePorts(container.Ports), strings.Join(names, ",")) container.Status, api.DisplayablePorts(container.Ports), strings.Join(names, ","))
if *size { if *size {
if container.SizeRootFs > 0 { if container.SizeRootFs > 0 {

View file

@ -10,7 +10,6 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/engine"
"github.com/docker/docker/pkg/parsers" "github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/pkg/version" "github.com/docker/docker/pkg/version"
"github.com/docker/libtrust" "github.com/docker/libtrust"
@ -32,65 +31,13 @@ func ValidateHost(val string) (string, error) {
return host, nil return host, nil
} }
// TODO remove, used on < 1.5 in getContainersJSON
// TODO this can go away when we get rid of engine.table
func DisplayablePorts(ports *engine.Table) string {
var (
result = []string{}
hostMappings = []string{}
firstInGroupMap map[string]int
lastInGroupMap map[string]int
)
firstInGroupMap = make(map[string]int)
lastInGroupMap = make(map[string]int)
ports.SetKey("PrivatePort")
ports.Sort()
for _, port := range ports.Data {
var (
current = port.GetInt("PrivatePort")
portKey = port.Get("Type")
firstInGroup int
lastInGroup int
)
if port.Get("IP") != "" {
if port.GetInt("PublicPort") != current {
hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.Get("IP"), port.GetInt("PublicPort"), port.GetInt("PrivatePort"), port.Get("Type")))
continue
}
portKey = fmt.Sprintf("%s/%s", port.Get("IP"), port.Get("Type"))
}
firstInGroup = firstInGroupMap[portKey]
lastInGroup = lastInGroupMap[portKey]
if firstInGroup == 0 {
firstInGroupMap[portKey] = current
lastInGroupMap[portKey] = current
continue
}
if current == (lastInGroup + 1) {
lastInGroupMap[portKey] = current
continue
}
result = append(result, FormGroup(portKey, firstInGroup, lastInGroup))
firstInGroupMap[portKey] = current
lastInGroupMap[portKey] = current
}
for portKey, firstInGroup := range firstInGroupMap {
result = append(result, FormGroup(portKey, firstInGroup, lastInGroupMap[portKey]))
}
result = append(result, hostMappings...)
return strings.Join(result, ", ")
}
type ByPrivatePort []types.Port type ByPrivatePort []types.Port
func (r ByPrivatePort) Len() int { return len(r) } func (r ByPrivatePort) Len() int { return len(r) }
func (r ByPrivatePort) Swap(i, j int) { r[i], r[j] = r[j], r[i] } func (r ByPrivatePort) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r ByPrivatePort) Less(i, j int) bool { return r[i].PrivatePort < r[j].PrivatePort } func (r ByPrivatePort) Less(i, j int) bool { return r[i].PrivatePort < r[j].PrivatePort }
// TODO Rename to DisplayablePorts (remove "New") when engine.Table goes away func DisplayablePorts(ports []types.Port) string {
func NewDisplayablePorts(ports []types.Port) string {
var ( var (
result = []string{} result = []string{}
hostMappings = []string{} hostMappings = []string{}

View file

@ -374,42 +374,32 @@ func getContainersTop(eng *engine.Engine, version version.Version, w http.Respon
} }
func getContainersJSON(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { func getContainersJSON(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := parseForm(r); err != nil { var err error
if err = parseForm(r); err != nil {
return err return err
} }
var (
err error
outs *engine.Table
job = eng.Job("containers")
)
job.Setenv("all", r.Form.Get("all")) config := &daemon.ContainersConfig{
job.Setenv("size", r.Form.Get("size")) All: r.Form.Get("all") == "1",
job.Setenv("since", r.Form.Get("since")) Size: r.Form.Get("size") == "1",
job.Setenv("before", r.Form.Get("before")) Since: r.Form.Get("since"),
job.Setenv("limit", r.Form.Get("limit")) Before: r.Form.Get("before"),
job.Setenv("filters", r.Form.Get("filters")) Filters: r.Form.Get("filters"),
}
if version.GreaterThanOrEqualTo("1.5") { if tmpLimit := r.Form.Get("limit"); tmpLimit != "" {
streamJSON(job, w, false) config.Limit, err = strconv.Atoi(tmpLimit)
} else if outs, err = job.Stdout.AddTable(); err != nil { if err != nil {
return err
}
if err = job.Run(); err != nil {
return err
}
if version.LessThan("1.5") { // Convert to legacy format
for _, out := range outs.Data {
ports := engine.NewTable("", 0)
ports.ReadListFrom([]byte(out.Get("Ports")))
out.Set("Ports", api.DisplayablePorts(ports))
}
w.Header().Set("Content-Type", "application/json")
if _, err = outs.WriteListTo(w); err != nil {
return err return err
} }
} }
return nil
containers, err := getDaemon(eng).Containers(config)
if err != nil {
return err
}
return writeJSON(w, http.StatusOK, containers)
} }
func getContainersStats(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { func getContainersStats(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

View file

@ -122,7 +122,6 @@ func (daemon *Daemon) Install(eng *engine.Engine) error {
"container_rename": daemon.ContainerRename, "container_rename": daemon.ContainerRename,
"container_inspect": daemon.ContainerInspect, "container_inspect": daemon.ContainerInspect,
"container_stats": daemon.ContainerStats, "container_stats": daemon.ContainerStats,
"containers": daemon.Containers,
"create": daemon.ContainerCreate, "create": daemon.ContainerCreate,
"rm": daemon.ContainerRm, "rm": daemon.ContainerRm,
"export": daemon.ContainerExport, "export": daemon.ContainerExport,

View file

@ -1,15 +1,12 @@
package daemon package daemon
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"sort"
"strconv" "strconv"
"strings" "strings"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/engine"
"github.com/docker/docker/graph" "github.com/docker/docker/graph"
"github.com/docker/docker/nat" "github.com/docker/docker/nat"
"github.com/docker/docker/pkg/graphdb" "github.com/docker/docker/pkg/graphdb"
@ -23,35 +20,35 @@ func (daemon *Daemon) List() []*Container {
return daemon.containers.List() return daemon.containers.List()
} }
type ByCreated []types.Container type ContainersConfig struct {
All bool
Since string
Before string
Limit int
Size bool
Filters string
}
func (r ByCreated) Len() int { return len(r) } func (daemon *Daemon) Containers(config *ContainersConfig) ([]*types.Container, error) {
func (r ByCreated) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r ByCreated) Less(i, j int) bool { return r[i].Created < r[j].Created }
func (daemon *Daemon) Containers(job *engine.Job) error {
var ( var (
foundBefore bool foundBefore bool
displayed int displayed int
all = job.GetenvBool("all") all = config.All
since = job.Getenv("since") n = config.Limit
before = job.Getenv("before")
n = job.GetenvInt("limit")
size = job.GetenvBool("size")
psFilters filters.Args psFilters filters.Args
filtExited []int filtExited []int
) )
containers := []types.Container{} containers := []*types.Container{}
psFilters, err := filters.FromParam(job.Getenv("filters")) psFilters, err := filters.FromParam(config.Filters)
if err != nil { if err != nil {
return err return nil, err
} }
if i, ok := psFilters["exited"]; ok { if i, ok := psFilters["exited"]; ok {
for _, value := range i { for _, value := range i {
code, err := strconv.Atoi(value) code, err := strconv.Atoi(value)
if err != nil { if err != nil {
return err return nil, err
} }
filtExited = append(filtExited, code) filtExited = append(filtExited, code)
} }
@ -71,17 +68,17 @@ func (daemon *Daemon) Containers(job *engine.Job) error {
}, 1) }, 1)
var beforeCont, sinceCont *Container var beforeCont, sinceCont *Container
if before != "" { if config.Before != "" {
beforeCont, err = daemon.Get(before) beforeCont, err = daemon.Get(config.Before)
if err != nil { if err != nil {
return err return nil, err
} }
} }
if since != "" { if config.Since != "" {
sinceCont, err = daemon.Get(since) sinceCont, err = daemon.Get(config.Since)
if err != nil { if err != nil {
return err return nil, err
} }
} }
@ -89,7 +86,7 @@ func (daemon *Daemon) Containers(job *engine.Job) error {
writeCont := func(container *Container) error { writeCont := func(container *Container) error {
container.Lock() container.Lock()
defer container.Unlock() defer container.Unlock()
if !container.Running && !all && n <= 0 && since == "" && before == "" { if !container.Running && !all && n <= 0 && config.Since == "" && config.Before == "" {
return nil return nil
} }
if !psFilters.Match("name", container.Name) { if !psFilters.Match("name", container.Name) {
@ -104,7 +101,7 @@ func (daemon *Daemon) Containers(job *engine.Job) error {
return nil return nil
} }
if before != "" && !foundBefore { if config.Before != "" && !foundBefore {
if container.ID == beforeCont.ID { if container.ID == beforeCont.ID {
foundBefore = true foundBefore = true
} }
@ -113,7 +110,7 @@ func (daemon *Daemon) Containers(job *engine.Job) error {
if n > 0 && displayed == n { if n > 0 && displayed == n {
return errLast return errLast
} }
if since != "" { if config.Since != "" {
if container.ID == sinceCont.ID { if container.ID == sinceCont.ID {
return errLast return errLast
} }
@ -135,7 +132,7 @@ func (daemon *Daemon) Containers(job *engine.Job) error {
return nil return nil
} }
displayed++ displayed++
newC := types.Container{ newC := &types.Container{
ID: container.ID, ID: container.ID,
Names: names[container.ID], Names: names[container.ID],
} }
@ -184,7 +181,7 @@ func (daemon *Daemon) Containers(job *engine.Job) error {
} }
} }
if size { if config.Size {
sizeRw, sizeRootFs := container.GetSize() sizeRw, sizeRootFs := container.GetSize()
newC.SizeRw = int(sizeRw) newC.SizeRw = int(sizeRw)
newC.SizeRootFs = int(sizeRootFs) newC.SizeRootFs = int(sizeRootFs)
@ -197,14 +194,10 @@ func (daemon *Daemon) Containers(job *engine.Job) error {
for _, container := range daemon.List() { for _, container := range daemon.List() {
if err := writeCont(container); err != nil { if err := writeCont(container); err != nil {
if err != errLast { if err != errLast {
return err return nil, err
} }
break break
} }
} }
sort.Sort(sort.Reverse(ByCreated(containers))) return containers, nil
if err = json.NewEncoder(job.Stdout).Encode(containers); err != nil {
return err
}
return nil
} }

View file

@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/docker/docker/builder" "github.com/docker/docker/builder"
"github.com/docker/docker/daemon"
"github.com/docker/docker/engine" "github.com/docker/docker/engine"
) )
@ -114,21 +115,17 @@ func TestRestartKillWait(t *testing.T) {
id := createTestContainer(eng, config, t) id := createTestContainer(eng, config, t)
job := eng.Job("containers") containers, err := runtime.Containers(&daemon.ContainersConfig{All: true})
job.SetenvBool("all", true)
outs, err := job.Stdout.AddListTable()
if err != nil { if err != nil {
t.Fatal(err) t.Errorf("Error getting containers1: %q", err)
}
if err := job.Run(); err != nil {
t.Fatal(err)
} }
if len(outs.Data) != 1 { if len(containers) != 1 {
t.Errorf("Expected 1 container, %v found", len(outs.Data)) t.Errorf("Expected 1 container, %v found", len(containers))
} }
job = eng.Job("start", id) job := eng.Job("start", id)
if err := job.ImportEnv(hostConfig); err != nil { if err := job.ImportEnv(hostConfig); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -141,23 +138,19 @@ func TestRestartKillWait(t *testing.T) {
} }
eng = newTestEngine(t, false, runtime.Config().Root) eng = newTestEngine(t, false, runtime.Config().Root)
runtime = mkDaemonFromEngine(eng, t)
containers, err = runtime.Containers(&daemon.ContainersConfig{All: true})
job = eng.Job("containers")
job.SetenvBool("all", true)
outs, err = job.Stdout.AddListTable()
if err != nil { if err != nil {
t.Fatal(err) t.Errorf("Error getting containers1: %q", err)
} }
if err := job.Run(); err != nil { if len(containers) != 1 {
t.Fatal(err) t.Errorf("Expected 1 container, %v found", len(containers))
}
if len(outs.Data) != 1 {
t.Errorf("Expected 1 container, %v found", len(outs.Data))
} }
setTimeout(t, "Waiting on stopped container timedout", 5*time.Second, func() { setTimeout(t, "Waiting on stopped container timedout", 5*time.Second, func() {
job = eng.Job("wait", outs.Data[0].Get("Id")) job = eng.Job("wait", containers[0].ID)
if err := job.Run(); err != nil { if err := job.Run(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -166,7 +159,8 @@ func TestRestartKillWait(t *testing.T) {
func TestCreateStartRestartStopStartKillRm(t *testing.T) { func TestCreateStartRestartStopStartKillRm(t *testing.T) {
eng := NewTestEngine(t) eng := NewTestEngine(t)
defer mkDaemonFromEngine(eng, t).Nuke() runtime := mkDaemonFromEngine(eng, t)
defer runtime.Nuke()
config, hostConfig, _, err := parseRun([]string{"-i", unitTestImageID, "/bin/cat"}) config, hostConfig, _, err := parseRun([]string{"-i", unitTestImageID, "/bin/cat"})
if err != nil { if err != nil {
@ -174,22 +168,13 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) {
} }
id := createTestContainer(eng, config, t) id := createTestContainer(eng, config, t)
containers, err := runtime.Containers(&daemon.ContainersConfig{All: true})
job := eng.Job("containers") if len(containers) != 1 {
job.SetenvBool("all", true) t.Errorf("Expected 1 container, %v found", len(containers))
outs, err := job.Stdout.AddListTable()
if err != nil {
t.Fatal(err)
}
if err := job.Run(); err != nil {
t.Fatal(err)
} }
if len(outs.Data) != 1 { job := eng.Job("start", id)
t.Errorf("Expected 1 container, %v found", len(outs.Data))
}
job = eng.Job("start", id)
if err := job.ImportEnv(hostConfig); err != nil { if err := job.ImportEnv(hostConfig); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -228,18 +213,10 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
job = eng.Job("containers") containers, err = runtime.Containers(&daemon.ContainersConfig{All: true})
job.SetenvBool("all", true)
outs, err = job.Stdout.AddListTable()
if err != nil {
t.Fatal(err)
}
if err := job.Run(); err != nil {
t.Fatal(err)
}
if len(outs.Data) != 0 { if len(containers) != 0 {
t.Errorf("Expected 0 container, %v found", len(outs.Data)) t.Errorf("Expected 0 container, %v found", len(containers))
} }
} }