-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·528 lines (436 loc) · 14 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·528 lines (436 loc) · 14 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#!/bin/bash
# LLM-Runner-Router Autonomous Setup Script
# This script provides complete automated setup for the LLM Router system
set -euo pipefail
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOG_FILE="$SCRIPT_DIR/setup.log"
ERROR_LOG="$SCRIPT_DIR/setup-errors.log"
BASE_URL="${BASE_URL:-http://localhost:3006}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Logging functions
log() {
echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" | tee -a "$LOG_FILE"
}
success() {
echo -e "${GREEN}✅ $1${NC}" | tee -a "$LOG_FILE"
}
error() {
echo -e "${RED}❌ $1${NC}" | tee -a "$ERROR_LOG"
}
warning() {
echo -e "${YELLOW}⚠️ $1${NC}" | tee -a "$LOG_FILE"
}
# System detection
detect_system() {
log "Detecting system environment..."
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
OS="linux"
if command -v apt-get &> /dev/null; then
DISTRO="debian"
elif command -v yum &> /dev/null; then
DISTRO="rhel"
else
DISTRO="unknown"
fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
OS="macos"
DISTRO="macos"
else
OS="unknown"
DISTRO="unknown"
fi
# Detect if running on VPS
if [[ -f "/sys/hypervisor/uuid" ]] || [[ -d "/proc/vz" ]] || [[ -f "/.dockerenv" ]]; then
ENVIRONMENT="vps"
else
ENVIRONMENT="local"
fi
log "System: $OS ($DISTRO), Environment: $ENVIRONMENT"
}
# Check prerequisites
check_prerequisites() {
log "Checking system prerequisites..."
local missing_deps=()
# Check Node.js
if ! command -v node &> /dev/null; then
missing_deps+=("nodejs")
else
NODE_VERSION=$(node --version | cut -d'v' -f2)
if [[ $(echo "$NODE_VERSION 20.0.0" | tr " " "\n" | sort -V | head -n1) != "20.0.0" ]]; then
warning "Node.js version $NODE_VERSION detected. Node.js 20.0.0 or higher is required"
missing_deps+=("nodejs-update")
else
success "Node.js $NODE_VERSION is compatible"
fi
fi
# Check npm
if ! command -v npm &> /dev/null; then
missing_deps+=("npm")
fi
# Check git
if ! command -v git &> /dev/null; then
missing_deps+=("git")
fi
# Check Python (for bindings)
if ! command -v python3 &> /dev/null; then
warning "Python3 not found - some bindings may not work"
fi
# Check Rust (for native bindings)
if ! command -v cargo &> /dev/null; then
warning "Rust/Cargo not found - native bindings will not be available"
fi
if [[ ${#missing_deps[@]} -gt 0 ]]; then
error "Missing dependencies: ${missing_deps[*]}"
log "Installing missing dependencies..."
install_dependencies "${missing_deps[@]}"
else
success "All prerequisites satisfied"
fi
}
# Install missing dependencies
install_dependencies() {
local deps=("$@")
for dep in "${deps[@]}"; do
case $dep in
"nodejs"|"nodejs-update")
install_nodejs
;;
"npm")
log "npm typically comes with Node.js"
;;
"git")
install_git
;;
esac
done
}
install_nodejs() {
log "Installing Node.js..."
if [[ "$DISTRO" == "debian" ]]; then
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
elif [[ "$DISTRO" == "rhel" ]]; then
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
sudo yum install -y nodejs npm
elif [[ "$OS" == "macos" ]]; then
if command -v brew &> /dev/null; then
brew install node
else
error "Please install Homebrew first: https://brew.sh"
exit 1
fi
else
error "Cannot auto-install Node.js on this system. Please install manually."
exit 1
fi
success "Node.js installed"
}
install_git() {
log "Installing Git..."
if [[ "$DISTRO" == "debian" ]]; then
sudo apt-get update && sudo apt-get install -y git
elif [[ "$DISTRO" == "rhel" ]]; then
sudo yum install -y git
elif [[ "$OS" == "macos" ]]; then
if command -v brew &> /dev/null; then
brew install git
else
log "Git should be available via Xcode Command Line Tools"
fi
fi
success "Git installed"
}
# Setup environment
setup_environment() {
log "Setting up environment configuration..."
# Copy environment files if they don't exist
if [[ ! -f "$SCRIPT_DIR/.env" ]]; then
if [[ "$ENVIRONMENT" == "vps" ]]; then
cp "$SCRIPT_DIR/.env.vps" "$SCRIPT_DIR/.env" 2>/dev/null || cp "$SCRIPT_DIR/.env.example" "$SCRIPT_DIR/.env"
else
cp "$SCRIPT_DIR/.env.example" "$SCRIPT_DIR/.env"
fi
log "Created .env file from template"
else
log "Using existing .env file"
fi
# Generate secure secrets if needed
if grep -q "your-session-secret-here" "$SCRIPT_DIR/.env"; then
if command -v openssl &> /dev/null; then
SESSION_SECRET=$(openssl rand -hex 32)
sed -i "s/your-session-secret-here/$SESSION_SECRET/" "$SCRIPT_DIR/.env"
log "Generated secure session secret"
else
warning "OpenSSL not available - please manually set SESSION_SECRET in .env"
fi
fi
success "Environment configuration ready"
}
# Install Node.js dependencies
install_dependencies_npm() {
log "Installing Node.js dependencies..."
cd "$SCRIPT_DIR"
# Clean install
if [[ -d "node_modules" ]]; then
log "Cleaning existing node_modules..."
rm -rf node_modules package-lock.json
fi
# Install with optimization for VPS
if [[ "$ENVIRONMENT" == "vps" ]]; then
log "Optimizing installation for VPS environment..."
npm ci --only=production --no-optional --no-audit 2>/dev/null || npm install --only=production --no-optional --no-audit
else
npm install
fi
success "Dependencies installed"
}
# Setup bindings (optional)
setup_bindings() {
log "Setting up language bindings..."
cd "$SCRIPT_DIR"
# Python bindings
if command -v python3 &> /dev/null && [[ -d "bindings/python" ]]; then
log "Setting up Python bindings..."
cd "bindings/python"
if command -v pip3 &> /dev/null; then
pip3 install -e . --user 2>/dev/null || warning "Python bindings setup failed"
fi
cd "$SCRIPT_DIR"
fi
# Rust bindings
if command -v cargo &> /dev/null && [[ -d "bindings/rust" ]]; then
log "Setting up Rust bindings..."
cd "bindings/rust"
cargo build --release 2>/dev/null || warning "Rust bindings build failed"
cd "$SCRIPT_DIR"
fi
# WASM bindings
if command -v wasm-pack &> /dev/null && [[ -d "bindings/wasm" ]]; then
log "Setting up WASM bindings..."
cd "bindings/wasm"
wasm-pack build --target web 2>/dev/null || warning "WASM bindings build failed"
cd "$SCRIPT_DIR"
fi
success "Bindings setup completed"
}
# Build project
build_project() {
log "Building project..."
cd "$SCRIPT_DIR"
# Run linting and fix what we can
npm run lint 2>/dev/null || warning "Linting completed with warnings"
# Build documentation
npm run docs 2>/dev/null || warning "Documentation build had issues"
# Build distribution
npm run build
success "Project built successfully"
}
# Run tests
run_tests() {
log "Running tests..."
cd "$SCRIPT_DIR"
# Run basic tests first
if npm run test:unit 2>/dev/null; then
success "Unit tests passed"
else
warning "Some unit tests failed - check logs"
fi
# Skip integration tests on VPS to avoid resource issues
if [[ "$ENVIRONMENT" != "vps" ]]; then
if npm run test:integration 2>/dev/null; then
success "Integration tests passed"
else
warning "Some integration tests failed - check logs"
fi
else
log "Skipping integration tests on VPS"
fi
}
# Setup systemd service (Linux only)
setup_service() {
if [[ "$OS" == "linux" ]] && [[ "$ENVIRONMENT" == "vps" ]]; then
log "Setting up systemd service..."
cat > /tmp/llm-router.service << EOF
[Unit]
Description=LLM Runner Router
After=network.target
[Service]
Type=simple
User=$USER
WorkingDirectory=$SCRIPT_DIR
ExecStart=$(which node) server.js
Restart=always
RestartSec=10
Environment=NODE_ENV=production
Environment=PATH=$(dirname $(which node)):/usr/bin:/bin
[Install]
WantedBy=multi-user.target
EOF
if sudo cp /tmp/llm-router.service /etc/systemd/system/ 2>/dev/null; then
sudo systemctl daemon-reload
sudo systemctl enable llm-router
success "Systemd service installed"
log "Use 'sudo systemctl start llm-router' to start the service"
else
warning "Could not install systemd service (insufficient permissions)"
fi
rm -f /tmp/llm-router.service
fi
}
# Setup monitoring and health checks
setup_monitoring() {
log "Setting up monitoring and health checks..."
# Create monitoring script
cat > "$SCRIPT_DIR/scripts/health-check.sh" << 'EOF'
#!/bin/bash
# Health check script for LLM Router
BASE_URL="${BASE_URL:-http://localhost:3006}"
HEALTH_URL="$BASE_URL/api/health"
MAX_RETRIES=3
RETRY_DELAY=5
for i in $(seq 1 $MAX_RETRIES); do
if curl -f -s "$HEALTH_URL" > /dev/null 2>&1; then
echo "✅ LLM Router is healthy"
exit 0
else
echo "⚠️ Health check failed (attempt $i/$MAX_RETRIES)"
if [[ $i -lt $MAX_RETRIES ]]; then
sleep $RETRY_DELAY
fi
fi
done
echo "❌ LLM Router health check failed after $MAX_RETRIES attempts"
exit 1
EOF
chmod +x "$SCRIPT_DIR/scripts/health-check.sh"
# Create startup script
cat > "$SCRIPT_DIR/start.sh" << 'EOF'
#!/bin/bash
# Startup script with health monitoring
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PID_FILE="$SCRIPT_DIR/llm-router.pid"
cd "$SCRIPT_DIR"
# Check if already running
if [[ -f "$PID_FILE" ]]; then
PID=$(cat "$PID_FILE")
if ps -p "$PID" > /dev/null 2>&1; then
echo "LLM Router is already running (PID: $PID)"
exit 1
else
rm -f "$PID_FILE"
fi
fi
# Start the server
echo "Starting LLM Router..."
nohup node server.js > server.log 2>&1 &
PID=$!
echo $PID > "$PID_FILE"
# Wait for startup
sleep 5
# Health check
if bash "$SCRIPT_DIR/scripts/health-check.sh"; then
echo "LLM Router started successfully (PID: $PID)"
echo "Logs: tail -f $SCRIPT_DIR/server.log"
echo "Stop: bash $SCRIPT_DIR/stop.sh"
else
echo "LLM Router failed to start properly"
kill $PID 2>/dev/null || true
rm -f "$PID_FILE"
exit 1
fi
EOF
chmod +x "$SCRIPT_DIR/start.sh"
# Create stop script
cat > "$SCRIPT_DIR/stop.sh" << 'EOF'
#!/bin/bash
# Stop script for LLM Router
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PID_FILE="$SCRIPT_DIR/llm-router.pid"
if [[ -f "$PID_FILE" ]]; then
PID=$(cat "$PID_FILE")
if ps -p "$PID" > /dev/null 2>&1; then
echo "Stopping LLM Router (PID: $PID)..."
kill $PID
sleep 2
if ps -p "$PID" > /dev/null 2>&1; then
echo "Force stopping..."
kill -9 $PID
fi
rm -f "$PID_FILE"
echo "LLM Router stopped"
else
echo "LLM Router is not running"
rm -f "$PID_FILE"
fi
else
echo "No PID file found"
fi
EOF
chmod +x "$SCRIPT_DIR/stop.sh"
success "Monitoring and control scripts created"
}
# Performance optimization for VPS
optimize_for_vps() {
if [[ "$ENVIRONMENT" == "vps" ]]; then
log "Applying VPS-specific optimizations..."
# Update .env with VPS-optimized settings
sed -i 's/LLM_MAX_THREADS=.*/LLM_MAX_THREADS=2/' "$SCRIPT_DIR/.env"
sed -i 's/LLM_CONTEXT_SIZE=.*/LLM_CONTEXT_SIZE=1024/' "$SCRIPT_DIR/.env"
sed -i 's/LLM_BATCH_SIZE=.*/LLM_BATCH_SIZE=4/' "$SCRIPT_DIR/.env"
sed -i 's/MAX_CONCURRENT=.*/MAX_CONCURRENT=5/' "$SCRIPT_DIR/.env"
# Set production environment
sed -i 's/NODE_ENV=.*/NODE_ENV=production/' "$SCRIPT_DIR/.env"
success "VPS optimizations applied"
fi
}
# Main setup function
main() {
log "🚀 Starting LLM-Runner-Router autonomous setup..."
# Initialize log files
: > "$LOG_FILE"
: > "$ERROR_LOG"
detect_system
check_prerequisites
setup_environment
install_dependencies_npm
setup_bindings
build_project
run_tests
setup_service
setup_monitoring
optimize_for_vps
success "🎉 Setup completed successfully!"
echo ""
log "📋 Setup Summary:"
log " • System: $OS ($DISTRO)"
log " • Environment: $ENVIRONMENT"
log " • Log file: $LOG_FILE"
log " • Configuration: .env"
echo ""
log "🚀 Quick Start:"
log " • Development: npm run dev"
log " • Production: bash start.sh"
log " • Health check: bash scripts/health-check.sh"
log " • Stop server: bash stop.sh"
if [[ "$OS" == "linux" ]] && [[ "$ENVIRONMENT" == "vps" ]]; then
echo ""
log "📡 Service Management:"
log " • Start service: sudo systemctl start llm-router"
log " • Stop service: sudo systemctl stop llm-router"
log " • Enable auto-start: sudo systemctl enable llm-router"
log " • View logs: sudo journalctl -u llm-router -f"
fi
echo ""
log "📚 Documentation: $BASE_URL/docs.html"
log "🔗 Repository: https://github.com/MCERQUA/LLM-Runner-Router"
}
# Handle script interruption
trap 'error "Setup interrupted"; exit 1' INT TERM
# Run main function
main "$@"