containers.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. "strconv"
  19. "github.com/compose-spec/compose-go/types"
  20. "github.com/docker/compose/v2/pkg/api"
  21. "github.com/docker/compose/v2/pkg/utils"
  22. moby "github.com/docker/docker/api/types"
  23. "github.com/docker/docker/api/types/filters"
  24. )
  25. // Containers is a set of moby Container
  26. type Containers []moby.Container
  27. type oneOff int
  28. const (
  29. oneOffInclude = oneOff(iota)
  30. oneOffExclude
  31. oneOffOnly
  32. )
  33. func (s *composeService) getContainers(ctx context.Context, project string, oneOff oneOff, stopped bool, selectedServices ...string) (Containers, error) {
  34. var containers Containers
  35. f := getDefaultFilters(project, oneOff, selectedServices...)
  36. containers, err := s.apiClient().ContainerList(ctx, moby.ContainerListOptions{
  37. Filters: filters.NewArgs(f...),
  38. All: stopped,
  39. })
  40. if err != nil {
  41. return nil, err
  42. }
  43. if len(selectedServices) > 1 {
  44. containers = containers.filter(isService(selectedServices...))
  45. }
  46. return containers, nil
  47. }
  48. func getDefaultFilters(projectName string, oneOff oneOff, selectedServices ...string) []filters.KeyValuePair {
  49. f := []filters.KeyValuePair{projectFilter(projectName)}
  50. if len(selectedServices) == 1 {
  51. f = append(f, serviceFilter(selectedServices[0]))
  52. }
  53. f = append(f, hasConfigHashLabel())
  54. switch oneOff {
  55. case oneOffOnly:
  56. f = append(f, oneOffFilter(true))
  57. case oneOffExclude:
  58. f = append(f, oneOffFilter(false))
  59. case oneOffInclude:
  60. }
  61. return f
  62. }
  63. func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName string, oneOff oneOff, stopped bool, serviceName string, containerIndex int) (moby.Container, error) {
  64. defaultFilters := getDefaultFilters(projectName, oneOff, serviceName)
  65. if containerIndex > 0 {
  66. defaultFilters = append(defaultFilters, containerNumberFilter(containerIndex))
  67. }
  68. containers, err := s.apiClient().ContainerList(ctx, moby.ContainerListOptions{
  69. Filters: filters.NewArgs(
  70. defaultFilters...,
  71. ),
  72. All: stopped,
  73. })
  74. if err != nil {
  75. return moby.Container{}, err
  76. }
  77. if len(containers) < 1 {
  78. if containerIndex > 0 {
  79. return moby.Container{}, fmt.Errorf("service %q is not running container #%d", serviceName, containerIndex)
  80. }
  81. return moby.Container{}, fmt.Errorf("service %q is not running", serviceName)
  82. }
  83. sort.Slice(containers, func(i, j int) bool {
  84. x, _ := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel])
  85. y, _ := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel])
  86. return x < y
  87. })
  88. container := containers[0]
  89. return container, nil
  90. }
  91. // containerPredicate define a predicate we want container to satisfy for filtering operations
  92. type containerPredicate func(c moby.Container) bool
  93. func isService(services ...string) containerPredicate {
  94. return func(c moby.Container) bool {
  95. service := c.Labels[api.ServiceLabel]
  96. return utils.StringContains(services, service)
  97. }
  98. }
  99. func isRunning() containerPredicate {
  100. return func(c moby.Container) bool {
  101. return c.State == "running"
  102. }
  103. }
  104. func isNotService(services ...string) containerPredicate {
  105. return func(c moby.Container) bool {
  106. service := c.Labels[api.ServiceLabel]
  107. return !utils.StringContains(services, service)
  108. }
  109. }
  110. // isOrphaned is a predicate to select containers without a matching service definition in compose project
  111. func isOrphaned(project *types.Project) containerPredicate {
  112. var services []string
  113. for _, s := range project.Services {
  114. services = append(services, s.Name)
  115. }
  116. for _, s := range project.DisabledServices {
  117. services = append(services, s.Name)
  118. }
  119. return func(c moby.Container) bool {
  120. service := c.Labels[api.ServiceLabel]
  121. return !utils.StringContains(services, service)
  122. }
  123. }
  124. func isNotOneOff(c moby.Container) bool {
  125. v, ok := c.Labels[api.OneoffLabel]
  126. return !ok || v == "False"
  127. }
  128. // filter return Containers with elements to match predicate
  129. func (containers Containers) filter(predicate containerPredicate) Containers {
  130. var filtered Containers
  131. for _, c := range containers {
  132. if predicate(c) {
  133. filtered = append(filtered, c)
  134. }
  135. }
  136. return filtered
  137. }
  138. func (containers Containers) names() []string {
  139. var names []string
  140. for _, c := range containers {
  141. names = append(names, getCanonicalContainerName(c))
  142. }
  143. return names
  144. }
  145. func (containers Containers) forEach(fn func(moby.Container)) {
  146. for _, c := range containers {
  147. fn(c)
  148. }
  149. }
  150. func (containers Containers) sorted() Containers {
  151. sort.Slice(containers, func(i, j int) bool {
  152. return getCanonicalContainerName(containers[i]) < getCanonicalContainerName(containers[j])
  153. })
  154. return containers
  155. }