initial-import: 2026-04-11 21:18:55

This commit is contained in:
cfdaily
2026-04-11 21:18:55 +08:00
commit 5e6b2d73eb
264 changed files with 117047 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
#!/bin/bash
# 创建所有预设用户
echo "创建所有预设用户..."
# 用户列表: username id
users=(
"zhugeliang 1"
"pangtong 2"
"simayi 3"
"zhangfei 4"
"guanyu 5"
"zhaoyun 6"
"jiangwei 7"
)
BASE_DIR="/Users/chufeng/.openclaw/sanguo_projects/sanguo_vnpy"
for entry in "${users[@]}"; do
read username id <<< "$entry"
echo "----------------------------------------"
echo "创建用户: $username, ID: $id"
# 创建目录结构
mkdir -p "$BASE_DIR/users/$username"/{data,logs,strategies}
echo "目录创建完成"
done
echo "----------------------------------------"
echo "所有用户目录创建完成!"
+105
View File
@@ -0,0 +1,105 @@
#!/bin/bash
#
# 创建新用户脚本 - NAS sanguo_vnpy 部署
# 用法: ./create-user.sh <username> <user-id>
# 示例: ./create-user.sh jiangwei 7
#
set -e
# 配置
BASE_DIR="/mnt/nas-volume/sanguo_vnpy"
TEMPLATE_DIR="/mnt/nas-volume/sanguo_vnpy/jiangwei-platform/deploy/nas/templates"
PORT_ALLOCATION="/mnt/nas-volume/sanguo_vnpy/jiangwei-platform/deploy/nas/port-allocation.md"
# 参数检查
if [ $# -ne 2 ]; then
echo "用法: $0 <username> <user-id>"
echo "示例: $0 jiangwei 7"
exit 1
fi
USERNAME=$1
USER_ID=$2
# 计算端口
RPC_REQUEST_PORT=$((2000 + USER_ID * 10))
RPC_SUBSCRIBE_PORT=$((RPC_REQUEST_PORT + 1))
WEB_PORT=$((8000 + USER_ID))
NGINX_PATH="/$USERNAME/"
echo "=========================================="
echo "创建用户: $USERNAME"
echo "用户编号: $USER_ID"
echo "RPC请求端口: $RPC_REQUEST_PORT"
echo "RPC订阅端口: $RPC_SUBSCRIBE_PORT"
echo "Web端口: $WEB_PORT"
echo "Nginx路径: $NGINX_PATH"
echo "=========================================="
# 创建用户目录
USER_DIR="$BASE_DIR/users/$USERNAME"
mkdir -p "$USER_DIR"/{data,logs,strategies}
echo "目录创建完成: $USER_DIR"
# 生成启动脚本
sed \
-e "s/{{username}}/$USERNAME/g" \
-e "s/{{rpc_request_port}}/$RPC_REQUEST_PORT/g" \
-e "s/{{rpc_subscribe_port}}/$RPC_SUBSCRIBE_PORT/g" \
"$TEMPLATE_DIR/start_trade.py.template" > "$USER_DIR/start_trade.py"
sed \
-e "s/{{username}}/$USERNAME/g" \
-e "s/{{rpc_request_port}}/$RPC_REQUEST_PORT/g" \
-e "s/{{rpc_subscribe_port}}/$RPC_SUBSCRIBE_PORT/g" \
-e "s/{{web_port}}/$WEB_PORT/g" \
"$TEMPLATE_DIR/start_web.py.template" > "$USER_DIR/start_web.py"
chmod +x "$USER_DIR/start_trade.py" "$USER_DIR/start_web.py"
echo "启动脚本生成完成"
# 生成systemd服务文件
SYSTEMD_DIR="$BASE_DIR/config/systemd"
mkdir -p "$SYSTEMD_DIR"
sed \
-e "s/{{username}}/$USERNAME/g" \
-e "s|/mnt/nas-volume/sanguo_vnpy|$BASE_DIR|g" \
"$TEMPLATE_DIR/systemd/trade.service.template" > "$SYSTEMD_DIR/sanguo-trade-$USERNAME.service"
sed \
-e "s/{{username}}/$USERNAME/g" \
-e "s|/mnt/nas-volume/sanguo_vnpy|$BASE_DIR|g" \
"$TEMPLATE_DIR/systemd/web.service.template" > "$SYSTEMD_DIR/sanguo-web-$USERNAME.service"
echo "systemd服务文件生成完成: $SYSTEMD_DIR"
# 生成nginx location配置
NGINX_DIR="$BASE_DIR/config/nginx"
mkdir -p "$NGINX_DIR"
sed \
-e "s/{{username}}/$USERNAME/g" \
-e "s/{{nginx_path}}/$NGINX_PATH/g" \
-e "s/{{web_port}}/$WEB_PORT/g" \
"$TEMPLATE_DIR/nginx/location.conf.template" > "$NGINX_DIR/$USERNAME.conf"
echo "Nginx配置生成完成: $NGINX_DIR/$USERNAME.conf"
# 更新端口分配表
echo "" >> "$PORT_ALLOCATION"
echo "| $USERNAME | $USER_ID | $RPC_REQUEST_PORT | $RPC_SUBSCRIBE_PORT | $WEB_PORT | $NGINX_PATH | 已创建 |" >> "$PORT_ALLOCATION"
echo "=========================================="
echo "用户 $USERNAME 创建完成!"
echo ""
echo "后续步骤:"
echo "1. 将 $SYSTEMD_DIR/sanguo-trade-$USERNAME.service 复制到 /etc/systemd/system/"
echo "2. 将 $SYSTEMD_DIR/sanguo-web-$USERNAME.service 复制到 /etc/systemd/system/"
echo "3. 执行: systemctl daemon-reload"
echo "4. 执行: systemctl enable --now sanguo-trade-$USERNAME.service"
echo "5. 执行: systemctl enable --now sanguo-web-$USERNAME.service"
echo "6. 将 $NGINX_DIR/$USERNAME.conf 包含到Nginx主配置中"
echo "7. 重载Nginx配置"
echo "=========================================="
+80
View File
@@ -0,0 +1,80 @@
#!/bin/bash
# 为所有用户生成配置文件
echo "为所有用户生成配置文件..."
# 用户列表: username id
users=(
"zhugeliang 1"
"pangtong 2"
"simayi 3"
"zhangfei 4"
"guanyu 5"
"zhaoyun 6"
"jiangwei 7"
)
BASE_DIR="/Users/chufeng/.openclaw/sanguo_projects/sanguo_vnpy"
TEMPLATE_DIR="/Users/chufeng/.openclaw/sanguo_projects/sanguo_vnpy/jiangwei-platform/deploy/nas/templates"
CONFIG_DIR="/Users/chufeng/.openclaw/sanguo_projects/sanguo_vnpy/config"
mkdir -p "$CONFIG_DIR"/{systemd,nginx}
for entry in "${users[@]}"; do
read username id <<< "$entry"
RPC_REQUEST_PORT=$((2000 + id * 10))
RPC_SUBSCRIBE_PORT=$((RPC_REQUEST_PORT + 1))
WEB_PORT=$((8000 + id))
NGINX_PATH="/$username/"
echo "----------------------------------------"
echo "用户: $username, ID: $id"
echo " RPC请求: $RPC_REQUEST_PORT"
echo " RPC订阅: $RPC_SUBSCRIBE_PORT"
echo " Web端口: $WEB_PORT"
# 生成启动脚本
sed \
-e "s/{{username}}/$username/g" \
-e "s/{{rpc_request_port}}/$RPC_REQUEST_PORT/g" \
-e "s/{{rpc_subscribe_port}}/$RPC_SUBSCRIBE_PORT/g" \
"$TEMPLATE_DIR/start_trade.py.template" > "$BASE_DIR/users/$username/start_trade.py"
sed \
-e "s/{{username}}/$username/g" \
-e "s/{{rpc_request_port}}/$RPC_REQUEST_PORT/g" \
-e "s/{{rpc_subscribe_port}}/$RPC_SUBSCRIBE_PORT/g" \
-e "s/{{web_port}}/$WEB_PORT/g" \
"$TEMPLATE_DIR/start_web.py.template" > "$BASE_DIR/users/$username/start_web.py"
chmod +x "$BASE_DIR/users/$username/start_trade.py" "$BASE_DIR/users/$username/start_web.py"
# 生成systemd
sed \
-e "s/{{username}}/$username/g" \
-e "s|{{base_dir}}|$BASE_DIR|g" \
"$TEMPLATE_DIR/systemd/trade.service.template" > "$CONFIG_DIR/systemd/sanguo-trade-$username.service"
sed \
-e "s/{{username}}/$username/g" \
-e "s|{{base_dir}}|$BASE_DIR|g" \
"$TEMPLATE_DIR/systemd/web.service.template" > "$CONFIG_DIR/systemd/sanguo-web-$username.service"
# 生成nginx - 使用!作为分隔符避免和路径斜杠冲突
sed \
-e "s!{{username}}!$username!g" \
-e "s!{{nginx_path}}!$NGINX_PATH!g" \
-e "s!{{web_port}}!$WEB_PORT!g" \
"$TEMPLATE_DIR/nginx/location.conf.template" > "$CONFIG_DIR/nginx/$username.conf"
echo " 配置生成完成"
done
echo "----------------------------------------"
echo "所有用户配置生成完成!"
echo ""
echo "输出目录:"
echo " 启动脚本: $BASE_DIR/users/<username>/"
echo " systemd: $CONFIG_DIR/systemd/"
echo " nginx: $CONFIG_DIR/nginx/"
@@ -0,0 +1,24 @@
# 端口分配规则 - NAS sanguo_vnpy 部署
## 分配规则
每位用户编号从1开始递增:
- RPC请求端口 = `2000 + 用户编号 * 10`
- RPC订阅端口 = `2000 + 用户编号 * 10 + 1`
- Web服务端口 = `8000 + 用户编号`
## 当前分配表
| 用户 | 编号 | RPC请求 | RPC订阅 | Web端口 | Nginx路径 | 状态 |
|------|------|---------|---------|---------|-----------|------|
| 诸葛亮 | 1 | 2010 | 2011 | 8001 | `/zhugeliang/` | 未创建 |
| 庞统 | 2 | 2020 | 2021 | 8002 | `/pangtong/` | 未创建 |
| 司马懿 | 3 | 2030 | 2031 | 8003 | `/simayi/` | 未创建 |
| 张飞 | 4 | 2040 | 2041 | 8004 | `/zhangfei/` | 未创建 |
| 关羽 | 5 | 2050 | 2051 | 8005 | `/guanyu/` | 未创建 |
| 赵云 | 6 | 2060 | 2061 | 8006 | `/zhaoyun/` | 未创建 |
| 姜维 | 7 | 2070 | 2071 | 8007 | `/jiangwei/` | 未创建 |
## 添加新用户
在表格下方按顺序添加,保持格式不变。
@@ -0,0 +1,13 @@
# {{username}} - Web Trader
location ~* ^{{nginx_path}}(.*)$ {
proxy_pass http://127.0.0.1:{{web_port}}/$1$is_args$args;
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;
# WebSocket支持
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
用户: {{username}}
交易进程启动脚本 - 启动RPC服务端
"""
from vnpy.trader.event_engine import EventEngine
from vnpy.trader.main_engine import MainEngine
from vnpy.rpc import RpcServer
# 导入需要的gateway
# from vnpy_ctp import CtpGateway
# from vnpy_ib import IbGateway
# 导入需要的app
# from vnpy_ctastrategy import CtaStrategyApp
def main():
# 创建核心引擎
event_engine = EventEngine()
main_engine = MainEngine(event_engine)
# 添加gateway
# main_engine.add_gateway(CtpGateway)
# main_engine.add_gateway(IbGateway)
# 添加应用模块
# main_engine.add_app(CtaStrategyApp)
# main_engine.add_app(PortfolioStrategyApp)
# 启动RPC服务
rpc_request_port = {{rpc_request_port}}
rpc_subscribe_port = {{rpc_subscribe_port}}
rpc_server = RpcServer(
main_engine,
("0.0.0.0", rpc_request_port),
("0.0.0.0", rpc_subscribe_port)
)
rpc_server.start()
print(f"[{username}] RPC服务已启动")
print(f"- 请求地址: tcp://0.0.0.0:{rpc_request_port}")
print(f"- 订阅地址: tcp://0.0.0.0:{rpc_subscribe_port}")
print("按回车键退出...")
input()
if __name__ == "__main__":
main()
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
用户: {{username}}
Web进程启动脚本 - 启动FastAPI Web服务
"""
from vnpy_webtrader import run_web_trader
def main():
# RPC连接地址(连接本地交易进程
rpc_request_port = {{rpc_request_port}}
rpc_subscribe_port = {{rpc_subscribe_port}}
web_port = {{web_port}}
rpc_request_address = f"tcp://127.0.0.1:{rpc_request_port}"
rpc_subscribe_address = f"tcp://127.0.0.1:{rpc_subscribe_port}"
print(f"[{username}] Web服务启动")
print(f"- RPC请求: {rpc_request_address}")
print(f"- RPC订阅: {rpc_subscribe_address}")
print(f"- Web端口: {web_port}")
run_web_trader(
rpc_request_address,
rpc_subscribe_address,
host="127.0.0.1",
port=web_port,
cors_allow_all=True
)
if __name__ == "__main__":
main()
@@ -0,0 +1,15 @@
[Unit]
Description=sanguo_vnpy 交易进程 - {{username}}
After=network.target
[Service]
Type=simple
User={{username}}
WorkingDirectory={{base_dir}}/users/{{username}}
ExecStart=/usr/bin/python3 {{base_dir}}/users/{{username}}/start_trade.py
Restart=always
RestartSec=10
Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target
@@ -0,0 +1,15 @@
[Unit]
Description=sanguo_vnpy Web进程 - {{username}}
After=network.target sanguo-trade-{{username}}.service
[Service]
Type=simple
User={{username}}
WorkingDirectory={{base_dir}}/users/{{username}}
ExecStart=/usr/bin/python3 {{base_dir}}/users/{{username}}/start_web.py
Restart=always
RestartSec=10
Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target