down.go 2.6 KB

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