ls.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "strings"
  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. func (s *composeService) List(ctx context.Context, opts api.ListOptions) ([]api.Stack, error) {
  25. list, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
  26. Filters: filters.NewArgs(hasProjectLabelFilter()),
  27. All: opts.All,
  28. })
  29. if err != nil {
  30. return nil, err
  31. }
  32. return containersToStacks(list)
  33. }
  34. func containersToStacks(containers []moby.Container) ([]api.Stack, error) {
  35. containersByLabel, keys, err := groupContainerByLabel(containers, api.ProjectLabel)
  36. if err != nil {
  37. return nil, err
  38. }
  39. var projects []api.Stack
  40. for _, project := range keys {
  41. configFiles, err := combinedConfigFiles(containersByLabel[project])
  42. if err != nil {
  43. return nil, err
  44. }
  45. projects = append(projects, api.Stack{
  46. ID: project,
  47. Name: project,
  48. Status: combinedStatus(containerToState(containersByLabel[project])),
  49. ConfigFiles: configFiles,
  50. })
  51. }
  52. return projects, nil
  53. }
  54. func combinedConfigFiles(containers []moby.Container) (string, error) {
  55. configFiles := []string{}
  56. for _, c := range containers {
  57. files, ok := c.Labels[api.ConfigFilesLabel]
  58. if !ok {
  59. return "", fmt.Errorf("No label %q set on container %q of compose project", api.ConfigFilesLabel, c.ID)
  60. }
  61. for _, f := range strings.Split(files, ",") {
  62. if !utils.StringContains(configFiles, f) {
  63. configFiles = append(configFiles, f)
  64. }
  65. }
  66. }
  67. return strings.Join(configFiles, ","), nil
  68. }
  69. func containerToState(containers []moby.Container) []string {
  70. statuses := []string{}
  71. for _, c := range containers {
  72. statuses = append(statuses, c.State)
  73. }
  74. return statuses
  75. }
  76. func combinedStatus(statuses []string) string {
  77. nbByStatus := map[string]int{}
  78. keys := []string{}
  79. for _, status := range statuses {
  80. nb, ok := nbByStatus[status]
  81. if !ok {
  82. nb = 0
  83. keys = append(keys, status)
  84. }
  85. nbByStatus[status] = nb + 1
  86. }
  87. sort.Strings(keys)
  88. result := ""
  89. for _, status := range keys {
  90. nb := nbByStatus[status]
  91. if result != "" {
  92. result = result + ", "
  93. }
  94. result = result + fmt.Sprintf("%s(%d)", status, nb)
  95. }
  96. return result
  97. }
  98. func groupContainerByLabel(containers []moby.Container, labelName string) (map[string][]moby.Container, []string, error) {
  99. containersByLabel := map[string][]moby.Container{}
  100. keys := []string{}
  101. for _, c := range containers {
  102. label, ok := c.Labels[labelName]
  103. if !ok {
  104. return nil, nil, fmt.Errorf("No label %q set on container %q of compose project", labelName, c.ID)
  105. }
  106. labelContainers, ok := containersByLabel[label]
  107. if !ok {
  108. labelContainers = []moby.Container{}
  109. keys = append(keys, label)
  110. }
  111. labelContainers = append(labelContainers, c)
  112. containersByLabel[label] = labelContainers
  113. }
  114. sort.Strings(keys)
  115. return containersByLabel, keys, nil
  116. }