forked from cds-snc/notification-api
-
Notifications
You must be signed in to change notification settings - Fork 9
277 lines (238 loc) · 10.4 KB
/
task-defnition-cleanup.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
name: Cleanup AWS ECS Task Definitions
on:
schedule:
- cron: '0 0 * * 0' # weekly -- Sunday at 00:00 UTC
workflow_dispatch:
inputs:
dry_run:
description: 'Perform a dry run without deregistering task definitions'
required: true
default: false
type: boolean
jobs:
cleanup-task-definitions:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.VAEC_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.VAEC_AWS_SECRET_ACCESS_KEY }}
aws-region: us-gov-west-1
role-to-assume: ${{ secrets.VAEC_DEPLOY_ROLE }}
role-skip-session-tagging: true
role-duration-seconds: 1800
- name: Cleanup Old ECS Task Definitions
id: cleanup-active
env:
AWS_REGION: "us-gov-west-1"
DRY_RUN: ${{ github.event.inputs.dry_run || 'false' }}
run: |
#!/bin/bash
set -e
MAX_REV=10
REGION="$AWS_REGION"
DRY_RUN="$DRY_RUN"
echo "Starting ECS Task Definitions cleanup in region: $REGION"
echo "Dry run mode: $DRY_RUN"
# -----------------------------------------------------------------------------
# 1. Function to deregister task definitions (with exponential backoff & jitter).
# -----------------------------------------------------------------------------
deregister_task_definition() {
local task_def_arn="$1"
if [ "$DRY_RUN" = "true" ]; then
echo "[Dry Run] Would deregister task definition: $task_def_arn"
else
echo "Deregistering task definition: $task_def_arn"
# We'll attempt up to 5 times in case of rate limiting
for attempt in {1..5}; do
if aws ecs deregister-task-definition --task-definition "$task_def_arn" --region "$REGION"; then
echo "Deregistered $task_def_arn"
break
else
echo "Attempt $attempt to deregister $task_def_arn failed. Sleeping before retry..."
sleep $((attempt * 2)) # exponential backoff (2, 4, 6, 8, 10 seconds)
fi
done
# Introduce a small random jitter between deregistrations
sleep_time=$((1 + RANDOM % 3)) # 1–3 seconds
echo "Sleeping for $sleep_time second(s) to reduce rate-limit risk..."
sleep $sleep_time
fi
}
# -----------------------------------------------------------------------------
# 2. Function to list all task definitions for a given family (with pagination).
# We sort in descending order so that the newest revisions come first.
# -----------------------------------------------------------------------------
list_all_task_definitions() {
local family_filter="$1"
local next_token=""
local task_defs=()
while : ; do
if [ -z "$next_token" ]; then
response=$(aws ecs list-task-definitions \
--region "$REGION" \
--family-prefix "$family_filter" \
--sort DESC \
--max-items 1000 \
--output json \
--query '{taskDefinitionArns: taskDefinitionArns, nextToken: nextToken}')
else
response=$(aws ecs list-task-definitions \
--region "$REGION" \
--family-prefix "$family_filter" \
--sort DESC \
--max-items 1000 \
--starting-token "$next_token" \
--output json \
--query '{taskDefinitionArns: taskDefinitionArns, nextToken: nextToken}')
fi
current_batch=$(echo "$response" | jq -r '.taskDefinitionArns[]')
if [ -n "$current_batch" ]; then
task_defs+=( $current_batch )
fi
next_token=$(echo "$response" | jq -r '.nextToken // empty')
if [ -z "$next_token" ]; then
break
fi
done
# Return all found ARNs
echo "${task_defs[@]}"
}
# -----------------------------------------------------------------------------
# 3. List of families to clean up, each keeping only the latest MAX_REV revisions.
# -----------------------------------------------------------------------------
TARGET_FAMILIES=(
"dev-notification-api-db-migrations-task"
"dev-notification-api-task"
"dev-va-enp-api-task"
"perf-notification-api-db-migrations-task"
"perf-notification-api-task"
"perf-va-enp-api-task"
"prod-notification-api-db-migrations-task"
"prod-notification-api-task"
"staging-notification-api-db-migrations-task"
"staging-notification-api-task"
"dev-notification-celery-beat-task"
"dev-notification-celery-task"
"perf-notification-celery-beat-task"
"perf-notification-celery-task"
"prod-notification-celery-beat-task"
"prod-notification-celery-task"
"staging-notification-celery-beat-task"
"staging-notification-celery-task"
)
# -----------------------------------------------------------------------------
# 4. Iterate over each family, keep the newest MAX_REV, and deregister older ones.
# -----------------------------------------------------------------------------
for FAMILY in "${TARGET_FAMILIES[@]}"; do
echo "--------------------------------------------------------------------------------"
echo "Processing Task Family: $FAMILY"
REVISIONS=$(list_all_task_definitions "$FAMILY")
if [ -z "$REVISIONS" ]; then
echo "No revisions found for family: $FAMILY"
continue
fi
REV_COUNT=0
for REV_ARN in $REVISIONS; do
REV_COUNT=$((REV_COUNT + 1))
if [ "$REV_COUNT" -le "$MAX_REV" ]; then
echo "Keeping revision $REV_COUNT: $REV_ARN"
else
deregister_task_definition "$REV_ARN"
fi
done
done
echo "ECS Task Definitions cleanup completed successfully."
- name: Delete Inactive ECS Task Definitions
if: ${{ steps.cleanup-active.outcome == 'success' }}
env:
AWS_REGION: "us-gov-west-1"
DRY_RUN: ${{ github.event.inputs.dry_run || 'false' }}
run: |
#!/bin/bash
set -e
REGION="$AWS_REGION"
DRY_RUN="$DRY_RUN"
echo "======================================================="
echo "Step 2: Delete all INACTIVE ECS Task Definitions (Paginated)."
echo "Region: $REGION"
echo "Dry run mode: $DRY_RUN"
echo "======================================================="
# Paginate manually over INACTIVE definitions
list_inactive_task_definitions() {
local next_token=""
local definitions=()
while : ; do
if [ -z "$next_token" ]; then
response=$(aws ecs list-task-definitions \
--status INACTIVE \
--region "$REGION" \
--output json \
--query '{taskDefinitionArns: taskDefinitionArns, nextToken: nextToken}')
else
response=$(aws ecs list-task-definitions \
--status INACTIVE \
--starting-token "$next_token" \
--region "$REGION" \
--output json \
--query '{taskDefinitionArns: taskDefinitionArns, nextToken: nextToken}')
fi
current_batch=$(echo "$response" | jq -r '.taskDefinitionArns[]?')
if [ -n "$current_batch" ]; then
definitions+=( $current_batch )
fi
next_token=$(echo "$response" | jq -r '.nextToken // empty')
[ -z "$next_token" ] && break
done
echo "${definitions[@]}"
}
INACTIVE_TASKS_ARRAY=($(list_inactive_task_definitions))
TOTAL_INACTIVE=${#INACTIVE_TASKS_ARRAY[@]}
if [ "$TOTAL_INACTIVE" -eq 0 ]; then
echo "No INACTIVE task definitions found. Nothing to delete."
exit 0
fi
echo "Found $TOTAL_INACTIVE INACTIVE task definitions total."
echo "We'll delete them in chunks of up to 10."
# Function to delete up to 10 definitions (with backoff & jitter):
delete_chunk() {
local chunk=("$@")
echo "Deleting the following INACTIVE tasks:"
printf '%s\n' "${chunk[@]}"
for attempt in {1..5}; do
if aws ecs delete-task-definitions \
--task-definitions "${chunk[@]}" \
--region "$REGION"; then
echo "Successfully deleted chunk of up to 10 tasks."
break
else
echo "Attempt $attempt failed. Sleeping before retry..."
sleep $((attempt * 2)) # exponential backoff
fi
if [ "$attempt" -eq 5 ]; then
echo "ERROR: Failed to delete chunk after 5 attempts."
exit 1
fi
done
# Random jitter of 1–3s
local sleep_time=$((1 + RANDOM % 3))
echo "Sleeping for $sleep_time second(s)..."
sleep $sleep_time
}
# Chunk the array
CHUNK_SIZE=10
i=0
while [ $i -lt $TOTAL_INACTIVE ]; do
CHUNK=("${INACTIVE_TASKS_ARRAY[@]:i:CHUNK_SIZE}")
i=$((i + CHUNK_SIZE))
if [ "$DRY_RUN" = "true" ]; then
echo "[Dry Run] Would delete the following tasks:"
printf '%s\n' "${CHUNK[@]}"
else
delete_chunk "${CHUNK[@]}"
fi
done
echo "Step 2 complete: All possible INACTIVE definitions have been fully deleted."