From c6cedb6c5e73ede33d7e501c43d9c63c4ca1c265 Mon Sep 17 00:00:00 2001 From: Nitin Goyal Date: Mon, 26 Aug 2024 20:03:51 +0530 Subject: [PATCH] hack: cleanup noobaa stale buckets in the aws Signed-off-by: Nitin Goyal --- hack/aws-cleanup.sh | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/hack/aws-cleanup.sh b/hack/aws-cleanup.sh index b543787b1..bbc9b6893 100755 --- a/hack/aws-cleanup.sh +++ b/hack/aws-cleanup.sh @@ -108,6 +108,28 @@ delete_elb_load_balancer() { } +# Delete the s3 bucket older than 3 hours +delete_s3_bucket() { + local region=$1 + local s3_bucket_name=$2 + + # Get the creation date of the s3 bucket + creation_date=$(aws s3api list-buckets --region "$region" \ + --query "Buckets[?Name=='$s3_bucket_name'].{CreationDate:CreationDate}" --output text) + + # If creation_date is empty return + [[ -z "$creation_date" ]] && return + + age_hours=$(calculate_age "$creation_date") + + # Delete the s3 bucket if older than 3 hours + if [ "$age_hours" -gt 3 ]; then + echo "Deleting the s3 bucket $s3_bucket_name in region $region with creation date $creation_date" + aws s3 rb s3://"$s3_bucket_name" --region "$region" --force + fi +} + + for region in us-east-1 us-east-2 us-west-1 us-west-2; do # List ec2 instances which are running for instance_id in $(aws ec2 describe-instances --region "$region" \ @@ -156,4 +178,12 @@ for region in us-east-1 us-east-2 us-west-1 us-west-2; do # aws ec2 delete-vpc --region "$region" --vpc-id "$vpc_id" #done + # List s3 buckets starting with nb that belongs to noobaa + for bucket_name in $(aws s3api list-buckets --region "$region" \ + --query "Buckets[?starts_with(Name, 'nb')].Name" --output text); do + + # Delete the s3 buckets starting with nb + delete_s3_bucket "$region" "$bucket_name" + done + done