ps.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. "sort"
  17. "strings"
  18. "github.com/moby/moby/api/types/container"
  19. "github.com/moby/moby/client"
  20. "golang.org/x/sync/errgroup"
  21. "github.com/docker/compose/v5/pkg/api"
  22. )
  23. //nolint:gocyclo
  24. func (s *composeService) Ps(ctx context.Context, projectName string, options api.PsOptions) ([]api.ContainerSummary, error) {
  25. projectName = strings.ToLower(projectName)
  26. oneOff := oneOffExclude
  27. if options.All {
  28. oneOff = oneOffInclude
  29. }
  30. containers, err := s.getContainers(ctx, projectName, oneOff, options.All, options.Services...)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if len(options.Services) != 0 {
  35. containers = containers.filter(isService(options.Services...))
  36. }
  37. summary := make([]api.ContainerSummary, len(containers))
  38. eg, ctx := errgroup.WithContext(ctx)
  39. for i, ctr := range containers {
  40. eg.Go(func() error {
  41. publishers := make([]api.PortPublisher, len(ctr.Ports))
  42. sort.Slice(ctr.Ports, func(i, j int) bool {
  43. return ctr.Ports[i].PrivatePort < ctr.Ports[j].PrivatePort
  44. })
  45. for i, p := range ctr.Ports {
  46. var url string
  47. if p.IP.IsValid() {
  48. url = p.IP.String()
  49. }
  50. publishers[i] = api.PortPublisher{
  51. URL: url, // TODO(thaJeztah); change this to a netip.Addr ??
  52. TargetPort: int(p.PrivatePort),
  53. PublishedPort: int(p.PublicPort),
  54. Protocol: p.Type,
  55. }
  56. }
  57. inspect, err := s.apiClient().ContainerInspect(ctx, ctr.ID, client.ContainerInspectOptions{})
  58. if err != nil {
  59. return err
  60. }
  61. var (
  62. health container.HealthStatus
  63. exitCode int
  64. )
  65. if inspect.Container.State != nil {
  66. switch inspect.Container.State.Status {
  67. case container.StateRunning:
  68. if inspect.Container.State.Health != nil {
  69. health = inspect.Container.State.Health.Status
  70. }
  71. case container.StateExited, container.StateDead:
  72. exitCode = inspect.Container.State.ExitCode
  73. }
  74. }
  75. var (
  76. local int
  77. mounts []string
  78. )
  79. for _, m := range ctr.Mounts {
  80. name := m.Name
  81. if name == "" {
  82. name = m.Source
  83. }
  84. if m.Driver == "local" {
  85. local++
  86. }
  87. mounts = append(mounts, name)
  88. }
  89. var networks []string
  90. if ctr.NetworkSettings != nil {
  91. for k := range ctr.NetworkSettings.Networks {
  92. networks = append(networks, k)
  93. }
  94. }
  95. summary[i] = api.ContainerSummary{
  96. ID: ctr.ID,
  97. Name: getCanonicalContainerName(ctr),
  98. Names: ctr.Names,
  99. Image: ctr.Image,
  100. Project: ctr.Labels[api.ProjectLabel],
  101. Service: ctr.Labels[api.ServiceLabel],
  102. Command: ctr.Command,
  103. State: ctr.State,
  104. Status: ctr.Status,
  105. Created: ctr.Created,
  106. Labels: ctr.Labels,
  107. SizeRw: ctr.SizeRw,
  108. SizeRootFs: ctr.SizeRootFs,
  109. Mounts: mounts,
  110. LocalVolumes: local,
  111. Networks: networks,
  112. Health: health,
  113. ExitCode: exitCode,
  114. Publishers: publishers,
  115. }
  116. return nil
  117. })
  118. }
  119. return summary, eg.Wait()
  120. }