ps.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. convert "github.com/docker/compose-cli/local/moby"
  19. "github.com/docker/compose-cli/api/compose"
  20. moby "github.com/docker/docker/api/types"
  21. "github.com/docker/docker/api/types/filters"
  22. )
  23. func (s *composeService) Ps(ctx context.Context, projectName string) ([]compose.ContainerSummary, error) {
  24. containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
  25. Filters: filters.NewArgs(
  26. projectFilter(projectName),
  27. ),
  28. })
  29. if err != nil {
  30. return nil, err
  31. }
  32. var summary []compose.ContainerSummary
  33. for _, c := range containers {
  34. var publishers []compose.PortPublisher
  35. for _, p := range c.Ports {
  36. var url string
  37. if p.PublicPort != 0 {
  38. url = fmt.Sprintf("%s:%d", p.IP, p.PublicPort)
  39. }
  40. publishers = append(publishers, compose.PortPublisher{
  41. URL: url,
  42. TargetPort: int(p.PrivatePort),
  43. PublishedPort: int(p.PublicPort),
  44. Protocol: p.Type,
  45. })
  46. }
  47. summary = append(summary, compose.ContainerSummary{
  48. ID: c.ID,
  49. Name: getContainerName(c),
  50. Project: c.Labels[projectLabel],
  51. Service: c.Labels[serviceLabel],
  52. State: c.State,
  53. Publishers: publishers,
  54. })
  55. }
  56. return summary, nil
  57. }
  58. func containersToServiceStatus(containers []moby.Container) ([]compose.ServiceStatus, error) {
  59. containersByLabel, keys, err := groupContainerByLabel(containers, serviceLabel)
  60. if err != nil {
  61. return nil, err
  62. }
  63. var services []compose.ServiceStatus
  64. for _, service := range keys {
  65. containers := containersByLabel[service]
  66. runnningContainers := []moby.Container{}
  67. for _, container := range containers {
  68. if container.State == convert.ContainerRunning {
  69. runnningContainers = append(runnningContainers, container)
  70. }
  71. }
  72. services = append(services, compose.ServiceStatus{
  73. ID: service,
  74. Name: service,
  75. Desired: len(containers),
  76. Replicas: len(runnningContainers),
  77. })
  78. }
  79. return services, nil
  80. }
  81. func groupContainerByLabel(containers []moby.Container, labelName string) (map[string][]moby.Container, []string, error) {
  82. containersByLabel := map[string][]moby.Container{}
  83. keys := []string{}
  84. for _, c := range containers {
  85. label, ok := c.Labels[labelName]
  86. if !ok {
  87. return nil, nil, fmt.Errorf("No label %q set on container %q of compose project", labelName, c.ID)
  88. }
  89. labelContainers, ok := containersByLabel[label]
  90. if !ok {
  91. labelContainers = []moby.Container{}
  92. keys = append(keys, label)
  93. }
  94. labelContainers = append(labelContainers, c)
  95. containersByLabel[label] = labelContainers
  96. }
  97. sort.Strings(keys)
  98. return containersByLabel, keys, nil
  99. }