File size: 4,668 Bytes
583e2d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/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