diff --git a/scripts/check-env.sh b/scripts/check-env.sh new file mode 100755 index 0000000..93a3e0c --- /dev/null +++ b/scripts/check-env.sh @@ -0,0 +1,165 @@ +#!/bin/bash +# Laboratori Cloud - Environment Check Script +# Part of: "Corso Soluzioni Cloud" +# +# Description: Verifies Docker Engine >= 24.0, Compose V2, network utilities, and system resources +# Usage: ./scripts/check-env.sh + +# Exit on error, but handle command failures gracefully + +# Color codes for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# Counters for summary +PASS_COUNT=0 +FAIL_COUNT=0 + +# Function to print pass message +print_pass() { + echo -e "${GREEN}[PASS]${NC} $1" + ((PASS_COUNT++)) +} + +# Function to print fail message +print_fail() { + echo -e "${RED}[FAIL]${NC} $1" + ((FAIL_COUNT++)) +} + +# Function to print warn message +print_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +# Function to print info message +print_info() { + echo "[INFO] $1" +} + +echo "==========================================" +echo "Laboratori Cloud - Environment Check" +echo "==========================================" +echo "" + +# 1. Docker Engine version check +print_info "Checking Docker Engine version..." +if command -v docker &> /dev/null; then + DOCKER_VERSION=$(docker --version | grep -oP '\d+\.\d+\.\d+' | head -1) + DOCKER_MAJOR=$(echo "$DOCKER_VERSION" | cut -d. -f1) + + print_info "Found Docker Engine version: $DOCKER_VERSION" + + if [ "$DOCKER_MAJOR" -ge 24 ]; then + print_pass "Docker Engine >= 24.0 (found $DOCKER_VERSION)" + else + print_fail "Docker Engine >= 24.0 required. Found: $DOCKER_VERSION" + fi +else + print_fail "Docker Engine not found. Please install Docker >= 24.0" +fi +echo "" + +# 2. Docker Compose V2 check +print_info "Checking Docker Compose V2..." +if docker compose version &> /dev/null; then + COMPOSE_VERSION=$(docker compose version | grep -oP 'Docker Compose version v?\K[\d.]+' | head -1) + print_info "Found Docker Compose V2 version: $COMPOSE_VERSION" + print_pass "Docker Compose V2 is available (use 'docker compose' not 'docker-compose')" +elif command -v docker-compose &> /dev/null; then + print_fail "Docker Compose V2 required. Found legacy 'docker-compose'. Please upgrade to Docker Compose V2" +else + print_fail "Docker Compose V2 required. Use 'docker compose' not 'docker-compose'" +fi +echo "" + +# 3. Network utilities check +print_info "Checking network utilities..." +MISSING_UTILS=() + +# Check for netcat/nc +if command -v nc &> /dev/null; then + print_pass "netcat (nc) is available" +elif command -v netcat &> /dev/null; then + print_pass "netcat is available" +else + MISSING_UTILS+=("netcat/nc") + print_fail "netcat (or nc) is required" +fi + +# Check for curl +if command -v curl &> /dev/null; then + print_pass "curl is available" +else + MISSING_UTILS+=("curl") + print_fail "curl is required" +fi + +# Check for ip (iproute2) +if command -v ip &> /dev/null; then + print_pass "ip (iproute2) is available" +else + MISSING_UTILS+=("ip (iproute2)") + print_fail "ip (iproute2) is required" +fi + +if [ ${#MISSING_UTILS[@]} -gt 0 ]; then + print_fail "Missing network utilities: ${MISSING_UTILS[*]}" +fi +echo "" + +# 4. System resources check +print_info "Checking system resources..." + +# Check RAM +if [ -f /proc/meminfo ]; then + TOTAL_MEM_KB=$(grep MemTotal /proc/meminfo | awk '{print $2}') + TOTAL_MEM_GB=$((TOTAL_MEM_KB / 1024 / 1024)) + print_info "Total RAM: ${TOTAL_MEM_GB}GB" + + if [ "$TOTAL_MEM_GB" -ge 16 ]; then + print_pass "System has ${TOTAL_MEM_GB}GB RAM (recommended: 16GB)" + elif [ "$TOTAL_MEM_GB" -ge 8 ]; then + print_warn "System has ${TOTAL_MEM_GB}GB RAM (recommended: 16GB for all labs)" + else + print_fail "System has ${TOTAL_MEM_GB}GB RAM (minimum: 8GB, recommended: 16GB)" + fi +elif command -v free &> /dev/null; then + TOTAL_MEM=$(free -h | awk '/^Mem:/ {print $2}') + print_info "Total RAM: $TOTAL_MEM" + print_warn "Could not determine exact RAM value. Please verify you have at least 8GB" +else + print_warn "Could not determine system RAM. Please verify manually" +fi + +# Check CPU cores +if command -v nproc &> /dev/null; then + CPU_CORES=$(nproc) + print_info "CPU cores: $CPU_CORES" + + if [ "$CPU_CORES" -ge 4 ]; then + print_pass "System has $CPU_CORES CPU cores" + else + print_warn "System has $CPU_CORES CPU cores (recommended: 4+ for best performance)" + fi +else + print_warn "Could not determine CPU count" +fi +echo "" + +# Summary +echo "==========================================" +echo "Summary: $PASS_COUNT passed, $FAIL_COUNT failed" +echo "==========================================" + +if [ "$FAIL_COUNT" -gt 0 ]; then + echo "" + print_fail "Environment check failed. Please fix the issues above before starting labs." + exit 1 +else + echo "" + print_pass "All environment checks passed! You're ready to start the labs." + exit 0 +fi