-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup-ec2.sh
More file actions
executable file
Β·282 lines (236 loc) Β· 8.01 KB
/
setup-ec2.sh
File metadata and controls
executable file
Β·282 lines (236 loc) Β· 8.01 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
#!/bin/bash
# AI Calling Agent - EC2 Setup Script
# Run this script on your EC2 instance to set up the project
set -e # Exit on error
echo "π Starting AI Calling Agent Setup on EC2..."
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Check if running as root
if [ "$EUID" -eq 0 ]; then
echo -e "${RED}Please do not run as root. Run as a regular user.${NC}"
exit 1
fi
# Update system packages
echo -e "${GREEN}π¦ Updating system packages...${NC}"
sudo yum update -y || sudo apt-get update -y
# Install Python 3.12+ and pip
echo -e "${GREEN}π Installing Python 3.12+...${NC}"
if command -v python3.12 &> /dev/null; then
echo "Python 3.12+ already installed"
else
# For Amazon Linux 2/2023
sudo yum install -y python3.12 python3.12-pip python3.12-venv || \
sudo apt-get install -y python3.12 python3.12-pip python3.12-venv || \
echo -e "${YELLOW}β οΈ Python 3.12 not available in default repos. Installing Python 3.11...${NC}" && \
sudo yum install -y python3 python3-pip || sudo apt-get install -y python3 python3-pip
fi
# Install Node.js 18+
echo -e "${GREEN}π¦ Installing Node.js 18+...${NC}"
if command -v node &> /dev/null && [ "$(node -v | cut -d'v' -f2 | cut -d'.' -f1)" -ge 18 ]; then
echo "Node.js 18+ already installed: $(node -v)"
else
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash - || \
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo bash -
sudo yum install -y nodejs || sudo apt-get install -y nodejs
fi
# Install nginx (optional, for reverse proxy)
echo -e "${GREEN}π Installing nginx...${NC}"
sudo yum install -y nginx || sudo apt-get install -y nginx
# Install git if not present
if ! command -v git &> /dev/null; then
echo -e "${GREEN}π₯ Installing git...${NC}"
sudo yum install -y git || sudo apt-get install -y git
fi
# Navigate to home directory
cd ~
# Clone or navigate to project directory
if [ -d "AI-Calling-Agent" ]; then
echo -e "${GREEN}π Project directory exists, updating...${NC}"
cd AI-Calling-Agent
git pull
else
echo -e "${YELLOW}β οΈ Project directory not found. Please clone the repository first:${NC}"
echo "git clone https://github.com/ranjan2829/AI-Calling-Agent.git"
echo "cd AI-Calling-Agent"
exit 1
fi
# ============================================
# BACKEND SETUP
# ============================================
echo -e "${GREEN}π§ Setting up Backend...${NC}"
cd Backend
# Create virtual environment
if [ ! -d "venv" ]; then
python3 -m venv venv
fi
source venv/bin/activate
# Upgrade pip
pip install --upgrade pip
# Install Python dependencies
echo -e "${GREEN}π¦ Installing Python dependencies...${NC}"
pip install -r requirements.txt
# Create .env file if it doesn't exist
if [ ! -f ".env" ]; then
echo -e "${YELLOW}π Creating .env file template...${NC}"
cat > .env << 'EOF'
# Twilio Configuration
account_sid=YOUR_TWILIO_ACCOUNT_SID
auth_token=YOUR_TWILIO_AUTH_TOKEN
TWILIO_PHONE_NUMBER=+1234567890
WEBHOOK_BASE_URL=https://your-domain.com
# AWS Configuration
AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_KEY
AWS_REGION=ap-south-1
S3_BUCKET=amzn-twillio-recordings
# OpenAI Configuration
OPENAI_API_KEY=YOUR_OPENAI_API_KEY
EOF
echo -e "${YELLOW}β οΈ Please edit Backend/.env and add your credentials!${NC}"
else
echo -e "${GREEN}β
.env file already exists${NC}"
fi
deactivate
cd ..
# ============================================
# FRONTEND SETUP
# ============================================
echo -e "${GREEN}π¨ Setting up Frontend...${NC}"
cd interview-bot-frontend
# Install Node dependencies
echo -e "${GREEN}π¦ Installing Node.js dependencies...${NC}"
npm install
# Build frontend for production
echo -e "${GREEN}ποΈ Building frontend...${NC}"
npm run build
cd ..
# ============================================
# CREATE SYSTEMD SERVICES
# ============================================
echo -e "${GREEN}βοΈ Creating systemd services...${NC}"
PROJECT_DIR=$(pwd)
USER=$(whoami)
# Backend service
sudo tee /etc/systemd/system/ai-calling-backend.service > /dev/null << EOF
[Unit]
Description=AI Calling Agent Backend
After=network.target
[Service]
Type=simple
User=$USER
WorkingDirectory=$PROJECT_DIR/Backend
Environment="PATH=$PROJECT_DIR/Backend/venv/bin"
ExecStart=$PROJECT_DIR/Backend/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
# Frontend service (serving built files with a simple HTTP server)
sudo tee /etc/systemd/system/ai-calling-frontend.service > /dev/null << EOF
[Unit]
Description=AI Calling Agent Frontend
After=network.target
[Service]
Type=simple
User=$USER
WorkingDirectory=$PROJECT_DIR/interview-bot-frontend
ExecStart=/usr/bin/npx serve -s dist -l 3000
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
# Install serve for frontend
sudo npm install -g serve
# Reload systemd
sudo systemctl daemon-reload
# ============================================
# NGINX CONFIGURATION (Optional)
# ============================================
echo -e "${GREEN}π Configuring nginx...${NC}"
sudo tee /etc/nginx/conf.d/ai-calling-agent.conf > /dev/null << 'EOF'
server {
listen 80;
server_name _;
# Frontend
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
# Backend API
location /api {
rewrite ^/api/(.*) /$1 break;
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
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;
}
}
EOF
# Test nginx configuration
sudo nginx -t
# Enable and start services
echo -e "${GREEN}π Enabling and starting services...${NC}"
sudo systemctl enable ai-calling-backend
sudo systemctl enable ai-calling-frontend
sudo systemctl enable nginx
# Start services
sudo systemctl start ai-calling-backend
sudo systemctl start ai-calling-frontend
sudo systemctl start nginx
# ============================================
# FIREWALL CONFIGURATION
# ============================================
echo -e "${GREEN}π₯ Configuring firewall...${NC}"
# For Amazon Linux 2
if command -v firewall-cmd &> /dev/null; then
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
fi
# For EC2 Security Groups - remind user
echo -e "${YELLOW}β οΈ Don't forget to configure EC2 Security Group:${NC}"
echo " - Allow inbound HTTP (port 80)"
echo " - Allow inbound HTTPS (port 443)"
echo " - Allow inbound port 8000 (backend) if not using nginx"
echo " - Allow inbound port 3000 (frontend) if not using nginx"
# ============================================
# FINAL INSTRUCTIONS
# ============================================
echo -e "${GREEN}β
Setup complete!${NC}"
echo ""
echo -e "${YELLOW}π Next steps:${NC}"
echo "1. Edit Backend/.env and add your credentials:"
echo " - Twilio Account SID and Auth Token"
echo " - Twilio Phone Number"
echo " - WEBHOOK_BASE_URL (your EC2 public IP or domain)"
echo " - AWS Access Key ID and Secret"
echo " - OpenAI API Key"
echo ""
echo "2. Restart backend service:"
echo " sudo systemctl restart ai-calling-backend"
echo ""
echo "3. Check service status:"
echo " sudo systemctl status ai-calling-backend"
echo " sudo systemctl status ai-calling-frontend"
echo " sudo systemctl status nginx"
echo ""
echo "4. View logs:"
echo " sudo journalctl -u ai-calling-backend -f"
echo " sudo journalctl -u ai-calling-frontend -f"
echo ""
echo "5. Access the application:"
echo " Frontend: http://$(curl -s ifconfig.me || echo 'YOUR_EC2_IP')"
echo " Backend API: http://$(curl -s ifconfig.me || echo 'YOUR_EC2_IP')/api"
echo ""
echo -e "${GREEN}π All done!${NC}"