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