trading.ai/docker-start.sh
2025-09-18 20:54:30 +08:00

217 lines
4.5 KiB
Bash
Executable File

#!/bin/bash
# A股量化交易系统 Docker 启动脚本
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 输出带颜色的信息
info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检查Docker是否安装
check_docker() {
if ! command -v docker &> /dev/null; then
error "Docker 未安装,请先安装 Docker"
exit 1
fi
if ! command -v docker-compose &> /dev/null; then
error "Docker Compose 未安装,请先安装 Docker Compose"
exit 1
fi
success "Docker 环境检查通过"
}
# 创建必要的目录
create_directories() {
info "创建必要的目录..."
mkdir -p data logs config nginx/ssl
# 如果配置文件不存在,创建默认配置
if [ ! -f "config/config.yaml" ]; then
warning "配置文件不存在,创建默认配置..."
cat > config/config.yaml << EOF
# A股量化交易系统配置
database:
path: "data/trading.db"
data_source:
provider: "adata"
strategy:
kline_pattern:
min_entity_ratio: 0.55
final_yang_min_ratio: 0.40
max_turnover_ratio: 40.0
timeframes: ["daily", "weekly"]
notification:
dingtalk:
enabled: false
webhook_url: ""
logging:
level: "INFO"
file: "logs/trading.log"
EOF
success "已创建默认配置文件"
fi
}
# 创建Nginx配置
create_nginx_config() {
if [ ! -f "nginx/nginx.conf" ]; then
info "创建 Nginx 配置文件..."
mkdir -p nginx
cat > nginx/nginx.conf << EOF
events {
worker_connections 1024;
}
http {
upstream trading_app {
server trading-web:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://trading_app;
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
success "已创建 Nginx 配置文件"
fi
}
# 启动服务
start_services() {
info "启动 A股量化交易系统..."
# 构建镜像
info "构建 Docker 镜像..."
docker-compose build
# 启动服务
info "启动服务容器..."
docker-compose up -d
# 等待服务启动
info "等待服务启动..."
sleep 10
# 检查服务状态
if docker-compose ps | grep -q "Up"; then
success "服务启动成功!"
info "Web界面访问地址: http://localhost:8080"
info "如果配置了Nginx: http://localhost"
echo ""
info "查看日志: docker-compose logs -f"
info "停止服务: docker-compose down"
info "重启服务: docker-compose restart"
else
error "服务启动失败,请检查日志"
docker-compose logs
exit 1
fi
}
# 停止服务
stop_services() {
info "停止服务..."
docker-compose down
success "服务已停止"
}
# 查看日志
show_logs() {
docker-compose logs -f
}
# 重启服务
restart_services() {
info "重启服务..."
docker-compose restart
success "服务已重启"
}
# 清理
cleanup() {
warning "这将删除所有容器、镜像和数据卷,是否继续?(y/N)"
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
docker-compose down -v --rmi all
success "清理完成"
else
info "取消清理"
fi
}
# 主函数
main() {
case "$1" in
"start")
check_docker
create_directories
create_nginx_config
start_services
;;
"stop")
stop_services
;;
"restart")
restart_services
;;
"logs")
show_logs
;;
"cleanup")
cleanup
;;
*)
echo "A股量化交易系统 Docker 管理脚本"
echo ""
echo "使用方法: $0 {start|stop|restart|logs|cleanup}"
echo ""
echo "命令说明:"
echo " start - 启动服务"
echo " stop - 停止服务"
echo " restart - 重启服务"
echo " logs - 查看日志"
echo " cleanup - 清理所有数据(危险操作)"
echo ""
;;
esac
}
main "$@"