containers.go 3.1 KB

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