ps.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 ecs
  14. import (
  15. "context"
  16. "fmt"
  17. "strings"
  18. "github.com/docker/compose-cli/api/compose"
  19. )
  20. func (b *ecsAPIService) Ps(ctx context.Context, project string) ([]compose.ServiceStatus, error) {
  21. resources, err := b.SDK.ListStackResources(ctx, project)
  22. if err != nil {
  23. return nil, err
  24. }
  25. var (
  26. cluster = project
  27. servicesARN []string
  28. )
  29. for _, r := range resources {
  30. switch r.Type {
  31. case "AWS::ECS::Service":
  32. servicesARN = append(servicesARN, r.ARN)
  33. case "AWS::ECS::Cluster":
  34. cluster = r.ARN
  35. }
  36. }
  37. if len(servicesARN) == 0 {
  38. return nil, nil
  39. }
  40. status, err := b.SDK.DescribeServices(ctx, cluster, servicesARN)
  41. if err != nil {
  42. return nil, err
  43. }
  44. for i, state := range status {
  45. ports := []string{}
  46. for _, lb := range state.Publishers {
  47. ports = append(ports, fmt.Sprintf(
  48. "%s:%d->%d/%s",
  49. lb.URL,
  50. lb.PublishedPort,
  51. lb.TargetPort,
  52. strings.ToLower(lb.Protocol)))
  53. }
  54. state.Ports = ports
  55. status[i] = state
  56. }
  57. return status, nil
  58. }