Files
biiproject-kit-v2/run.sh
T

170 lines
7.0 KiB
Bash
Executable File

#!/bin/bash
# --- Color Definitions for Premium Look ---
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
BOLD='\033[1m'
NC='\033[0m' # No Color
echo -e "${CYAN}======================================================${NC}"
echo -e "${GREEN}${BOLD} 🐳 Biiproject Kit v2 — Docker Runtime Engine 🐳 ${NC}"
echo -e "${CYAN}======================================================${NC}"
# 1. Pre-flight Checks
echo -e "${CYAN}[1/6] Checking system environment...${NC}"
if ! command -v docker &> /dev/null; then
echo -e " ${RED}❌ Error: Docker is not installed on this system.${NC}"
exit 1
fi
if ! docker info &> /dev/null; then
echo -e " ${RED}❌ Error: Docker daemon is not running.${NC}"
exit 1
fi
echo -e " ${GREEN}✔ Docker is installed and running perfectly.${NC}"
echo ""
# 2. Port & Container Conflicts Resolution (Self-Healing)
echo -e "${CYAN}[2/6] Preventing port and container name conflicts...${NC}"
# Resolve ports dynamically
PORT_8000=$(grep "^APP_PORT=" .env 2>/dev/null | cut -d'=' -f2- | tr -d '"'\''')
PORT_8000=${PORT_8000:-8000}
PORT_5432=$(grep -E "^(FORWARD_DB_PORT|DB_PORT)=" .env 2>/dev/null | head -n1 | cut -d'=' -f2- | tr -d '"'\''')
PORT_5432=${PORT_5432:-5432}
PORT_6379=$(grep -E "^(FORWARD_REDIS_PORT|REDIS_PORT)=" .env 2>/dev/null | head -n1 | cut -d'=' -f2- | tr -d '"'\''')
PORT_6379=${PORT_6379:-6379}
# Kill local system processes occupying these ports
if command -v lsof &>/dev/null && command -v fuser &>/dev/null; then
for port in "$PORT_8000" "$PORT_5432" "$PORT_6379"; do
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null ; then
echo -e " ${YELLOW}⚠ Port $port is occupied locally. Freeing up port $port...${NC}"
fuser -k $port/tcp 2>/dev/null
sleep 1
fi
done
fi
# Scan and remove conflicting stopped containers with same names
CONFLICT_CONTAINERS=("bii-kit-web" "bii-kit-pgsql" "bii-kit-redis")
for container in "${CONFLICT_CONTAINERS[@]}"; do
if [ ! -z "$(docker ps -a -q -f name=^/${container}$ 2>/dev/null)" ]; then
# Check if container is running
if [ ! -z "$(docker ps -q -f name=^/${container}$ 2>/dev/null)" ]; then
echo -ne " ${YELLOW}Stopping conflicting running container: $container...${NC}"
docker stop "$container" &>/dev/null
fi
echo -ne " ${YELLOW}Removing conflicting container $container to avoid duplication...${NC}"
docker rm -f "$container" &>/dev/null
echo -e "\r ${GREEN}✔ Cleared conflicting container: $container. ${NC}"
fi
done
echo -e " ${GREEN}✔ Environment conflicts resolved successfully.${NC}"
echo ""
# 3. Booting Docker Compose Stack
echo -e "${CYAN}[3/6] Starting application containers...${NC}"
docker compose up -d
if [ $? -ne 0 ]; then
echo -e " ${RED}❌ Error: Failed to start containers. Try running: docker compose down && docker compose up -d${NC}"
exit 1
fi
echo -e " ${GREEN}✔ Containers booted successfully.${NC}"
echo ""
# 4. Service Health Check & Permissions Correction
echo -e "${CYAN}[4/6] Waiting for database health & setting permissions...${NC}"
DB_CONTAINER="bii-kit-pgsql"
REDIS_CONTAINER="bii-kit-redis"
echo -ne " ${YELLOW}Checking PostgreSQL database ($DB_CONTAINER)...${NC}"
RETRIES=0
MAX_RETRIES=40
while [ $RETRIES -lt $MAX_RETRIES ]; do
STATUS=$(docker inspect --format='{{.State.Health.Status}}' "$DB_CONTAINER" 2>/dev/null)
if [ "$STATUS" = "healthy" ]; then
echo -e "\r ${GREEN}✔ PostgreSQL is healthy and ready to accept connections! ${NC}"
break
elif [ "$STATUS" = "unhealthy" ]; then
echo -e "\r ${RED}⚠ PostgreSQL health check failed. Proceeding... ${NC}"
break
elif [ -z "$STATUS" ]; then
RUNNING=$(docker inspect --format='{{.State.Running}}' "$DB_CONTAINER" 2>/dev/null)
if [ "$RUNNING" = "true" ]; then
echo -e "\r ${GREEN}✔ PostgreSQL is running (no healthcheck defined). ${NC}"
break
fi
fi
echo -n "."
sleep 1.5
RETRIES=$((RETRIES + 1))
done
echo -ne " ${YELLOW}Checking Redis cache ($REDIS_CONTAINER)...${NC}"
RETRIES=0
while [ $RETRIES -lt $MAX_RETRIES ]; do
STATUS=$(docker inspect --format='{{.State.Health.Status}}' "$REDIS_CONTAINER" 2>/dev/null)
if [ "$STATUS" = "healthy" ]; then
echo -e "\r ${GREEN}✔ Redis is healthy and ready! ${NC}"
break
elif [ -z "$STATUS" ]; then
RUNNING=$(docker inspect --format='{{.State.Running}}' "$REDIS_CONTAINER" 2>/dev/null)
if [ "$RUNNING" = "true" ]; then
echo -e "\r ${GREEN}✔ Redis is running. ${NC}"
break
fi
fi
echo -n "."
sleep 1.5
RETRIES=$((RETRIES + 1))
done
# Extra sleep for container initialization
sleep 2
# Writable directories permission fix
docker compose exec -u root laravel.test chown -R sail:sail /var/www/html/storage /var/www/html/bootstrap/cache 2>/dev/null
docker compose exec -u root laravel.test chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache 2>/dev/null
echo -e " ${GREEN}✔ Folder write permissions secured.${NC}"
echo ""
# 5. Database Migration Sync
echo -e "${CYAN}[5/6] Checking for pending database migrations...${NC}"
docker compose exec -u sail laravel.test php artisan migrate
echo ""
# 6. Booting Live-reload Dev Server
echo -e "${CYAN}[6/6] Launching React Vite dev server...${NC}"
# Stop any duplicate background dev servers inside container
docker compose exec -u sail laravel.test pkill -f "vite" &>/dev/null
# Start Vite server in detached mode
docker compose exec -d -u sail laravel.test npm run dev
echo -e " ${GREEN}✔ Vite live-reload dev server launched in background.${NC}"
echo ""
# Final Premium Screen Info
echo -e "${CYAN}=========================================================${NC}"
echo -e " 🎉 ${GREEN}${BOLD}Biiproject Kit v2 is running successfully!${NC} 🎉"
echo -e "${CYAN}=========================================================${NC}"
echo -e " 🚀 ${WHITE}${BOLD}Web Application:${NC} ${GREEN}http://localhost:${PORT_8000}${NC}"
echo -e " 📋 ${WHITE}${BOLD}Activity Telemetry:${NC} ${GREEN}http://localhost:${PORT_8000}/activity-logs${NC}"
echo -e " 📖 ${WHITE}${BOLD}Documentation Hub:${NC} ${GREEN}http://localhost:${PORT_8000}/documentation${NC}"
echo -e " 🐘 ${WHITE}${BOLD}Postgres Port:${NC} ${YELLOW}${PORT_5432}${NC}"
echo -e " 🍒 ${WHITE}${BOLD}Redis Port:${NC} ${YELLOW}${PORT_6379}${NC}"
echo -e "${CYAN}=========================================================${NC}"
echo -e " ${YELLOW}${BOLD}Useful Commands:${NC}"
echo -e " 👉 View live logs: ${CYAN}docker compose logs -f${NC}"
echo -e " 👉 Stop the stack: ${CYAN}docker compose down${NC}"
echo -e " 👉 Restart containers: ${CYAN}./run.sh${NC}"
echo -e "${CYAN}=========================================================${NC}"
echo ""