list.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "github.com/docker/compose-cli/api/compose"
  18. )
  19. func (b *ecsAPIService) List(ctx context.Context, project string) ([]compose.Stack, error) {
  20. stacks, err := b.SDK.ListStacks(ctx, project)
  21. if err != nil {
  22. return nil, err
  23. }
  24. for _, stack := range stacks {
  25. if stack.Status == compose.STARTING {
  26. if err := b.checkStackState(ctx, stack.Name); err != nil {
  27. stack.Status = compose.FAILED
  28. stack.Reason = err.Error()
  29. }
  30. }
  31. }
  32. return stacks, nil
  33. }
  34. func (b *ecsAPIService) checkStackState(ctx context.Context, name string) error {
  35. resources, err := b.SDK.ListStackResources(ctx, name)
  36. if err != nil {
  37. return err
  38. }
  39. svcArns := []string{}
  40. svcNames := map[string]string{}
  41. var cluster string
  42. for _, r := range resources {
  43. if r.Type == "AWS::ECS::Cluster" {
  44. cluster = r.ARN
  45. continue
  46. }
  47. if r.Type == "AWS::ECS::Service" {
  48. if r.ARN == "" {
  49. continue
  50. }
  51. svcArns = append(svcArns, r.ARN)
  52. svcNames[r.ARN] = r.LogicalID
  53. }
  54. }
  55. if len(svcArns) == 0 {
  56. return nil
  57. }
  58. services, err := b.SDK.GetServiceTaskDefinition(ctx, cluster, svcArns)
  59. if err != nil {
  60. return err
  61. }
  62. for service, taskDef := range services {
  63. if err := b.checkServiceState(ctx, cluster, service, taskDef); err != nil {
  64. return fmt.Errorf("%s %s", svcNames[service], err.Error())
  65. }
  66. }
  67. return nil
  68. }
  69. func (b *ecsAPIService) checkServiceState(ctx context.Context, cluster string, service string, taskdef string) error {
  70. runningTasks, err := b.SDK.GetServiceTasks(ctx, cluster, service, false)
  71. if err != nil {
  72. return err
  73. }
  74. if len(runningTasks) > 0 {
  75. return nil
  76. }
  77. stoppedTasks, err := b.SDK.GetServiceTasks(ctx, cluster, service, true)
  78. if err != nil {
  79. return err
  80. }
  81. if len(stoppedTasks) == 0 {
  82. return nil
  83. }
  84. // filter tasks by task definition
  85. tasks := []string{}
  86. for _, t := range stoppedTasks {
  87. if t.TaskDefinitionArn != nil && *t.TaskDefinitionArn == taskdef {
  88. tasks = append(tasks, *t.TaskArn)
  89. }
  90. }
  91. if len(tasks) == 0 {
  92. return nil
  93. }
  94. reason, err := b.SDK.GetTaskStoppedReason(ctx, cluster, tasks[0])
  95. if err != nil {
  96. return err
  97. }
  98. return fmt.Errorf("%s", reason)
  99. }