containers.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "sort"
  18. "github.com/docker/compose/v2/pkg/api"
  19. "github.com/docker/compose/v2/pkg/utils"
  20. moby "github.com/docker/docker/api/types"
  21. "github.com/docker/docker/api/types/filters"
  22. )
  23. // Containers is a set of moby Container
  24. type Containers []moby.Container
  25. type oneOff int
  26. const (
  27. oneOffInclude = oneOff(iota)
  28. oneOffExclude
  29. oneOffOnly
  30. )
  31. func (s *composeService) getContainers(ctx context.Context, project string, oneOff oneOff, stopped bool, selectedServices ...string) (Containers, error) {
  32. var containers Containers
  33. f := getDefaultFilters(project, oneOff, selectedServices...)
  34. containers, err := s.apiClient().ContainerList(ctx, moby.ContainerListOptions{
  35. Filters: filters.NewArgs(f...),
  36. All: stopped,
  37. })
  38. if err != nil {
  39. return nil, err
  40. }
  41. if len(selectedServices) > 1 {
  42. containers = containers.filter(isService(selectedServices...))
  43. }
  44. return containers, nil
  45. }
  46. func getDefaultFilters(projectName string, oneOff oneOff, selectedServices ...string) []filters.KeyValuePair {
  47. f := []filters.KeyValuePair{projectFilter(projectName)}
  48. if len(selectedServices) == 1 {
  49. f = append(f, serviceFilter(selectedServices[0]))
  50. }
  51. f = append(f, hasConfigHashLabel())
  52. switch oneOff {
  53. case oneOffOnly:
  54. f = append(f, oneOffFilter(true))
  55. case oneOffExclude:
  56. f = append(f, oneOffFilter(false))
  57. case oneOffInclude:
  58. }
  59. return f
  60. }
  61. func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName string, oneOff oneOff, stopped bool, serviceName string, containerIndex int) (moby.Container, error) {
  62. defaultFilters := getDefaultFilters(projectName, oneOff, serviceName)
  63. defaultFilters = append(defaultFilters, containerNumberFilter(containerIndex))
  64. containers, err := s.apiClient().ContainerList(ctx, moby.ContainerListOptions{
  65. Filters: filters.NewArgs(
  66. defaultFilters...,
  67. ),
  68. All: stopped,
  69. })
  70. if err != nil {
  71. return moby.Container{}, err
  72. }
  73. if len(containers) < 1 {
  74. return moby.Container{}, fmt.Errorf("service %q is not running container #%d", serviceName, containerIndex)
  75. }
  76. container := containers[0]
  77. return container, nil
  78. }
  79. // containerPredicate define a predicate we want container to satisfy for filtering operations
  80. type containerPredicate func(c moby.Container) bool
  81. func isService(services ...string) containerPredicate {
  82. return func(c moby.Container) bool {
  83. service := c.Labels[api.ServiceLabel]
  84. return utils.StringContains(services, service)
  85. }
  86. }
  87. func isRunning() containerPredicate {
  88. return func(c moby.Container) bool {
  89. return c.State == "running"
  90. }
  91. }
  92. func isNotService(services ...string) containerPredicate {
  93. return func(c moby.Container) bool {
  94. service := c.Labels[api.ServiceLabel]
  95. return !utils.StringContains(services, service)
  96. }
  97. }
  98. func isNotOneOff(c moby.Container) bool {
  99. v, ok := c.Labels[api.OneoffLabel]
  100. return !ok || v == "False"
  101. }
  102. // filter return Containers with elements to match predicate
  103. func (containers Containers) filter(predicate containerPredicate) Containers {
  104. var filtered Containers
  105. for _, c := range containers {
  106. if predicate(c) {
  107. filtered = append(filtered, c)
  108. }
  109. }
  110. return filtered
  111. }
  112. func (containers Containers) names() []string {
  113. var names []string
  114. for _, c := range containers {
  115. names = append(names, getCanonicalContainerName(c))
  116. }
  117. return names
  118. }
  119. func (containers Containers) forEach(fn func(moby.Container)) {
  120. for _, c := range containers {
  121. fn(c)
  122. }
  123. }
  124. func (containers Containers) sorted() Containers {
  125. sort.Slice(containers, func(i, j int) bool {
  126. return getCanonicalContainerName(containers[i]) < getCanonicalContainerName(containers[j])
  127. })
  128. return containers
  129. }
  130. func (containers Containers) remove(id string) Containers {
  131. for i, c := range containers {
  132. if c.ID == id {
  133. l := len(containers) - 1
  134. containers[i] = containers[l]
  135. return containers[:l]
  136. }
  137. }
  138. return containers
  139. }