list.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. names := map[string][]string{}
  42. daemon.ContainerGraph().Walk("/", func(p string, e *graphdb.Entity) error {
  43. names[e.ID()] = append(names[e.ID()], p)
  44. return nil
  45. }, -1)
  46. var beforeCont, sinceCont *Container
  47. if before != "" {
  48. beforeCont = daemon.Get(before)
  49. if beforeCont == nil {
  50. return job.Error(fmt.Errorf("Could not find container with name or id %s", before))
  51. }
  52. }
  53. if since != "" {
  54. sinceCont = daemon.Get(since)
  55. if sinceCont == nil {
  56. return job.Error(fmt.Errorf("Could not find container with name or id %s", since))
  57. }
  58. }
  59. errLast := errors.New("last container")
  60. writeCont := func(container *Container) error {
  61. container.Lock()
  62. defer container.Unlock()
  63. if !container.Running && !all && n <= 0 && since == "" && before == "" {
  64. return nil
  65. }
  66. if !psFilters.Match("name", container.Name) {
  67. return nil
  68. }
  69. if !psFilters.Match("id", container.ID) {
  70. return nil
  71. }
  72. if before != "" && !foundBefore {
  73. if container.ID == beforeCont.ID {
  74. foundBefore = true
  75. }
  76. return nil
  77. }
  78. if n > 0 && displayed == n {
  79. return errLast
  80. }
  81. if since != "" {
  82. if container.ID == sinceCont.ID {
  83. return errLast
  84. }
  85. }
  86. if len(filt_exited) > 0 && !container.Running {
  87. should_skip := true
  88. for _, code := range filt_exited {
  89. if code == container.ExitCode {
  90. should_skip = false
  91. break
  92. }
  93. }
  94. if should_skip {
  95. return nil
  96. }
  97. }
  98. if !psFilters.Match("status", container.State.StateString()) {
  99. return nil
  100. }
  101. displayed++
  102. out := &engine.Env{}
  103. out.Set("Id", container.ID)
  104. out.SetList("Names", names[container.ID])
  105. out.Set("Image", daemon.Repositories().ImageName(container.Image))
  106. if len(container.Args) > 0 {
  107. args := []string{}
  108. for _, arg := range container.Args {
  109. if strings.Contains(arg, " ") {
  110. args = append(args, fmt.Sprintf("'%s'", arg))
  111. } else {
  112. args = append(args, arg)
  113. }
  114. }
  115. argsAsString := strings.Join(args, " ")
  116. out.Set("Command", fmt.Sprintf("\"%s %s\"", container.Path, argsAsString))
  117. } else {
  118. out.Set("Command", fmt.Sprintf("\"%s\"", container.Path))
  119. }
  120. out.SetInt64("Created", container.Created.Unix())
  121. out.Set("Status", container.State.String())
  122. str, err := container.NetworkSettings.PortMappingAPI().ToListString()
  123. if err != nil {
  124. return err
  125. }
  126. out.Set("Ports", str)
  127. if size {
  128. sizeRw, sizeRootFs := container.GetSize()
  129. out.SetInt64("SizeRw", sizeRw)
  130. out.SetInt64("SizeRootFs", sizeRootFs)
  131. }
  132. outs.Add(out)
  133. return nil
  134. }
  135. for _, container := range daemon.List() {
  136. if err := writeCont(container); err != nil {
  137. if err != errLast {
  138. return job.Error(err)
  139. }
  140. break
  141. }
  142. }
  143. outs.ReverseSort()
  144. if _, err := outs.WriteListTo(job.Stdout); err != nil {
  145. return job.Error(err)
  146. }
  147. return engine.StatusOK
  148. }