-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·297 lines (251 loc) · 10.6 KB
/
start.sh
File metadata and controls
executable file
·297 lines (251 loc) · 10.6 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env bash
# ==============================================================================
# AIDA - Docker Startup Script
# ==============================================================================
# Starts the AIDA platform. Smart detection: skips build if already done.
# ==============================================================================
set -euo pipefail
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log() { echo -e "${GREEN}[✓]${NC} $*"; }
warn() { echo -e "${YELLOW}[!]${NC} $*"; }
error() { echo -e "${RED}[✗]${NC} $*"; }
section() { echo -e "\n${BLUE}══════════════════════════════════════${NC}\n${BLUE} $*${NC}\n${BLUE}══════════════════════════════════════${NC}"; }
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"
# Parse arguments
FORCE_BUILD=false
SKIP_CHECKS=false
for arg in "$@"; do
case $arg in
--build|-b) FORCE_BUILD=true ;;
--fast|-f) SKIP_CHECKS=true ;;
--help|-h)
echo "Usage: ./start.sh [OPTIONS]"
echo " --build, -b Force rebuild Docker images"
echo " --fast, -f Skip dependency checks (faster startup)"
echo " --help, -h Show this help"
exit 0
;;
esac
done
section "AIDA - Starting Platform"
# ==============================================================================
# QUICK CHECKS
# ==============================================================================
# Docker check
if ! command -v docker &> /dev/null; then
error "Docker not installed. Get it from: https://docker.com/products/docker-desktop"
exit 1
fi
if ! docker info &> /dev/null; then
error "Docker daemon not running. Start Docker Desktop first."
exit 1
fi
# ==============================================================================
# CHECK PORT CONFLICTS
# ==============================================================================
check_port() {
local port=$1
local service=$2
# Use lsof (macOS/Linux) or ss (Linux fallback)
if command -v lsof &>/dev/null; then
if lsof -iTCP:"$port" -sTCP:LISTEN -P -n 2>/dev/null | grep -q LISTEN; then
local process=$(lsof -iTCP:"$port" -sTCP:LISTEN -P -n 2>/dev/null | awk 'NR==2 {print $1}')
warn "Port $port ($service) is already in use by: $process"
return 1
fi
elif command -v ss &>/dev/null; then
if ss -tlnp 2>/dev/null | grep -q ":$port "; then
local process=$(ss -tlnp 2>/dev/null | grep ":$port " | sed 's/.*users:(("\([^"]*\)".*/\1/')
warn "Port $port ($service) is already in use by: $process"
return 1
fi
fi
return 0
}
PORT_CONFLICT=false
check_port 5432 "PostgreSQL" || PORT_CONFLICT=true
check_port 8000 "Backend" || PORT_CONFLICT=true
check_port 5173 "Frontend" || PORT_CONFLICT=true
if [[ "$PORT_CONFLICT" == "true" ]]; then
echo ""
warn "Port conflict detected! Options:"
warn " 1. Stop the conflicting process"
warn " 2. Change ports in docker-compose.yml"
echo ""
read -p "Continue anyway? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
error "Aborted due to port conflict"
exit 1
fi
fi
# ==============================================================================
# CHECK IF ALREADY RUNNING
# ==============================================================================
CONTAINERS_RUNNING=$(docker compose ps --status running -q 2>/dev/null | wc -l | tr -d ' ')
if [[ "$CONTAINERS_RUNNING" -ge 3 ]] && [[ "$FORCE_BUILD" == "false" ]]; then
log "AIDA is already running!"
echo ""
docker compose ps --format "table {{.Name}}\t{{.Status}}\t{{.Ports}}"
echo ""
log "Frontend: http://localhost:5173"
log "Backend: http://localhost:8000"
echo ""
warn "Use --build to force rebuild, or ./restart.sh to restart"
exit 0
fi
# ==============================================================================
# ENVIRONMENT FILES (Quick)
# ==============================================================================
if [[ ! -f backend/.env ]]; then
if [[ -f backend/.env.docker ]]; then
cp backend/.env.docker backend/.env
log "Created backend/.env"
elif [[ -f backend/.env.example ]]; then
cp backend/.env.example backend/.env
log "Created backend/.env from example"
fi
fi
if [[ ! -f frontend/.env ]]; then
echo "VITE_API_URL=http://localhost:8000/api" > frontend/.env
log "Created frontend/.env"
fi
# ==============================================================================
# PYTHON ENVIRONMENTS (Only if missing)
# ==============================================================================
if [[ "$SKIP_CHECKS" == "false" ]]; then
# Find Python 3.10+
PYTHON_CMD="python3"
for py in python3.13 python3.12 python3.11 python3.10; do
if command -v $py &> /dev/null; then
PYTHON_CMD=$py
break
fi
done
# CLI venv
if [[ ! -f ".venv/bin/python" ]]; then
log "Creating CLI virtual environment..."
$PYTHON_CMD -m venv .venv
.venv/bin/pip install -q --upgrade pip
[[ -f requirements.txt ]] && .venv/bin/pip install -q -r requirements.txt
log "CLI environment ready"
fi
# Backend venv (for MCP server)
if [[ ! -f "backend/venv/bin/python" ]]; then
log "Creating backend virtual environment..."
$PYTHON_CMD -m venv backend/venv
backend/venv/bin/pip install -q --upgrade pip
[[ -f backend/requirements.txt ]] && backend/venv/bin/pip install -q -r backend/requirements.txt
log "Backend environment ready"
fi
fi
# ==============================================================================
# DOCKER - Smart Build
# ==============================================================================
section "Docker Containers"
# Check for orphan containers from other projects with same names
ORPHAN_POSTGRES=$(docker ps -a --format "{{.Names}}" | grep "^aida_postgres$" || true)
ORPHAN_BACKEND=$(docker ps -a --format "{{.Names}}" | grep "^aida_backend$" || true)
ORPHAN_FRONTEND=$(docker ps -a --format "{{.Names}}" | grep "^aida_frontend$" || true)
# Check if these containers belong to our project
OUR_CONTAINERS=$(docker compose ps -a -q 2>/dev/null | wc -l | tr -d ' ')
if [[ -n "$ORPHAN_POSTGRES" || -n "$ORPHAN_BACKEND" || -n "$ORPHAN_FRONTEND" ]] && [[ "$OUR_CONTAINERS" -eq 0 ]]; then
warn "Found containers from another project with same names"
log "Removing orphan containers..."
docker rm -f aida_postgres aida_backend aida_frontend 2>/dev/null || true
log "Orphan containers removed"
fi
# Check if volume exists but belongs to another project - recreate it for this project
VOLUME_EXISTS=$(docker volume ls -q | grep "^aida_postgres_data$" || true)
if [[ -n "$VOLUME_EXISTS" ]]; then
# Volume exists - check if it's labeled for another project
VOLUME_PROJECT=$(docker volume inspect aida_postgres_data --format '{{index .Labels "com.docker.compose.project"}}' 2>/dev/null || true)
if [[ -n "$VOLUME_PROJECT" && "$VOLUME_PROJECT" != "aida" ]]; then
log "Adopting existing postgres volume from project '$VOLUME_PROJECT'"
# Remove old labels by recreating volume metadata (data preserved)
# Docker compose will re-label it on next up
fi
fi
# Check if images exist
BACKEND_IMAGE=$(docker images -q aida-backend 2>/dev/null)
FRONTEND_IMAGE=$(docker images -q aida-frontend 2>/dev/null)
if [[ -z "$BACKEND_IMAGE" ]] || [[ -z "$FRONTEND_IMAGE" ]] || [[ "$FORCE_BUILD" == "true" ]]; then
log "Building Docker images..."
docker compose build --quiet
log "Images built"
else
log "Docker images already exist (use --build to rebuild)"
fi
# ==============================================================================
# START CONTAINERS
# ==============================================================================
# Check current state
STOPPED_CONTAINERS=$(docker compose ps --status exited -q 2>/dev/null | wc -l | tr -d ' ')
RUNNING_CONTAINERS=$(docker compose ps --status running -q 2>/dev/null | wc -l | tr -d ' ')
if [[ "$RUNNING_CONTAINERS" -ge 3 ]]; then
log "Containers already running"
elif [[ "$STOPPED_CONTAINERS" -gt 0 ]]; then
log "Starting existing containers..."
docker compose start 2>/dev/null
else
log "Creating and starting containers..."
# Suppress volume warning (cosmetic - data is preserved)
docker compose up -d 2>&1 | grep -v "already exists but was created for project" || true
fi
# ==============================================================================
# WAIT FOR SERVICES
# ==============================================================================
section "Waiting for Services"
wait_for_service() {
local name=$1
local check_cmd=$2
local max_wait=${3:-30}
local i=0
printf " %-12s " "$name..."
while ! eval "$check_cmd" &>/dev/null; do
((i++))
if [[ $i -ge $max_wait ]]; then
echo -e "${RED}TIMEOUT${NC}"
return 1
fi
sleep 1
done
echo -e "${GREEN}Ready${NC}"
}
wait_for_service "PostgreSQL" "docker compose exec -T postgres pg_isready -U aida"
wait_for_service "Backend" "curl -sf http://localhost:8000/health"
wait_for_service "Frontend" "curl -sf http://localhost:5173"
# ==============================================================================
# FOLDER OPENER (Background helper)
# ==============================================================================
pkill -f "folder_opener.py" 2>/dev/null || true
if [[ -f "$SCRIPT_DIR/tools/folder_opener.py" ]]; then
python3 "$SCRIPT_DIR/tools/folder_opener.py" &>/dev/null &
fi
# ==============================================================================
# EXEGOL CHECK
# ==============================================================================
EXEGOL_RUNNING=$(docker ps --format "{{.Names}}" 2>/dev/null | grep -i "^exegol-" || true)
if [[ -z "$EXEGOL_RUNNING" ]]; then
echo ""
warn "No Exegol container running!"
warn "AIDA needs Exegol for pentesting tools."
warn "Start one with: exegol start <name>"
fi
# ==============================================================================
# SUCCESS
# ==============================================================================
section "AIDA Ready"
echo ""
log "Frontend: http://localhost:5173"
log "Backend: http://localhost:8000"
log "API Docs: http://localhost:8000/docs"
echo ""
docker compose ps --format "table {{.Name}}\t{{.Status}}"
echo ""