#!/bin/bash # Sharenet Cleanup Script # This script cleans up Docker resources and registry images set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration CLEANUP_TYPE="${CLEANUP_TYPE:-ci-cd}" # ci-cd or production REGISTRY_DIR="${REGISTRY_DIR:-/opt/registry}" DRY_RUN="${DRY_RUN:-false}" # Functions log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } show_help() { cat << EOF Sharenet Cleanup Script Usage: $0 [OPTIONS] Options: --type TYPE Cleanup type: ci-cd or production (default: ci-cd) --registry-dir DIR Registry directory (default: /opt/registry) --dry-run Show what would be done without executing --help Show this help message Environment Variables: CLEANUP_TYPE Set cleanup type (ci-cd/production) REGISTRY_DIR Set registry directory path DRY_RUN Set to 'true' for dry run mode Examples: $0 # Cleanup CI/CD environment $0 --type production # Cleanup production environment $0 --dry-run # Show what would be cleaned DRY_RUN=true $0 # Dry run mode EOF } cleanup_docker_resources() { log_info "Cleaning up Docker resources..." if [ "$DRY_RUN" = "true" ]; then log_warning "DRY RUN MODE - No changes will be made" echo "Would run: docker image prune -f" echo "Would run: docker volume prune -f" echo "Would run: docker network prune -f" return fi # Remove unused images log_info "Removing unused Docker images..." docker image prune -f # Remove unused volumes log_info "Removing unused Docker volumes..." docker volume prune -f # Remove unused networks log_info "Removing unused Docker networks..." docker network prune -f log_success "Docker resources cleanup completed" } cleanup_registry() { if [ "$CLEANUP_TYPE" != "ci-cd" ]; then log_info "Skipping registry cleanup (not CI/CD environment)" return fi log_info "Cleaning up registry images..." if [ ! -d "$REGISTRY_DIR" ]; then log_warning "Registry directory not found: $REGISTRY_DIR" return fi if [ "$DRY_RUN" = "true" ]; then log_warning "DRY RUN MODE - No changes will be made" echo "Would run: cd $REGISTRY_DIR && docker-compose exec registry registry garbage-collect /etc/docker/registry/config.yml" return fi # Change to registry directory cd "$REGISTRY_DIR" # Check if registry is running if ! docker-compose ps | grep -q "registry.*Up"; then log_warning "Registry is not running, skipping registry cleanup" return fi # Run registry garbage collection (keep last 10 tags per repository) log_info "Running registry garbage collection..." docker-compose exec -T registry registry garbage-collect /etc/docker/registry/config.yml log_success "Registry cleanup completed" } cleanup_production() { log_info "Cleaning up production environment..." # Check if we're in the application directory if [ -f "docker-compose.yml" ]; then log_info "Found docker-compose.yml, cleaning up application resources..." if [ "$DRY_RUN" = "true" ]; then log_warning "DRY RUN MODE - No changes will be made" echo "Would run: docker-compose down" echo "Would run: docker image prune -f" return fi # Stop containers to free up resources log_info "Stopping application containers..." docker-compose down # Clean up Docker resources cleanup_docker_resources # Start containers again log_info "Starting application containers..." docker-compose up -d log_success "Production cleanup completed" else log_warning "Not in application directory (docker-compose.yml not found)" cleanup_docker_resources fi } cleanup_ci_cd() { log_info "Cleaning up CI/CD environment..." # Clean up Docker resources cleanup_docker_resources # Clean up registry cleanup_registry log_success "CI/CD cleanup completed" } # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in --type) CLEANUP_TYPE="$2" shift 2 ;; --registry-dir) REGISTRY_DIR="$2" shift 2 ;; --dry-run) DRY_RUN="true" shift ;; --help|-h) show_help exit 0 ;; *) log_error "Unknown option: $1" show_help exit 1 ;; esac done # Main cleanup logic case "$CLEANUP_TYPE" in production) cleanup_production ;; ci-cd) cleanup_ci_cd ;; *) log_error "Invalid cleanup type: $CLEANUP_TYPE" log_error "Valid types: production, ci-cd" exit 1 ;; esac log_success "Cleanup completed successfully"