CollisionAvoidance/scripts/deploy.sh
2024-12-20 19:51:58 +08:00

256 lines
6.8 KiB
Bash

#!/bin/bash
# Set up logging
LOGFILE="/tmp/deploy.log"
# 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"
echo "[INFO] $1" >> "$LOGFILE"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
echo "[WARN] $1" >> "$LOGFILE"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
echo "[ERROR] $1" >> "$LOGFILE"
}
# 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}")"
# Install RPM packages
log_info "Installing RPM packages..."
if [ ! -d "${PROJECT_ROOT}/rpm" ]; then
log_error "RPM directory not found: ${PROJECT_ROOT}/rpm"
exit 1
fi
cd "${PROJECT_ROOT}/rpm"
# Install Python3 and its dependencies
if ! ls python3*.rpm >/dev/null 2>&1; then
log_error "Python3 RPM packages not found"
exit 1
fi
rpm -Uvh --nodeps python3*.rpm
# Install Python packages from wheel files
log_info "Installing Python packages..."
if [ ! -d "${PROJECT_ROOT}/python" ]; then
log_error "Python packages directory not found: ${PROJECT_ROOT}/python"
exit 1
fi
# Install wheel files in correct order
log_info "Installing Python packages..."
cd "${PROJECT_ROOT}/python"
for pkg in *.whl; do
if [ ! -f "$pkg" ]; then
log_error "Required package not found: $pkg"
exit 1
fi
log_info "Installing $pkg..."
pip3 install --no-index "$pkg"
done
# Verify Python packages
log_info "Verifying Python packages..."
python3 -c "import flask; import werkzeug; import jinja2; import markupsafe" || {
log_error "Failed to verify Python packages"
exit 1
}
# Create work directory
WORK_DIR="/opt/collision_avoidance"
log_info "Creating work directory: $WORK_DIR"
mkdir -p $WORK_DIR
mkdir -p $WORK_DIR/bin
mkdir -p $WORK_DIR/lib
mkdir -p $WORK_DIR/tools
mkdir -p $WORK_DIR/config
mkdir -p $WORK_DIR/logs
# Set directory permissions
log_info "Setting directory permissions..."
chmod 755 $WORK_DIR
chmod 755 $WORK_DIR/bin
chmod 755 $WORK_DIR/lib
chmod 755 $WORK_DIR/tools
chmod 755 $WORK_DIR/config
chmod 755 $WORK_DIR/logs
chown -R root:root $WORK_DIR
# Copy files
log_info "Copying files..."
# Check SELinux status
log_info "Checking SELinux status..."
if command -v getenforce &> /dev/null; then
selinux_status=$(getenforce)
log_info "SELinux status: $selinux_status"
if [ "$selinux_status" = "Enforcing" ]; then
log_info "Setting SELinux contexts..."
# 设置用目录的 SELinux 上下文
log_info "Setting context for application directories..."
chcon -R -t usr_t $WORK_DIR
chcon -t bin_t $WORK_DIR/bin
chcon -t lib_t $WORK_DIR/lib
chcon -t etc_t $WORK_DIR/config
# 恢复默认上下文
log_info "Restoring default contexts..."
restorecon -Rv $WORK_DIR
fi
else
log_warn "SELinux tools not found"
fi
for dir in bin lib python tools config; do
if [ ! -d "${PROJECT_ROOT}/${dir}" ]; then
log_error "Directory not found: ${PROJECT_ROOT}/${dir}"
exit 1
fi
done
# Copy executable
log_info "Copying executable..."
if [ "$EXECUTABLE_COPIED" != "1" ]; then
if ! cp -v "${PROJECT_ROOT}/bin/"* "$WORK_DIR/bin/"; then
log_error "Failed to copy executable"
exit 1
fi
fi
# Copy libraries
log_info "Copying libraries..."
if ! cp -v "${PROJECT_ROOT}/lib/"* "$WORK_DIR/lib/"; then
log_error "Failed to copy libraries"
exit 1
fi
# Copy tools
log_info "Copying tools..."
if ! cp -v "${PROJECT_ROOT}/tools/"* "$WORK_DIR/tools/"; then
log_error "Failed to copy tools"
exit 1
fi
# Create config directory
CONFIG_DIR="$WORK_DIR/config"
log_info "Creating config directory: $CONFIG_DIR"
mkdir -p $CONFIG_DIR
log_info "Copying configuration files..."
for config_file in system_config.json vehicle_control.json airport_bounds.json intersections.json; do
if [ ! -f "${PROJECT_ROOT}/config/$config_file" ]; then
log_error "Config file not found: ${PROJECT_ROOT}/config/$config_file"
exit 1
fi
if ! cp -v "${PROJECT_ROOT}/config/$config_file" "$CONFIG_DIR/"; then
log_error "Failed to copy config file: $config_file"
exit 1
fi
done
# 设置配置文件权限
log_info "Setting config file permissions..."
chmod 644 "$CONFIG_DIR"/*.json
chown -R root:root "$CONFIG_DIR"
# 验证配置文件
log_info "Verifying config files..."
if ! ls -l "$CONFIG_DIR"/*.json; then
log_error "No config files found in $CONFIG_DIR"
exit 1
fi
# Create service file
log_info "Creating system service..."
cat > /etc/systemd/system/collision-avoidance.service << EOL
[Unit]
Description=Collision Avoidance Service
After=network.target
[Service]
Type=simple
ExecStart=$WORK_DIR/bin/collision_avoidance
WorkingDirectory=$WORK_DIR
User=root
Restart=always
RestartSec=3
Environment=CONFIG_PATH=$WORK_DIR/config
Environment=LD_LIBRARY_PATH=$WORK_DIR/lib
StandardOutput=append:$WORK_DIR/logs/collision-avoidance.log
StandardError=append:$WORK_DIR/logs/collision-avoidance.error.log
[Install]
WantedBy=multi-user.target
EOL
# Configure firewall
log_info "Configuring firewall..."
if command -v firewall-cmd &> /dev/null; then
if ! systemctl is-active --quiet firewalld; then
log_info "Starting firewalld service..."
systemctl start firewalld
fi
firewall-cmd --permanent --add-port=8010/tcp
firewall-cmd --permanent --add-port=8081/tcp
firewall-cmd --reload
else
log_warn "firewalld not detected, please configure firewall manually"
fi
# Start service
log_info "Starting service..."
systemctl daemon-reload
systemctl enable collision-avoidance
systemctl start collision-avoidance
# Check service status
if systemctl is-active --quiet collision-avoidance; then
log_info "Service started successfully"
else
log_error "Service failed to start, check logs"
journalctl -u collision-avoidance -n 50
exit 1
fi
# Display service status
log_info "Deployment completed, service status:"
systemctl status collision-avoidance
# Display usage instructions
echo -e "\n${GREEN}Deployment completed!${NC}"
echo "Use these commands to manage the service:"
echo " Start service: systemctl start collision-avoidance"
echo " Stop service: systemctl stop collision-avoidance"
echo " Restart service: systemctl restart collision-avoidance"
echo " Check status: systemctl status collision-avoidance"
echo " View logs: journalctl -u collision-avoidance -f"
echo -e "\nConfig files location: ${CONFIG_DIR}"
echo "WebSocket port: 8010"
echo "Mock Server port: 8081"
# 创建日志文件
log_info "Creating log files..."
touch $WORK_DIR/logs/collision-avoidance.log
touch $WORK_DIR/logs/collision-avoidance.error.log
chmod 644 $WORK_DIR/logs/*.log
chown root:root $WORK_DIR/logs/*.log