Files
nvhi-atsila-microservice/Infrastructure/foundation/cleanup.bash
2025-08-03 00:10:47 +00:00

131 lines
4.5 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Enterprise CI/CD Foundation Cleanup Script
# This script removes the bootstrap S3 bucket and DynamoDB table
# Run this AFTER terraform destroy to completely clean up
set -e
echo "🧹 Foundation Layer Cleanup Script"
# Load configuration if available
if [ -f .backend-config ]; then
echo "📋 Loading configuration from .backend-config..."
source .backend-config
else
echo "⚠️ No .backend-config found. Using environment variables..."
BUCKET_NAME="${BUCKET_NAME:-}"
TABLE_NAME="${TABLE_NAME:-}"
AWS_REGION="${AWS_REGION:-us-east-1}"
PROJECT_NAME="${PROJECT_NAME:-enterprise-cicd}"
ENVIRONMENT="${ENVIRONMENT:-dev}"
fi
# Verify AWS credentials
echo "🔐 Verifying AWS credentials..."
aws sts get-caller-identity || {
echo "❌ AWS credentials not configured or invalid"
exit 1
}
# Interactive confirmation
echo ""
echo "⚠️ WARNING: This will permanently delete bootstrap resources!"
echo ""
echo "Resources to delete:"
echo " S3 Bucket: ${BUCKET_NAME}"
echo " DynamoDB Table: ${TABLE_NAME}"
echo " Region: ${AWS_REGION}"
echo ""
read -p "Are you sure you want to proceed? (type 'DELETE' to confirm): " confirmation
if [ "$confirmation" != "DELETE" ]; then
echo "❌ Cleanup cancelled"
exit 1
fi
echo ""
echo "💀 Starting cleanup process..."
# Check if Terraform state still exists
if [ -f terraform.tfstate ] || [ -f .terraform/terraform.tfstate ]; then
echo "❌ Error: Terraform state files still exist!"
echo "Please run 'terraform destroy' first to destroy all infrastructure"
echo "Then run this cleanup script to remove bootstrap resources"
exit 1
fi
# Check if S3 bucket contains state files
if [ -n "${BUCKET_NAME}" ] && aws s3api head-bucket --bucket "${BUCKET_NAME}" 2>/dev/null; then
STATE_FILES=$(aws s3 ls "s3://${BUCKET_NAME}/foundation/" --recursive 2>/dev/null || echo "")
if [ -n "${STATE_FILES}" ]; then
echo "❌ Error: S3 bucket contains Terraform state files!"
echo "Found state files:"
echo "${STATE_FILES}"
echo ""
echo "Please run 'terraform destroy' first to clean up all infrastructure"
echo "This will remove the state files from S3"
exit 1
fi
fi
# Remove S3 bucket
if [ -n "${BUCKET_NAME}" ] && aws s3api head-bucket --bucket "${BUCKET_NAME}" 2>/dev/null; then
echo "🗑️ Removing S3 bucket: ${BUCKET_NAME}"
# Remove all objects and versions
echo " Removing all objects and versions..."
aws s3api list-object-versions --bucket "${BUCKET_NAME}" \
--query 'Versions[].[Key,VersionId]' --output text | \
while read key version; do
if [ -n "$key" ] && [ -n "$version" ]; then
aws s3api delete-object --bucket "${BUCKET_NAME}" --key "$key" --version-id "$version"
fi
done
# Remove delete markers
aws s3api list-object-versions --bucket "${BUCKET_NAME}" \
--query 'DeleteMarkers[].[Key,VersionId]' --output text | \
while read key version; do
if [ -n "$key" ] && [ -n "$version" ]; then
aws s3api delete-object --bucket "${BUCKET_NAME}" --key "$key" --version-id "$version"
fi
done
# Delete the bucket
aws s3api delete-bucket --bucket "${BUCKET_NAME}" --region "${AWS_REGION}"
echo "✅ S3 bucket ${BUCKET_NAME} deleted"
else
echo " S3 bucket ${BUCKET_NAME} not found or already deleted"
fi
# Remove DynamoDB table
if [ -n "${TABLE_NAME}" ] && aws dynamodb describe-table --table-name "${TABLE_NAME}" --region "${AWS_REGION}" >/dev/null 2>&1; then
echo "🗑️ Removing DynamoDB table: ${TABLE_NAME}"
aws dynamodb delete-table --table-name "${TABLE_NAME}" --region "${AWS_REGION}"
# Wait for deletion to complete
echo "⏳ Waiting for table deletion to complete..."
aws dynamodb wait table-not-exists --table-name "${TABLE_NAME}" --region "${AWS_REGION}"
echo "✅ DynamoDB table ${TABLE_NAME} deleted"
else
echo " DynamoDB table ${TABLE_NAME} not found or already deleted"
fi
# Clean up local files
echo "🧹 Cleaning up local files..."
rm -f backend.tf
rm -f .backend-config
rm -f terraform.tfstate.backup
rm -f .terraform.lock.hcl
rm -rf .terraform/
echo ""
echo "🎉 Cleanup completed successfully!"
echo ""
echo "📋 What was removed:"
echo " ✅ S3 bucket: ${BUCKET_NAME}"
echo " ✅ DynamoDB table: ${TABLE_NAME}"
echo " ✅ Local backend configuration files"
echo ""
echo "💡 You can now run the bootstrap script again to create new resources"