1
0

containers.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "sort"
  17. "strconv"
  18. moby "github.com/docker/docker/api/types"
  19. "github.com/docker/docker/api/types/filters"
  20. "github.com/docker/compose/v2/pkg/api"
  21. "github.com/docker/compose/v2/pkg/utils"
  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 := []filters.KeyValuePair{projectFilter(project)}
  34. if len(selectedServices) == 1 {
  35. f = append(f, serviceFilter(selectedServices[0]))
  36. }
  37. switch oneOff {
  38. case oneOffOnly:
  39. f = append(f, oneOffFilter(true))
  40. case oneOffExclude:
  41. f = append(f, oneOffFilter(false))
  42. case oneOffInclude:
  43. }
  44. containers, err := s.apiClient().ContainerList(ctx, moby.ContainerListOptions{
  45. Filters: filters.NewArgs(f...),
  46. All: stopped,
  47. })
  48. if err != nil {
  49. return nil, err
  50. }
  51. if len(selectedServices) > 1 {
  52. containers = containers.filter(isService(selectedServices...))
  53. }
  54. return containers, nil
  55. }
  56. // containerPredicate define a predicate we want container to satisfy for filtering operations
  57. type containerPredicate func(c moby.Container) bool
  58. func isService(services ...string) containerPredicate {
  59. return func(c moby.Container) bool {
  60. service := c.Labels[api.ServiceLabel]
  61. return utils.StringContains(services, service)
  62. }
  63. }
  64. func isNotService(services ...string) containerPredicate {
  65. return func(c moby.Container) bool {
  66. service := c.Labels[api.ServiceLabel]
  67. return !utils.StringContains(services, service)
  68. }
  69. }
  70. func isNotOneOff(c moby.Container) bool {
  71. v, ok := c.Labels[api.OneoffLabel]
  72. return !ok || v == "False"
  73. }
  74. func indexed(index int) containerPredicate {
  75. return func(c moby.Container) bool {
  76. number := c.Labels[api.ContainerNumberLabel]
  77. idx, err := strconv.Atoi(number)
  78. return err == nil && index == idx
  79. }
  80. }
  81. // filter return Containers with elements to match predicate
  82. func (containers Containers) filter(predicate containerPredicate) Containers {
  83. var filtered Containers
  84. for _, c := range containers {
  85. if predicate(c) {
  86. filtered = append(filtered, c)
  87. }
  88. }
  89. return filtered
  90. }
  91. func (containers Containers) names() []string {
  92. var names []string
  93. for _, c := range containers {
  94. names = append(names, getCanonicalContainerName(c))
  95. }
  96. return names
  97. }
  98. func (containers Containers) forEach(fn func(moby.Container)) {
  99. for _, c := range containers {
  100. fn(c)
  101. }
  102. }
  103. func (containers Containers) sorted() Containers {
  104. sort.Slice(containers, func(i, j int) bool {
  105. return getCanonicalContainerName(containers[i]) < getCanonicalContainerName(containers[j])
  106. })
  107. return containers
  108. }