-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncCode-local.sh
More file actions
524 lines (438 loc) · 13.1 KB
/
Copy pathSyncCode-local.sh
File metadata and controls
524 lines (438 loc) · 13.1 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
#!/bin/bash
# ============================================
# SyncCode - Complete Local Production Setup
# ============================================
# This script sets up everything you need
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Banner
echo ""
echo -e "${CYAN}============================================${NC}"
echo -e "${CYAN} SyncCode - Local Production Setup${NC}"
echo -e "${CYAN} Complete Stack with All Features${NC}"
echo -e "${CYAN}============================================${NC}"
echo ""
# ============================================
# Prerequisites Check
# ============================================
echo -e "${BLUE}📋 Checking prerequisites...${NC}"
# Check Node.js
if ! command -v node &> /dev/null; then
echo -e "${RED}❌ Node.js not found${NC}"
echo -e "${YELLOW} Install from: https://nodejs.org/${NC}"
exit 1
fi
echo -e "${GREEN}✓ Node.js $(node --version)${NC}"
# Check npm
if ! command -v npm &> /dev/null; then
echo -e "${RED}❌ npm not found${NC}"
exit 1
fi
echo -e "${GREEN}✓ npm $(npm --version)${NC}"
# Check Docker
if ! command -v docker &> /dev/null; then
echo -e "${RED}❌ Docker not found${NC}"
echo -e "${YELLOW} Install from: https://docs.docker.com/get-docker/${NC}"
exit 1
fi
echo -e "${GREEN}✓ Docker $(docker --version | cut -d' ' -f3 | tr -d ',')${NC}"
# Check Docker Compose
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
echo -e "${RED}❌ Docker Compose not found${NC}"
exit 1
fi
echo -e "${GREEN}✓ Docker Compose${NC}"
echo ""
# ============================================
# Directory Structure
# ============================================
echo -e "${BLUE}📁 Creating directory structure...${NC}"
mkdir -p synccode_backend/logs
mkdir -p synccode-executor/logs
mkdir -p synccode-executor/temp
mkdir -p monitoring/grafana/provisioning/{dashboards,datasources}
mkdir -p nginx
echo -e "${GREEN}✓ Directories created${NC}"
echo ""
# ============================================
# Environment Configuration
# ============================================
echo -e "${BLUE}⚙️ Creating environment configuration...${NC}"
if [ ! -f .env ]; then
# Generate JWT secret
JWT_SECRET=$(openssl rand -base64 32 2>/dev/null || cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
cat > .env << EOF
# SyncCode - Local Production Configuration
JWT_SECRET=${JWT_SECRET}
REDIS_PASSWORD=synccode123
GRAFANA_PASSWORD=admin
USE_REDIS=true
REDIS_URL=redis://:synccode123@localhost:6379
ALLOWED_ORIGINS=http://localhost:3000,http://localhost:5173
LOG_LEVEL=info
EXECUTION_TIMEOUT=5000
MEMORY_LIMIT=128m
CPU_LIMIT=0.5
MAX_CODE_SIZE=50000
MAX_CONCURRENT_EXECUTIONS=5
EOF
echo -e "${GREEN}✓ Created .env file with JWT secret${NC}"
else
echo -e "${YELLOW}⚠ .env file already exists, skipping${NC}"
fi
# Backend .env
if [ ! -f synccode_backend/.env ]; then
cp .env synccode_backend/.env
echo -e "${GREEN}✓ Created synccode_backend/.env${NC}"
fi
# Executor .env
if [ ! -f synccode-executor/.env ]; then
cp .env synccode-executor/.env
echo -e "${GREEN}✓ Created synccode-executor/.env${NC}"
fi
echo ""
# ============================================
# Package.json Files
# ============================================
echo -e "${BLUE}📦 Creating package.json files...${NC}"
# Backend package.json
if [ ! -f synccode_backend/package.json ]; then
cat > synccode_backend/package.json << 'EOF'
{
"name": "synccode_backend",
"version": "1.0.0",
"description": "SyncCode local production backend",
"main": "server.js",
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
},
"dependencies": {
"express": "^4.18.2",
"socket.io": "^4.6.1",
"@socket.io/redis-adapter": "^8.2.1",
"cors": "^2.8.5",
"yjs": "^13.6.10",
"pako": "^2.1.0",
"helmet": "^7.1.0",
"express-rate-limit": "^7.1.5",
"validator": "^13.11.0",
"jsonwebtoken": "^9.0.2",
"bcrypt": "^5.1.1",
"ioredis": "^5.3.2",
"winston": "^3.11.0",
"dotenv": "^16.3.1"
},
"devDependencies": {
"nodemon": "^3.0.1"
}
}
EOF
echo -e "${GREEN}✓ Created synccode_backend/package.json${NC}"
fi
# Executor package.json
if [ ! -f synccode-executor/package.json ]; then
cat > synccode-executor/package.json << 'EOF'
{
"name": "synccode-executor",
"version": "1.0.0",
"description": "SyncCode local production executor",
"main": "executor-service.js",
"scripts": {
"start": "node executor-service.js",
"dev": "nodemon executor-service.js"
},
"dependencies": {
"express": "^4.18.2",
"cors": "^2.8.5",
"helmet": "^7.1.0",
"express-rate-limit": "^7.1.5",
"validator": "^13.11.0",
"uuid": "^9.0.0",
"winston": "^3.11.0",
"dotenv": "^16.3.1"
},
"devDependencies": {
"nodemon": "^3.0.1"
}
}
EOF
echo -e "${GREEN}✓ Created synccode-executor/package.json${NC}"
fi
echo ""
# ============================================
# Install Dependencies
# ============================================
echo -e "${BLUE}📦 Installing dependencies...${NC}"
cd synccode_backend
echo -e "${CYAN} Installing backend dependencies...${NC}"
npm install --silent
cd ..
cd synccode-executor
echo -e "${CYAN} Installing executor dependencies...${NC}"
npm install --silent
cd ..
echo -e "${GREEN}✓ Dependencies installed${NC}"
echo ""
# ============================================
# Docker Images
# ============================================
echo -e "${BLUE}🐳 Pulling Docker images...${NC}"
echo -e "${YELLOW} This may take several minutes on first run...${NC}"
docker pull node:24-alpine &
docker pull python:3.11-alpine &
docker inspect gcc:13-alpine > /dev/null 2>&1 && echo "Image exists" || echo "Image not found" &
docker pull redis:7-alpine &
docker pull prom/prometheus:latest &
docker pull grafana/grafana:latest &
docker pull nginx:alpine &
wait
echo -e "${GREEN}✓ All Docker images pulled${NC}"
echo ""
# ============================================
# Prometheus Configuration
# ============================================
echo -e "${BLUE}📊 Creating monitoring configuration...${NC}"
cat > monitoring/prometheus.yml << 'EOF'
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'synccode_backend'
static_configs:
- targets: ['backend:3001']
metrics_path: '/api/metrics'
- job_name: 'synccode-executor'
static_configs:
- targets: ['executor:3002']
metrics_path: '/metrics'
EOF
echo -e "${GREEN}✓ Prometheus configuration created${NC}"
# Grafana datasource
cat > monitoring/grafana/provisioning/datasources/prometheus.yml << 'EOF'
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://prometheus:9090
isDefault: true
editable: false
EOF
echo -e "${GREEN}✓ Grafana datasource configured${NC}"
echo ""
# ============================================
# Nginx Configuration
# ============================================
cat > nginx/nginx.conf << 'EOF'
events {
worker_connections 1024;
}
http {
upstream backend {
server backend:3001;
}
upstream executor {
server executor:3002;
}
server {
listen 80;
server_name localhost;
location / {
return 200 'SyncCode Local Production - Use direct ports or configure frontend';
add_header Content-Type text/plain;
}
location /api/ {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /socket.io/ {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /execute {
proxy_pass http://executor;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /health {
proxy_pass http://backend/health;
}
}
}
EOF
echo -e "${GREEN}✓ Nginx configuration created${NC}"
echo ""
# ============================================
# Dockerfiles
# ============================================
echo -e "${BLUE}🐳 Creating Dockerfiles...${NC}"
# Backend Dockerfile
cat > synccode_backend/Dockerfile << 'EOF'
FROM node:24-alpine
RUN apk add --no-cache wget
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN mkdir -p logs && \
addgroup -g 1001 -S nodejs && \
adduser -S nodejs -u 1001 && \
chown -R nodejs:nodejs /app
USER nodejs
EXPOSE 3001
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s \
CMD wget --quiet --tries=1 --spider http://localhost:3001/health || exit 1
CMD ["node", "server.js"]
EOF
# Executor Dockerfile
cat > synccode-executor/Dockerfile << 'EOF'
FROM node:24-alpine
RUN apk add --no-cache wget docker-cli
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN mkdir -p logs temp && chmod 777 temp
EXPOSE 3002
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s \
CMD wget --quiet --tries=1 --spider http://localhost:3002/health || exit 1
CMD ["node", "executor-service.js"]
EOF
echo -e "${GREEN}✓ Dockerfiles created${NC}"
echo ""
# ============================================
# Helper Scripts
# ============================================
echo -e "${BLUE}📝 Creating helper scripts...${NC}"
# Start script (Docker Compose)
cat > start.sh << 'EOF'
#!/bin/bash
echo "🚀 Starting SyncCode Local Production Stack..."
docker-compose up -d
echo ""
echo "✅ Services started!"
echo ""
echo "📊 Access points:"
echo " Backend: http://localhost:3001"
echo " Executor: http://localhost:3002"
echo " Prometheus: http://localhost:9090"
echo " Grafana: http://localhost:3000 (admin/admin)"
echo " Nginx: http://localhost:80"
echo ""
echo "📝 View logs: docker-compose logs -f"
echo "🔍 Check status: docker-compose ps"
echo "🛑 Stop: ./stop.sh"
EOF
# Start script (Native)
cat > start-native.sh << 'EOF'
#!/bin/bash
echo "🚀 Starting SyncCode in native mode (no Docker Compose)..."
echo ""
# Start Redis if available
if command -v redis-server &> /dev/null; then
redis-server --daemonize yes --requirepass synccode123 2>/dev/null || true
echo "✓ Redis started"
fi
# Start backend
cd synccode_backend
npm start > ../logs/backend.log 2>&1 &
BACKEND_PID=$!
cd ..
echo "✓ Backend started (PID: $BACKEND_PID)"
# Start executor
cd synccode-executor
npm start > ../logs/executor.log 2>&1 &
EXECUTOR_PID=$!
cd ..
echo "✓ Executor started (PID: $EXECUTOR_PID)"
echo ""
echo "📊 Services running:"
echo " Backend: http://localhost:3001 (PID: $BACKEND_PID)"
echo " Executor: http://localhost:3002 (PID: $EXECUTOR_PID)"
echo ""
echo "📝 Logs: tail -f logs/*.log"
echo "🛑 Stop: ./stop-native.sh"
# Save PIDs
echo "$BACKEND_PID" > .backend.pid
echo "$EXECUTOR_PID" > .executor.pid
EOF
# Stop script (Docker Compose)
cat > stop.sh << 'EOF'
#!/bin/bash
echo "🛑 Stopping SyncCode Local Production Stack..."
docker-compose down
echo "✅ All services stopped"
EOF
# Stop script (Native)
cat > stop-native.sh << 'EOF'
#!/bin/bash
echo "🛑 Stopping SyncCode services..."
if [ -f .backend.pid ]; then
kill $(cat .backend.pid) 2>/dev/null || true
rm .backend.pid
echo "✓ Backend stopped"
fi
if [ -f .executor.pid ]; then
kill $(cat .executor.pid) 2>/dev/null || true
rm .executor.pid
echo "✓ Executor stopped"
fi
# Stop Redis
pkill redis-server 2>/dev/null || true
echo "✅ All services stopped"
EOF
# Make scripts executable
chmod +x start.sh stop.sh start-native.sh stop-native.sh
echo -e "${GREEN}✓ Helper scripts created${NC}"
echo ""
# ============================================
# Summary
# ============================================
echo ""
echo -e "${CYAN}============================================${NC}"
echo -e "${GREEN}✅ Setup Complete!${NC}"
echo -e "${CYAN}============================================${NC}"
echo ""
echo -e "${MAGENTA}📋 Next Steps:${NC}"
echo ""
echo -e "${YELLOW}1. Copy server files:${NC}"
echo " cp server.js synccode_backend/"
echo " cp executor-service.js synccode-executor/"
echo ""
echo -e "${YELLOW}2. Start services:${NC}"
echo ""
echo -e " ${BLUE}Option A - Docker Compose (Recommended):${NC}"
echo " ./start.sh"
echo ""
echo -e " ${BLUE}Option B - Native (No containers):${NC}"
echo " ./start-native.sh"
echo ""
echo -e "${YELLOW}3. Access services:${NC}"
echo " Backend: http://localhost:3001"
echo " Executor: http://localhost:3002"
echo " Prometheus: http://localhost:9090"
echo " Grafana: http://localhost:3000"
echo ""
echo -e "${YELLOW}4. Test the setup:${NC}"
echo " curl http://localhost:3001/health"
echo " curl http://localhost:3002/health"
echo ""
echo -e "${YELLOW}5. View logs:${NC}"
echo " Docker: docker-compose logs -f"
echo " Native: tail -f logs/*.log"
echo ""
echo -e "${CYAN}============================================${NC}"
echo -e "${GREEN}🎉 Ready to code!${NC}"
echo -e "${CYAN}============================================${NC}"
echo ""