wait.go 2.6 KB

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