Newer
Older
cortex-hub / local_rebuild.sh
#!/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 project directory to the directory where this script is located.
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"


# --- Helper Function ---
# Find the correct docker-compose command (modern plugin or standalone v1)
if docker compose version &> /dev/null; then
    DOCKER_CMD="docker compose"
else
    DOCKER_CMD="docker-compose"
fi

# --- Script Execution ---
echo "๐Ÿš€ Starting AI Hub deployment process..."

# 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; }

# Stop and remove any existing containers to ensure a clean deployment.
echo "๐Ÿ›‘ Stopping and removing old Docker containers and networks..."
sudo $DOCKER_CMD down || true

# Pull the latest images if they are hosted on a registry.
# echo "๐Ÿ“ฅ Pulling latest Docker images..."
# sudo $DOCKER_CMD pull

# Build new images and start the services. 
echo "๐Ÿ—๏ธ Building and starting new containers..."

COMPOSE_FILES="-f docker-compose.yml"
if [ -f "docker-compose.test-nodes.yml" ]; then
    echo "๐Ÿ”— Including Internal Test Nodes in deployment..."
    COMPOSE_FILES="$COMPOSE_FILES -f docker-compose.test-nodes.yml"
fi

# We use --remove-orphans only if we are SURE we want to clean up everything not in these files.
sudo $DOCKER_CMD $COMPOSE_FILES up -d --build --remove-orphans

echo "โœ… Containers started! Checking status..."
sudo docker ps --filter "name=ai_"
sudo docker ps --filter "name=cortex-"

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).
sudo docker image prune -f || true

echo "โœจ Cleanup finished."