#!/bin/bash # Function to prompt installation of Homebrew install_homebrew() { echo "Homebrew is not installed. Please install it by running the following command:" echo '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"' exit 1 } # Function to install Ollama using Homebrew install_ollama() { echo "Ollama is not installed. Installing Ollama using Homebrew..." brew install ollama if [ $? -ne 0 ]; then echo "Failed to install Ollama via Homebrew. Please install it manually." exit 1 fi } # Function to terminate existing Ollama process terminate_ollama() { echo "Checking for existing Ollama instances..." EXISTING_PIDS=$(pgrep -f "ollama serve") if [ -n "$EXISTING_PIDS" ]; then echo "Found existing Ollama process(es) with PID(s): $EXISTING_PIDS" echo "Terminating existing Ollama process(es)..." kill $EXISTING_PIDS # Wait for the processes to terminate sleep 5 # Verify termination STILL_RUNNING=$(pgrep -f "ollama serve") if [ -n "$STILL_RUNNING" ]; then echo "Ollama did not terminate gracefully. Forcing termination..." kill -9 $STILL_RUNNING fi echo "Existing Ollama process(es) terminated." else echo "No existing Ollama instances found." fi } # Function to terminate existing Docker Compose services terminate_docker_compose() { echo "Checking for existing Docker Compose services..." # Check if docker-compose is running with the specified files if docker-compose -f ../docker-compose.yml -f ../docker-compose.dev.yml ps | grep -q "Up"; then echo "Existing Docker Compose services are running. Stopping them..." docker-compose -f ../docker-compose.yml -f ../docker-compose.dev.yml down echo "Docker Compose services stopped." else echo "No running Docker Compose services found." fi } # Function to install Homebrew if not present check_homebrew() { if ! command -v brew >/dev/null 2>&1; then install_homebrew fi } # Function to install Ollama if not present check_ollama() { if ! command -v ollama >/dev/null 2>&1; then install_ollama fi } # Function to start Ollama start_ollama() { echo "Starting Ollama natively..." ollama serve & ollama pull mistral OLLAMA_PID=$! echo "Ollama started with PID: $OLLAMA_PID" } # Function to wait for Ollama to be ready wait_for_ollama() { echo "Performing minimal health check to verify Ollama is ready..." MAX_RETRIES=30 RETRY_COUNT=0 SLEEP_INTERVAL=2 while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:11434/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "mistral", "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "Ping" } ] }' || true) if [ "$RESPONSE" -eq 200 ]; then echo "Ollama is up and responding." return 0 fi echo -n "." sleep $SLEEP_INTERVAL RETRY_COUNT=$((RETRY_COUNT + 1)) done echo "" echo "Ollama did not become ready in time." exit 1 } # Function to start Docker Compose services start_docker_compose() { echo "Starting Docker Compose services..." docker-compose -f ../docker-compose.yml -f ../docker-compose.dev.yml up --build & DOCKER_COMPOSE_PID=$! echo "Docker Compose started with PID: $DOCKER_COMPOSE_PID" } # Function to handle script termination cleanup() { echo "" echo "Script is terminating. Cleaning up..." if [ -n "$OLLAMA_PID" ]; then echo "Stopping Ollama (PID: $OLLAMA_PID)..." kill $OLLAMA_PID fi if [ -n "$DOCKER_COMPOSE_PID" ]; then echo "Stopping Docker Compose (PID: $DOCKER_COMPOSE_PID)..." kill $DOCKER_COMPOSE_PID fi # Optionally, terminate any remaining Ollama instances terminate_ollama echo "Cleanup complete. Exiting." exit } # Trap termination signals to execute cleanup trap cleanup SIGINT SIGTERM # Main Execution Flow # Step 1: Ensure Homebrew is installed check_homebrew # Step 2: Ensure Ollama is installed check_ollama # Step 3: Terminate any existing Ollama instances terminate_ollama # Step 4: Terminate any existing Docker Compose services # terminate_docker_compose # Step 5: Start Ollama start_ollama # Step 6: Wait for Ollama to be ready wait_for_ollama # Step 7: Start Docker Compose services # start_docker_compose # Step 8: Wait for Docker Compose to finish wait $DOCKER_COMPOSE_PID