diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 38b2ea0..757c59a 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -1,12 +1,12 @@ #!/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" } @@ -19,30 +19,53 @@ log_error() { echo -e "${RED}[ERROR]${NC} $1" } -# 检查是否为 root 用户 -if [ "$EUID" -ne 0 ]; then - log_error "请使用 root 用户运行此脚本" - exit 1 -fi - -# 检查系统版本 +# Check if running on CentOS if [ ! -f /etc/centos-release ]; then - log_error "此脚本仅支持 CentOS 系统" + log_error "This script must be run on CentOS" exit 1 fi -# 检查是否存在离线安装包目录 -OFFLINE_PACKAGES="./packages" -if [ -d "$OFFLINE_PACKAGES" ]; then +# 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 "$OFFLINE_PACKAGES/offline-deps.txt" ]; then + if [ -f "${PACKAGES_DIR}/offline-deps.txt" ]; then while IFS= read -r pkg; do - if [ -f "$OFFLINE_PACKAGES/$pkg" ]; then + if [ -f "${PACKAGES_DIR}/$pkg" ]; then log_info "正在安装: $pkg" - rpm -ivh "$OFFLINE_PACKAGES/$pkg" || { + rpm -ivh "${PACKAGES_DIR}/$pkg" || { log_error "安装包失败: $pkg" exit 1 } @@ -50,20 +73,33 @@ if [ -d "$OFFLINE_PACKAGES" ]; then log_error "找不到安装包: $pkg" exit 1 fi - done < "$OFFLINE_PACKAGES/offline-deps.txt" + 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 git nlohmann-json-devel boost-devel openssl-devel + 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 @@ -72,7 +108,7 @@ cd $WORK_DIR || { exit 1 } -# 编译项目 +# Compile project log_info "开始编译项目..." mkdir -p build cd build || { @@ -87,26 +123,26 @@ if [ $? -ne 0 ]; then 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] @@ -126,22 +162,23 @@ Environment=CONFIG_PATH=/etc/collision_avoidance 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 @@ -150,11 +187,11 @@ else 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" @@ -163,4 +200,5 @@ echo " 重启服务:systemctl restart collision-avoidance" echo " 查看状态:systemctl status collision-avoidance" echo " 查看日志:journalctl -u collision-avoidance -f" echo -e "\n配置文件位置:${CONFIG_DIR}" -echo "WebSocket 服务端口:8010" \ No newline at end of file +echo "WebSocket 服务端口:8010" +echo "Mock Server 端口:8081" \ No newline at end of file diff --git a/scripts/prepare_offline_packages.sh b/scripts/prepare_offline_packages.sh index bdd979a..936fbac 100644 --- a/scripts/prepare_offline_packages.sh +++ b/scripts/prepare_offline_packages.sh @@ -19,6 +19,55 @@ log_error() { echo -e "${RED}[ERROR]${NC} $1" } +# Print usage +usage() { + echo "Usage: $0 [-a|--arch ]" + echo "Options:" + echo " -a, --arch Target architecture (default: $(uname -m))" + echo "Example:" + echo " $0 --arch x86_64 # Prepare packages for 64-bit systems" + echo " $0 --arch i686 # Prepare packages for 32-bit systems" +} + +# Parse command line arguments +TARGET_ARCH=$(uname -m) # Default to current system architecture +while [[ $# -gt 0 ]]; do + case $1 in + -a|--arch) + TARGET_ARCH="$2" + shift 2 + ;; + -h|--help) + usage + exit 0 + ;; + *) + log_error "Unknown option: $1" + usage + exit 1 + ;; + esac +done + +# Validate target architecture +case ${TARGET_ARCH} in + x86_64) + ARCH_SUFFIX="x86_64" + YUM_ARCH="--setarch=x86_64" + ;; + i686|i386) + ARCH_SUFFIX="i686" + YUM_ARCH="--setarch=i686" + ;; + *) + log_error "Unsupported architecture: ${TARGET_ARCH}" + log_error "Supported architectures: x86_64, i686" + exit 1 + ;; +esac + +log_info "Target architecture: ${TARGET_ARCH}" + # Check if running on CentOS if [ ! -f /etc/centos-release ]; then log_error "This script must be run on CentOS" @@ -40,8 +89,8 @@ if [ ! -f "${PROJECT_ROOT}/CMakeLists.txt" ] || \ exit 1 fi -# Create packages directory -PACKAGES_DIR="${PROJECT_ROOT}/packages" +# Create packages directory with architecture suffix +PACKAGES_DIR="${PROJECT_ROOT}/packages_${TARGET_ARCH}" log_info "Creating packages directory: ${PACKAGES_DIR}" mkdir -p "${PACKAGES_DIR}" cd "${PACKAGES_DIR}" || { @@ -52,7 +101,7 @@ cd "${PACKAGES_DIR}" || { # Function to get package dependencies get_dependencies() { local pkg=$1 - yum deplist "$pkg" | grep provider: | awk '{print $2}' | sort -u + yum ${YUM_ARCH} deplist "$pkg" | grep provider: | awk '{print $2}' | sort -u } # Download system dependencies @@ -62,7 +111,7 @@ log_info "Downloading system packages and their dependencies..." PACKAGES=( "cmake3" "nlohmann-json-devel" - "boost-devel" + "boost-devel.${ARCH_SUFFIX}" "openssl-devel" "python3" "python3-pip" @@ -77,7 +126,7 @@ for pkg in "${PACKAGES[@]}"; do log_info "Checking dependencies for $pkg..." if [[ "$pkg" == @* ]]; then # Handle package groups - yum groupinfo "${pkg#@}" | grep "Mandatory Packages:" -A 100 | grep "^[[:space:]]*[^[:space:]]*" >> "$TEMP_PKGS" + yum ${YUM_ARCH} groupinfo "${pkg#@}" | grep "Mandatory Packages:" -A 100 | grep "^[[:space:]]*[^[:space:]]*" >> "$TEMP_PKGS" else echo "$pkg" >> "$TEMP_PKGS" get_dependencies "$pkg" >> "$TEMP_PKGS" @@ -91,7 +140,7 @@ sort -u "$TEMP_PKGS" -o "$TEMP_PKGS" log_info "Downloading packages..." while read -r pkg; do log_info "Downloading $pkg..." - yum install --downloadonly --downloaddir=. "$pkg" || { + yum ${YUM_ARCH} install --downloadonly --downloaddir=. "$pkg" || { log_warn "Failed to download $pkg, continuing..." } done < "$TEMP_PKGS" @@ -143,7 +192,7 @@ cd "${PROJECT_ROOT}" || { # Create archive of the project log_info "Creating project archive..." -ARCHIVE_NAME="${PROJECT_NAME}.tar.gz" +ARCHIVE_NAME="${PROJECT_NAME}_${TARGET_ARCH}.tar.gz" ARCHIVE_PATH="${PROJECT_ROOT}/../${ARCHIVE_NAME}" tar czf "${ARCHIVE_PATH}" \ @@ -158,7 +207,7 @@ log_info "Successfully created ${ARCHIVE_NAME}" # Print summary echo -e "\n${GREEN}Offline package preparation completed!${NC}" -echo "Created files:" +echo "Created files for ${TARGET_ARCH} architecture:" echo " - ${PACKAGES_DIR}/offline-deps.txt (System package list)" echo " - ${PACKAGES_DIR}/requirements.txt (Python package list)" echo " - ${PACKAGES_DIR}/*.rpm (System packages)"