From 00522a0655c9e7168325806f75e68412177abdd1 Mon Sep 17 00:00:00 2001 From: Mubbasher Ahmed Qureshi Date: Fri, 18 Jul 2025 17:59:44 +0500 Subject: [PATCH] feat: added port checker warning for local setup --- Makefile | 34 ++++- containers/README.md | 51 +++++++- containers/check-ports.sh | 185 ++++++++++++++++++++++++++++ containers/check-sonarqube-ports.sh | 143 +++++++++++++++++++++ 4 files changed, 409 insertions(+), 4 deletions(-) create mode 100755 containers/check-ports.sh create mode 100755 containers/check-sonarqube-ports.sh diff --git a/Makefile b/Makefile index 0cc8846..9201b7a 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # ============================================================================= # Complete local development setup (MAIN COMMAND) -local-setup: docker-cleanup docker-setup-env install-commit-tools setup-git-hooks +local-setup: docker-cleanup docker-setup-env check-ports install-commit-tools setup-git-hooks @echo "๐Ÿš€ SETUP: Complete local development environment..." @echo "" @echo "๐Ÿ“ฆ Setting up Docker containers..." @@ -65,6 +65,24 @@ release: @echo "RELEASE: Creating release with release-please..." npm run release +# ============================================================================= +# Port Management +# ============================================================================= + +# Check port availability (standalone command) +check-ports-standalone: + @echo "๐Ÿ” PORTS: Checking port availability for Docker services..." + @bash containers/check-ports.sh + @echo "" + @echo "๐Ÿ’ก TIP: Use 'make local-setup' to automatically check ports before setup" + +# Check SonarQube port availability (standalone command) +check-sonarqube-ports-standalone: + @echo "๐Ÿ” SONARQUBE PORTS: Checking SonarQube port availability..." + @bash containers/check-sonarqube-ports.sh + @echo "" + @echo "๐Ÿ’ก TIP: Use 'make sonarqube-setup' to automatically check ports before SonarQube setup" + # ============================================================================= # Docker Environment Management # ============================================================================= @@ -96,6 +114,12 @@ docker-verify-env: @echo "VERIFY: Docker environment setup..." bash containers/verify-env-setup.sh +# Check port availability before Docker setup +check-ports: + @echo "๐Ÿ” PORTS: Checking port availability for Docker services..." + @bash containers/check-ports.sh || (echo "โŒ Port check failed. Please resolve port conflicts before continuing." && exit 1) + @echo "โœ… SUCCESS: All required ports are available!" + # ============================================================================= # Docker Development Environment # ============================================================================= @@ -235,7 +259,7 @@ health: # ============================================================================= # Complete SonarQube setup and analysis -sonarqube-setup: sonarqube-setup-env sonarqube-start +sonarqube-setup: sonarqube-setup-env check-sonarqube-ports sonarqube-start @echo "๐Ÿ” SONARQUBE: Complete setup and analysis..." @echo "โณ SonarQube is starting up... This may take a few minutes." @echo "๐Ÿ“Š SonarQube will be available at: http://localhost:9000" @@ -258,6 +282,12 @@ sonarqube-stop: cd containers && docker-compose -f docker-compose.sonarqube.yml down @echo "SUCCESS: SonarQube server stopped!" +# Check SonarQube port availability +check-sonarqube-ports: + @echo "๐Ÿ” SONARQUBE PORTS: Checking port availability..." + @bash containers/check-sonarqube-ports.sh || (echo "โš ๏ธ SonarQube port check failed. You can continue without SonarQube or resolve port conflicts." && read -p "Continue anyway? (y/N): " confirm && [ "$$confirm" = "y" ] || [ "$$confirm" = "Y" ] || exit 1) + @echo "โœ… SUCCESS: SonarQube ports are available!" + # Setup SonarQube environment and token sonarqube-setup-token: @echo "SONARQUBE: Setting up SonarQube environment and token..." diff --git a/containers/README.md b/containers/README.md index 9614846..0363c5e 100644 --- a/containers/README.md +++ b/containers/README.md @@ -1,6 +1,26 @@ # Docker Setup for Laravel Blog API -This directory contains all Docker configuration files for local development and testing of the Laravel Blog API project. +This directory contains all Docker configuration files for local dev### ๐Ÿš€ Main Setup Commands +```bash +make local-setup # Complete local development setup (MAIN COMMAND) +make sonarqube-setup # Optional SonarQube setup +``` + +### ๐Ÿ” Port Management +```bash +make check-ports-standalone # Check all required ports availability +make check-sonarqube-ports-standalone # Check SonarQube ports availability +``` + +### ๐Ÿณ Container Management +```bash +make docker-up # Start containers only (no setup) +make docker-down # Stop all containers +make status # Container status and access URLs +make health # Check application health +make logs # View all container logs +make shell # Access main container shell +```sting of the Laravel Blog API project. ## ๏ฟฝ Quick Setup @@ -19,7 +39,8 @@ make local-setup This automated setup will: - ๐Ÿงน Clean up any existing containers and images -- ๐Ÿ”‘ **Auto-generate unique APP_KEY** for all environments +- ๏ฟฝ **Check port availability** and warn of conflicts +- ๏ฟฝ๐Ÿ”‘ **Auto-generate unique APP_KEY** for all environments - ๐Ÿ“ **Copy and configure** all environment files automatically - ๐Ÿณ Start both **main AND testing** environments - ๐Ÿ“ฆ Install Composer dependencies @@ -28,6 +49,32 @@ This automated setup will: - ๐Ÿ› ๏ธ Install Git hooks and semantic commit tools - โœ… Provide complete development and testing setup +### Port Conflict Prevention + +**Before starting Docker containers, the setup automatically checks port availability:** + +```bash +# Check ports for main setup (included in local-setup) +make check-ports-standalone + +# Check ports for SonarQube (included in sonarqube-setup) +make check-sonarqube-ports-standalone +``` + +**Required ports that must be available:** +- `8081` - Laravel API (main application) +- `8001` - Xdebug debugging port +- `3306` - MySQL database +- `6379` - Redis cache +- `3307` - MySQL test database +- `6380` - Redis test cache + +**Optional ports (SonarQube):** +- `9000` - SonarQube web interface +- `5432` - PostgreSQL (SonarQube database) + +**If ports are in use:** The script will suggest alternative ports and show you exactly how to modify the docker-compose files. + ### Optional: SonarQube Code Quality Analysis ```bash diff --git a/containers/check-ports.sh b/containers/check-ports.sh new file mode 100755 index 0000000..a6ce502 --- /dev/null +++ b/containers/check-ports.sh @@ -0,0 +1,185 @@ +#!/usr/bin/env bash + +# ============================================================================= +# Port Availability Checker for Laravel Blog API Docker Setup +# ============================================================================= + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# Ports used by the application (port:description format) +REQUIRED_PORTS=( + "8081:Laravel API (Main App)" + "8001:Xdebug Port" + "3306:MySQL Database" + "6379:Redis Cache" + "3307:MySQL Test Database" + "6380:Redis Test Cache" +) + +OPTIONAL_PORTS=( + "9000:SonarQube Web Interface" + "5432:PostgreSQL (SonarQube)" +) + +# Function to check if a port is available +check_port() { + local port=$1 + local description=$2 + local optional=${3:-false} + + if nc -z localhost $port 2>/dev/null; then + if [ "$optional" = true ]; then + echo -e "${YELLOW}โš ๏ธ OPTIONAL PORT $port ($description) is already in use${NC}" + else + echo -e "${RED}โŒ PORT $port ($description) is already in use${NC}" + return 1 + fi + else + if [ "$optional" = true ]; then + echo -e "${GREEN}โœ… OPTIONAL PORT $port ($description) is available${NC}" + else + echo -e "${GREEN}โœ… PORT $port ($description) is available${NC}" + fi + fi + return 0 +} + +# Function to suggest alternative ports +suggest_alternative_port() { + local base_port=$1 + local description=$2 + local start_range=$((base_port + 1)) + local end_range=$((base_port + 100)) + + echo -e "${BLUE}๐Ÿ’ก Searching for alternative ports for $description...${NC}" + + for ((port=start_range; port<=end_range; port++)); do + if ! nc -z localhost $port 2>/dev/null; then + echo -e "${GREEN} Suggested alternative: $port${NC}" + return 0 + fi + done + + echo -e "${YELLOW} No alternatives found in range $start_range-$end_range${NC}" + return 1 +} + +# Function to show port change instructions +show_port_change_instructions() { + local port=$1 + local description=$2 + local suggested_port=$3 + + echo -e "${BLUE}๐Ÿ“ To change port $port ($description) to $suggested_port:${NC}" + + case $port in + 8081) + echo " Update containers/docker-compose.yml: ports: \"$suggested_port:80\"" + ;; + 8001) + echo " Update containers/docker-compose.yml: ports: \"$suggested_port:8001\"" + ;; + 3306) + echo " Update containers/docker-compose.yml: ports: \"$suggested_port:3306\"" + ;; + 6379) + echo " Update containers/docker-compose.yml: ports: \"$suggested_port:6379\"" + ;; + 3307) + echo " Update containers/docker-compose.test.yml: ports: \"$suggested_port:3306\"" + ;; + 6380) + echo " Update containers/docker-compose.test.yml: ports: \"$suggested_port:6379\"" + ;; + 9000) + echo " Update containers/docker-compose.sonarqube.yml: ports: \"$suggested_port:9000\"" + ;; + 5432) + echo " Update containers/docker-compose.sonarqube.yml: ports: \"$suggested_port:5432\"" + ;; + esac + echo "" +} + +# Main function +main() { + echo -e "${BLUE}๐Ÿ” Checking port availability for Laravel Blog API Docker setup...${NC}" + echo "" + + local unavailable_ports=() + local failed_required=false + + # Check required ports + echo -e "${BLUE}๐Ÿ“‹ Checking required ports:${NC}" + for port_info in "${REQUIRED_PORTS[@]}"; do + local port="${port_info%%:*}" + local description="${port_info#*:}" + if ! check_port "$port" "$description"; then + unavailable_ports+=("$port_info") + failed_required=true + fi + done + + echo "" + + # Check optional ports (SonarQube) + echo -e "${BLUE}๐Ÿ“‹ Checking optional ports (SonarQube):${NC}" + for port_info in "${OPTIONAL_PORTS[@]}"; do + local port="${port_info%%:*}" + local description="${port_info#*:}" + check_port "$port" "$description" true + done + + echo "" + + # Summary and recommendations + if [ "$failed_required" = true ]; then + echo -e "${RED}โŒ SETUP CANNOT CONTINUE - Some required ports are unavailable!${NC}" + echo "" + echo -e "${YELLOW}๐Ÿ”ง SOLUTIONS:${NC}" + echo "1. Stop the services using these ports" + echo "2. Use alternative ports (see suggestions below)" + echo "3. Modify docker-compose files with new ports" + echo "" + + echo -e "${BLUE}๐Ÿ’ก PORT ALTERNATIVES:${NC}" + for port_info in "${unavailable_ports[@]}"; do + local port="${port_info%%:*}" + local description="${port_info#*:}" + + # Find suggested alternative + local start_range=$((port + 1)) + for ((alt_port=start_range; alt_port<=start_range+50; alt_port++)); do + if ! nc -z localhost $alt_port 2>/dev/null; then + show_port_change_instructions "$port" "$description" "$alt_port" + break + fi + done + done + + echo -e "${YELLOW}โš ๏ธ After making changes, run this script again to verify.${NC}" + return 1 + else + echo -e "${GREEN}โœ… ALL REQUIRED PORTS ARE AVAILABLE!${NC}" + echo -e "${GREEN}๐Ÿš€ Docker setup can proceed safely.${NC}" + return 0 + fi +} + +# Check if netcat is available +if ! command -v nc &> /dev/null; then + echo -e "${RED}โŒ Error: 'nc' (netcat) is required but not installed.${NC}" + echo "Please install netcat:" + echo " macOS: brew install netcat" + echo " Ubuntu/Debian: sudo apt-get install netcat" + echo " CentOS/RHEL: sudo yum install nc" + exit 1 +fi + +# Run the main function +main "$@" diff --git a/containers/check-sonarqube-ports.sh b/containers/check-sonarqube-ports.sh new file mode 100755 index 0000000..6a1a24b --- /dev/null +++ b/containers/check-sonarqube-ports.sh @@ -0,0 +1,143 @@ +#!/usr/bin/env bash + +# ============================================================================= +# SonarQube Port Availability Checker +# ============================================================================= + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# SonarQube ports (port:description format) +SONARQUBE_PORTS=( + "9000:SonarQube Web Interface" + "5432:PostgreSQL (SonarQube Database)" +) + +# Function to check if a port is available +check_port() { + local port=$1 + local description=$2 + + if nc -z localhost $port 2>/dev/null; then + echo -e "${RED}โŒ PORT $port ($description) is already in use${NC}" + return 1 + else + echo -e "${GREEN}โœ… PORT $port ($description) is available${NC}" + fi + return 0 +} + +# Function to suggest alternative ports for SonarQube +suggest_sonarqube_alternatives() { + local port=$1 + local description=$2 + + echo -e "${BLUE}๐Ÿ’ก Searching for alternative ports for $description...${NC}" + + case $port in + 9000) + # Suggest common alternative ports for web interfaces + local alternatives=(9001 9002 9003 9080 9090 8080 8090) + ;; + 5432) + # Suggest common alternative ports for PostgreSQL + local alternatives=(5433 5434 5435 15432 25432) + ;; + esac + + for alt_port in "${alternatives[@]}"; do + if ! nc -z localhost $alt_port 2>/dev/null; then + echo -e "${GREEN} Suggested alternative: $alt_port${NC}" + show_sonarqube_port_change_instructions "$port" "$description" "$alt_port" + return 0 + fi + done + + echo -e "${YELLOW} No common alternatives available. Try manually selecting a port.${NC}" + return 1 +} + +# Function to show SonarQube port change instructions +show_sonarqube_port_change_instructions() { + local port=$1 + local description=$2 + local suggested_port=$3 + + echo -e "${BLUE}๐Ÿ“ To change SonarQube port $port ($description) to $suggested_port:${NC}" + + case $port in + 9000) + echo " Update containers/docker-compose.sonarqube.yml:" + echo " ports: \"$suggested_port:9000\"" + echo " Also update SONAR_WEB_PORT environment variable if needed" + ;; + 5432) + echo " Update containers/docker-compose.sonarqube.yml:" + echo " ports: \"$suggested_port:5432\"" + ;; + esac + echo "" +} + +# Main function +main() { + echo -e "${BLUE}๐Ÿ” Checking SonarQube port availability...${NC}" + echo "" + + local unavailable_ports=() + local failed=false + + # Check SonarQube ports + for port_info in "${SONARQUBE_PORTS[@]}"; do + local port="${port_info%%:*}" + local description="${port_info#*:}" + if ! check_port "$port" "$description"; then + unavailable_ports+=("$port_info") + failed=true + fi + done + + echo "" + + # Summary and recommendations + if [ "$failed" = true ]; then + echo -e "${YELLOW}โš ๏ธ Some SonarQube ports are unavailable!${NC}" + echo "" + echo -e "${YELLOW}๐Ÿ”ง SOLUTIONS:${NC}" + echo "1. Stop the services using these ports" + echo "2. Use alternative ports (see suggestions below)" + echo "3. Skip SonarQube setup (it's optional)" + echo "" + + echo -e "${BLUE}๐Ÿ’ก PORT ALTERNATIVES:${NC}" + for port_info in "${unavailable_ports[@]}"; do + local port="${port_info%%:*}" + local description="${port_info#*:}" + suggest_sonarqube_alternatives "$port" "$description" + done + + echo -e "${YELLOW}๐Ÿ’ก NOTE: SonarQube is optional. You can continue without it.${NC}" + return 1 + else + echo -e "${GREEN}โœ… ALL SONARQUBE PORTS ARE AVAILABLE!${NC}" + echo -e "${GREEN}๐Ÿš€ SonarQube setup can proceed safely.${NC}" + return 0 + fi +} + +# Check if netcat is available +if ! command -v nc &> /dev/null; then + echo -e "${RED}โŒ Error: 'nc' (netcat) is required but not installed.${NC}" + echo "Please install netcat:" + echo " macOS: brew install netcat" + echo " Ubuntu/Debian: sudo apt-get install netcat" + echo " CentOS/RHEL: sudo yum install nc" + exit 1 +fi + +# Run the main function +main "$@"