ls.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "github.com/docker/compose/v2/pkg/api"
  19. moby "github.com/docker/docker/api/types"
  20. "github.com/docker/docker/api/types/filters"
  21. )
  22. func (s *composeService) List(ctx context.Context, opts api.ListOptions) ([]api.Stack, error) {
  23. list, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
  24. Filters: filters.NewArgs(hasProjectLabelFilter()),
  25. All: opts.All,
  26. })
  27. if err != nil {
  28. return nil, err
  29. }
  30. return containersToStacks(list)
  31. }
  32. func containersToStacks(containers []moby.Container) ([]api.Stack, error) {
  33. containersByLabel, keys, err := groupContainerByLabel(containers, api.ProjectLabel)
  34. if err != nil {
  35. return nil, err
  36. }
  37. var projects []api.Stack
  38. for _, project := range keys {
  39. projects = append(projects, api.Stack{
  40. ID: project,
  41. Name: project,
  42. Status: combinedStatus(containerToState(containersByLabel[project])),
  43. })
  44. }
  45. return projects, nil
  46. }
  47. func containerToState(containers []moby.Container) []string {
  48. statuses := []string{}
  49. for _, c := range containers {
  50. statuses = append(statuses, c.State)
  51. }
  52. return statuses
  53. }
  54. func combinedStatus(statuses []string) string {
  55. nbByStatus := map[string]int{}
  56. keys := []string{}
  57. for _, status := range statuses {
  58. nb, ok := nbByStatus[status]
  59. if !ok {
  60. nb = 0
  61. keys = append(keys, status)
  62. }
  63. nbByStatus[status] = nb + 1
  64. }
  65. sort.Strings(keys)
  66. result := ""
  67. for _, status := range keys {
  68. nb := nbByStatus[status]
  69. if result != "" {
  70. result = result + ", "
  71. }
  72. result = result + fmt.Sprintf("%s(%d)", status, nb)
  73. }
  74. return result
  75. }
  76. func groupContainerByLabel(containers []moby.Container, labelName string) (map[string][]moby.Container, []string, error) {
  77. containersByLabel := map[string][]moby.Container{}
  78. keys := []string{}
  79. for _, c := range containers {
  80. label, ok := c.Labels[labelName]
  81. if !ok {
  82. return nil, nil, fmt.Errorf("No label %q set on container %q of compose project", labelName, c.ID)
  83. }
  84. labelContainers, ok := containersByLabel[label]
  85. if !ok {
  86. labelContainers = []moby.Container{}
  87. keys = append(keys, label)
  88. }
  89. labelContainers = append(labelContainers, c)
  90. containersByLabel[label] = labelContainers
  91. }
  92. sort.Strings(keys)
  93. return containersByLabel, keys, nil
  94. }