#!/bin/bash # --- Deployment Script for AI Hub --- # This script is designed to automate the deployment of the AI Hub application # using Docker Compose. It's intended to be run on the production server. # # The script performs the following actions: # 1. Defines project-specific configuration variables. # 2. **Installs Docker Compose if it's not found on the system.** # 3. Navigates to the correct project directory. # 4. Stops and removes any currently running Docker containers for the project. # 5. Pulls the latest Docker images from a registry (if applicable). # 6. Builds the new Docker images from the source code. # 7. Starts the new containers in detached mode, with production settings. # 8. Performs cleanup of old, unused Docker images. # --- Configuration --- # Set the absolute path to your project directory. PROJECT_DIR="/home/coder/project/cortex-hub" # Set the name of your production environment file (if you are using one). # If you are not using a separate env file, you can leave this variable empty. PROD_ENV_FILE="ai-hub/.env" # --- Script Execution --- echo "๐ Starting AI Hub deployment process..." # Check and install Docker Compose if not found. echo "๐ Checking for Docker Compose..." if ! command -v docker-compose &> /dev/null; then echo "โ ๏ธ Docker Compose not found. Installing..." sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose echo "โ Docker Compose installed." else echo "โ Docker Compose is already installed." fi # Navigate to the project directory. Exit if the directory doesn't exist. cd "$PROJECT_DIR" || { echo "Error: Project directory '$PROJECT_DIR' not found. Exiting."; exit 1; } # Check for the existence of the production environment file, if specified. if [ -n "$PROD_ENV_FILE" ] && [ ! -f "$PROD_ENV_FILE" ]; then echo "Warning: Production environment file '$PROD_ENV_FILE' not found." echo "The script will proceed, but some services may not have the correct environment variables." fi # Stop and remove any existing containers to ensure a clean deployment. echo "๐ Stopping and removing old Docker containers and networks..." docker-compose down || true # Pull the latest images if they are hosted on a registry. # This step is optional and can be commented out if you only build from source. # echo "๐ฅ Pulling latest Docker images..." # docker-compose pull # Build new images and start the services. The `--build` flag ensures # the images are re-built from their respective Dockerfiles. echo "๐๏ธ Building and starting new containers..." sudo docker-compose up -d --build echo "โ Deployment complete! The AI Hub application is now running." # --- Post-Deployment Cleanup --- echo "๐งน Cleaning up unused Docker resources..." # Remove dangling images (images without a tag). The '|| true' prevents the script # from exiting if there are no dangling images to remove. sudo docker system prune -f || true echo "โจ Cleanup finished."