#!/bin/bash # KVM Manager 后端一键安装脚本 (仅后端) set -e RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' echo -e "${GREEN}======================================" echo -e " KVM Manager 后端一键安装" echo -e "======================================${NC}\n" if [[ $EUID -ne 0 ]]; then echo -e "${RED}[错误] 请使用 root 用户运行此脚本${NC}" echo -e "${YELLOW}提示: sudo bash $0${NC}" exit 1 fi SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" if [[ -f /etc/debian_version ]]; then PKG_MANAGER="apt-get" elif [[ -f /etc/redhat-release ]]; then PKG_MANAGER="yum" elif [[ -f /etc/arch-release ]]; then PKG_MANAGER="pacman" else echo -e "${RED}[错误] 不支持的操作系统${NC}" exit 1 fi echo -e "${YELLOW}[1/4] 安装系统依赖...${NC}" if [[ "$PKG_MANAGER" == "apt-get" ]]; then apt-get update apt-get install -y libvirt-dev pkg-config python3-dev python3-venv elif [[ "$PKG_MANAGER" == "yum" ]]; then yum install -y libvirt-devel pkg-config python3-devel python3-pip elif [[ "$PKG_MANAGER" == "pacman" ]]; then pacman -Sy --noconfirm libvirt python python-pip fi echo -e "${YELLOW}[2/4] 检查 libvirtd 服务...${NC}" if systemctl is-active --quiet libvirtd; then echo -e "${GREEN} ✓ libvirtd 运行中${NC}" else echo -e "${YELLOW} 启动 libvirtd...${NC}" systemctl start libvirtd systemctl enable libvirtd fi echo -e "${YELLOW}[3/4] 配置 Python 环境...${NC}" cd "$SCRIPT_DIR" if [[ -d "venv" ]]; then echo -e " 删除旧 venv..." rm -rf venv fi python3 -m venv venv source venv/bin/activate pip install --upgrade pip pip install -r requirements.txt echo -e "${YELLOW}[4/4] 验证安装...${NC}" python -c " import libvirt print(f' ✓ libvirt 模块加载成功, 版本: {libvirt.getVersion() // 1000000}') " || { echo -e "${RED}[错误] libvirt 模块验证失败${NC}" exit 1 } echo "" echo -e "${GREEN}======================================" echo -e " 安装完成!" echo -e "======================================${NC}" echo "" echo -e "${YELLOW}启动服务:${NC}" echo -e " cd $SCRIPT_DIR" echo -e " source venv/bin/activate" echo -e " python -m uvicorn app.main:app --host 0.0.0.0 --port 8004" echo "" echo -e "${YELLOW}后台运行:${NC}" echo -e " nohup python -m uvicorn app.main:app --host 0.0.0.0 --port 8004 > app.log 2>&1 &" echo ""