list.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package daemon
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "github.com/docker/docker/pkg/graphdb"
  8. "github.com/docker/docker/engine"
  9. "github.com/docker/docker/pkg/parsers/filters"
  10. )
  11. // List returns an array of all containers registered in the daemon.
  12. func (daemon *Daemon) List() []*Container {
  13. return daemon.containers.List()
  14. }
  15. func (daemon *Daemon) Containers(job *engine.Job) engine.Status {
  16. var (
  17. foundBefore bool
  18. displayed int
  19. all = job.GetenvBool("all")
  20. since = job.Getenv("since")
  21. before = job.Getenv("before")
  22. n = job.GetenvInt("limit")
  23. size = job.GetenvBool("size")
  24. psFilters filters.Args
  25. filt_exited []int
  26. )
  27. outs := engine.NewTable("Created", 0)
  28. psFilters, err := filters.FromParam(job.Getenv("filters"))
  29. if err != nil {
  30. return job.Error(err)
  31. }
  32. if i, ok := psFilters["exited"]; ok {
  33. for _, value := range i {
  34. code, err := strconv.Atoi(value)
  35. if err != nil {
  36. return job.Error(err)
  37. }
  38. filt_exited = append(filt_exited, code)
  39. }
  40. }
  41. if i, ok := psFilters["status"]; ok {
  42. for _, value := range i {
  43. if value == "exited" {
  44. all = true
  45. }
  46. }
  47. }
  48. names := map[string][]string{}
  49. daemon.ContainerGraph().Walk("/", func(p string, e *graphdb.Entity) error {
  50. names[e.ID()] = append(names[e.ID()], p)
  51. return nil
  52. }, -1)
  53. var beforeCont, sinceCont *Container
  54. if before != "" {
  55. beforeCont = daemon.Get(before)
  56. if beforeCont == nil {
  57. return job.Error(fmt.Errorf("Could not find container with name or id %s", before))
  58. }
  59. }
  60. if since != "" {
  61. sinceCont = daemon.Get(since)
  62. if sinceCont == nil {
  63. return job.Error(fmt.Errorf("Could not find container with name or id %s", since))
  64. }
  65. }
  66. errLast := errors.New("last container")
  67. writeCont := func(container *Container) error {
  68. container.Lock()
  69. defer container.Unlock()
  70. if !container.Running && !all && n <= 0 && since == "" && before == "" {
  71. return nil
  72. }
  73. if !psFilters.Match("name", container.Name) {
  74. return nil
  75. }
  76. if !psFilters.Match("id", container.ID) {
  77. return nil
  78. }
  79. if before != "" && !foundBefore {
  80. if container.ID == beforeCont.ID {
  81. foundBefore = true
  82. }
  83. return nil
  84. }
  85. if n > 0 && displayed == n {
  86. return errLast
  87. }
  88. if since != "" {
  89. if container.ID == sinceCont.ID {
  90. return errLast
  91. }
  92. }
  93. if len(filt_exited) > 0 {
  94. should_skip := true
  95. for _, code := range filt_exited {
  96. if code == container.ExitCode && !container.Running {
  97. should_skip = false
  98. break
  99. }
  100. }
  101. if should_skip {
  102. return nil
  103. }
  104. }
  105. if !psFilters.Match("status", container.State.StateString()) {
  106. return nil
  107. }
  108. displayed++
  109. out := &engine.Env{}
  110. out.SetJson("Id", container.ID)
  111. out.SetList("Names", names[container.ID])
  112. out.SetJson("Image", daemon.Repositories().ImageName(container.ImageID))
  113. if len(container.Args) > 0 {
  114. args := []string{}
  115. for _, arg := range container.Args {
  116. if strings.Contains(arg, " ") {
  117. args = append(args, fmt.Sprintf("'%s'", arg))
  118. } else {
  119. args = append(args, arg)
  120. }
  121. }
  122. argsAsString := strings.Join(args, " ")
  123. out.Set("Command", fmt.Sprintf("\"%s %s\"", container.Path, argsAsString))
  124. } else {
  125. out.Set("Command", fmt.Sprintf("\"%s\"", container.Path))
  126. }
  127. out.SetInt64("Created", container.Created.Unix())
  128. out.Set("Status", container.State.String())
  129. str, err := container.NetworkSettings.PortMappingAPI().ToListString()
  130. if err != nil {
  131. return err
  132. }
  133. out.Set("Ports", str)
  134. if size {
  135. sizeRw, sizeRootFs := container.GetSize()
  136. out.SetInt64("SizeRw", sizeRw)
  137. out.SetInt64("SizeRootFs", sizeRootFs)
  138. }
  139. outs.Add(out)
  140. return nil
  141. }
  142. for _, container := range daemon.List() {
  143. if err := writeCont(container); err != nil {
  144. if err != errLast {
  145. return job.Error(err)
  146. }
  147. break
  148. }
  149. }
  150. outs.ReverseSort()
  151. if _, err := outs.WriteListTo(job.Stdout); err != nil {
  152. return job.Error(err)
  153. }
  154. return engine.StatusOK
  155. }