-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-scalable.sh
More file actions
executable file
Β·285 lines (233 loc) Β· 7.58 KB
/
setup-scalable.sh
File metadata and controls
executable file
Β·285 lines (233 loc) Β· 7.58 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
#!/bin/bash
echo "π Setting up Scalable Task Management API"
echo "=========================================="
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
print_status() {
echo -e "${BLUE}βΉοΈ $1${NC}"
}
print_success() {
echo -e "${GREEN}β
$1${NC}"
}
print_warning() {
echo -e "${YELLOW}β οΈ $1${NC}"
}
print_error() {
echo -e "${RED}β $1${NC}"
}
print_status "Checking prerequisites..."
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed. Please install Docker first."
echo "Visit: https://docs.docker.com/get-docker/"
exit 1
fi
if ! command -v docker-compose &> /dev/null; then
print_error "Docker Compose is not installed. Please install Docker Compose first."
echo "Visit: https://docs.docker.com/compose/install/"
exit 1
fi
if ! docker info &> /dev/null; then
print_error "Docker daemon is not running. Please start Docker first."
exit 1
fi
print_success "Docker and Docker Compose are available"
print_status "Checking for port conflicts..."
check_port() {
local port=$1
local service=$2
if lsof -i :$port > /dev/null 2>&1; then
print_warning "Port $port is already in use (needed for $service)"
return 1
fi
return 0
}
POSTGRES_PORT=5432
REDIS_PORT=6379
BACKEND_PORT=8080
FRONTEND_PORT=3000
if ! check_port $POSTGRES_PORT "PostgreSQL"; then
POSTGRES_PORT=5433
print_status "Using alternative PostgreSQL port: $POSTGRES_PORT"
fi
if ! check_port $REDIS_PORT "Redis"; then
REDIS_PORT=6380
print_status "Using alternative Redis port: $REDIS_PORT"
fi
if ! check_port $BACKEND_PORT "Backend API"; then
BACKEND_PORT=8081
print_status "Using alternative Backend port: $BACKEND_PORT"
fi
if ! check_port $FRONTEND_PORT "Frontend"; then
FRONTEND_PORT=3001
print_status "Using alternative Frontend port: $FRONTEND_PORT"
fi
print_success "Port conflict check completed"
if command -v git &> /dev/null; then
print_success "Git is available"
else
print_warning "Git is not installed. You may need it for version control."
fi
print_status "Setting up environment configuration..."
if [ ! -f .env ]; then
print_status "Creating .env file with default values..."
cat > .env << EOF
# Database Configuration
DB_HOST=postgres
DB_PORT=$POSTGRES_PORT
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=task_manager
DB_MAX_OPEN_CONNS=25
DB_MAX_IDLE_CONNS=10
DB_CONN_MAX_LIFETIME=1h
DB_CONN_MAX_IDLE_TIME=30m
# Redis Configuration
REDIS_HOST=redis
REDIS_PORT=$REDIS_PORT
REDIS_PASSWORD=
REDIS_DB=0
REDIS_POOL_SIZE=10
REDIS_MIN_IDLE_CONNS=5
REDIS_MAX_RETRIES=3
# Server Configuration
HOST=0.0.0.0
PORT=$BACKEND_PORT
ENVIRONMENT=production
SCALABLE_MODE=true
READ_TIMEOUT=30s
WRITE_TIMEOUT=30s
IDLE_TIMEOUT=60s
# JWT Configuration (CHANGE IN PRODUCTION!)
JWT_SECRET=your-super-secret-jwt-key-change-in-production
JWT_EXPIRY=24h
JWT_REFRESH_EXPIRY=720h
# Worker Configuration
WORKER_CONCURRENCY=4
WORKER_POLL_INTERVAL=5s
# Rate Limiting
RATE_LIMIT_ENABLED=true
RATE_LIMIT_RPM=100
RATE_LIMIT_BURST=10
# CORS Configuration
CORS_ORIGINS=http://localhost:$FRONTEND_PORT,http://localhost:$BACKEND_PORT
CORS_METHODS=GET,POST,PUT,DELETE,OPTIONS
CORS_HEADERS=Origin,Content-Type,Authorization
# Monitoring
METRICS_ENABLED=true
HEALTH_CHECK_ENABLED=true
# Logging
LOG_LEVEL=info
LOG_FORMAT=json
# Port Configuration (for docker-compose)
POSTGRES_PORT=$POSTGRES_PORT
REDIS_PORT=$REDIS_PORT
BACKEND_PORT=$BACKEND_PORT
FRONTEND_PORT=$FRONTEND_PORT
EOF
print_success "Created .env file with port configuration: PostgreSQL:$POSTGRES_PORT, Redis:$REDIS_PORT, Backend:$BACKEND_PORT, Frontend:$FRONTEND_PORT"
print_warning "Please review and update the .env file for production use!"
else
print_success ".env file already exists"
if ! grep -q "POSTGRES_PORT=" .env; then
echo "POSTGRES_PORT=$POSTGRES_PORT" >> .env
echo "REDIS_PORT=$REDIS_PORT" >> .env
echo "BACKEND_PORT=$BACKEND_PORT" >> .env
echo "FRONTEND_PORT=$FRONTEND_PORT" >> .env
print_status "Updated .env with port configuration"
fi
fi
print_status "Creating necessary directories..."
mkdir -p logs
mkdir -p test-results
mkdir -p monitoring/grafana/dashboards
mkdir -p monitoring/prometheus
print_success "Directories created"
print_status "Pulling Docker images..."
export POSTGRES_PORT
export REDIS_PORT
export BACKEND_PORT
export FRONTEND_PORT
docker-compose -f docker-compose.scalable.yml pull
print_success "Docker images pulled successfully"
print_status "Building application images..."
docker-compose -f docker-compose.scalable.yml build --no-cache
if [ $? -eq 0 ]; then
print_success "Application images built successfully"
else
print_error "Failed to build application images"
exit 1
fi
print_status "Setting up database..."
print_status "Starting PostgreSQL container on port $POSTGRES_PORT..."
if ! docker-compose -f docker-compose.scalable.yml up -d postgres; then
print_error "Failed to start PostgreSQL container"
print_error "This might be due to port conflicts. Please check:"
echo " 1. Stop any local PostgreSQL services: brew services stop postgresql"
echo " 2. Kill any processes using port $POSTGRES_PORT: sudo lsof -ti:$POSTGRES_PORT | xargs kill -9"
echo " 3. Try running the setup again"
exit 1
fi
print_status "Waiting for PostgreSQL to be ready..."
sleep 10
max_attempts=30
attempt=0
while [ $attempt -lt $max_attempts ]; do
if docker-compose -f docker-compose.scalable.yml exec -T postgres pg_isready -U postgres > /dev/null 2>&1; then
break
fi
attempt=$((attempt + 1))
print_status "Waiting for PostgreSQL... (attempt $attempt/$max_attempts)"
sleep 2
done
if [ $attempt -eq $max_attempts ]; then
print_error "PostgreSQL failed to start within expected time"
exit 1
fi
print_success "PostgreSQL is ready"
docker-compose -f docker-compose.scalable.yml stop postgres
print_status "Setting executable permissions on scripts..."
chmod +x run-dev.sh
chmod +x run-prod.sh
chmod +x scale.sh
chmod +x stop.sh
print_success "Script permissions set"
print_success "π Scalable Task Management API setup completed!"
echo ""
echo "π Next Steps:"
echo "==============="
echo ""
echo "1. π Review and update the .env file:"
echo " ${YELLOW}nano .env${NC}"
echo ""
echo "2. π§ͺ Start in development mode:"
echo " ${YELLOW}./run-dev.sh${NC}"
echo ""
echo "3. π Or start in production mode:"
echo " ${YELLOW}./run-prod.sh${NC}"
echo ""
echo "4. β‘ Scale the backend:"
echo " ${YELLOW}./scale.sh 3${NC} (scales to 3 instances)"
echo ""
echo "5. π Stop all services:"
echo " ${YELLOW}./stop.sh${NC}"
echo ""
echo "π Monitoring URLs (after startup):"
echo "===================================="
echo "β’ Application: ${BLUE}http://localhost${NC} (production) or ${BLUE}http://localhost:$FRONTEND_PORT${NC} (dev)"
echo "β’ API Health: ${BLUE}http://localhost:$BACKEND_PORT/health${NC} (dev) or ${BLUE}http://localhost/health${NC} (prod)"
echo "β’ Grafana: ${BLUE}http://localhost:3001${NC} (admin/admin)"
echo "β’ Prometheus: ${BLUE}http://localhost:9090${NC}"
echo ""
if [ "$POSTGRES_PORT" != "5432" ] || [ "$REDIS_PORT" != "6379" ] || [ "$BACKEND_PORT" != "8080" ] || [ "$FRONTEND_PORT" != "3000" ]; then
print_warning "Non-standard ports detected due to conflicts:"
echo "β’ PostgreSQL: $POSTGRES_PORT (default: 5432)"
echo "β’ Redis: $REDIS_PORT (default: 6379)"
echo "β’ Backend: $BACKEND_PORT (default: 8080)"
echo "β’ Frontend: $FRONTEND_PORT (default: 3000)"
echo ""
fi
echo ""
print_warning "Remember to change JWT_SECRET and database passwords for production!"