#!/bin/bash set -e # Function to check internet connectivity check_internet() { if ! curl -s --connect-timeout 5 https://registry-1.docker.io/v2/ > /dev/null; then echo "โš ๏ธ No internet connection to Docker Hub - will use local images only" return 1 fi return 0 } # Function to pull image with retries pull_image() { local image=$1 local max_attempts=3 local attempt=1 local wait_time=5 while [ $attempt -le $max_attempts ]; do echo "๐Ÿ“ฅ Attempting to pull $image (Attempt $attempt/$max_attempts)..." if podman pull $image; then echo "โœ… Successfully pulled $image" return 0 fi echo "โš ๏ธ Pull attempt $attempt failed. Waiting before retry..." sleep $wait_time attempt=$((attempt + 1)) wait_time=$((wait_time * 2)) done return 1 } # Function to clean up while preserving base images cleanup_with_preserve() { echo "๐Ÿงน Cleaning up while preserving base images..." # Save the base image IDs if they exist local python_image_id=$(podman images -q python:3.12-slim) local postgres_image_id=$(podman images -q postgres:16-alpine) # Run system prune podman system prune -af # If we had base images, pull them again if [ ! -z "$python_image_id" ]; then echo "๐Ÿ”„ Restoring Python base image..." pull_image python:3.12-slim fi if [ ! -z "$postgres_image_id" ]; then echo "๐Ÿ”„ Restoring PostgreSQL base image..." pull_image postgres:16-alpine fi } echo "๐Ÿš€ Starting local deployment process..." # Check available disk space echo "๐Ÿ’พ Checking disk space..." AVAILABLE_SPACE=$(df -h . | awk 'NR==2 {print $4}' | tr -d 'G' | tr -d 'i') if [ "$AVAILABLE_SPACE" -lt 10 ]; then echo "โš ๏ธ Low disk space detected (${AVAILABLE_SPACE}G available). Cleaning up..." cleanup_with_preserve echo "โœ… Cleanup complete" fi # Check for required base images echo "๐Ÿ” Checking for required base images..." for image in "python:3.12-slim" "postgres:16-alpine"; do if podman images | grep -q "$image"; then echo "โœ… $image found locally" else echo "โš ๏ธ $image not found locally" if ! check_internet; then echo "โš ๏ธ No internet connection - attempting to build without base image" else echo "๐Ÿ“ฆ Pulling $image from Docker Hub..." if ! pull_image "$image"; then echo "โš ๏ธ Failed to pull $image - attempting to build without it" fi fi fi done # Create necessary directories and set permissions echo "๐Ÿ“ Setting up directories..." mkdir -p instance logs db_data db_backups chmod -R 777 instance logs db_data db_backups # Generate a secure secret key if not exists if [ ! -f .env ]; then echo "๐Ÿ”‘ Generating new secret key..." echo "SECRET_KEY=$(openssl rand -hex 32)" > .env echo "DATABASE_URL=postgresql://mold_user:mold_password@db:5432/mold_cost_calculator" >> .env fi # Clear template cache echo "๐Ÿงน Clearing template cache..." find . -type d -name "__pycache__" -exec rm -r {} + find . -type f -name "*.pyc" -delete # Stop and remove existing containers echo "๐Ÿงน Cleaning up existing containers..." podman-compose down 2>/dev/null || true # Build the containers with retry logic echo "๐Ÿ“ฆ Building containers..." max_build_attempts=3 build_attempt=1 while [ $build_attempt -le $max_build_attempts ]; do echo "๐Ÿ—๏ธ Build attempt $build_attempt/$max_build_attempts..." if podman-compose build; then echo "โœ… Build successful" break fi if [ $build_attempt -eq $max_build_attempts ]; then echo "โŒ Build failed after $max_build_attempts attempts" exit 1 fi echo "โš ๏ธ Build attempt $build_attempt failed. Waiting before retry..." sleep $((build_attempt * 5)) build_attempt=$((build_attempt + 1)) done # Start the containers echo "๐Ÿš€ Starting containers..." podman-compose up -d # Wait for database to be ready echo "โณ Waiting for database to be ready..." max_wait=30 wait_count=0 while [ $wait_count -lt $max_wait ]; do if podman exec mold_cost_db pg_isready -U mold_user -d mold_cost_calculator >/dev/null 2>&1; then echo "โœ… Database is ready" break fi echo "โณ Waiting for database... ($((wait_count + 1))/$max_wait)" sleep 1 wait_count=$((wait_count + 1)) done if [ $wait_count -eq $max_wait ]; then echo "โŒ Database failed to start within timeout" echo "๐Ÿ“ Checking container logs..." podman-compose logs db exit 1 fi # Check if the containers are running if podman-compose ps | grep -q "Up"; then echo "โœ… Deployment complete!" echo "๐ŸŒ Application is available at: http://localhost:5003" echo "๐Ÿ“ To view logs: podman-compose logs" echo "๐Ÿ“ To view app logs: podman-compose logs web" echo "๐Ÿ“ To view db logs: podman-compose logs db" echo "๐Ÿ›‘ To stop: podman-compose down" else echo "โŒ Containers failed to start. Check logs with: podman-compose logs" exit 1 fi