#!/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. The `--build` flag ensures
# the images are re-built from their respective Dockerfiles.
# The `--remove-orphans` flag ensures old service containers are cleaned up.
echo "๐๏ธ Building and starting new containers..."
sudo $DOCKER_CMD up -d --build --remove-orphans
echo "โ
Containers started! Checking status..."
sudo docker ps --filter "name=ai_"
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 system prune -f || true
echo "โจ Cleanup finished."