Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·153 lines (128 loc) · 4.32 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·153 lines (128 loc) · 4.32 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
#!/bin/bash
# AlphaBot一键部署脚本
# 作者: BenalexCheung
# 版本: 1.0.0
# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# 打印带颜色的消息
print_message() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检查Docker是否安装
check_docker() {
print_message "检查 Docker 是否安装..."
if ! command -v docker &> /dev/null; then
print_error "Docker 未安装,请先安装 Docker: https://docs.docker.com/get-docker/"
exit 1
fi
print_message "Docker 已安装 ✓"
print_message "检查 Docker Compose 是否安装..."
if ! command -v docker-compose &> /dev/null; then
print_error "Docker Compose 未安装,请先安装 Docker Compose: https://docs.docker.com/compose/install/"
exit 1
fi
print_message "Docker Compose 已安装 ✓"
}
# 检查环境文件
check_env_file() {
print_message "检查环境配置文件..."
if [ ! -f "./backend/.env" ]; then
print_warning "未找到 backend/.env 文件,将从示例文件创建"
if [ -f "./backend/.env.example" ]; then
cp ./backend/.env.example ./backend/.env
print_warning "已从示例创建 backend/.env;其中 SECRET_KEY 是公开示例值,JWT 存在伪造风险,建议尽快替换为私有随机密钥"
else
print_error "未找到 .env.example 文件,请手动创建 .env 文件"
exit 1
fi
else
print_message "环境配置文件已存在 ✓"
fi
if [ ! -f "./frontend/.env.local" ]; then
if [ ! -f "./frontend/.env.example" ]; then
print_error "未找到 frontend/.env.example"
exit 1
fi
cp ./frontend/.env.example ./frontend/.env.local
print_message "已从示例文件创建 frontend/.env.local 文件"
fi
}
# 训练机器学习模型
train_ml_model() {
print_message "检查是否需要训练机器学习模型..."
if [ ! -f "./backend/models/stock_analysis_model.pkl" ]; then
print_message "未找到预训练模型,开始训练..."
# 使用Docker运行训练脚本
docker-compose run --rm backend python train_model.py
if [ $? -eq 0 ]; then
print_message "模型训练成功 ✓"
else
print_error "模型训练失败,将使用规则分析模式"
# 确保 .env 文件中使用规则分析模式
sed -i 's/DEFAULT_ANALYSIS_MODE=.*/DEFAULT_ANALYSIS_MODE="rule"/' ./backend/.env
fi
else
print_message "预训练模型已存在 ✓"
fi
}
# 构建并启动服务
build_and_start() {
print_message "构建并启动服务..."
docker-compose down
print_message "构建Docker镜像..."
if ! docker-compose build; then
print_error "Docker镜像构建失败,请检查错误信息"
exit 1
fi
print_message "Docker镜像构建成功 ✓"
print_message "启动服务..."
if ! docker-compose up -d; then
print_error "服务启动失败,请检查Docker日志"
exit 1
fi
if [ $? -eq 0 ]; then
print_message "服务启动成功 ✓"
print_message "后端API: http://localhost:8000/api/v1/docs"
print_message "前端界面: http://localhost:3000"
else
print_error "服务启动失败,请检查日志"
exit 1
fi
}
# 显示日志
show_logs() {
read -p "是否查看服务日志? (y/n): " show_logs
if [[ $show_logs == "y" || $show_logs == "Y" ]]; then
docker-compose logs -f
fi
}
# 主函数
main() {
echo "=================================================="
echo " AlphaBot一键部署脚本 "
echo "=================================================="
# 检查Docker
check_docker
# 检查环境文件
check_env_file
# 训练机器学习模型
train_ml_model
# 构建并启动服务
build_and_start
# 显示日志
show_logs
echo "=================================================="
echo " 部署完成 "
echo "=================================================="
}
# 执行主函数
main