-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·387 lines (340 loc) · 11 KB
/
deploy.sh
File metadata and controls
executable file
·387 lines (340 loc) · 11 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
#!/bin/bash
#
# Automated Architecture Discovery System
# Copyright (c) 2025 Abhishek Datta
#
# Licensed under the MIT License.
# See LICENSE file in the project root for full license information.
#
# This file is part of the Automated Architecture Discovery System,
# an educational project demonstrating microservices architecture discovery.
#
#########################################################
# AWS Deployment Script for Architecture Discovery
# Amazon Linux 2023 Version
# Deploys 6 Microservices + Splunk Logger
#########################################################
echo "=========================================="
echo "ARCHITECTURE DISCOVERY - AWS DEPLOYMENT"
echo "Amazon Linux 2023"
echo "=========================================="
echo ""
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to print colored output
print_success() {
echo -e "${GREEN}✓ $1${NC}"
}
print_error() {
echo -e "${RED}✗ $1${NC}"
}
print_info() {
echo -e "${YELLOW}ℹ $1${NC}"
}
# Detect OS
print_info "Detecting Operating System..."
if [ -f /etc/os-release ]; then
. /etc/os-release
echo "OS: $NAME $VERSION"
fi
echo ""
# Step 1: System Update
echo "Step 1: Updating system packages..."
sudo dnf update -y
if [ $? -eq 0 ]; then
print_success "System packages updated"
else
print_error "Failed to update system packages"
exit 1
fi
echo ""
# Step 2: Install Python and dependencies
echo "Step 2: Installing Python 3 and pip..."
sudo dnf install -y python3 python3-pip
if [ $? -eq 0 ]; then
print_success "Python 3 installed"
python3 --version
pip3 --version
else
print_error "Failed to install Python 3"
exit 1
fi
echo ""
# Step 3: Create project directory
echo "Step 3: Creating project directory..."
PROJECT_DIR="/home/ec2-user/architecture-discovery"
# PROJECT_DIR="/home/ec2-user/architecture-discovery-aws"
mkdir -p $PROJECT_DIR
cd $PROJECT_DIR
print_success "Project directory created: $PROJECT_DIR"
echo ""
# Step 4: Create virtual environment
echo "Step 4: Creating Python virtual environment..."
# python3 -m venv aads-venv
python3 -m venv venv
# if [ -d "venv" ]; then
# rm -rf venv
# fi
if [ $? -eq 0 ]; then
print_success "Virtual environment created"
else
print_error "Failed to create virtual environment"
exit 1
fi
echo ""
# Step 5: Activate virtual environment
echo "Step 5: Activating virtual environment..."
source venv/bin/activate
print_success "Virtual environment activated"
echo ""
# Step 6: Upgrade pip
echo "Step 6: Upgrading pip..."
pip install --upgrade pip
print_success "Pip upgraded"
echo ""
# Step 7: Install Python packages
echo "Step 7: Installing Python dependencies..."
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
if [ $? -eq 0 ]; then
print_success "Dependencies installed from requirements.txt"
else
print_error "Failed to install dependencies"
exit 1
fi
else
print_info "requirements.txt not found, installing manually..."
pip install Flask==3.0.0 Werkzeug==3.0.1 requests==2.31.0 python-dotenv==1.0.0 anthropic==0.40.0
print_success "Dependencies installed manually"
fi
echo ""
# Step 8: Create systemd service files
echo "Step 8: Creating systemd service files..."
# Splunk Logger Service
sudo tee /etc/systemd/system/splunk-logger.service > /dev/null <<EOF
[Unit]
Description=Splunk Log Collector
After=network.target
[Service]
Type=simple
User=ec2-user
WorkingDirectory=$PROJECT_DIR
Environment="PATH=$PROJECT_DIR/venv/bin"
ExecStart=$PROJECT_DIR/venv/bin/python3 splunk_logger.py
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
SyslogIdentifier=splunk-logger
[Install]
WantedBy=multi-user.target
EOF
print_success "Splunk Logger service file created"
# Microservices Service
sudo tee /etc/systemd/system/microservices.service > /dev/null <<EOF
[Unit]
Description=E-Commerce Microservices (6 services)
After=network.target splunk-logger.service
Requires=splunk-logger.service
[Service]
Type=simple
User=ec2-user
WorkingDirectory=$PROJECT_DIR
Environment="PATH=$PROJECT_DIR/venv/bin"
ExecStart=$PROJECT_DIR/venv/bin/python3 run_services.py
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
print_success "Microservices service file created"
echo ""
# Step 9: Reload systemd
echo "Step 9: Reloading systemd daemon..."
sudo systemctl daemon-reload
print_success "Systemd daemon reloaded"
echo ""
# Step 10: Enable services
echo "Step 10: Enabling services to start on boot..."
sudo systemctl enable splunk-logger.service
sudo systemctl enable microservices.service
print_success "Services enabled"
echo ""
# Step 11: Start Splunk Logger first
echo "Step 11: Starting Splunk Logger..."
sudo systemctl start splunk-logger.service
sleep 3
# Check if Splunk started
if sudo systemctl is-active --quiet splunk-logger.service; then
print_success "Splunk Logger is running"
else
print_error "Splunk Logger failed to start"
echo "Checking logs..."
sudo journalctl -u splunk-logger.service -n 20 --no-pager
exit 1
fi
echo ""
# Step 12: Start Microservices
echo "Step 12: Starting Microservices..."
sudo systemctl start microservices.service
sleep 5
# Check if Microservices started
if sudo systemctl is-active --quiet microservices.service; then
print_success "Microservices are running"
else
print_error "Microservices failed to start"
echo "Checking logs..."
sudo journalctl -u microservices.service -n 20 --no-pager
exit 1
fi
echo ""
# Step 13: Verify services
echo "Step 13: Verifying all services..."
echo ""
# Wait a moment for services to fully initialize
sleep 3
# Check Splunk
echo "Checking Splunk Logger (port 8088)..."
SPLUNK_CHECK=$(curl -s http://localhost:8088/health 2>/dev/null)
if echo "$SPLUNK_CHECK" | grep -q "healthy"; then
print_success "Splunk Logger: HEALTHY"
else
print_error "Splunk Logger: NOT RESPONDING"
echo "Response: $SPLUNK_CHECK"
fi
# Check each microservice
services=(
"5001:Auth"
"5002:Product"
"5003:Order"
"5004:Payment"
"5005:Loyalty"
"5006:Policy"
)
echo ""
for service in "${services[@]}"; do
IFS=':' read -r port name <<< "$service"
echo "Checking $name Service (port $port)..."
SERVICE_CHECK=$(curl -s http://localhost:$port/health 2>/dev/null)
if echo "$SERVICE_CHECK" | grep -q "healthy"; then
print_success "$name Service: HEALTHY"
else
print_error "$name Service: NOT RESPONDING"
echo "Response: $SERVICE_CHECK"
fi
done
echo ""
# Step 14: Install additional utilities
echo "Step 14: Installing additional utilities..."
sudo dnf install -y net-tools curl jq
print_success "Utilities installed"
echo ""
# Step 15: Display summary
echo "=========================================="
echo "DEPLOYMENT COMPLETE!"
echo "=========================================="
echo ""
# Display service status
echo "Services Status:"
echo "----------------"
sudo systemctl status splunk-logger.service --no-pager -l | head -n 5
echo ""
sudo systemctl status microservices.service --no-pager -l | head -n 5
echo ""
# Get IP addresses
PUBLIC_IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)
PRIVATE_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
echo "Instance Information:"
echo "====================="
echo ""
echo "Public IP: $PUBLIC_IP"
echo "Private IP: $PRIVATE_IP"
echo ""
echo "Service Endpoints (External Access):"
echo "======================================"
echo ""
echo "Health Checks:"
echo " • Splunk Logger: http://$PUBLIC_IP:8088/health"
echo " • Auth Service: http://$PUBLIC_IP:5001/health"
echo " • Product Service: http://$PUBLIC_IP:5002/health"
echo " • Order Service: http://$PUBLIC_IP:5003/health"
echo " • Payment Service: http://$PUBLIC_IP:5004/health"
echo " • Loyalty Service: http://$PUBLIC_IP:5005/health"
echo " • Policy Service: http://$PUBLIC_IP:5006/health"
echo ""
echo "API Endpoints:"
echo " • Login: POST http://$PUBLIC_IP:5001/api/auth/login"
echo " • Search Products: GET http://$PUBLIC_IP:5002/api/products/search?query=laptop"
echo " • Loyalty Points: GET http://$PUBLIC_IP:5005/api/loyalty/points"
echo " • Policies: GET http://$PUBLIC_IP:5006/api/policies/return"
echo " • Splunk Stats: GET http://$PUBLIC_IP:8088/api/stats"
echo ""
echo "Useful Commands:"
echo "================"
echo ""
echo "View Logs:"
echo " • Splunk logs: sudo journalctl -u splunk-logger -f"
echo " • Services logs: sudo journalctl -u microservices -f"
echo " • Both logs: sudo journalctl -u splunk-logger -u microservices -f"
echo " • Last 50 lines: sudo journalctl -u microservices -n 50"
echo ""
echo "Service Management:"
echo " • Restart Splunk: sudo systemctl restart splunk-logger"
echo " • Restart Services: sudo systemctl restart microservices"
echo " • Stop all: sudo systemctl stop splunk-logger microservices"
echo " • Start all: sudo systemctl start splunk-logger microservices"
echo " • Check status: sudo systemctl status splunk-logger microservices"
echo ""
echo "Check Network:"
echo " • Open ports: sudo netstat -tlnp | grep -E ':(5001|5002|5003|5004|5005|5006|8088)'"
echo " • Or use ss: sudo ss -tlnp | grep -E ':(5001|5002|5003|5004|5005|5006|8088)'"
echo ""
echo "File Locations:"
echo " • Project dir: $PROJECT_DIR"
echo " • Logs file: $PROJECT_DIR/splunk_logs.jsonl"
echo " • Virtual env: $PROJECT_DIR/venv"
echo " • Service files: /etc/systemd/system/splunk-logger.service"
echo " /etc/systemd/system/microservices.service"
echo ""
echo "Test Commands (run from EC2):"
echo "=============================="
echo ""
echo "# Test health endpoints"
echo "curl http://localhost:8088/health"
echo "curl http://localhost:5001/health"
echo ""
echo "# Test login"
echo 'curl -X POST http://localhost:5001/api/auth/login \'
echo ' -H "Content-Type: application/json" \'
echo ' -d '"'"'{"email":"user1@test.com","password":"password123"}'"'"
echo ""
echo "# Test product search"
echo "curl http://localhost:5002/api/products/search?query=laptop"
echo ""
echo "# Check Splunk stats"
echo "curl http://localhost:8088/api/stats"
echo ""
echo "=========================================="
echo "NEXT STEPS:"
echo "=========================================="
echo ""
echo "1. Test services from this EC2 instance (use commands above)"
echo "2. Test from your local machine using public IP"
echo "3. Verify Security Group allows inbound on ports 5001-5006, 8088"
echo "4. Run test_services.sh from your local machine"
echo "5. If all tests pass, proceed to deploy user journey simulator"
echo ""
echo "Troubleshooting:"
echo " • If services not accessible externally, check Security Group"
echo " • If services crash, check logs: sudo journalctl -u microservices -n 100"
echo " • If port conflicts, check: sudo netstat -tlnp | grep :5001"
echo ""
echo "=========================================="
echo "Deployment script completed successfully!"
echo "=========================================="
echo ""