2020-08-18 12:38:23 +03:00
|
|
|
/*
|
2020-09-22 13:13:00 +03:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-08-18 12:38:23 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-08-17 18:48:52 +03:00
|
|
|
package ecs
|
2020-06-11 20:22:12 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-02 15:08:23 +03:00
|
|
|
|
2021-08-31 20:08:07 +03:00
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
|
|
|
"github.com/docker/compose/v2/pkg/progress"
|
2021-06-07 12:21:57 +03:00
|
|
|
"github.com/pkg/errors"
|
2020-06-11 20:22:12 +03:00
|
|
|
)
|
|
|
|
|
2021-06-14 17:26:14 +03:00
|
|
|
func (b *ecsAPIService) Down(ctx context.Context, projectName string, options api.DownOptions) error {
|
2021-06-07 12:21:57 +03:00
|
|
|
if options.Volumes {
|
2021-06-14 17:26:14 +03:00
|
|
|
return errors.Wrap(api.ErrNotImplemented, "--volumes option is not supported on ECS")
|
2021-06-07 12:21:57 +03:00
|
|
|
}
|
|
|
|
if options.Images != "" {
|
2021-06-14 17:26:14 +03:00
|
|
|
return errors.Wrap(api.ErrNotImplemented, "--rmi option is not supported on ECS")
|
2021-06-07 12:21:57 +03:00
|
|
|
}
|
|
|
|
return progress.Run(ctx, func(ctx context.Context) error {
|
|
|
|
return b.down(ctx, projectName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ecsAPIService) down(ctx context.Context, projectName string) error {
|
2020-12-16 12:14:31 +03:00
|
|
|
resources, err := b.aws.ListStackResources(ctx, projectName)
|
2020-10-02 15:08:23 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-14 09:47:03 +03:00
|
|
|
err = resources.apply(awsTypeCapacityProvider, doDelete(ctx, b.aws.DeleteCapacityProvider))
|
2020-10-02 15:08:23 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-14 09:47:03 +03:00
|
|
|
err = resources.apply(awsTypeAutoscalingGroup, doDelete(ctx, b.aws.DeleteAutoscalingGroup))
|
2020-10-02 15:08:23 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-16 12:14:31 +03:00
|
|
|
previousEvents, err := b.previousStackEvents(ctx, projectName)
|
2020-06-11 20:22:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-02 15:08:23 +03:00
|
|
|
|
2020-12-16 12:14:31 +03:00
|
|
|
err = b.aws.DeleteStack(ctx, projectName)
|
2020-10-02 15:08:23 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-16 12:14:31 +03:00
|
|
|
return b.WaitStackCompletion(ctx, projectName, stackDelete, previousEvents...)
|
2020-10-02 15:08:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ecsAPIService) previousStackEvents(ctx context.Context, project string) ([]string, error) {
|
2020-10-12 17:21:48 +03:00
|
|
|
events, err := b.aws.DescribeStackEvents(ctx, project)
|
2020-10-02 15:08:23 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var previousEvents []string
|
|
|
|
for _, e := range events {
|
|
|
|
previousEvents = append(previousEvents, *e.EventId)
|
|
|
|
}
|
|
|
|
return previousEvents, nil
|
|
|
|
}
|
|
|
|
|
2020-10-14 09:47:03 +03:00
|
|
|
func doDelete(ctx context.Context, delete func(ctx context.Context, arn string) error) func(r stackResource) error {
|
2020-10-02 15:08:23 +03:00
|
|
|
return func(r stackResource) error {
|
|
|
|
w := progress.ContextWriter(ctx)
|
2020-11-27 19:39:22 +03:00
|
|
|
w.Event(progress.RemovingEvent(r.LogicalID))
|
|
|
|
err := delete(ctx, r.ARN)
|
|
|
|
if err != nil {
|
2020-11-27 20:18:14 +03:00
|
|
|
w.Event(progress.ErrorEvent(r.LogicalID))
|
2020-11-27 19:39:22 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Event(progress.RemovedEvent(r.LogicalID))
|
|
|
|
return nil
|
2020-10-02 15:08:23 +03:00
|
|
|
}
|
2020-07-15 17:27:24 +03:00
|
|
|
}
|