Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: don't delete resources with owners by default #1880

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cleanup/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
def imageName = "${JOB_BASE_NAME}-${env.BUILD_NUMBER}"
def summary
def BUILD_TRIGGER_BY = "\n${currentBuild.getBuildCauses()[0].shortDescription}"
def DELETE_RESOURCES_WITH_OWNERS = params.DELETE_RESOURCES_WITH_OWNERS ? params.DELETE_RESOURCES_WITH_OWNERS : false

node {

Expand All @@ -14,6 +15,7 @@ node {
--env AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY} \
--env AWS_SECRET_ACCESS_KEY=${AWS_SECRET_KEY} \
--env AWS_DEFAULT_REGION=us-east-1 \
--env DELETE_RESOURCES_WITH_OWNERS=${DELETE_RESOURCES_WITH_OWNERS} \
${imageName}
"""
}
Expand Down
56 changes: 31 additions & 25 deletions cleanup/scripts/cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ for INSTANCE in ${ALL_INSTANCES[@]}; do
echo " Launch Time: ${LAUNCH_TIME} (${TIMESTAMP}), Diff: ${TIME_DIFF}"
if [[ $TIME_DIFF -gt $THRESHOLD_IN_SEC ]]; then INSTANCE_IDS+=("$INSTANCE_ID"); fi
done
aws ec2 terminate-instances --instance-ids "${INSTANCE_IDS[@]}"
echo " instances ${INSTANCE_IDS[*]} shutting-down"
while [[ -n $(aws ec2 describe-instances --instance-ids "${INSTANCE_IDS[@]}" | jq '.Reservations[].Instances[].State.Name' | grep -v "terminated") ]]; do
echo "Wait for instances terminated ..."
sleep 5s
done
if [[ -n "${INSTANCE_IDS}" ]]; then
aws ec2 terminate-instances --instance-ids "${INSTANCE_IDS[@]}"
echo " instances ${INSTANCE_IDS[*]} shutting-down"
while [[ -n $(aws ec2 describe-instances --instance-ids "${INSTANCE_IDS[@]}" | jq '.Reservations[].Instances[].State.Name' | grep -v "terminated") ]]; do
echo "Wait for instances terminated ..."
sleep 5s
done
fi

echo "[Step 5] List long-running resources with owner:"
THRESHOLD_IN_SEC=$((86400 * 3))
Expand All @@ -190,22 +192,26 @@ if [[ -e "${LONG_RUNNING_INSTANCES}" ]]; then
echo -e "\nEC2 instances running for more than 3 days:\n$(cat ${LONG_RUNNING_INSTANCES})" > "${LONG_RUNNING_INSTANCES}"
fi

echo "[Step 6] Prepare to delete long-running resources with owner:"
THRESHOLD_IN_SEC=$((86400 * 7))
ALL_INSTANCES=$(aws ec2 describe-instances --query 'Reservations[].Instances[?not_null(Tags[?Key == `Owner`].Value)] | []' | jq '.[] | select(.State.Name != "terminated") | {LaunchTime: .LaunchTime, InstanceId: .InstanceId, Tags: .Tags}' | jq -c)
INSTANCE_IDS=()
for INSTANCE in ${ALL_INSTANCES[@]}; do
INSTANCE_ID=$(echo "${INSTANCE}" | jq '.InstanceId' | tr -d '"')
echo " * Instance ${INSTANCE_ID} ==>"
LAUNCH_TIME=$(echo "${INSTANCE}" | jq '.LaunchTime' | tr -d '"')
TIMESTAMP=$(date -D "%Y-%m-%dT%H:%M:%S+00:00" -d "${LAUNCH_TIME}" +%s)
TIME_DIFF=$((CURRENT_TIMESTAMP-TIMESTAMP))
echo " Launch Time: ${LAUNCH_TIME} (${TIMESTAMP}), Diff: ${TIME_DIFF}"
if [[ $TIME_DIFF -gt $THRESHOLD_IN_SEC ]]; then INSTANCE_IDS+=("$INSTANCE_ID"); fi
done
aws ec2 terminate-instances --instance-ids "${INSTANCE_IDS[@]}"
echo " instance ${INSTANCE_IDS[*]} shutting-down"
while [[ -n $(aws ec2 describe-instances --instance-ids "${INSTANCE_IDS[@]}" | jq '.Reservations[].Instances[].State.Name' | grep -v "terminated") ]]; do
echo "Wait for instances terminated ..."
sleep 5s
done
if [[ "${DELETE_RESOURCES_WITH_OWNERS}" == true ]]; then
echo "[Step 6] Prepare to delete long-running resources with owner:"
THRESHOLD_IN_SEC=$((86400 * 7))
ALL_INSTANCES=$(aws ec2 describe-instances --query 'Reservations[].Instances[?not_null(Tags[?Key == `Owner`].Value)] | []' | jq '.[] | select(.State.Name != "terminated") | {LaunchTime: .LaunchTime, InstanceId: .InstanceId, Tags: .Tags}' | jq -c)
INSTANCE_IDS=()
for INSTANCE in ${ALL_INSTANCES[@]}; do
INSTANCE_ID=$(echo "${INSTANCE}" | jq '.InstanceId' | tr -d '"')
echo " * Instance ${INSTANCE_ID} ==>"
LAUNCH_TIME=$(echo "${INSTANCE}" | jq '.LaunchTime' | tr -d '"')
TIMESTAMP=$(date -D "%Y-%m-%dT%H:%M:%S+00:00" -d "${LAUNCH_TIME}" +%s)
TIME_DIFF=$((CURRENT_TIMESTAMP-TIMESTAMP))
echo " Launch Time: ${LAUNCH_TIME} (${TIMESTAMP}), Diff: ${TIME_DIFF}"
if [[ $TIME_DIFF -gt $THRESHOLD_IN_SEC ]]; then INSTANCE_IDS+=("$INSTANCE_ID"); fi
done
if [[ -n "${INSTANCE_IDS}" ]]; then
aws ec2 terminate-instances --instance-ids "${INSTANCE_IDS[@]}"
echo " instance ${INSTANCE_IDS[*]} shutting-down"
while [[ -n $(aws ec2 describe-instances --instance-ids "${INSTANCE_IDS[@]}" | jq '.Reservations[].Instances[].State.Name' | grep -v "terminated") ]]; do
echo "Wait for instances terminated ..."
sleep 5s
done
fi
fi