wait.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "sort"
  18. "strings"
  19. "time"
  20. "github.com/aws/aws-sdk-go/aws"
  21. "github.com/iancoleman/strcase"
  22. "github.com/docker/compose-cli/progress"
  23. )
  24. func (b *ecsAPIService) WaitStackCompletion(ctx context.Context, name string, operation int, ignored ...string) error { //nolint:gocyclo
  25. knownEvents := map[string]struct{}{}
  26. for _, id := range ignored {
  27. knownEvents[id] = struct{}{}
  28. }
  29. // progress writer
  30. w := progress.ContextWriter(ctx)
  31. // Get the unique Stack ID so we can collect events without getting some from previous deployments with same name
  32. stackID, err := b.aws.GetStackID(ctx, name)
  33. if err != nil {
  34. return err
  35. }
  36. ticker := time.NewTicker(1 * time.Second)
  37. done := make(chan bool)
  38. go func() {
  39. b.aws.WaitStackComplete(ctx, stackID, operation) //nolint:errcheck
  40. ticker.Stop()
  41. done <- true
  42. }()
  43. var completed bool
  44. var stackErr error
  45. for !completed {
  46. select {
  47. case <-done:
  48. completed = true
  49. case <-ticker.C:
  50. }
  51. events, err := b.aws.DescribeStackEvents(ctx, stackID)
  52. if err != nil {
  53. return err
  54. }
  55. sort.Slice(events, func(i, j int) bool {
  56. return events[i].Timestamp.Before(*events[j].Timestamp)
  57. })
  58. for _, event := range events {
  59. if _, ok := knownEvents[*event.EventId]; ok {
  60. continue
  61. }
  62. knownEvents[*event.EventId] = struct{}{}
  63. resource := aws.StringValue(event.LogicalResourceId)
  64. reason := aws.StringValue(event.ResourceStatusReason)
  65. status := aws.StringValue(event.ResourceStatus)
  66. progressStatus := progress.Working
  67. switch status {
  68. case "CREATE_COMPLETE":
  69. if operation == stackCreate {
  70. progressStatus = progress.Done
  71. }
  72. case "UPDATE_COMPLETE":
  73. if operation == stackUpdate {
  74. progressStatus = progress.Done
  75. }
  76. case "DELETE_COMPLETE":
  77. if operation == stackDelete {
  78. progressStatus = progress.Done
  79. }
  80. default:
  81. if strings.HasSuffix(status, "_FAILED") {
  82. progressStatus = progress.Error
  83. if stackErr == nil {
  84. operation = stackDelete
  85. stackErr = fmt.Errorf(reason)
  86. }
  87. }
  88. }
  89. w.Event(progress.Event{
  90. ID: resource,
  91. Status: progressStatus,
  92. StatusText: fmt.Sprintf("%s %s", toCamelCase(status), reason),
  93. })
  94. }
  95. if operation != stackCreate || stackErr != nil {
  96. continue
  97. }
  98. if err := b.checkStackState(ctx, name); err != nil {
  99. if e := b.aws.DeleteStack(ctx, name); e != nil {
  100. return e
  101. }
  102. stackErr = err
  103. operation = stackDelete
  104. w.Event(progress.ErrorEvent(name, err.Error()))
  105. }
  106. }
  107. return stackErr
  108. }
  109. func toCamelCase(status string) string {
  110. return strcase.ToCamel(strings.ToLower(status))
  111. }