|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/aws/aws-sdk-go/aws" |
| 5 | + "github.com/aws/aws-sdk-go/aws/session" |
| 6 | + "github.com/aws/aws-sdk-go/service/ecs" |
| 7 | + "github.com/rebuy-de/aws-nuke/v2/pkg/types" |
| 8 | +) |
| 9 | + |
| 10 | +type ECSTask struct { |
| 11 | + svc *ecs.ECS |
| 12 | + taskARN *string |
| 13 | + clusterARN *string |
| 14 | +} |
| 15 | + |
| 16 | +func init() { |
| 17 | + register("ECSTask", ListECSTasks) |
| 18 | +} |
| 19 | + |
| 20 | +func ListECSTasks(sess *session.Session) ([]Resource, error) { |
| 21 | + svc := ecs.New(sess) |
| 22 | + resources := []Resource{} |
| 23 | + clusters := []*string{} |
| 24 | + |
| 25 | + clusterParams := &ecs.ListClustersInput{ |
| 26 | + MaxResults: aws.Int64(100), |
| 27 | + } |
| 28 | + |
| 29 | + // Discover all clusters |
| 30 | + for { |
| 31 | + output, err := svc.ListClusters(clusterParams) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + |
| 36 | + clusters = append(clusters, output.ClusterArns...) |
| 37 | + |
| 38 | + if output.NextToken == nil { |
| 39 | + break |
| 40 | + } |
| 41 | + |
| 42 | + clusterParams.NextToken = output.NextToken |
| 43 | + } |
| 44 | + |
| 45 | + // Discover all running tasks from all clusters |
| 46 | + for _, clusterArn := range clusters { |
| 47 | + taskParams := &ecs.ListTasksInput{ |
| 48 | + Cluster: clusterArn, |
| 49 | + MaxResults: aws.Int64(10), |
| 50 | + DesiredStatus: aws.String("RUNNING"), |
| 51 | + } |
| 52 | + output, err := svc.ListTasks(taskParams) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + for _, taskArn := range output.TaskArns { |
| 58 | + resources = append(resources, &ECSTask{ |
| 59 | + svc: svc, |
| 60 | + taskARN: taskArn, |
| 61 | + clusterARN: clusterArn, |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + if output.NextToken == nil { |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + taskParams.NextToken = output.NextToken |
| 70 | + } |
| 71 | + |
| 72 | + return resources, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (t *ECSTask) Filter() error { |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func (t *ECSTask) Properties() types.Properties { |
| 80 | + properties := types.NewProperties() |
| 81 | + properties.Set("TaskARN", t.taskARN) |
| 82 | + properties.Set("ClusterARN", t.clusterARN) |
| 83 | + |
| 84 | + return properties |
| 85 | +} |
| 86 | + |
| 87 | +func (t *ECSTask) Remove() error { |
| 88 | + // When StopTask is called on a task, the equivalent of docker stop is issued to the |
| 89 | + // containers running in the task. This results in a SIGTERM value and a default |
| 90 | + // 30-second timeout, after which the SIGKILL value is sent and the containers are |
| 91 | + // forcibly stopped. If the container handles the SIGTERM value gracefully and exits |
| 92 | + // within 30 seconds from receiving it, no SIGKILL value is sent. |
| 93 | + |
| 94 | + _, err := t.svc.StopTask(&ecs.StopTaskInput{ |
| 95 | + Cluster: t.clusterARN, |
| 96 | + Task: t.taskARN, |
| 97 | + Reason: aws.String("Task stopped via AWS Nuke"), |
| 98 | + }) |
| 99 | + |
| 100 | + return err |
| 101 | +} |
0 commit comments