From a25bfde4e5cb3a01c496a55f1b6995fd65b839ab Mon Sep 17 00:00:00 2001 From: Yang Chiu Date: Wed, 1 May 2024 12:50:58 +0800 Subject: [PATCH] ci: don't delete resources with owners by default Signed-off-by: Yang Chiu --- cleanup/Jenkinsfile | 2 ++ cleanup/scripts/cleanup.sh | 56 +++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/cleanup/Jenkinsfile b/cleanup/Jenkinsfile index 3eadb00e84..99056aea28 100644 --- a/cleanup/Jenkinsfile +++ b/cleanup/Jenkinsfile @@ -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 { @@ -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} """ } diff --git a/cleanup/scripts/cleanup.sh b/cleanup/scripts/cleanup.sh index 9922e1e2b3..3f7c979910 100755 --- a/cleanup/scripts/cleanup.sh +++ b/cleanup/scripts/cleanup.sh @@ -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)) @@ -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 \ No newline at end of file +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