ps.go 3.3 KB

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