list.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. Copyright 2020 Docker, Inc.
  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/api/compose"
  19. "github.com/compose-spec/compose-go/cli"
  20. )
  21. func (b *ecsAPIService) Ps(ctx context.Context, options *cli.ProjectOptions) ([]compose.ServiceStatus, error) {
  22. projectName, err := b.projectName(options)
  23. if err != nil {
  24. return nil, err
  25. }
  26. parameters, err := b.SDK.ListStackParameters(ctx, projectName)
  27. if err != nil {
  28. return nil, err
  29. }
  30. cluster := parameters[parameterClusterName]
  31. resources, err := b.SDK.ListStackResources(ctx, projectName)
  32. if err != nil {
  33. return nil, err
  34. }
  35. servicesARN := []string{}
  36. for _, r := range resources {
  37. switch r.Type {
  38. case "AWS::ECS::Service":
  39. servicesARN = append(servicesARN, r.ARN)
  40. case "AWS::ECS::Cluster":
  41. cluster = r.ARN
  42. }
  43. }
  44. if len(servicesARN) == 0 {
  45. return nil, nil
  46. }
  47. status, err := b.SDK.DescribeServices(ctx, cluster, servicesARN)
  48. if err != nil {
  49. return nil, err
  50. }
  51. for i, state := range status {
  52. ports := []string{}
  53. for _, lb := range state.Publishers {
  54. ports = append(ports, fmt.Sprintf(
  55. "%s:%d->%d/%s",
  56. lb.URL,
  57. lb.PublishedPort,
  58. lb.TargetPort,
  59. strings.ToLower(lb.Protocol)))
  60. }
  61. state.Ports = ports
  62. status[i] = state
  63. }
  64. return status, nil
  65. }