wait.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, ignored ...string) error { //nolint:gocyclo
  24. knownEvents := map[string]struct{}{}
  25. for _, id := range ignored {
  26. knownEvents[id] = struct{}{}
  27. }
  28. // progress writer
  29. w := progress.ContextWriter(ctx)
  30. // Get the unique Stack ID so we can collect events without getting some from previous deployments with same name
  31. stackID, err := b.SDK.GetStackID(ctx, name)
  32. if err != nil {
  33. return err
  34. }
  35. ticker := time.NewTicker(1 * time.Second)
  36. done := make(chan bool)
  37. go func() {
  38. b.SDK.WaitStackComplete(ctx, stackID, operation) //nolint:errcheck
  39. ticker.Stop()
  40. done <- true
  41. }()
  42. var completed bool
  43. var stackErr error
  44. for !completed {
  45. select {
  46. case <-done:
  47. completed = true
  48. case <-ticker.C:
  49. }
  50. events, err := b.SDK.DescribeStackEvents(ctx, stackID)
  51. if err != nil {
  52. return err
  53. }
  54. sort.Slice(events, func(i, j int) bool {
  55. return events[i].Timestamp.Before(*events[j].Timestamp)
  56. })
  57. for _, event := range events {
  58. if _, ok := knownEvents[*event.EventId]; ok {
  59. continue
  60. }
  61. knownEvents[*event.EventId] = struct{}{}
  62. resource := aws.StringValue(event.LogicalResourceId)
  63. reason := shortenMessage(
  64. 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: 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.SDK.DeleteStack(ctx, name); e != nil {
  100. return e
  101. }
  102. stackErr = err
  103. operation = stackDelete
  104. reason := shortenMessage(err.Error())
  105. w.Event(progress.Event{
  106. ID: name,
  107. Status: progress.Error,
  108. StatusText: reason,
  109. })
  110. }
  111. }
  112. return stackErr
  113. }
  114. func shortenMessage(message string) string {
  115. if len(message) < 30 {
  116. return message
  117. }
  118. return message[:30] + "..."
  119. }