687 lines
16 KiB
Markdown
687 lines
16 KiB
Markdown
# AI监控系统 - BT离线部署指南
|
||
|
||
## 📋 概述
|
||
|
||
本指南介绍如何使用BitTorrent(BT)技术在无网络环境中部署AI监控系统到昇腾服务器,包括种子制作、P2P传输和离线安装。
|
||
|
||
## 🔧 BT部署原理
|
||
|
||
### 工作流程
|
||
```
|
||
有网络环境 → 制作种子 → 分发种子 → P2P下载 → 离线安装
|
||
```
|
||
|
||
### 优势
|
||
- **高速传输**:多节点并行下载
|
||
- **带宽节省**:P2P共享减少服务器压力
|
||
- **断点续传**:网络中断后可继续传输
|
||
- **完整性校验**:自动验证文件完整性
|
||
|
||
## 🌱 制作BT种子
|
||
|
||
### 1. 准备完整部署包
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
# create_deployment_package.sh
|
||
|
||
echo "=== 制作AI监控系统部署包 ==="
|
||
|
||
# 创建部署目录结构
|
||
DEPLOY_DIR="AIMonitor_deploy_v1.0"
|
||
mkdir -p $DEPLOY_DIR/{packages,drivers,models,configs,scripts}
|
||
|
||
# 1. 复制项目文件
|
||
echo "1. 复制项目文件..."
|
||
cp -r *.py *.yaml *.md $DEPLOY_DIR/
|
||
cp -r requirements.txt $DEPLOY_DIR/
|
||
|
||
# 2. 下载Python依赖包
|
||
echo "2. 下载Python依赖包..."
|
||
pip download -r requirements.txt -d $DEPLOY_DIR/packages/ \
|
||
--platform linux_x86_64 --only-binary=:all:
|
||
|
||
# 3. 下载昇腾驱动和工具包
|
||
echo "3. 下载昇腾相关包..."
|
||
cd $DEPLOY_DIR/drivers/
|
||
|
||
# Atlas 300I驱动
|
||
wget https://ascend-repo.huawei.com/Atlas%20200I%20DK/Ascend-hdk-23.0.0-ubuntu20.04.aarch64.run
|
||
|
||
# CANN工具包
|
||
wget https://ascend-repo.huawei.com/CANN/CANN%205.0.2/Ascend-cann-toolkit_5.0.2_linux-aarch64.run
|
||
|
||
# 4. 准备模型文件
|
||
echo "4. 准备AI模型..."
|
||
cp -r YOLO_Weight/ $DEPLOY_DIR/models/
|
||
|
||
# 5. 创建配置文件模板
|
||
echo "5. 创建配置模板..."
|
||
cat > $DEPLOY_DIR/configs/config_template.yaml << EOF
|
||
cameras:
|
||
- id: 1
|
||
name: "摄像头1"
|
||
rtsp_url: "rtsp://admin:password@192.168.1.100:554/stream1"
|
||
- id: 2
|
||
name: "摄像头2"
|
||
rtsp_url: "rtsp://admin:password@192.168.1.101:554/stream1"
|
||
EOF
|
||
|
||
# 6. 创建安装脚本
|
||
echo "6. 创建安装脚本..."
|
||
cat > $DEPLOY_DIR/scripts/install.sh << 'EOF'
|
||
#!/bin/bash
|
||
echo "=== AI监控系统安装 ==="
|
||
|
||
# 安装系统依赖
|
||
sudo rpm -ivh packages/*.rpm 2>/dev/null || sudo dpkg -i packages/*.deb
|
||
|
||
# 安装Python依赖
|
||
python3 -m pip install packages/*.whl --no-index --find-links=packages/
|
||
|
||
# 安装昇腾驱动
|
||
sudo bash drivers/Ascend-hdk-*.run --silent
|
||
|
||
# 安装CANN工具包
|
||
sudo bash drivers/Ascend-cann-toolkit-*.run --silent
|
||
|
||
# 配置环境
|
||
echo "source /usr/local/Ascend/ascend-toolkit/set_env.sh" >> ~/.bashrc
|
||
|
||
echo "安装完成!"
|
||
EOF
|
||
|
||
chmod +x $DEPLOY_DIR/scripts/install.sh
|
||
|
||
# 7. 创建启动脚本
|
||
cat > $DEPLOY_DIR/scripts/start.sh << 'EOF'
|
||
#!/bin/bash
|
||
cd /opt/AIMonitor
|
||
source ~/.bashrc
|
||
python3 rtsp_service_ws.py &
|
||
python3 static_server.py &
|
||
EOF
|
||
|
||
chmod +x $DEPLOY_DIR/scripts/start.sh
|
||
|
||
# 8. 创建说明文档
|
||
cat > $DEPLOY_DIR/README.txt << EOF
|
||
AI监控系统离线部署包 v1.0
|
||
|
||
安装步骤:
|
||
1. 运行 sudo ./scripts/install.sh
|
||
2. 配置 configs/config.yaml
|
||
3. 运行 ./scripts/start.sh
|
||
|
||
技术支持:请参考 deploy_升腾.md
|
||
EOF
|
||
|
||
# 9. 压缩部署包
|
||
echo "7. 压缩部署包..."
|
||
tar -czf AIMonitor_deploy_v1.0.tar.gz $DEPLOY_DIR/
|
||
|
||
echo "✓ 部署包制作完成: AIMonitor_deploy_v1.0.tar.gz"
|
||
```
|
||
|
||
### 2. 制作BT种子
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
# create_torrent.sh
|
||
|
||
echo "=== 制作BT种子 ==="
|
||
|
||
# 安装BT制作工具
|
||
pip install transmission-rpc
|
||
|
||
# 创建种子文件
|
||
transmission-create -o AIMonitor_deploy_v1.0.torrent \
|
||
-t "https://tracker1.example.com:6969/announce" \
|
||
-t "https://tracker2.example.com:6969/announce" \
|
||
-c "AI监控系统离线部署包 v1.0 - 昇腾服务器专用" \
|
||
AIMonitor_deploy_v1.0.tar.gz
|
||
|
||
# 创建带校验和的种子
|
||
transmission-show -i AIMonitor_deploy_v1.0.torrent
|
||
|
||
echo "✓ BT种子创建完成: AIMonitor_deploy_v1.0.torrent"
|
||
```
|
||
|
||
### 3. 创建私有Tracker
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
# setup_tracker.sh
|
||
|
||
echo "=== 搭建私有BT Tracker ==="
|
||
|
||
# 使用opentracker作为tracker服务器
|
||
docker run -d \
|
||
--name bt-tracker \
|
||
-p 6969:6969 \
|
||
-v $(pwd)/tracker.conf:/etc/opentracker/tracker.conf \
|
||
--restart unless-stopped \
|
||
prologic/opentracker
|
||
|
||
# 创建tracker配置
|
||
cat > tracker.conf << EOF
|
||
listen.ipv4_addr = 0.0.0.0
|
||
listen.port = 6969
|
||
daemon = 0
|
||
access.log = access.log
|
||
error.log = error.log
|
||
debug.log = debug.log
|
||
EOF
|
||
|
||
echo "✓ Private Tracker started on port 6969"
|
||
```
|
||
|
||
## 🚀 P2P部署方案
|
||
|
||
### 方案一:服务器间P2P传输
|
||
|
||
#### 1. 在有网络的服务器上
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
# seed_deployment.sh
|
||
|
||
echo "=== 启动种子分享 ==="
|
||
|
||
# 1. 创建完整部署包
|
||
./create_deployment_package.sh
|
||
|
||
# 2. 制作BT种子
|
||
./create_torrent.sh
|
||
|
||
# 3. 启动Transmission作为种子服务器
|
||
transmission-daemon
|
||
transmission-remote -a AIMonitor_deploy_v1.0.torrent \
|
||
-w ./ \
|
||
--find
|
||
|
||
echo "✓ 开始分享AI监控系统部署包"
|
||
echo "Torrent Info:"
|
||
transmission-show -i AIMonitor_deploy_v1.0.torrent
|
||
```
|
||
|
||
#### 2. 在目标昇腾服务器上
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
# download_deployment.sh
|
||
|
||
echo "=== 下载AI监控系统部署包 ==="
|
||
|
||
# 1. 安装下载工具
|
||
pip install transmission-cli
|
||
|
||
# 2. 从种子文件下载
|
||
transmission-cli -f AIMonitor_deploy_v1.0.torrent \
|
||
-w /tmp/ \
|
||
--seedlimit 0
|
||
|
||
# 3. 校验文件完整性
|
||
echo "校验下载文件..."
|
||
if [ -f "/tmp/AIMonitor_deploy_v1.0.tar.gz" ]; then
|
||
echo "✓ 文件下载完成"
|
||
|
||
# 4. 解压并安装
|
||
cd /tmp
|
||
tar -xzf AIMonitor_deploy_v1.0.tar.gz
|
||
cd AIMonitor_deploy_v1.0
|
||
|
||
# 5. 运行安装脚本
|
||
sudo ./scripts/install.sh
|
||
|
||
echo "✓ AI监控系统安装完成"
|
||
else
|
||
echo "✗ 文件下载失败"
|
||
fi
|
||
```
|
||
|
||
### 方案二:多节点分布式部署
|
||
|
||
#### 1. 主节点(有网络)
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
# master_node.sh
|
||
|
||
echo "=== 主节点配置 ==="
|
||
|
||
# 1. 启动Tracker服务
|
||
docker-compose up -d
|
||
|
||
# 2. 创建多个种子版本
|
||
python3 create_multi_version_torrents.py
|
||
|
||
# 3. 启动种子服务
|
||
transmission-daemon
|
||
|
||
# 添加所有种子
|
||
for torrent in *.torrent; do
|
||
transmission-remote -a "$torrent" -w ./
|
||
done
|
||
|
||
echo "✓ 主节点启动完成"
|
||
```
|
||
|
||
#### 2. 从节点(无网络)
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
# slave_node.sh
|
||
|
||
echo "=== 从节点部署 ==="
|
||
|
||
# 1. 连接到主节点Tracker
|
||
TRACKER_URL="http://master-server:6969/announce"
|
||
|
||
# 2. 下载种子文件
|
||
curl -O http://master-server:torrents/AIMonitor_deploy_v1.0.torrent
|
||
|
||
# 3. 多线程下载
|
||
transmission-cli \
|
||
-f AIMonitor_deploy_v1.0.torrent \
|
||
-w /tmp/ \
|
||
--peer-info \
|
||
--encryption-preferred
|
||
|
||
# 4. 验证并安装
|
||
if [ -f "/tmp/AIMonitor_deploy_v1.0.tar.gz" ]; then
|
||
cd /tmp && tar -xzf AIMonitor_deploy_v1.0.tar.gz
|
||
cd AIMonitor_deploy_v1.0
|
||
sudo ./scripts/install.sh
|
||
else
|
||
echo "下载失败,请检查网络连接"
|
||
fi
|
||
```
|
||
|
||
## 🛠️ BT部署工具
|
||
|
||
### 1. 自动化部署脚本
|
||
|
||
```python
|
||
#!/usr/bin/env python3
|
||
# bt_deployment_tool.py
|
||
|
||
import os
|
||
import sys
|
||
import hashlib
|
||
import subprocess
|
||
from pathlib import Path
|
||
|
||
class BTDeploymentTool:
|
||
def __init__(self):
|
||
self.deployment_dir = "AIMonitor_bt_deploy"
|
||
self.torrent_file = None
|
||
|
||
def create_deployment_package(self):
|
||
"""创建部署包"""
|
||
print("创建部署包...")
|
||
|
||
# 创建目录结构
|
||
Path(self.deployment_dir).mkdir(exist_ok=True)
|
||
Path(f"{self.deployment_dir}/packages").mkdir(exist_ok=True)
|
||
Path(f"{self.deployment_dir}/scripts").mkdir(exist_ok=True)
|
||
|
||
# 复制必要文件
|
||
essential_files = [
|
||
"rtsp_service_ws.py", "static_server.py", "monitor_gui.py",
|
||
"npu_yolo_onnx.py", "config.yaml", "requirements.txt"
|
||
]
|
||
|
||
for file in essential_files:
|
||
if os.path.exists(file):
|
||
os.system(f"cp {file} {self.deployment_dir}/")
|
||
|
||
print(f"✓ 部署包创建完成: {self.deployment_dir}")
|
||
|
||
def create_torrent(self, tracker_urls=None):
|
||
"""创建BT种子"""
|
||
print("创建BT种子...")
|
||
|
||
tracker_urls = tracker_urls or [
|
||
"https://tracker1.example.com:6969/announce",
|
||
"https://tracker2.example.com:6969/announce"
|
||
]
|
||
|
||
# 使用libtorrent创建种子
|
||
import libtorrent as lt
|
||
|
||
# 创建torrent
|
||
fs = lt.file_storage()
|
||
lt.add_files(fs, self.deployment_dir)
|
||
|
||
t = lt.create_torrent(fs)
|
||
t.add_tracker(tracker_urls[0])
|
||
t.set_creator("AI监控系统部署工具")
|
||
t.set_comment("昇腾AI监控系统离线部署包")
|
||
|
||
# 生成torrent文件
|
||
self.torrent_file = f"{self.deployment_dir}.torrent"
|
||
with open(self.torrent_file, 'wb') as f:
|
||
f.write(lt.bencode(t.generate()))
|
||
|
||
print(f"✓ BT种子创建完成: {self.torrent_file}")
|
||
return self.torrent_file
|
||
|
||
def calculate_checksum(self, file_path):
|
||
"""计算文件校验和"""
|
||
sha256_hash = hashlib.sha256()
|
||
with open(file_path, "rb") as f:
|
||
for chunk in iter(lambda: f.read(4096), b""):
|
||
sha256_hash.update(chunk)
|
||
return sha256_hash.hexdigest()
|
||
|
||
def deploy_from_torrent(self, torrent_path, download_dir="/tmp"):
|
||
"""从种子部署"""
|
||
print(f"从种子部署: {torrent_path}")
|
||
|
||
# 使用transmission下载
|
||
cmd = [
|
||
"transmission-cli", "-f", torrent_path,
|
||
"-w", download_dir, "--seedlimit", "0"
|
||
]
|
||
|
||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||
|
||
if result.returncode == 0:
|
||
# 验证下载文件
|
||
expected_file = os.path.join(download_dir, self.deployment_dir + ".tar.gz")
|
||
if os.path.exists(expected_file):
|
||
print("✓ 下载完成,开始安装...")
|
||
|
||
# 解压并安装
|
||
os.chdir(download_dir)
|
||
os.system(f"tar -xzf {os.path.basename(expected_file)}")
|
||
|
||
install_script = os.path.join(self.deployment_dir, "scripts", "install.sh")
|
||
if os.path.exists(install_script):
|
||
os.system(f"sudo {install_script}")
|
||
print("✓ 部署完成")
|
||
else:
|
||
print("✗ 安装脚本不存在")
|
||
else:
|
||
print("✗ 下载文件不完整")
|
||
else:
|
||
print(f"✗ 下载失败: {result.stderr}")
|
||
|
||
def main():
|
||
tool = BTDeploymentTool()
|
||
|
||
if len(sys.argv) < 2:
|
||
print("使用方法:")
|
||
print(" python3 bt_deployment_tool.py create # 创建部署包")
|
||
print(" python3 bt_deployment_tool.py deploy # 从种子部署")
|
||
return
|
||
|
||
command = sys.argv[1]
|
||
|
||
if command == "create":
|
||
tool.create_deployment_package()
|
||
torrent_file = tool.create_torrent()
|
||
print(f"\n种子文件: {torrent_file}")
|
||
|
||
elif command == "deploy":
|
||
if len(sys.argv) < 3:
|
||
print("请提供种子文件路径")
|
||
return
|
||
tool.deploy_from_torrent(sys.argv[2])
|
||
|
||
else:
|
||
print(f"未知命令: {command}")
|
||
|
||
if __name__ == "__main__":
|
||
main()
|
||
```
|
||
|
||
### 2. Docker容器化BT部署
|
||
|
||
```dockerfile
|
||
# Dockerfile
|
||
FROM ubuntu:20.04
|
||
|
||
RUN apt-get update && apt-get install -y \
|
||
python3-pip \
|
||
transmission-cli \
|
||
curl \
|
||
tar \
|
||
&& rm -rf /var/lib/apt/lists/*
|
||
|
||
RUN pip3 install transmission-rpc libtorrent
|
||
|
||
WORKDIR /app
|
||
|
||
COPY . /app/
|
||
|
||
CMD ["python3", "bt_deployment_tool.py"]
|
||
```
|
||
|
||
```yaml
|
||
# docker-compose.yml
|
||
version: '3.8'
|
||
|
||
services:
|
||
bt-tracker:
|
||
image: prologic/opentracker
|
||
ports:
|
||
- "6969:6969"
|
||
volumes:
|
||
- ./tracker.conf:/etc/opentracker/tracker.conf
|
||
|
||
bt-seeder:
|
||
build: .
|
||
volumes:
|
||
- ./deployment:/app/deployment
|
||
command: ["python3", "bt_deployment_tool.py", "create"]
|
||
depends_on:
|
||
- bt-tracker
|
||
|
||
bt-downloader:
|
||
build: .
|
||
volumes:
|
||
- ./downloads:/app/downloads
|
||
command: ["python3", "bt_deployment_tool.py", "deploy", "/torrents/AIMonitor_deploy_v1.0.torrent"]
|
||
depends_on:
|
||
- bt-tracker
|
||
```
|
||
|
||
## 🔐 安全配置
|
||
|
||
### 1. 加密传输
|
||
|
||
```bash
|
||
# 启用加密传输
|
||
transmission-cli \
|
||
--encryption-preferred \
|
||
--encryption-required \
|
||
-f deployment.torrent
|
||
```
|
||
|
||
### 2. 访问控制
|
||
|
||
```python
|
||
# tracker访问控制
|
||
ALLOWED_PEERS = [
|
||
"192.168.1.0/24",
|
||
"10.0.0.0/8",
|
||
"172.16.0.0/12"
|
||
]
|
||
|
||
def is_peer_allowed(peer_ip):
|
||
"""检查对等节点是否被允许"""
|
||
import ipaddress
|
||
peer_addr = ipaddress.ip_address(peer_ip)
|
||
|
||
for network in ALLOWED_PEERS:
|
||
if peer_addr in ipaddress.ip_network(network):
|
||
return True
|
||
return False
|
||
```
|
||
|
||
### 3. 完整性验证
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
# verify_deployment.sh
|
||
|
||
TORRENT_FILE=$1
|
||
DEPLOYMENT_FILE=$2
|
||
|
||
echo "验证部署包完整性..."
|
||
|
||
# 1. 验证torrent信息
|
||
transmission-show -i $TORRENT_FILE > torrent_info.txt
|
||
|
||
# 2. 计算实际文件校验和
|
||
ACTUAL_HASH=$(sha256sum $DEPLOYMENT_FILE | cut -d' ' -f1)
|
||
|
||
# 3. 验证文件大小
|
||
EXPECTED_SIZE=$(grep "Size:" torrent_info.txt | awk '{print $2}')
|
||
ACTUAL_SIZE=$(stat -c%s $DEPLOYMENT_FILE)
|
||
|
||
if [ "$EXPECTED_SIZE" = "$ACTUAL_SIZE" ]; then
|
||
echo "✓ 文件大小正确"
|
||
else
|
||
echo "✗ 文件大小不匹配"
|
||
exit 1
|
||
fi
|
||
|
||
echo "✓ 部署包验证通过"
|
||
```
|
||
|
||
## 📊 监控和管理
|
||
|
||
### 1. BT传输监控
|
||
|
||
```python
|
||
#!/usr/bin/env python3
|
||
# bt_monitor.py
|
||
|
||
import time
|
||
import transmission_rpc
|
||
from datetime import datetime
|
||
|
||
class BTMonitor:
|
||
def __init__(self, host='localhost', port=9091):
|
||
self.tc = transmission_rpc.Client(host=host, port=port)
|
||
|
||
def monitor_download(self, torrent_hash):
|
||
"""监控下载进度"""
|
||
while True:
|
||
try:
|
||
torrent = self.tc.get_torrent(torrent_hash)
|
||
progress = torrent.progress
|
||
status = torrent.status
|
||
|
||
print(f"[{datetime.now()}] 进度: {progress:.2f}%, 状态: {status}")
|
||
|
||
if status == 'seeding':
|
||
print("下载完成")
|
||
break
|
||
|
||
except Exception as e:
|
||
print(f"监控错误: {e}")
|
||
|
||
time.sleep(10)
|
||
|
||
def list_active_torrents(self):
|
||
"""列出活跃的种子"""
|
||
torrents = self.tc.get_torrents()
|
||
for torrent in torrents:
|
||
print(f"{torrent.name}: {torrent.progress:.2f}%")
|
||
|
||
if __name__ == "__main__":
|
||
monitor = BTMonitor()
|
||
monitor.list_active_torrents()
|
||
```
|
||
|
||
### 2. 自动重试机制
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
# auto_retry_download.sh
|
||
|
||
TORRENT_FILE=$1
|
||
MAX_RETRIES=5
|
||
RETRY_DELAY=60
|
||
|
||
for ((i=1; i<=MAX_RETRIES; i++)); do
|
||
echo "尝试下载 (第 $i 次)..."
|
||
|
||
if transmission-cli -f $TORRENT_FILE -w /tmp/ --seedlimit 0; then
|
||
echo "✓ 下载成功"
|
||
exit 0
|
||
else
|
||
echo "✗ 下载失败,${RETRY_DELAY}秒后重试..."
|
||
sleep $RETRY_DELAY
|
||
fi
|
||
done
|
||
|
||
echo "✗ 达到最大重试次数,下载失败"
|
||
exit 1
|
||
```
|
||
|
||
## 🎯 部署流程
|
||
|
||
### 完整部署步骤
|
||
|
||
#### 1. 准备阶段(有网络环境)
|
||
```bash
|
||
# 1. 创建部署包
|
||
./create_deployment_package.sh
|
||
|
||
# 2. 制作BT种子
|
||
./create_torrent.sh
|
||
|
||
# 3. 启动种子服务
|
||
./seed_deployment.sh
|
||
```
|
||
|
||
#### 2. 传输阶段(P2P)
|
||
```bash
|
||
# 1. 分发种子文件
|
||
scp AIMonitor_deploy_v1.0.torrent user@target-server:/tmp/
|
||
|
||
# 2. 在目标服务器下载
|
||
./download_deployment.sh
|
||
```
|
||
|
||
#### 3. 安装阶段(离线环境)
|
||
```bash
|
||
# 1. 解压部署包
|
||
tar -xzf AIMonitor_deploy_v1.0.tar.gz
|
||
|
||
# 2. 运行安装脚本
|
||
sudo ./scripts/install.sh
|
||
|
||
# 3. 配置系统
|
||
./scripts/configure.sh
|
||
|
||
# 4. 启动服务
|
||
./scripts/start.sh
|
||
```
|
||
|
||
### 验证部署结果
|
||
|
||
```bash
|
||
# 1. 检查服务状态
|
||
systemctl status aimonitor
|
||
|
||
# 2. 验证端口监听
|
||
netstat -tulpn | grep -E "(8765|5000)"
|
||
|
||
# 3. 测试NPU功能
|
||
npu-smi
|
||
|
||
# 4. 启动GUI界面
|
||
python3 monitor_gui.py
|
||
```
|
||
|
||
---
|
||
|
||
**文档版本**: v1.0
|
||
**适用平台**: 昇腾Atlas系列服务器
|
||
**网络要求**: 支持P2P传输的内网环境 |