feat: add targeted lab preparation workflow

This commit is contained in:
Luca Sacchi Ricciardi
2026-04-10 14:50:06 +00:00
parent 1b58727f68
commit 478e940b43
8 changed files with 327 additions and 76 deletions
+151
View File
@@ -0,0 +1,151 @@
#!/bin/bash
# Laboratori Cloud - Lab Preparation Script
# Part of: "Corso Soluzioni Cloud"
#
# Description: Cleans only resources created by this course to avoid port/network conflicts before starting a lab.
# Usage: ./scripts/prepare-lab.sh [--dry-run] [--keep-volumes]
set -u
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
DRY_RUN=false
KEEP_VOLUMES=false
print_error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_usage() {
cat << EOF
Usage: $0 [--dry-run] [--keep-volumes]
Prepares the environment for a new lab by removing only resources created by this course.
Options:
--dry-run Show what would be removed without changing anything
--keep-volumes Remove containers and networks, but preserve named volumes
-h, --help Show this help message
What this script targets:
- Containers named lab01*, lab02-*, lab03-*, lab04-*, lab05-*
- Networks named lab02-vpc-*, lab05-vpc-*, lab-04-storage_default
- Volumes named lab-02-network_*, lab-03-compute_*, lab-04-storage_*, lab-05-database_*
Examples:
$0
$0 --dry-run
$0 --keep-volumes
Exit codes:
0 - Preparation completed successfully
1 - Invalid arguments or command failure
EOF
}
while [ $# -gt 0 ]; do
case "$1" in
--dry-run)
DRY_RUN=true
;;
--keep-volumes)
KEEP_VOLUMES=true
;;
-h|--help)
print_usage
exit 0
;;
*)
print_error "Unknown option: $1"
echo ""
print_usage
exit 1
;;
esac
shift
done
mapfile -t COURSE_CONTAINERS < <(docker ps -a --format '{{.ID}} {{.Names}}' 2>/dev/null | grep -E '^[0-9a-f]+ (lab01|lab02-|lab03-|lab04-|lab05-)' | cut -d' ' -f1)
mapfile -t COURSE_NETWORKS < <(docker network ls --format '{{.Name}}' 2>/dev/null | grep -E '^(lab02-vpc-|lab05-vpc-|lab-04-storage_default)' || true)
mapfile -t COURSE_VOLUMES < <(docker volume ls --format '{{.Name}}' 2>/dev/null | grep -E '^lab-(02-network|03-compute|04-storage|05-database)_' || true)
echo "=========================================="
echo "Laboratori Cloud - Prepare Lab"
echo "=========================================="
echo ""
if [ "$DRY_RUN" = true ]; then
print_warn "DRY RUN MODE: no changes will be made"
fi
if [ "$KEEP_VOLUMES" = true ]; then
print_warn "KEEP VOLUMES MODE: named volumes will be preserved"
fi
echo ""
print_info "Resources detected for this course:"
echo " - Containers: ${#COURSE_CONTAINERS[@]}"
echo " - Networks: ${#COURSE_NETWORKS[@]}"
echo " - Volumes: ${#COURSE_VOLUMES[@]}"
echo ""
if [ ${#COURSE_CONTAINERS[@]} -eq 0 ] && [ ${#COURSE_NETWORKS[@]} -eq 0 ] && { [ "$KEEP_VOLUMES" = true ] || [ ${#COURSE_VOLUMES[@]} -eq 0 ]; }; then
print_success "No course resources found. Environment already ready."
exit 0
fi
run_or_print() {
local description="$1"
shift
if [ "$DRY_RUN" = true ]; then
print_info "Would ${description}: $*"
else
"$@"
fi
}
if [ ${#COURSE_CONTAINERS[@]} -gt 0 ]; then
print_info "Removing course containers..."
run_or_print "remove containers" docker rm -f "${COURSE_CONTAINERS[@]}" >/dev/null 2>&1 || true
print_success "Processed ${#COURSE_CONTAINERS[@]} container(s)"
fi
if [ ${#COURSE_NETWORKS[@]} -gt 0 ]; then
print_info "Removing course networks..."
run_or_print "remove networks" docker network rm "${COURSE_NETWORKS[@]}" >/dev/null 2>&1 || true
print_success "Processed ${#COURSE_NETWORKS[@]} network(s)"
fi
if [ "$KEEP_VOLUMES" = false ] && [ ${#COURSE_VOLUMES[@]} -gt 0 ]; then
print_info "Removing course volumes..."
run_or_print "remove volumes" docker volume rm "${COURSE_VOLUMES[@]}" >/dev/null 2>&1 || true
print_success "Processed ${#COURSE_VOLUMES[@]} volume(s)"
elif [ "$KEEP_VOLUMES" = true ]; then
print_info "Skipping volume removal as requested"
fi
echo ""
if [ "$DRY_RUN" = true ]; then
print_success "Dry run completed"
else
print_success "Environment ready for the next lab"
fi
exit 0