Browse code

Move "containers" to daemon/list.go

This is part of an effort to break apart the deprecated server/ package

Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)

Solomon Hykes authored on 2014/08/01 06:26:25
Showing 4 changed files
... ...
@@ -161,14 +161,12 @@ func (daemon *Daemon) Install(eng *engine.Engine) error {
161 161
 	if err := eng.Register("top", daemon.ContainerTop); err != nil {
162 162
 		return err
163 163
 	}
164
+	if err := eng.Register("containers", daemon.Containers); err != nil {
165
+		return err
166
+	}
164 167
 	return nil
165 168
 }
166 169
 
167
-// List returns an array of all containers registered in the daemon.
168
-func (daemon *Daemon) List() []*Container {
169
-	return daemon.containers.List()
170
-}
171
-
172 170
 // Get looks for a container by the specified ID or name, and returns it.
173 171
 // If the container is not found, or if an error occurs, nil is returned.
174 172
 func (daemon *Daemon) Get(name string) *Container {
175 173
new file mode 100644
... ...
@@ -0,0 +1,121 @@
0
+package daemon
1
+
2
+import (
3
+	"errors"
4
+	"fmt"
5
+	"strings"
6
+
7
+	"github.com/docker/docker/pkg/graphdb"
8
+
9
+	"github.com/docker/docker/engine"
10
+)
11
+
12
+// List returns an array of all containers registered in the daemon.
13
+func (daemon *Daemon) List() []*Container {
14
+	return daemon.containers.List()
15
+}
16
+
17
+func (daemon *Daemon) Containers(job *engine.Job) engine.Status {
18
+	var (
19
+		foundBefore bool
20
+		displayed   int
21
+		all         = job.GetenvBool("all")
22
+		since       = job.Getenv("since")
23
+		before      = job.Getenv("before")
24
+		n           = job.GetenvInt("limit")
25
+		size        = job.GetenvBool("size")
26
+	)
27
+	outs := engine.NewTable("Created", 0)
28
+
29
+	names := map[string][]string{}
30
+	daemon.ContainerGraph().Walk("/", func(p string, e *graphdb.Entity) error {
31
+		names[e.ID()] = append(names[e.ID()], p)
32
+		return nil
33
+	}, -1)
34
+
35
+	var beforeCont, sinceCont *Container
36
+	if before != "" {
37
+		beforeCont = daemon.Get(before)
38
+		if beforeCont == nil {
39
+			return job.Error(fmt.Errorf("Could not find container with name or id %s", before))
40
+		}
41
+	}
42
+
43
+	if since != "" {
44
+		sinceCont = daemon.Get(since)
45
+		if sinceCont == nil {
46
+			return job.Error(fmt.Errorf("Could not find container with name or id %s", since))
47
+		}
48
+	}
49
+
50
+	errLast := errors.New("last container")
51
+	writeCont := func(container *Container) error {
52
+		container.Lock()
53
+		defer container.Unlock()
54
+		if !container.State.IsRunning() && !all && n <= 0 && since == "" && before == "" {
55
+			return nil
56
+		}
57
+		if before != "" && !foundBefore {
58
+			if container.ID == beforeCont.ID {
59
+				foundBefore = true
60
+			}
61
+			return nil
62
+		}
63
+		if n > 0 && displayed == n {
64
+			return errLast
65
+		}
66
+		if since != "" {
67
+			if container.ID == sinceCont.ID {
68
+				return errLast
69
+			}
70
+		}
71
+		displayed++
72
+		out := &engine.Env{}
73
+		out.Set("Id", container.ID)
74
+		out.SetList("Names", names[container.ID])
75
+		out.Set("Image", daemon.Repositories().ImageName(container.Image))
76
+		if len(container.Args) > 0 {
77
+			args := []string{}
78
+			for _, arg := range container.Args {
79
+				if strings.Contains(arg, " ") {
80
+					args = append(args, fmt.Sprintf("'%s'", arg))
81
+				} else {
82
+					args = append(args, arg)
83
+				}
84
+			}
85
+			argsAsString := strings.Join(args, " ")
86
+
87
+			out.Set("Command", fmt.Sprintf("\"%s %s\"", container.Path, argsAsString))
88
+		} else {
89
+			out.Set("Command", fmt.Sprintf("\"%s\"", container.Path))
90
+		}
91
+		out.SetInt64("Created", container.Created.Unix())
92
+		out.Set("Status", container.State.String())
93
+		str, err := container.NetworkSettings.PortMappingAPI().ToListString()
94
+		if err != nil {
95
+			return err
96
+		}
97
+		out.Set("Ports", str)
98
+		if size {
99
+			sizeRw, sizeRootFs := container.GetSize()
100
+			out.SetInt64("SizeRw", sizeRw)
101
+			out.SetInt64("SizeRootFs", sizeRootFs)
102
+		}
103
+		outs.Add(out)
104
+		return nil
105
+	}
106
+
107
+	for _, container := range daemon.List() {
108
+		if err := writeCont(container); err != nil {
109
+			if err != errLast {
110
+				return job.Error(err)
111
+			}
112
+			break
113
+		}
114
+	}
115
+	outs.ReverseSort()
116
+	if _, err := outs.WriteListTo(job.Stdout); err != nil {
117
+		return job.Error(err)
118
+	}
119
+	return engine.StatusOK
120
+}
0 121
deleted file mode 100644
... ...
@@ -1,120 +0,0 @@
1
-// DEPRECATION NOTICE. PLEASE DO NOT ADD ANYTHING TO THIS FILE.
2
-//
3
-// For additional commments see server/server.go
4
-//
5
-package server
6
-
7
-import (
8
-	"errors"
9
-	"fmt"
10
-	"strings"
11
-
12
-	"github.com/docker/docker/daemon"
13
-	"github.com/docker/docker/engine"
14
-	"github.com/docker/docker/pkg/graphdb"
15
-)
16
-
17
-func (srv *Server) Containers(job *engine.Job) engine.Status {
18
-	var (
19
-		foundBefore bool
20
-		displayed   int
21
-		all         = job.GetenvBool("all")
22
-		since       = job.Getenv("since")
23
-		before      = job.Getenv("before")
24
-		n           = job.GetenvInt("limit")
25
-		size        = job.GetenvBool("size")
26
-	)
27
-	outs := engine.NewTable("Created", 0)
28
-
29
-	names := map[string][]string{}
30
-	srv.daemon.ContainerGraph().Walk("/", func(p string, e *graphdb.Entity) error {
31
-		names[e.ID()] = append(names[e.ID()], p)
32
-		return nil
33
-	}, -1)
34
-
35
-	var beforeCont, sinceCont *daemon.Container
36
-	if before != "" {
37
-		beforeCont = srv.daemon.Get(before)
38
-		if beforeCont == nil {
39
-			return job.Error(fmt.Errorf("Could not find container with name or id %s", before))
40
-		}
41
-	}
42
-
43
-	if since != "" {
44
-		sinceCont = srv.daemon.Get(since)
45
-		if sinceCont == nil {
46
-			return job.Error(fmt.Errorf("Could not find container with name or id %s", since))
47
-		}
48
-	}
49
-
50
-	errLast := errors.New("last container")
51
-	writeCont := func(container *daemon.Container) error {
52
-		container.Lock()
53
-		defer container.Unlock()
54
-		if !container.State.IsRunning() && !all && n <= 0 && since == "" && before == "" {
55
-			return nil
56
-		}
57
-		if before != "" && !foundBefore {
58
-			if container.ID == beforeCont.ID {
59
-				foundBefore = true
60
-			}
61
-			return nil
62
-		}
63
-		if n > 0 && displayed == n {
64
-			return errLast
65
-		}
66
-		if since != "" {
67
-			if container.ID == sinceCont.ID {
68
-				return errLast
69
-			}
70
-		}
71
-		displayed++
72
-		out := &engine.Env{}
73
-		out.Set("Id", container.ID)
74
-		out.SetList("Names", names[container.ID])
75
-		out.Set("Image", srv.daemon.Repositories().ImageName(container.Image))
76
-		if len(container.Args) > 0 {
77
-			args := []string{}
78
-			for _, arg := range container.Args {
79
-				if strings.Contains(arg, " ") {
80
-					args = append(args, fmt.Sprintf("'%s'", arg))
81
-				} else {
82
-					args = append(args, arg)
83
-				}
84
-			}
85
-			argsAsString := strings.Join(args, " ")
86
-
87
-			out.Set("Command", fmt.Sprintf("\"%s %s\"", container.Path, argsAsString))
88
-		} else {
89
-			out.Set("Command", fmt.Sprintf("\"%s\"", container.Path))
90
-		}
91
-		out.SetInt64("Created", container.Created.Unix())
92
-		out.Set("Status", container.State.String())
93
-		str, err := container.NetworkSettings.PortMappingAPI().ToListString()
94
-		if err != nil {
95
-			return err
96
-		}
97
-		out.Set("Ports", str)
98
-		if size {
99
-			sizeRw, sizeRootFs := container.GetSize()
100
-			out.SetInt64("SizeRw", sizeRw)
101
-			out.SetInt64("SizeRootFs", sizeRootFs)
102
-		}
103
-		outs.Add(out)
104
-		return nil
105
-	}
106
-
107
-	for _, container := range srv.daemon.List() {
108
-		if err := writeCont(container); err != nil {
109
-			if err != errLast {
110
-				return job.Error(err)
111
-			}
112
-			break
113
-		}
114
-	}
115
-	outs.ReverseSort()
116
-	if _, err := outs.WriteListTo(job.Stdout); err != nil {
117
-		return job.Error(err)
118
-	}
119
-	return engine.StatusOK
120
-}
... ...
@@ -100,7 +100,6 @@ func InitServer(job *engine.Job) engine.Status {
100 100
 		"image_delete": srv.ImageDelete,
101 101
 		"events":       srv.Events,
102 102
 		"push":         srv.ImagePush,
103
-		"containers":   srv.Containers,
104 103
 	} {
105 104
 		if err := job.Eng.Register(name, srv.handlerWrap(handler)); err != nil {
106 105
 			return job.Error(err)