feat(01-01): add environment reset script (reset-env.sh)

- Stops all containers and removes user-created networks/volumes
- Interactive mode requires user confirmation before destructive operations
- --dry-run flag shows what would be deleted without actually deleting
- Preserves Docker default networks (bridge, host, none)
- Does NOT remove Docker images
- Color-coded output and clear warning messages
- Exit code 0 on success, 1 on cancellation or error

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Luca Sacchi Ricciardi
2026-03-24 19:52:24 +01:00
parent f9a3e1e342
commit 9b90ed2169

232
scripts/reset-env.sh Executable file
View File

@@ -0,0 +1,232 @@
#!/bin/bash
# Laboratori Cloud - Environment Reset Script
# Part of: "Corso Soluzioni Cloud"
#
# Description: Stops all containers, removes all networks and volumes (with confirmation)
# Usage: ./scripts/reset-env.sh [--dry-run]
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Flags
DRY_RUN=false
# Function to print error message
print_error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
# Function to print success message
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
# Function to print warn message
print_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
# Function to print info message
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
# Function to print usage
print_usage() {
cat << EOF
Usage: $0 [--dry-run]
Stops all containers, removes all user-created networks and volumes.
Options:
--dry-run Show what would be deleted without actually deleting
WARNING: This will stop ALL containers and remove ALL user-created networks and volumes.
Docker default networks (bridge, host, none) are preserved.
Docker images are NOT removed.
Examples:
$0 # Interactive mode with confirmation
$0 --dry-run # Show what would be deleted
Exit codes:
0 - Cleanup completed successfully
1 - User cancelled or error occurred
EOF
}
# Parse arguments
while [ "$1" != "" ]; do
case $1 in
--dry-run )
DRY_RUN=true
;;
-h | --help )
print_usage
exit 0
;;
* )
print_error "Unknown option: $1"
echo ""
print_usage
exit 1
;;
esac
shift
done
echo "=========================================="
echo "Laboratori Cloud - Environment Reset"
echo "=========================================="
echo ""
if [ "$DRY_RUN" = true ]; then
print_warn "DRY RUN MODE: No actual changes will be made"
echo ""
fi
# Count objects before cleanup
RUNNING_CONTAINERS=$(docker ps -q 2>/dev/null | wc -l)
ALL_CONTAINERS=$(docker ps -aq 2>/dev/null | wc -l)
USER_NETWORKS=$(docker network ls -q 2>/dev/null | grep -v -E '^[0-9a-f]+$|bridge|host|none' | wc -l)
ALL_VOLUMES=$(docker volume ls -q 2>/dev/null | wc -l)
print_info "Current environment state:"
echo " - Running containers: $RUNNING_CONTAINERS"
echo " - Total containers: $ALL_CONTAINERS"
echo " - User-created networks: $USER_NETWORKS"
echo " - Total volumes: $ALL_VOLUMES"
echo ""
# Check if there's anything to clean
if [ "$RUNNING_CONTAINERS" -eq 0 ] && [ "$ALL_CONTAINERS" -eq 0 ] && [ "$USER_NETWORKS" -eq 0 ] && [ "$ALL_VOLUMES" -eq 0 ]; then
print_success "Environment is already clean. Nothing to do."
exit 0
fi
# Confirmation prompt (skip in dry-run mode)
if [ "$DRY_RUN" = false ]; then
echo -e "${RED}WARNING: This will stop ALL containers and remove ALL user-created networks and volumes.${NC}"
echo -e "${YELLOW}Docker default networks (bridge, host, none) will be preserved.${NC}"
echo -e "${YELLOW}Docker images will NOT be removed.${NC}"
echo ""
read -p "Are you sure you want to proceed? (yes/no): " CONFIRM
if [ "$CONFIRM" != "yes" ] && [ "$CONFIRM" != "y" ]; then
print_info "Cleanup cancelled by user"
exit 1
fi
echo ""
fi
# Counter for removed objects
STOPPED_CONTAINERS=0
REMOVED_CONTAINERS=0
REMOVED_NETWORKS=0
REMOVED_VOLUMES=0
# 1. Stop running containers
if [ "$RUNNING_CONTAINERS" -gt 0 ]; then
print_info "Stopping running containers..."
CONTAINER_IDS=$(docker ps -q 2>/dev/null)
if [ -n "$CONTAINER_IDS" ]; then
if [ "$DRY_RUN" = true ]; then
print_info "Would stop containers: $CONTAINER_IDS"
STOPPED_CONTAINERS=$(echo "$CONTAINER_IDS" | wc -l)
else
docker stop $CONTAINER_IDS > /dev/null 2>&1
STOPPED_CONTAINERS=$?
if [ $STOPPED_CONTAINERS -eq 0 ]; then
STOPPED_CONTAINERS=$(echo "$CONTAINER_IDS" | wc -l)
fi
fi
print_success "Stopped $STOPPED_CONTAINERS container(s)"
fi
fi
# 2. Remove all containers
if [ "$ALL_CONTAINERS" -gt 0 ]; then
print_info "Removing all containers..."
CONTAINER_IDS=$(docker ps -aq 2>/dev/null)
if [ -n "$CONTAINER_IDS" ]; then
if [ "$DRY_RUN" = true ]; then
print_info "Would remove containers: $CONTAINER_IDS"
REMOVED_CONTAINERS=$(echo "$CONTAINER_IDS" | wc -l)
else
docker rm $CONTAINER_IDS > /dev/null 2>&1
REMOVED_CONTAINERS=$?
if [ $REMOVED_CONTAINERS -eq 0 ]; then
REMOVED_CONTAINERS=$(echo "$CONTAINER_IDS" | wc -l)
fi
fi
print_success "Removed $REMOVED_CONTAINERS container(s)"
fi
fi
# 3. Remove user-created networks
if [ "$USER_NETWORKS" -gt 0 ]; then
print_info "Removing user-created networks..."
NETWORK_IDS=$(docker network ls -q 2>/dev/null | grep -v -E '^[0-9a-f]+$|bridge|host|none' || true)
if [ -n "$NETWORK_IDS" ]; then
if [ "$DRY_RUN" = true ]; then
print_info "Would remove networks: $NETWORK_IDS"
REMOVED_NETWORKS=$(echo "$NETWORK_IDS" | wc -l)
else
docker network rm $NETWORK_IDS > /dev/null 2>&1
REMOVED_NETWORKS=$?
if [ $REMOVED_NETWORKS -eq 0 ]; then
REMOVED_NETWORKS=$(echo "$NETWORK_IDS" | wc -l)
fi
fi
print_success "Removed $REMOVED_NETWORKS network(s)"
fi
fi
# 4. Remove volumes
if [ "$ALL_VOLUMES" -gt 0 ]; then
print_info "Removing volumes..."
VOLUME_IDS=$(docker volume ls -q 2>/dev/null)
if [ -n "$VOLUME_IDS" ]; then
if [ "$DRY_RUN" = true ]; then
print_info "Would remove volumes: $VOLUME_IDS"
REMOVED_VOLUMES=$(echo "$VOLUME_IDS" | wc -l)
else
docker volume rm $VOLUME_IDS > /dev/null 2>&1
REMOVED_VOLUMES=$?
if [ $REMOVED_VOLUMES -eq 0 ]; then
REMOVED_VOLUMES=$(echo "$VOLUME_IDS" | wc -l)
fi
fi
print_success "Removed $REMOVED_VOLUMES volume(s)"
fi
fi
# Summary
echo ""
echo "=========================================="
echo "Summary"
echo "=========================================="
if [ "$DRY_RUN" = true ]; then
print_info "Dry run completed. No actual changes were made."
echo " - Would stop: $STOPPED_CONTAINERS container(s)"
echo " - Would remove: $REMOVED_CONTAINERS container(s)"
echo " - Would remove: $REMOVED_NETWORKS network(s)"
echo " - Would remove: $REMOVED_VOLUMES volume(s)"
else
print_success "Environment reset completed successfully"
echo " - Stopped: $STOPPED_CONTAINERS container(s)"
echo " - Removed: $REMOVED_CONTAINERS container(s)"
echo " - Removed: $REMOVED_NETWORKS network(s)"
echo " - Removed: $REMOVED_VOLUMES volume(s)"
fi
exit 0