-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·309 lines (274 loc) · 7.87 KB
/
deploy.sh
File metadata and controls
executable file
·309 lines (274 loc) · 7.87 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
#!/bin/bash
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 打印带颜色的消息
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 显示横幅
show_banner() {
echo -e "${BLUE}"
cat << 'EOF'
╔════════════════════════════════════════════════════════════╗
║ DeepTrade Docker 一键部署脚本 ║
║ ║
║ AI-Powered Cryptocurrency Trading System ║
╚════════════════════════════════════════════════════════════╝
EOF
echo -e "${NC}"
}
# 检查Docker是否安装
check_docker() {
print_info "检查Docker环境..."
if ! command -v docker &> /dev/null; then
print_error "Docker未安装!请先安装Docker:"
echo " Ubuntu/Debian: sudo apt-get install docker.io"
echo " CentOS/RHEL: sudo yum install docker"
echo " MacOS: brew install docker"
exit 1
fi
if ! command -v docker compose &> /dev/null && ! docker compose version &> /dev/null; then
print_error "Docker Compose未安装!请先安装Docker Compose:"
echo " https://docs.docker.com/compose/install/"
exit 1
fi
print_success "Docker环境检查通过"
}
# 创建必要目录
create_directories() {
print_info "创建必要目录..."
mkdir -p backend/data
mkdir -p backend/logs
mkdir -p nginx/ssl
mkdir -p logs
print_success "目录创建完成"
}
# 检查环境变量文件
check_env_files() {
print_info "检查环境变量文件..."
# 检查根目录.env
if [ ! -f .env ]; then
print_warning "未找到根目录 .env 文件,正在从 .env.example 复制..."
if [ -f .env.example ]; then
cp .env.example .env
print_success "已复制 .env.example 为 .env,请根据需要修改配置"
else
print_error "未找到 .env.example 文件!"
exit 1
fi
else
print_success "根目录 .env 文件已存在"
fi
# 检查后端.env
if [ ! -f backend/.env ]; then
print_warning "未找到 backend/.env 文件"
if [ -f backend/.env.example ]; then
print_info "请复制 backend/.env.example 为 backend/.env 并配置API密钥"
fi
else
print_success "backend/.env 文件已存在"
fi
# 检查前端.env
if [ ! -f frontend/.env ]; then
print_warning "未找到 frontend/.env 文件"
if [ -f frontend/.env.example ]; then
print_info "请复制 frontend/.env.example 为 frontend/.env"
fi
else
print_success "frontend/.env 文件已存在"
fi
}
# 构建镜像
build_images() {
print_info "构建Docker镜像..."
docker compose build --no-cache
print_success "镜像构建完成"
}
# 启动服务
start_services() {
print_info "启动DeepTrade服务..."
docker compose up -d
print_success "服务启动完成"
}
# 等待服务就绪
wait_for_services() {
print_info "等待服务就绪..."
# 等待后端健康检查
print_info "检查后端服务..."
for i in {1..30}; do
if curl -s http://localhost:8080/health > /dev/null 2>&1; then
print_success "后端服务就绪"
break
fi
if [ $i -eq 30 ]; then
print_error "后端服务启动超时"
docker compose logs backend
exit 1
fi
echo -n "."
sleep 2
done
# 等待Nginx健康检查
print_info "检查Nginx服务..."
for i in {1..30}; do
if curl -s http://localhost/health > /dev/null 2>&1; then
print_success "Nginx服务就绪"
break
fi
if [ $i -eq 30 ]; then
print_warning "Nginx服务可能未完全就绪,但将继续执行"
break
fi
echo -n "."
sleep 2
done
}
# 显示访问信息
show_access_info() {
echo ""
print_success "================================"
print_success " DeepTrade 部署完成!"
print_success "================================"
echo ""
echo -e "${GREEN}访问地址:${NC}"
echo " 🌐 前端地址: http://localhost:${NGINX_HTTP_PORT:-80}"
echo " 🔗 API地址: http://localhost:${NGINX_HTTP_PORT:-80}/api"
echo " 📊 WebSocket: ws://localhost:${NGINX_HTTP_PORT:-80}/ws"
echo ""
echo -e "${GREEN}管理命令:${NC}"
echo " 查看日志: ./deploy.sh logs"
echo " 停止服务: ./deploy.sh stop"
echo " 重启服务: ./deploy.sh restart"
echo " 重新部署: ./deploy.sh redeploy"
echo " 查看状态: ./deploy.sh status"
echo ""
echo -e "${YELLOW}注意事项:${NC}"
echo " 1. 确保已正确配置 backend/.env 中的API密钥"
echo " 2. 生产环境请配置HTTPS(参考 nginx/ssl/README.md)"
echo " 3. 首次使用请设置 AUTO_TRADE=false 进行测试"
echo ""
}
# 查看日志
show_logs() {
docker compose logs -f
}
# 停止服务
stop_services() {
print_info "停止DeepTrade服务..."
docker compose down
print_success "服务已停止"
}
# 重启服务
restart_services() {
print_info "重启DeepTrade服务..."
docker compose restart
print_success "服务已重启"
}
# 重新部署
redeploy() {
print_info "重新部署DeepTrade..."
docker compose down
docker compose build --no-cache
docker compose up -d
wait_for_services
show_access_info
}
# 查看状态
show_status() {
print_info "服务状态:"
docker compose ps
echo ""
print_info "资源使用:"
docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}"
}
# 清理数据
clean_data() {
print_warning "这将删除所有数据(数据库、日志等),确定要继续吗? (y/N)"
read -r response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
print_info "清理数据..."
docker compose down -v
docker system prune -af
print_success "数据清理完成"
else
print_info "取消清理"
fi
}
# 显示帮助
show_help() {
cat << EOF
DeepTrade Docker 部署脚本
用法: ./deploy.sh [命令]
命令:
deploy 部署服务(默认)
logs 查看实时日志
stop 停止服务
restart 重启服务
status 查看服务状态
redeploy 重新部署
clean 清理所有数据
help 显示帮助信息
示例:
./deploy.sh # 部署服务
./deploy.sh logs # 查看日志
./deploy.sh stop # 停止服务
./deploy.sh status # 查看状态
EOF
}
# 主函数
main() {
show_banner
case "${1:-deploy}" in
"deploy")
check_docker
create_directories
check_env_files
build_images
start_services
wait_for_services
show_access_info
;;
"logs")
show_logs
;;
"stop")
stop_services
;;
"restart")
restart_services
;;
"redeploy")
redeploy
;;
"status")
show_status
;;
"clean")
clean_data
;;
"help"|"-h"|"--help")
show_help
;;
*)
print_error "未知命令: $1"
show_help
exit 1
;;
esac
}
# 执行主函数
main "$@"