-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-slow.sh
More file actions
executable file
·243 lines (216 loc) · 6.56 KB
/
deploy-slow.sh
File metadata and controls
executable file
·243 lines (216 loc) · 6.56 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
#!/bin/bash
# Content Forge AI - Deployment with Rate Limiting Handling
# Adds delays between commands to avoid connection refusal
set -e
SSH_HOST="96.9.212.72"
SSH_USER="jixGPXKTrdtOezXO"
SSH_PASS="gcsObp5HmFRJFzGJ"
SSH_PORT="22"
DEPLOY_PATH="/var/www/content-forge-ai"
PROJECT_ROOT="/Users/fadel/Downloads/content-forge-ai-v2"
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m'
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Content Forge AI - Auto Deployment${NC}"
echo -e "${GREEN}========================================${NC}"
echo -e "${BLUE}Server: ${SSH_USER}@${SSH_HOST}:${SSH_PORT}${NC}"
echo -e "${YELLOW}Using slow connection mode (delays between commands)${NC}"
echo -e ""
# Function with retry and delay
ssh_exec_with_retry() {
local cmd="$1"
local max_retries=3
local retry=0
while [ $retry -lt $max_retries ]; do
if [ $retry -gt 0 ]; then
echo -e "${YELLOW}Retry $retry/$max_retries... waiting 5 seconds...${NC}"
sleep 5
fi
expect << EOF 2>/dev/null
set timeout 90
spawn ssh -p $SSH_PORT -o StrictHostKeyChecking=no -o ConnectTimeout=15 $SSH_USER@$SSH_HOST "$cmd"
expect {
"password:" {
send "$SSH_PASS\r"
exp_continue
}
"\[sudo\] password" {
send "$SSH_PASS\r"
exp_continue
}
"Password:" {
send "$SSH_PASS\r"
exp_continue
}
"yes/no" {
send "yes\r"
exp_continue
}
timeout {
exit 1
}
eof
}
EOF
if [ $? -eq 0 ]; then
return 0
fi
retry=$((retry + 1))
done
return 1
}
# Step 1: Verify build
echo -e "${YELLOW}[1/8] Verifying build...${NC}"
cd "$PROJECT_ROOT"
if [ ! -d "dist" ]; then
npm run build
fi
echo -e "${GREEN}✅ Build ready${NC}"
# Step 2: Test SSH
echo -e "\n${YELLOW}[2/8] Testing SSH connection...${NC}"
sleep 2 # Delay before first connection
if ssh_exec_with_retry "echo 'SSH OK' && pwd && whoami" | grep -q "SSH OK"; then
echo -e "${GREEN}✅ SSH connection successful${NC}"
else
echo -e "${RED}❌ SSH connection failed${NC}"
exit 1
fi
# Step 3: Setup directories
echo -e "\n${YELLOW}[3/8] Setting up server directories...${NC}"
sleep 3 # Delay between commands
if ssh_exec_with_retry "sudo mkdir -p $DEPLOY_PATH/frontend $DEPLOY_PATH/backend $DEPLOY_PATH/logs && sudo chown -R \\\$USER:\\\$USER $DEPLOY_PATH && ls -la $DEPLOY_PATH"; then
echo -e "${GREEN}✅ Server directories ready${NC}"
else
echo -e "${RED}❌ Failed to create directories${NC}"
exit 1
fi
# Step 4: Upload frontend
echo -e "\n${YELLOW}[4/8] Uploading frontend files...${NC}"
sleep 3
expect << EOF
set timeout 600
spawn rsync -avz --progress -e "ssh -p $SSH_PORT -o StrictHostKeyChecking=no -o ConnectTimeout=15" "$PROJECT_ROOT/dist/" "$SSH_USER@$SSH_HOST:$DEPLOY_PATH/frontend/"
expect {
"password:" {
send "$SSH_PASS\r"
exp_continue
}
"yes/no" {
send "yes\r"
exp_continue
}
timeout {
exit 1
}
eof
}
EOF
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ Frontend uploaded${NC}"
else
echo -e "${RED}❌ Frontend upload failed${NC}"
exit 1
fi
# Step 5: Upload backend
echo -e "\n${YELLOW}[5/8] Uploading backend files...${NC}"
sleep 3
expect << EOF
set timeout 600
spawn rsync -avz --progress -e "ssh -p $SSH_PORT -o StrictHostKeyChecking=no -o ConnectTimeout=15" "$PROJECT_ROOT/backend/" "$SSH_USER@$SSH_HOST:$DEPLOY_PATH/backend/"
expect {
"password:" {
send "$SSH_PASS\r"
exp_continue
}
"yes/no" {
send "yes\r"
exp_continue
}
timeout {
exit 1
}
eof
}
EOF
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ Backend uploaded${NC}"
else
echo -e "${RED}❌ Backend upload failed${NC}"
exit 1
fi
# Upload ecosystem.config.js
if [ -f "$PROJECT_ROOT/ecosystem.config.js" ]; then
sleep 2
expect << EOF
set timeout 60
spawn scp -P $SSH_PORT -o StrictHostKeyChecking=no "$PROJECT_ROOT/ecosystem.config.js" "$SSH_USER@$SSH_HOST:$DEPLOY_PATH/"
expect {
"password:" { send "$SSH_PASS\r"; exp_continue }
"yes/no" { send "yes\r"; exp_continue }
eof
}
EOF
fi
# Step 6: Setup backend
echo -e "\n${YELLOW}[6/8] Setting up backend...${NC}"
sleep 3
ssh_exec_with_retry "bash -s" << 'REMOTE_SCRIPT'
cd /var/www/content-forge-ai/backend
if ! command -v node &> /dev/null; then
echo "Installing Node.js..."
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
fi
echo "Installing npm dependencies..."
npm install --production 2>&1 | tail -20 || npm install 2>&1 | tail -20
chmod 600 .env 2>/dev/null || true
chmod 644 *.js *.json 2>/dev/null || true
echo "Backend setup complete"
REMOTE_SCRIPT
echo -e "${GREEN}✅ Backend configured${NC}"
# Step 7: Setup PM2
echo -e "\n${YELLOW}[7/8] Setting up PM2...${NC}"
sleep 3
ssh_exec_with_retry "bash -s" << 'REMOTE_SCRIPT'
cd /var/www/content-forge-ai
if ! command -v pm2 &> /dev/null; then
echo "Installing PM2..."
sudo npm install -g pm2
fi
pm2 stop content-forge-ai-api 2>/dev/null || true
pm2 delete content-forge-ai-api 2>/dev/null || true
if [ -f ecosystem.config.js ]; then
pm2 start ecosystem.config.js
else
pm2 start backend/server.js --name content-forge-ai-api --cwd /var/www/content-forge-ai
fi
pm2 save || true
echo ""
pm2 status
REMOTE_SCRIPT
echo -e "${GREEN}✅ PM2 configured${NC}"
# Step 8: Test
echo -e "\n${YELLOW}[8/8] Testing deployment...${NC}"
sleep 3
BACKEND_RESPONSE=$(ssh_exec_with_retry "curl -s http://localhost:5000/api/health" 2>/dev/null | grep -o "ok" || echo "")
if [ -n "$BACKEND_RESPONSE" ]; then
echo -e "${GREEN}✅ Backend API is running${NC}"
else
echo -e "${YELLOW}⚠️ Checking PM2 status...${NC}"
ssh_exec_with_retry "pm2 status && pm2 logs content-forge-ai-api --lines 10 --nostream" 2>/dev/null || true
fi
# Summary
echo -e "\n${GREEN}========================================${NC}"
echo -e "${GREEN}✅ Deployment Complete!${NC}"
echo -e "${GREEN}========================================${NC}"
echo -e "${BLUE}Server: ${SSH_USER}@${SSH_HOST}:${SSH_PORT}${NC}"
echo -e "${BLUE}Path: ${DEPLOY_PATH}${NC}"
echo -e ""
echo -e "${YELLOW}Next Steps:${NC}"
echo -e "1. Configure OpenLiteSpeed: ${BLUE}https://${SSH_HOST}:7080${NC}"
echo -e "2. Follow Step 7 in DEPLOYMENT_STANDALONE.md"
echo -e "3. Test frontend: ${BLUE}http://${SSH_HOST}${NC}"
echo -e "${GREEN}========================================${NC}"