From 1bfa80bdd9ac05b01867492d2e6dda668aa7715c Mon Sep 17 00:00:00 2001 From: Doug Davis Date: Tue, 7 Apr 2015 12:34:30 -0700 Subject: [PATCH] Remove Job from PS API Signed-off-by: Doug Davis --- api/client/ps.go | 2 +- api/common.go | 55 +----------------------------- api/server/server.go | 48 +++++++++++--------------- daemon/daemon.go | 1 - daemon/list.go | 63 ++++++++++++++++------------------ integration/server_test.go | 69 +++++++++++++------------------------- 6 files changed, 72 insertions(+), 166 deletions(-) diff --git a/api/client/ps.go b/api/client/ps.go index fdc8ef5a9a..be20d7a6f6 100644 --- a/api/client/ps.go +++ b/api/client/ps.go @@ -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, 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 container.SizeRootFs > 0 { diff --git a/api/common.go b/api/common.go index 39224a9c10..693df38876 100644 --- a/api/common.go +++ b/api/common.go @@ -10,7 +10,6 @@ import ( "github.com/Sirupsen/logrus" "github.com/docker/docker/api/types" - "github.com/docker/docker/engine" "github.com/docker/docker/pkg/parsers" "github.com/docker/docker/pkg/version" "github.com/docker/libtrust" @@ -32,65 +31,13 @@ func ValidateHost(val string) (string, error) { 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 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) Less(i, j int) bool { return r[i].PrivatePort < r[j].PrivatePort } -// TODO Rename to DisplayablePorts (remove "New") when engine.Table goes away -func NewDisplayablePorts(ports []types.Port) string { +func DisplayablePorts(ports []types.Port) string { var ( result = []string{} hostMappings = []string{} diff --git a/api/server/server.go b/api/server/server.go index 502f1ae9e1..feabe700f8 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -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 { - if err := parseForm(r); err != nil { + var err error + if err = parseForm(r); err != nil { return err } - var ( - err error - outs *engine.Table - job = eng.Job("containers") - ) - job.Setenv("all", r.Form.Get("all")) - job.Setenv("size", r.Form.Get("size")) - job.Setenv("since", r.Form.Get("since")) - job.Setenv("before", r.Form.Get("before")) - job.Setenv("limit", r.Form.Get("limit")) - job.Setenv("filters", r.Form.Get("filters")) + config := &daemon.ContainersConfig{ + All: r.Form.Get("all") == "1", + Size: r.Form.Get("size") == "1", + Since: r.Form.Get("since"), + Before: r.Form.Get("before"), + Filters: r.Form.Get("filters"), + } - if version.GreaterThanOrEqualTo("1.5") { - streamJSON(job, w, false) - } else if outs, err = job.Stdout.AddTable(); 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 { + if tmpLimit := r.Form.Get("limit"); tmpLimit != "" { + config.Limit, err = strconv.Atoi(tmpLimit) + if err != nil { 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 { diff --git a/daemon/daemon.go b/daemon/daemon.go index a072fc34a4..a01ba4a17f 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -122,7 +122,6 @@ func (daemon *Daemon) Install(eng *engine.Engine) error { "container_rename": daemon.ContainerRename, "container_inspect": daemon.ContainerInspect, "container_stats": daemon.ContainerStats, - "containers": daemon.Containers, "create": daemon.ContainerCreate, "rm": daemon.ContainerRm, "export": daemon.ContainerExport, diff --git a/daemon/list.go b/daemon/list.go index d18e4407bc..511b49605d 100644 --- a/daemon/list.go +++ b/daemon/list.go @@ -1,15 +1,12 @@ package daemon import ( - "encoding/json" "errors" "fmt" - "sort" "strconv" "strings" "github.com/docker/docker/api/types" - "github.com/docker/docker/engine" "github.com/docker/docker/graph" "github.com/docker/docker/nat" "github.com/docker/docker/pkg/graphdb" @@ -23,35 +20,35 @@ func (daemon *Daemon) List() []*Container { 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 (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 { +func (daemon *Daemon) Containers(config *ContainersConfig) ([]*types.Container, error) { var ( foundBefore bool displayed int - all = job.GetenvBool("all") - since = job.Getenv("since") - before = job.Getenv("before") - n = job.GetenvInt("limit") - size = job.GetenvBool("size") + all = config.All + n = config.Limit psFilters filters.Args filtExited []int ) - containers := []types.Container{} + containers := []*types.Container{} - psFilters, err := filters.FromParam(job.Getenv("filters")) + psFilters, err := filters.FromParam(config.Filters) if err != nil { - return err + return nil, err } if i, ok := psFilters["exited"]; ok { for _, value := range i { code, err := strconv.Atoi(value) if err != nil { - return err + return nil, err } filtExited = append(filtExited, code) } @@ -71,17 +68,17 @@ func (daemon *Daemon) Containers(job *engine.Job) error { }, 1) var beforeCont, sinceCont *Container - if before != "" { - beforeCont, err = daemon.Get(before) + if config.Before != "" { + beforeCont, err = daemon.Get(config.Before) if err != nil { - return err + return nil, err } } - if since != "" { - sinceCont, err = daemon.Get(since) + if config.Since != "" { + sinceCont, err = daemon.Get(config.Since) 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 { container.Lock() defer container.Unlock() - if !container.Running && !all && n <= 0 && since == "" && before == "" { + if !container.Running && !all && n <= 0 && config.Since == "" && config.Before == "" { return nil } if !psFilters.Match("name", container.Name) { @@ -104,7 +101,7 @@ func (daemon *Daemon) Containers(job *engine.Job) error { return nil } - if before != "" && !foundBefore { + if config.Before != "" && !foundBefore { if container.ID == beforeCont.ID { foundBefore = true } @@ -113,7 +110,7 @@ func (daemon *Daemon) Containers(job *engine.Job) error { if n > 0 && displayed == n { return errLast } - if since != "" { + if config.Since != "" { if container.ID == sinceCont.ID { return errLast } @@ -135,7 +132,7 @@ func (daemon *Daemon) Containers(job *engine.Job) error { return nil } displayed++ - newC := types.Container{ + newC := &types.Container{ ID: 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() newC.SizeRw = int(sizeRw) newC.SizeRootFs = int(sizeRootFs) @@ -197,14 +194,10 @@ func (daemon *Daemon) Containers(job *engine.Job) error { for _, container := range daemon.List() { if err := writeCont(container); err != nil { if err != errLast { - return err + return nil, err } break } } - sort.Sort(sort.Reverse(ByCreated(containers))) - if err = json.NewEncoder(job.Stdout).Encode(containers); err != nil { - return err - } - return nil + return containers, nil } diff --git a/integration/server_test.go b/integration/server_test.go index 6d12ad35ad..acbec8c2c4 100644 --- a/integration/server_test.go +++ b/integration/server_test.go @@ -6,6 +6,7 @@ import ( "time" "github.com/docker/docker/builder" + "github.com/docker/docker/daemon" "github.com/docker/docker/engine" ) @@ -114,21 +115,17 @@ func TestRestartKillWait(t *testing.T) { id := createTestContainer(eng, config, t) - job := eng.Job("containers") - job.SetenvBool("all", true) - outs, err := job.Stdout.AddListTable() + containers, err := runtime.Containers(&daemon.ContainersConfig{All: true}) + if err != nil { - t.Fatal(err) - } - if err := job.Run(); err != nil { - t.Fatal(err) + t.Errorf("Error getting containers1: %q", err) } - if len(outs.Data) != 1 { - t.Errorf("Expected 1 container, %v found", len(outs.Data)) + if len(containers) != 1 { + 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 { t.Fatal(err) } @@ -141,23 +138,19 @@ func TestRestartKillWait(t *testing.T) { } 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 { - t.Fatal(err) + t.Errorf("Error getting containers1: %q", err) } - if err := job.Run(); err != nil { - t.Fatal(err) - } - - if len(outs.Data) != 1 { - t.Errorf("Expected 1 container, %v found", len(outs.Data)) + if len(containers) != 1 { + t.Errorf("Expected 1 container, %v found", len(containers)) } 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 { t.Fatal(err) } @@ -166,7 +159,8 @@ func TestRestartKillWait(t *testing.T) { func TestCreateStartRestartStopStartKillRm(t *testing.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"}) if err != nil { @@ -174,22 +168,13 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) { } id := createTestContainer(eng, config, 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 { - t.Fatal(err) - } - if err := job.Run(); err != nil { - t.Fatal(err) + if len(containers) != 1 { + t.Errorf("Expected 1 container, %v found", len(containers)) } - if len(outs.Data) != 1 { - t.Errorf("Expected 1 container, %v found", len(outs.Data)) - } - - job = eng.Job("start", id) + job := eng.Job("start", id) if err := job.ImportEnv(hostConfig); err != nil { t.Fatal(err) } @@ -228,18 +213,10 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) { t.Fatal(err) } - job = eng.Job("containers") - job.SetenvBool("all", true) - outs, err = job.Stdout.AddListTable() - if err != nil { - t.Fatal(err) - } - if err := job.Run(); err != nil { - t.Fatal(err) - } + containers, err = runtime.Containers(&daemon.ContainersConfig{All: true}) - if len(outs.Data) != 0 { - t.Errorf("Expected 0 container, %v found", len(outs.Data)) + if len(containers) != 0 { + t.Errorf("Expected 0 container, %v found", len(containers)) } }