Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·680 lines (541 loc) · 16.4 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·680 lines (541 loc) · 16.4 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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#!/bin/bash
# Godis Coordinator - Automated Deployment Script
# This script automates the deployment of the Godis distributed AI training platform
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_NAME="godis"
DEFAULT_DOMAIN="catalyst.nothr.in"
DEFAULT_DB_PASSWORD=""
DEFAULT_JWT_SECRET=""
# Functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_step() {
echo -e "\n${PURPLE}==== $1 ====${NC}"
}
# Generate secure random password
generate_password() {
openssl rand -base64 32 | tr -d "=+/" | cut -c1-25
}
# Check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check system requirements
check_requirements() {
log_step "Checking System Requirements"
local missing_deps=()
# Check required commands
if ! command_exists docker; then
missing_deps+=("docker")
fi
if ! command_exists docker-compose; then
missing_deps+=("docker-compose")
fi
if ! command_exists git; then
missing_deps+=("git")
fi
if ! command_exists openssl; then
missing_deps+=("openssl")
fi
if [ ${#missing_deps[@]} -ne 0 ]; then
log_error "Missing required dependencies: ${missing_deps[*]}"
log_info "Please install the missing dependencies and run this script again."
if command_exists apt; then
log_info "On Ubuntu/Debian, run:"
log_info "sudo apt update && sudo apt install -y docker.io docker-compose git openssl"
elif command_exists yum; then
log_info "On CentOS/RHEL, run:"
log_info "sudo yum install -y docker docker-compose git openssl"
fi
exit 1
fi
log_success "All required dependencies are installed"
}
# Get user configuration
get_configuration() {
log_step "Configuration Setup"
# Domain name
echo -n "Enter domain name (default: $DEFAULT_DOMAIN): "
read -r DOMAIN
DOMAIN=${DOMAIN:-$DEFAULT_DOMAIN}
# Database password
echo -n "Enter database password (leave empty to generate): "
read -rs DB_PASSWORD
echo
if [ -z "$DB_PASSWORD" ]; then
DB_PASSWORD=$(generate_password)
log_info "Generated database password: $DB_PASSWORD"
fi
# JWT secret
echo -n "Enter JWT secret (leave empty to generate): "
read -rs JWT_SECRET
echo
if [ -z "$JWT_SECRET" ]; then
JWT_SECRET=$(generate_password)
log_info "Generated JWT secret: $JWT_SECRET"
fi
# Email for SSL certificate
echo -n "Enter email for SSL certificate: "
read -r SSL_EMAIL
# Deployment type
echo
echo "Select deployment type:"
echo "1) Docker Compose (Recommended)"
echo "2) Docker Swarm"
echo "3) Kubernetes"
echo -n "Enter choice (1-3): "
read -r DEPLOY_TYPE
log_success "Configuration completed"
}
# Create environment file
create_env_file() {
log_step "Creating Environment Configuration"
cat > .env << EOF
# Database Configuration
DB_PASSWORD=$DB_PASSWORD
DB_HOST=postgres
DB_PORT=5432
DB_USER=godis
DB_NAME=godis
# Security
JWT_SECRET=$JWT_SECRET
# Server Configuration
DOMAIN=$DOMAIN
PORT=8080
HOST=0.0.0.0
# SSL Configuration
SSL_EMAIL=$SSL_EMAIL
# Training Configuration
TRAINING_WORKSPACE=/app/training_workspace
PYTHON_SERVICE_HOST=python-training-service
PYTHON_SERVICE_PORT=50051
# Logging
LOG_LEVEL=info
LOG_FILE=/app/logs/coordinator.log
# Generated on $(date)
EOF
log_success "Environment file created: .env"
}
# Create Dockerfile
create_dockerfile() {
log_step "Creating Dockerfile"
cat > Dockerfile << 'EOF'
# Multi-stage build for Godis Coordinator
FROM golang:1.21-alpine AS builder
# Install build dependencies
RUN apk add --no-cache git ca-certificates
# Set working directory
WORKDIR /app
# Copy go mod files
COPY go.mod go.sum ./
RUN go mod download
# Copy source code
COPY . .
# Build the coordinator
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w -s' -o coordinator ./cmd/coordinator
# Production image
FROM alpine:latest
# Install runtime dependencies
RUN apk --no-cache add ca-certificates wget postgresql-client
# Create app user
RUN addgroup -g 1001 godis && \
adduser -D -s /bin/sh -u 1001 -G godis godis
# Set working directory
WORKDIR /app
# Copy binary from builder
COPY --from=builder /app/coordinator .
# Create necessary directories
RUN mkdir -p /app/training_workspace /app/logs && \
chown -R godis:godis /app
# Switch to non-root user
USER godis
# Expose port
EXPOSE 8080
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/api/points/stats || exit 1
# Run coordinator
CMD ["./coordinator"]
EOF
log_success "Dockerfile created"
}
# Create Python Dockerfile
create_python_dockerfile() {
log_step "Creating Python Training Service Dockerfile"
cat > Dockerfile.python << 'EOF'
FROM python:3.11-slim
# Install system dependencies
RUN apt-get update && apt-get install -y \
gcc \
g++ \
wget \
&& rm -rf /var/lib/apt/lists/*
# Set working directory
WORKDIR /app
# Copy Python requirements
COPY python/requirements.txt .
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy Python service files
COPY python/ .
COPY proto/ ./proto/
# Create training workspace
RUN mkdir -p /app/training_workspace
# Create non-root user
RUN useradd -r -u 1001 godis && \
chown -R godis:godis /app
USER godis
# Expose gRPC port
EXPOSE 50051
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python -c "import socket; s=socket.socket(); s.connect(('localhost', 50051)); s.close()" || exit 1
# Run training service
CMD ["python", "training_service.py"]
EOF
log_success "Python Dockerfile created"
}
# Create Docker Compose file
create_docker_compose() {
log_step "Creating Docker Compose Configuration"
cat > docker-compose.yml << 'EOF'
version: '3.8'
services:
postgres:
image: postgres:15-alpine
environment:
POSTGRES_DB: ${DB_NAME}
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
restart: unless-stopped
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"]
interval: 10s
timeout: 5s
retries: 5
networks:
- godis-network
coordinator:
build: .
environment:
- DB_HOST=postgres
- DB_PORT=5432
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
- DB_NAME=${DB_NAME}
- JWT_SECRET=${JWT_SECRET}
- PORT=${PORT}
ports:
- "8080:8080"
depends_on:
postgres:
condition: service_healthy
volumes:
- training_workspace:/app/training_workspace
- coordinator_logs:/app/logs
restart: unless-stopped
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8080/api/points/stats"]
interval: 30s
timeout: 10s
retries: 3
networks:
- godis-network
python-training-service:
build:
context: .
dockerfile: Dockerfile.python
ports:
- "50051:50051"
volumes:
- training_workspace:/app/training_workspace
restart: unless-stopped
depends_on:
- coordinator
networks:
- godis-network
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./ssl:/etc/nginx/ssl:ro
- nginx_logs:/var/log/nginx
depends_on:
- coordinator
restart: unless-stopped
networks:
- godis-network
volumes:
postgres_data:
training_workspace:
coordinator_logs:
nginx_logs:
networks:
godis-network:
driver: bridge
EOF
log_success "Docker Compose file created"
}
# Create Nginx configuration
create_nginx_config() {
log_step "Creating Nginx Configuration"
mkdir -p ssl
cat > nginx.conf << EOF
events {
worker_connections 1024;
}
http {
upstream coordinator {
server coordinator:8080;
}
# Rate limiting
limit_req_zone \$binary_remote_addr zone=api:10m rate=10r/s;
server {
listen 80;
server_name $DOMAIN;
# Redirect HTTP to HTTPS
return 301 https://\$server_name\$request_uri;
}
server {
listen 443 ssl http2;
server_name $DOMAIN;
# SSL Configuration (will be updated after certificate generation)
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512;
ssl_prefer_server_ciphers off;
# Security headers
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
# Rate limiting
limit_req zone=api burst=20 nodelay;
# Proxy settings
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
# Main API
location / {
proxy_pass http://coordinator;
proxy_timeout 300s;
proxy_read_timeout 300s;
}
# File upload size
client_max_body_size 100M;
# Logging
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
}
}
EOF
log_success "Nginx configuration created"
}
# Generate SSL certificates
generate_ssl_certificates() {
log_step "Generating SSL Certificates"
if [ "$SSL_EMAIL" != "" ]; then
log_info "Generating self-signed certificates for development..."
# Generate self-signed certificate for development
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout ssl/key.pem \
-out ssl/cert.pem \
-subj "/C=US/ST=State/L=City/O=Organization/CN=$DOMAIN"
log_success "Self-signed SSL certificates generated"
log_warning "For production, replace with Let's Encrypt certificates"
else
log_warning "No email provided, skipping SSL certificate generation"
fi
}
# Deploy with Docker Compose
deploy_docker_compose() {
log_step "Deploying with Docker Compose"
# Build and start services
log_info "Building Docker images..."
docker-compose build --no-cache
log_info "Starting services..."
docker-compose up -d
# Wait for services to be ready
log_info "Waiting for services to be ready..."
sleep 30
# Check service status
log_info "Checking service status..."
docker-compose ps
log_success "Docker Compose deployment completed"
}
# Create monitoring script
create_monitoring_script() {
log_step "Creating Monitoring Script"
cat > monitor.sh << 'EOF'
#!/bin/bash
# Godis Monitoring Script
check_service() {
local service=$1
local url=$2
if curl -f -s "$url" > /dev/null; then
echo "✅ $service - OK"
else
echo "❌ $service - FAILED"
return 1
fi
}
echo "🔍 Godis Health Check - $(date)"
echo "=================================="
# Check coordinator API
check_service "Coordinator API" "http://localhost:8080/api/points/stats"
# Check training service
if nc -z localhost 50051 2>/dev/null; then
echo "✅ Training Service - OK"
else
echo "❌ Training Service - FAILED"
fi
# Check database
if docker-compose exec -T postgres pg_isready -U godis > /dev/null 2>&1; then
echo "✅ Database - OK"
else
echo "❌ Database - FAILED"
fi
# Check Docker services
echo -e "\n📊 Docker Services:"
docker-compose ps
# Show resource usage
echo -e "\n💾 Resource Usage:"
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
echo -e "\n🔗 Access URLs:"
echo "API: https://$(grep DOMAIN .env | cut -d'=' -f2)"
echo "Health: https://$(grep DOMAIN .env | cut -d'=' -f2)/api/points/stats"
EOF
chmod +x monitor.sh
log_success "Monitoring script created: monitor.sh"
}
# Create backup script
create_backup_script() {
log_step "Creating Backup Script"
cat > backup.sh << 'EOF'
#!/bin/bash
# Godis Backup Script
BACKUP_DIR="./backups/$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"
echo "🗄️ Creating backup in $BACKUP_DIR"
# Backup database
echo "Backing up database..."
docker-compose exec -T postgres pg_dump -U godis godis > "$BACKUP_DIR/database.sql"
# Backup configuration
echo "Backing up configuration..."
cp .env "$BACKUP_DIR/"
cp docker-compose.yml "$BACKUP_DIR/"
cp nginx.conf "$BACKUP_DIR/"
# Backup training workspace
echo "Backing up training workspace..."
docker run --rm -v "$(pwd)"_training_workspace:/source -v "$(pwd)/$BACKUP_DIR":/backup alpine tar czf /backup/training_workspace.tar.gz -C /source .
echo "✅ Backup completed: $BACKUP_DIR"
EOF
chmod +x backup.sh
log_success "Backup script created: backup.sh"
}
# Main deployment function
main() {
echo -e "${CYAN}"
echo "╔═══════════════════════════════════════════════════════════════╗"
echo "║ Godis Deployment Script ║"
echo "║ Distributed AI Training Platform ║"
echo "╚═══════════════════════════════════════════════════════════════╝"
echo -e "${NC}\n"
# Check if running as root
if [ "$EUID" -eq 0 ]; then
log_warning "Running as root is not recommended for security reasons"
echo -n "Continue anyway? (y/N): "
read -r continue_root
if [ "$continue_root" != "y" ] && [ "$continue_root" != "Y" ]; then
log_info "Exiting..."
exit 1
fi
fi
# Check requirements
check_requirements
# Get configuration
get_configuration
# Create configuration files
create_env_file
create_dockerfile
create_python_dockerfile
create_docker_compose
create_nginx_config
generate_ssl_certificates
# Deploy based on type
case $DEPLOY_TYPE in
1)
deploy_docker_compose
;;
2)
log_error "Docker Swarm deployment not implemented yet"
exit 1
;;
3)
log_error "Kubernetes deployment not implemented yet"
exit 1
;;
*)
log_error "Invalid deployment type"
exit 1
;;
esac
# Create utility scripts
create_monitoring_script
create_backup_script
# Final instructions
log_step "Deployment Complete!"
echo -e "${GREEN}"
echo "🎉 Godis has been successfully deployed!"
echo -e "${NC}"
echo "📋 Next Steps:"
echo "1. Access your deployment: https://$DOMAIN"
echo "2. Test the API: https://$DOMAIN/api/points/stats"
echo "3. Monitor services: ./monitor.sh"
echo "4. Create backups: ./backup.sh"
echo ""
echo "📚 Useful Commands:"
echo "• View logs: docker-compose logs -f"
echo "• Restart services: docker-compose restart"
echo "• Update deployment: git pull && docker-compose up -d --build"
echo "• Scale services: docker-compose up -d --scale coordinator=2"
echo ""
echo "🔧 Configuration files created:"
echo "• .env - Environment variables"
echo "• docker-compose.yml - Service definitions"
echo "• nginx.conf - Reverse proxy configuration"
echo "• monitor.sh - Health monitoring script"
echo "• backup.sh - Backup script"
log_success "Deployment completed successfully! 🚀"
}
# Run main function
main "$@"