list.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.State.IsRunning() && !all && n <= 0 && since == "" && before == "" {
  64. return nil
  65. }
  66. if before != "" && !foundBefore {
  67. if container.ID == beforeCont.ID {
  68. foundBefore = true
  69. }
  70. return nil
  71. }
  72. if n > 0 && displayed == n {
  73. return errLast
  74. }
  75. if since != "" {
  76. if container.ID == sinceCont.ID {
  77. return errLast
  78. }
  79. }
  80. if len(filt_exited) > 0 && !container.State.IsRunning() {
  81. should_skip := true
  82. for _, code := range filt_exited {
  83. if code == container.State.GetExitCode() {
  84. should_skip = false
  85. break
  86. }
  87. }
  88. if should_skip {
  89. return nil
  90. }
  91. }
  92. displayed++
  93. out := &engine.Env{}
  94. out.Set("Id", container.ID)
  95. out.SetList("Names", names[container.ID])
  96. out.Set("Image", daemon.Repositories().ImageName(container.Image))
  97. if len(container.Args) > 0 {
  98. args := []string{}
  99. for _, arg := range container.Args {
  100. if strings.Contains(arg, " ") {
  101. args = append(args, fmt.Sprintf("'%s'", arg))
  102. } else {
  103. args = append(args, arg)
  104. }
  105. }
  106. argsAsString := strings.Join(args, " ")
  107. out.Set("Command", fmt.Sprintf("\"%s %s\"", container.Path, argsAsString))
  108. } else {
  109. out.Set("Command", fmt.Sprintf("\"%s\"", container.Path))
  110. }
  111. out.SetInt64("Created", container.Created.Unix())
  112. out.Set("Status", container.State.String())
  113. str, err := container.NetworkSettings.PortMappingAPI().ToListString()
  114. if err != nil {
  115. return err
  116. }
  117. out.Set("Ports", str)
  118. if size {
  119. sizeRw, sizeRootFs := container.GetSize()
  120. out.SetInt64("SizeRw", sizeRw)
  121. out.SetInt64("SizeRootFs", sizeRootFs)
  122. }
  123. outs.Add(out)
  124. return nil
  125. }
  126. for _, container := range daemon.List() {
  127. if err := writeCont(container); err != nil {
  128. if err != errLast {
  129. return job.Error(err)
  130. }
  131. break
  132. }
  133. }
  134. outs.ReverseSort()
  135. if _, err := outs.WriteListTo(job.Stdout); err != nil {
  136. return job.Error(err)
  137. }
  138. return engine.StatusOK
  139. }