1
0

down.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. "github.com/docker/compose-cli/api/compose"
  17. "github.com/docker/compose-cli/api/progress"
  18. )
  19. func (b *ecsAPIService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
  20. resources, err := b.aws.ListStackResources(ctx, projectName)
  21. if err != nil {
  22. return err
  23. }
  24. err = resources.apply(awsTypeCapacityProvider, doDelete(ctx, b.aws.DeleteCapacityProvider))
  25. if err != nil {
  26. return err
  27. }
  28. err = resources.apply(awsTypeAutoscalingGroup, doDelete(ctx, b.aws.DeleteAutoscalingGroup))
  29. if err != nil {
  30. return err
  31. }
  32. previousEvents, err := b.previousStackEvents(ctx, projectName)
  33. if err != nil {
  34. return err
  35. }
  36. err = b.aws.DeleteStack(ctx, projectName)
  37. if err != nil {
  38. return err
  39. }
  40. return b.WaitStackCompletion(ctx, projectName, stackDelete, previousEvents...)
  41. }
  42. func (b *ecsAPIService) previousStackEvents(ctx context.Context, project string) ([]string, error) {
  43. events, err := b.aws.DescribeStackEvents(ctx, project)
  44. if err != nil {
  45. return nil, err
  46. }
  47. var previousEvents []string
  48. for _, e := range events {
  49. previousEvents = append(previousEvents, *e.EventId)
  50. }
  51. return previousEvents, nil
  52. }
  53. func doDelete(ctx context.Context, delete func(ctx context.Context, arn string) error) func(r stackResource) error {
  54. return func(r stackResource) error {
  55. w := progress.ContextWriter(ctx)
  56. w.Event(progress.RemovingEvent(r.LogicalID))
  57. err := delete(ctx, r.ARN)
  58. if err != nil {
  59. w.Event(progress.ErrorEvent(r.LogicalID))
  60. return err
  61. }
  62. w.Event(progress.RemovedEvent(r.LogicalID))
  63. return nil
  64. }
  65. }