203 lines
5.2 KiB
Bash
203 lines
5.2 KiB
Bash
#!/bin/bash
|
||
|
||
# Set up logging
|
||
LOGFILE="/tmp/deploy.log"
|
||
# 重定向标准输出和错误输出到日志文件
|
||
exec &> >(tee -a "$LOGFILE")
|
||
|
||
# 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" | tee -a "$LOGFILE"
|
||
}
|
||
|
||
log_warn() {
|
||
echo -e "${YELLOW}[WARN]${NC} $1" | tee -a "$LOGFILE"
|
||
}
|
||
|
||
log_error() {
|
||
echo -e "${RED}[ERROR]${NC} $1" | tee -a "$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
|
||
|
||
# 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
|
||
|
||
# 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..."
|
||
|
||
# 设置<E8AEBE><E7BDAE><EFBFBD>用目录的 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 Python packages
|
||
log_info "Copying Python packages..."
|
||
if ! cp -v "${PROJECT_ROOT}/python/"* "/tmp/"; then
|
||
log_error "Failed to copy Python packages"
|
||
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
|
||
cp -r "${PROJECT_ROOT}/config/"* "$CONFIG_DIR/"
|
||
|
||
# Install Python packages
|
||
log_info "Installing Python packages..."
|
||
cd /tmp
|
||
for pkg in *.whl; do
|
||
if [ -f "$pkg" ]; then
|
||
pip3 install --no-index "$pkg"
|
||
fi
|
||
done
|
||
|
||
# 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/bin
|
||
User=root
|
||
Restart=always
|
||
RestartSec=3
|
||
Environment=CONFIG_PATH=$WORK_DIR/config
|
||
Environment=LD_LIBRARY_PATH=$WORK_DIR/lib
|
||
|
||
[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" |