#!/bin/bash # Color definitions for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Logging functions log_info() { echo -e "${GREEN}[INFO]${NC} $1" } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if running on CentOS if [ ! -f /etc/centos-release ]; then log_error "This script must be run on CentOS" exit 1 fi # Check if running as root if [ "$EUID" -ne 0 ]; then log_error "Please run as root" exit 1 fi # Get script directory and project root SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "${SCRIPT_DIR}")" # Detect system architecture CURRENT_ARCH=$(uname -m) case ${CURRENT_ARCH} in x86_64|i686|i386) log_info "System architecture: ${CURRENT_ARCH}" ;; *) log_error "Unsupported architecture: ${CURRENT_ARCH}" exit 1 ;; esac # Find the correct packages directory PACKAGES_DIR="${PROJECT_ROOT}/packages_${CURRENT_ARCH}" if [ ! -d "${PACKAGES_DIR}" ]; then log_error "Packages directory not found for architecture ${CURRENT_ARCH}: ${PACKAGES_DIR}" log_error "Please run prepare_offline_packages.sh with the correct architecture first" exit 1 fi # Check if offline packages exist if [ -d "${PACKAGES_DIR}" ]; then log_info "检测到离线安装包目录,使用离线安装模式..." # Install offline packages log_info "安装离线依赖包..." if [ -f "${PACKAGES_DIR}/offline-deps.txt" ]; then while IFS= read -r pkg; do if [ -f "${PACKAGES_DIR}/$pkg" ]; then log_info "正在安装: $pkg" rpm -ivh "${PACKAGES_DIR}/$pkg" || { log_error "安装包失败: $pkg" exit 1 } else log_error "找不到安装包: $pkg" exit 1 fi done < "${PACKAGES_DIR}/offline-deps.txt" else log_error "找不到离线依赖包列表文件: offline-deps.txt" exit 1 fi # Install Python packages if [ -f "${PACKAGES_DIR}/requirements.txt" ]; then log_info "安装 Python 依赖包..." pip3 install --no-index --find-links="${PACKAGES_DIR}/python_packages" -r "${PACKAGES_DIR}/requirements.txt" || { log_error "安装 Python 包失败" exit 1 } fi else # Online installation log_info "使用在线安装模式..." log_info "正在安装依赖包..." yum groupinstall -y "Development Tools" yum install -y cmake3 nlohmann-json-devel boost-devel openssl-devel python3 python3-pip # Install Python packages log_info "安装 Python 依赖..." pip3 install flask==2.0.1 fi # Create work directory WORK_DIR="/opt/collision_avoidance" log_info "创建工作目录: $WORK_DIR" mkdir -p $WORK_DIR cd $WORK_DIR || { log_error "无法进入工作目录" exit 1 } # Compile project log_info "开始编译项目..." mkdir -p build cd build || { log_error "无法进入构建目录" exit 1 } cmake .. make -j4 if [ $? -ne 0 ]; then log_error "编译失败" exit 1 fi # Create config directory CONFIG_DIR="/etc/collision_avoidance" log_info "创建配置目录: $CONFIG_DIR" mkdir -p $CONFIG_DIR # Copy config files log_info "复制配置文件..." cp ../config/* $CONFIG_DIR/ || { log_error "复制配置文件失败" exit 1 } # Copy executable log_info "安装可执行文件..." cp collision_avoidance /usr/local/bin/ || { log_error "复制可执行文件失败" exit 1 } # Create service file log_info "创建系统服务..." cat > /etc/systemd/system/collision-avoidance.service << EOL [Unit] Description=Collision Avoidance Service After=network.target [Service] Type=simple ExecStart=/usr/local/bin/collision_avoidance WorkingDirectory=/usr/local/bin User=root Restart=always RestartSec=3 Environment=CONFIG_PATH=/etc/collision_avoidance [Install] WantedBy=multi-user.target EOL # Configure firewall log_info "配置防火墙..." if command -v firewall-cmd &> /dev/null; then firewall-cmd --permanent --add-port=8010/tcp firewall-cmd --permanent --add-port=8081/tcp firewall-cmd --reload else log_warn "未检测到 firewalld,请手动配置防火墙" fi # Start service log_info "启动服务..." systemctl daemon-reload systemctl enable collision-avoidance systemctl start collision-avoidance # Check service status if systemctl is-active --quiet collision-avoidance; then log_info "服务启动成功" else log_error "服务启动失败,请检查日志" journalctl -u collision-avoidance -n 50 exit 1 fi # Display service status log_info "部署完成,服务状态:" systemctl status collision-avoidance # Display usage instructions echo -e "\n${GREEN}部署完成!${NC}" echo "使用以下命令管理服务:" echo " 启动服务:systemctl start collision-avoidance" echo " 停止服务:systemctl stop collision-avoidance" echo " 重启服务:systemctl restart collision-avoidance" echo " 查看状态:systemctl status collision-avoidance" echo " 查看日志:journalctl -u collision-avoidance -f" echo -e "\n配置文件位置:${CONFIG_DIR}" echo "WebSocket 服务端口:8010" echo "Mock Server 端口:8081"