forked from rebuy-de/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from oreillymedia/cl-545-ecs-tasks-module
CL-545 | Add ECSTasks module
- Loading branch information
Showing
1 changed file
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package resources | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/ecs" | ||
"github.com/rebuy-de/aws-nuke/v2/pkg/types" | ||
) | ||
|
||
type ECSTask struct { | ||
svc *ecs.ECS | ||
taskARN *string | ||
clusterARN *string | ||
} | ||
|
||
func init() { | ||
register("ECSTask", ListECSTasks) | ||
} | ||
|
||
func ListECSTasks(sess *session.Session) ([]Resource, error) { | ||
svc := ecs.New(sess) | ||
resources := []Resource{} | ||
clusters := []*string{} | ||
|
||
clusterParams := &ecs.ListClustersInput{ | ||
MaxResults: aws.Int64(100), | ||
} | ||
|
||
// Discover all clusters | ||
for { | ||
output, err := svc.ListClusters(clusterParams) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
clusters = append(clusters, output.ClusterArns...) | ||
|
||
if output.NextToken == nil { | ||
break | ||
} | ||
|
||
clusterParams.NextToken = output.NextToken | ||
} | ||
|
||
// Discover all running tasks from all clusters | ||
for _, clusterArn := range clusters { | ||
taskParams := &ecs.ListTasksInput{ | ||
Cluster: clusterArn, | ||
MaxResults: aws.Int64(10), | ||
DesiredStatus: aws.String("RUNNING"), | ||
} | ||
output, err := svc.ListTasks(taskParams) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, taskArn := range output.TaskArns { | ||
resources = append(resources, &ECSTask{ | ||
svc: svc, | ||
taskARN: taskArn, | ||
clusterARN: clusterArn, | ||
}) | ||
} | ||
|
||
if output.NextToken == nil { | ||
continue | ||
} | ||
|
||
taskParams.NextToken = output.NextToken | ||
} | ||
|
||
return resources, nil | ||
} | ||
|
||
func (t *ECSTask) Filter() error { | ||
return nil | ||
} | ||
|
||
func (t *ECSTask) Properties() types.Properties { | ||
properties := types.NewProperties() | ||
properties.Set("TaskARN", t.taskARN) | ||
properties.Set("ClusterARN", t.clusterARN) | ||
|
||
return properties | ||
} | ||
|
||
func (t *ECSTask) Remove() error { | ||
// When StopTask is called on a task, the equivalent of docker stop is issued to the | ||
// containers running in the task. This results in a SIGTERM value and a default | ||
// 30-second timeout, after which the SIGKILL value is sent and the containers are | ||
// forcibly stopped. If the container handles the SIGTERM value gracefully and exits | ||
// within 30 seconds from receiving it, no SIGKILL value is sent. | ||
|
||
_, err := t.svc.StopTask(&ecs.StopTaskInput{ | ||
Cluster: t.clusterARN, | ||
Task: t.taskARN, | ||
Reason: aws.String("Task stopped via AWS Nuke"), | ||
}) | ||
|
||
return err | ||
} |