ls.go 3.6 KB

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