ps.go 2.9 KB

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