CollisionAvoidance/scripts/prepare_offline_packages.sh
2024-12-19 19:53:13 +08:00

209 lines
5.6 KiB
Bash

#!/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"
}
# Print usage
usage() {
echo "Usage: $0"
echo "This script prepares packages"
}
# Parse command line arguments
if [ $# -gt 0 ]; then
log_error "This script does not accept any arguments"
usage
exit 1
fi
# Check if running on CentOS
if [ ! -f /etc/centos-release ]; then
log_error "This script must be run on CentOS"
exit 1
fi
# Get script directory and project root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "${SCRIPT_DIR}")"
PROJECT_NAME="$(basename "${PROJECT_ROOT}")"
# Check if running from the correct directory
if [ ! -f "${PROJECT_ROOT}/CMakeLists.txt" ] || \
[ ! -d "${PROJECT_ROOT}/src" ] || \
[ ! -d "${PROJECT_ROOT}/scripts" ]; then
log_error "This script must be run from the project root directory (CollisionAvoidance)"
log_error "Current directory: $(pwd)"
log_error "Please run: cd /path/to/CollisionAvoidance && ./scripts/prepare_offline_packages.sh"
exit 1
fi
# Create packages directory
PACKAGES_DIR="${PROJECT_ROOT}/packages"
log_info "Creating packages directory: ${PACKAGES_DIR}"
mkdir -p "${PACKAGES_DIR}"
# Install yum-plugin-downloadonly if not already installed
if ! rpm -q yum-plugin-downloadonly &>/dev/null; then
log_info "Installing yum-plugin-downloadonly..."
yum install -y yum-plugin-downloadonly || {
log_error "Failed to install yum-plugin-downloadonly"
exit 1
}
fi
# Function to get package dependencies
get_dependencies() {
local pkg=$1
yum deplist "$pkg" | grep provider: | awk '{print $2}' | sort -u
}
# Download system dependencies
log_info "Downloading system packages and their dependencies..."
# Define base packages
PACKAGES=(
"cmake3"
"json-c-devel"
"boost-devel"
"openssl-devel"
"python3"
"python3-pip"
"gcc"
"gcc-c++"
"make"
"autoconf"
"automake"
"libtool"
)
# Create a temporary file for all packages including dependencies
TEMP_PKGS=$(mktemp)
# Get all dependencies
for pkg in "${PACKAGES[@]}"; do
log_info "Checking dependencies for $pkg..."
echo "$pkg" >> "$TEMP_PKGS"
get_dependencies "$pkg" >> "$TEMP_PKGS"
done
# Remove duplicates and sort
sort -u "$TEMP_PKGS" -o "$TEMP_PKGS"
# Download all packages
log_info "Downloading packages..."
DOWNLOAD_FAILED=0
while read -r pkg; do
log_info "Downloading $pkg..."
if ! yum install --downloadonly --downloaddir="${PACKAGES_DIR}" "$pkg"; then
log_error "Failed to download $pkg"
DOWNLOAD_FAILED=1
fi
done < "$TEMP_PKGS"
rm "$TEMP_PKGS"
# Check if any RPM files were downloaded
cd "${PACKAGES_DIR}" || {
log_error "Failed to enter packages directory"
exit 1
}
if [ ! "$(ls -A *.rpm 2>/dev/null)" ]; then
if [ $DOWNLOAD_FAILED -eq 1 ]; then
log_error "Failed to download some packages. Please check your network connection and yum repository configuration."
else
log_error "No RPM packages were downloaded. Please check your yum configuration."
fi
exit 1
fi
# Create Python requirements file
log_info "Creating Python requirements file..."
cat > requirements.txt << EOL
flask==2.0.1
werkzeug==2.0.1
click==8.0.1
itsdangerous==2.0.1
Jinja2==3.0.1
MarkupSafe==2.0.1
EOL
# Create Python packages directory
PYTHON_PACKAGES_DIR="${PACKAGES_DIR}/python_packages"
mkdir -p "${PYTHON_PACKAGES_DIR}"
# Download Python packages
log_info "Downloading Python packages..."
if ! pip3 download -r requirements.txt -d "${PYTHON_PACKAGES_DIR}"; then
log_error "Failed to download Python packages"
exit 1
fi
# Create package list
log_info "Creating package list..."
if ! ls *.rpm > offline-deps.txt 2>/dev/null; then
log_error "No RPM packages found in the directory"
log_error "Please check the previous error messages"
exit 1
fi
# Sort package list to handle dependencies
log_info "Sorting package list for dependency order..."
if ! grep "boost-.*\.rpm" offline-deps.txt > boost-deps.txt 2>/dev/null; then
log_warn "No boost packages found"
fi
if ! grep -v "boost-.*\.rpm" offline-deps.txt > other-deps.txt 2>/dev/null; then
log_warn "No non-boost packages found"
fi
cat boost-deps.txt other-deps.txt > offline-deps.txt 2>/dev/null
rm -f boost-deps.txt other-deps.txt
# Return to project root directory
cd "${PROJECT_ROOT}" || {
log_error "Failed to return to project root directory"
exit 1
}
# Create archive of the project
log_info "Creating project archive..."
ARCHIVE_NAME="${PROJECT_NAME}.tar.gz"
ARCHIVE_PATH="${PROJECT_ROOT}/../${ARCHIVE_NAME}"
tar czf "${ARCHIVE_PATH}" \
--exclude=".git" \
--exclude="build" \
--exclude="*.tar.gz" \
-C "${PROJECT_ROOT}/.." "${PROJECT_NAME}" || {
log_error "Failed to create project archive"
exit 1
}
log_info "Successfully created ${ARCHIVE_NAME}"
# Print summary
echo -e "\n${GREEN}Offline package preparation completed!${NC}"
echo "Created files:"
echo " - ${PACKAGES_DIR}/offline-deps.txt (System package list)"
echo " - ${PACKAGES_DIR}/requirements.txt (Python package list)"
echo " - ${PACKAGES_DIR}/*.rpm (System packages)"
echo " - ${PACKAGES_DIR}/${PYTHON_PACKAGES_DIR}/* (Python packages)"
echo " - ${ARCHIVE_PATH} (Complete project archive)"
echo -e "\nNext steps:"
echo "1. Transfer ${ARCHIVE_NAME} to the target server"
echo "2. Extract the archive on the target server"
echo "3. Run the deployment script"