feat: add unified install.sh and enhance self-healing in run.sh
This commit is contained in:
@@ -6,40 +6,32 @@ BLUE='\033[0;34m'
|
||||
YELLOW='\033[1;33m'
|
||||
RED='\033[0;31m'
|
||||
CYAN='\033[0;36m'
|
||||
MAGENTA='\033[0;35m'
|
||||
WHITE='\033[1;37m'
|
||||
BOLD='\033[1m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
echo -e "${CYAN}======================================================${NC}"
|
||||
echo -e "${GREEN}${BOLD} 🐳 Biiproject Kit — Fully Containerized Setup 🐳 ${NC}"
|
||||
echo -e "${GREEN}${BOLD} 🐳 Biiproject Kit v2 — Docker Runtime Engine 🐳 ${NC}"
|
||||
echo -e "${CYAN}======================================================${NC}"
|
||||
|
||||
# 1. Pre-flight Checks (Docker & Daemon status)
|
||||
echo -e "${CYAN}[1/8] Checking Docker & system environment...${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}"
|
||||
echo -e " ${YELLOW}👉 Please install Docker and Docker Compose before running this script.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! docker info &> /dev/null; then
|
||||
echo -e " ${RED}❌ Error: Docker daemon is not running.${NC}"
|
||||
echo -e " ${YELLOW}👉 Please start your Docker service/desktop and try again.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
echo -e " ${GREEN}✔ Docker is installed and running perfectly.${NC}"
|
||||
echo ""
|
||||
|
||||
# 2. Prevent Port & Container conflicts
|
||||
echo -e "${CYAN}[2/8] Checking for port conflicts...${NC}"
|
||||
# 2. Port & Container Conflicts Resolution (Self-Healing)
|
||||
echo -e "${CYAN}[2/6] Preventing port and container name conflicts...${NC}"
|
||||
|
||||
# Ensure .env is present to parse ports
|
||||
if [ ! -f .env ] && [ -f .env.example ]; then
|
||||
cp .env.example .env
|
||||
fi
|
||||
|
||||
# Resolve ports dynamically from .env or defaults
|
||||
# Resolve ports dynamically
|
||||
PORT_8000=$(grep "^APP_PORT=" .env 2>/dev/null | cut -d'=' -f2- | tr -d '"'\''')
|
||||
PORT_8000=${PORT_8000:-8000}
|
||||
|
||||
@@ -49,111 +41,49 @@ 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}
|
||||
|
||||
# Stop local processes using these ports
|
||||
# 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 by a local process. Releasing it...${NC}"
|
||||
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
|
||||
|
||||
# Find the container names from our own docker-compose.yml to avoid stopping ourselves
|
||||
MY_CONTAINERS=$(grep "container_name:" docker-compose.yml 2>/dev/null | awk '{print $2}' | tr -d '"'\''')
|
||||
|
||||
stop_conflict_on_port() {
|
||||
local port=$1
|
||||
local port_type=$2
|
||||
|
||||
local conflicting_container=$(docker ps --filter "publish=$port" --format "{{.Names}}" 2>/dev/null)
|
||||
if [ ! -z "$conflicting_container" ]; then
|
||||
local is_ours=false
|
||||
for ours in $MY_CONTAINERS; do
|
||||
if [ "$conflicting_container" = "$ours" ]; then
|
||||
is_ours=true
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [ "$is_ours" = "false" ]; then
|
||||
echo -e "${YELLOW}⚠ Port $port ($port_type) is occupied by container '$conflicting_container'. Stopping conflict...${NC}"
|
||||
docker stop "$conflicting_container" &>/dev/null
|
||||
# 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
|
||||
}
|
||||
|
||||
stop_conflict_on_port "$PORT_8000" "Web Server"
|
||||
stop_conflict_on_port "$PORT_5432" "PostgreSQL Database"
|
||||
stop_conflict_on_port "$PORT_6379" "Redis Cache"
|
||||
echo -e " ${GREEN}✔ Ports $PORT_8000 (Web), $PORT_5432 (Postgres), and $PORT_6379 (Redis) are clear and ready.${NC}"
|
||||
done
|
||||
echo -e " ${GREEN}✔ Environment conflicts resolved successfully.${NC}"
|
||||
echo ""
|
||||
|
||||
# 3. Environment File (.env) check
|
||||
echo -e "${CYAN}[3/8] Verifying configuration environment (.env)...${NC}"
|
||||
if [ ! -f .env ]; then
|
||||
echo -e " ${YELLOW}⚠ .env file not found. Creating from template (.env.example)...${NC}"
|
||||
cp .env.example .env
|
||||
ENV_CREATED=true
|
||||
else
|
||||
echo -e " ${GREEN}✔ Environment configuration file (.env) is present.${NC}"
|
||||
ENV_CREATED=false
|
||||
fi
|
||||
echo ""
|
||||
|
||||
# 4. Install Composer dependencies (Zero-Dependency Host)
|
||||
echo -e "${CYAN}[4/8] Checking backend dependencies (Composer)...${NC}"
|
||||
if [ ! -d vendor ]; then
|
||||
echo -e " ${YELLOW}⚠ Vendor directory not found. Installing via lightweight PHP container...${NC}"
|
||||
docker run --rm \
|
||||
-u "$(id -u):$(id -g)" \
|
||||
-v "$(pwd):/var/www/html" \
|
||||
-w /var/www/html \
|
||||
laravelsail/php83-composer:latest \
|
||||
composer install --ignore-platform-reqs
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e " ${RED}❌ Error: Failed to install composer dependencies via Docker.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
echo -e " ${GREEN}✔ Composer dependencies successfully installed.${NC}"
|
||||
else
|
||||
echo -e " ${GREEN}✔ Composer dependencies are up to date.${NC}"
|
||||
fi
|
||||
echo ""
|
||||
|
||||
# 5. Start Docker Compose stack
|
||||
echo -e "${CYAN}[5/8] Booting up Docker Compose containers...${NC}"
|
||||
# 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 Docker containers. Please check docker-compose.yml.${NC}"
|
||||
echo -e " ${RED}❌ Error: Failed to start containers. Try running: docker compose down && docker compose up -d${NC}"
|
||||
exit 1
|
||||
fi
|
||||
echo -e " ${GREEN}✔ Docker Compose services booted successfully.${NC}"
|
||||
echo -e " ${GREEN}✔ Containers booted successfully.${NC}"
|
||||
echo ""
|
||||
|
||||
# Resolve DB container dynamically
|
||||
DB_CONTAINER=$(docker compose ps --format "{{.Name}}" pgsql 2>/dev/null)
|
||||
if [ -z "$DB_CONTAINER" ]; then
|
||||
DB_CONTAINER=$(grep -A 10 -E "^ pgsql:" docker-compose.yml 2>/dev/null | grep "container_name:" | head -n1 | awk '{print $2}' | tr -d '"'\''')
|
||||
fi
|
||||
if [ -z "$DB_CONTAINER" ]; then
|
||||
DB_CONTAINER="bii-kit-pgsql"
|
||||
fi
|
||||
|
||||
# Resolve Redis container dynamically
|
||||
REDIS_CONTAINER=$(docker compose ps --format "{{.Name}}" redis 2>/dev/null)
|
||||
if [ -z "$REDIS_CONTAINER" ]; then
|
||||
REDIS_CONTAINER=$(grep -A 10 -E "^ redis:" docker-compose.yml 2>/dev/null | grep "container_name:" | head -n1 | awk '{print $2}' | tr -d '"'\''')
|
||||
fi
|
||||
if [ -z "$REDIS_CONTAINER" ]; then
|
||||
REDIS_CONTAINER="bii-kit-redis"
|
||||
fi
|
||||
|
||||
# 6. Wait for Databases to be ready (Self-Healing)
|
||||
echo -e "${CYAN}[6/8] Waiting for services to be ready & configuring permissions...${NC}"
|
||||
# 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
|
||||
@@ -164,13 +94,12 @@ while [ $RETRIES -lt $MAX_RETRIES ]; do
|
||||
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 reported unhealthy. Proceeding anyway...${NC}"
|
||||
echo -e "\r ${RED}⚠ PostgreSQL health check failed. Proceeding... ${NC}"
|
||||
break
|
||||
elif [ -z "$STATUS" ]; then
|
||||
# Check if container is at least running
|
||||
RUNNING=$(docker inspect --format='{{.State.Running}}' "$DB_CONTAINER" 2>/dev/null)
|
||||
if [ "$RUNNING" = "true" ]; then
|
||||
echo -e "\r ${GREEN}✔ PostgreSQL container is running (no healthcheck status). ${NC}"
|
||||
echo -e "\r ${GREEN}✔ PostgreSQL is running (no healthcheck defined). ${NC}"
|
||||
break
|
||||
fi
|
||||
fi
|
||||
@@ -184,16 +113,12 @@ 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 to accept connections! ${NC}"
|
||||
break
|
||||
elif [ "$STATUS" = "unhealthy" ]; then
|
||||
echo -e "\r ${RED}⚠ Redis health check reported unhealthy. Proceeding anyway...${NC}"
|
||||
echo -e "\r ${GREEN}✔ Redis is healthy and ready! ${NC}"
|
||||
break
|
||||
elif [ -z "$STATUS" ]; then
|
||||
# Check if container is at least running
|
||||
RUNNING=$(docker inspect --format='{{.State.Running}}' "$REDIS_CONTAINER" 2>/dev/null)
|
||||
if [ "$RUNNING" = "true" ]; then
|
||||
echo -e "\r ${GREEN}✔ Redis container is running (no healthcheck status). ${NC}"
|
||||
echo -e "\r ${GREEN}✔ Redis is running. ${NC}"
|
||||
break
|
||||
fi
|
||||
fi
|
||||
@@ -202,87 +127,43 @@ while [ $RETRIES -lt $MAX_RETRIES ]; do
|
||||
RETRIES=$((RETRIES + 1))
|
||||
done
|
||||
|
||||
# Extra sleep to ensure Docker internal DNS has fully propagated the container names
|
||||
# Extra sleep for container initialization
|
||||
sleep 2
|
||||
|
||||
# Correct any permission issues on build and storage folders before continuing
|
||||
echo -ne " ${YELLOW}Securing workspace file permissions...${NC}"
|
||||
# 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
|
||||
if [ -d public/build ]; then
|
||||
docker compose exec -u root laravel.test chown -R sail:sail /var/www/html/public/build 2>/dev/null
|
||||
docker compose exec -u root laravel.test chmod -R 775 /var/www/html/public/build 2>/dev/null
|
||||
fi
|
||||
echo -e "\r ${GREEN}✔ Workspace file permissions secured. ${NC}"
|
||||
echo -e " ${GREEN}✔ Folder write permissions secured.${NC}"
|
||||
echo ""
|
||||
|
||||
# 7. Securing Application & Database Initialization
|
||||
echo -e "${CYAN}[7/8] Securely initializing application database & keys...${NC}"
|
||||
|
||||
# Check for empty key
|
||||
APP_KEY_VAL=$(grep "^APP_KEY=" .env | cut -d'=' -f2- | tr -d '"'\''')
|
||||
if [ -z "$APP_KEY_VAL" ] || [ "$APP_KEY_VAL" = "base64:" ] || [ "$APP_KEY_VAL" = "SomeRandomString" ]; then
|
||||
echo -e " ${YELLOW}⚙ Generating unique application encryption key...${NC}"
|
||||
docker compose exec -u sail laravel.test php artisan key:generate
|
||||
fi
|
||||
|
||||
# Check if tables exist to decide fresh migrate & seed or regular migrate
|
||||
TABLE_EXISTS=$(docker compose exec -u sail laravel.test php artisan tinker --execute="echo Schema::hasTable('users') ? 'yes' : 'no';" 2>/dev/null | tr -d '\r\n')
|
||||
|
||||
if [ "$TABLE_EXISTS" != "yes" ]; then
|
||||
echo -e " ${YELLOW}⚙ Database is uninitialized. Running migrations & seeding default accounts...${NC}"
|
||||
docker compose exec -u sail laravel.test php artisan migrate:fresh --seed
|
||||
else
|
||||
USER_COUNT=$(docker compose exec -u sail laravel.test php artisan tinker --execute="echo App\Models\User::count();" 2>/dev/null | tr -d '\r\n')
|
||||
if [[ -z "$USER_COUNT" || "$USER_COUNT" == "0" ]]; then
|
||||
echo -e " ${YELLOW}⚙ Database has tables but no records. Seeding default accounts...${NC}"
|
||||
docker compose exec -u sail laravel.test php artisan db:seed
|
||||
else
|
||||
echo -e " ${GREEN}✔ Database already initialized with $USER_COUNT users. Running pending migrations...${NC}"
|
||||
docker compose exec -u sail laravel.test php artisan migrate
|
||||
fi
|
||||
fi
|
||||
# 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 ""
|
||||
|
||||
# 8. Frontend Assets (NPM dependencies and dev server)
|
||||
echo -e "${CYAN}[8/8] Preparing frontend assets & live-reload server...${NC}"
|
||||
if [ ! -d node_modules ]; then
|
||||
echo -e " ${YELLOW}⚙ Node modules not found. Installing NPM packages inside container...${NC}"
|
||||
docker compose exec -u sail laravel.test npm install
|
||||
else
|
||||
echo -e " ${GREEN}✔ Node dependencies are already present.${NC}"
|
||||
fi
|
||||
# 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
|
||||
|
||||
# Ensure storage symlink exists
|
||||
docker compose exec -u sail laravel.test php artisan storage:link &>/dev/null
|
||||
|
||||
# Clean up permissions again in case npm install created any root-owned folders
|
||||
docker compose exec -u root laravel.test chown -R sail:sail /var/www/html/node_modules /var/www/html/package-lock.json 2>/dev/null
|
||||
|
||||
# Build production assets first to ensure page loads immediately
|
||||
echo -e " ${YELLOW}⚙ Compiling frontend production assets (Vite)...${NC}"
|
||||
docker compose exec -u sail laravel.test npm run build
|
||||
|
||||
# Start Vite live-reload server in background
|
||||
echo -e " ${GREEN}✔ Production assets compiled. Launching Vite live-reload server in background...${NC}"
|
||||
# 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 ""
|
||||
|
||||
# Retrieve ports for display
|
||||
PORT_8000=$(grep "^APP_PORT=" .env | cut -d'=' -f2- | tr -d '"'\''')
|
||||
PORT_8000=${PORT_8000:-8000}
|
||||
|
||||
# Final Premium Screen Info
|
||||
echo -e "${CYAN}=========================================================${NC}"
|
||||
echo -e " 🎉 ${GREEN}${BOLD}Biiproject Kit is running fully in Docker!${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}Database Port (Postgres):${NC} ${YELLOW}5432${NC}"
|
||||
echo -e " 🍒 ${WHITE}${BOLD}Redis Port (Cache):${NC} ${YELLOW}6379${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 ""
|
||||
|
||||
Reference in New Issue
Block a user