#!/usr/bin/env bash # # hwcloud-console 一键部署脚本 # # 功能: # - 检查系统环境 # - 安装所有依赖(Go、Node.js、Python、浏览器等) # - 克隆项目代码 # - 编译程序 # - 配置 systemd 服务 # - 启动服务 # # 使用方法: # curl -fsSL https://raw.githubusercontent.com/qiandali1999/hwcloud-automation/main/install.sh | bash # # 或者: # wget -qO- https://raw.githubusercontent.com/qiandali1999/hwcloud-automation/main/install.sh | bash # set -euo pipefail # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;36m' BOLD='\033[1m' NC='\033[0m' # No Color # 配置变量 INSTALL_DIR="/opt/hwcloud-demo" REPO_URL="https://github.com/qiandali1999/hwcloud-automation.git" SERVICE_PORT="8899" GO_VERSION="1.22.0" NODE_VERSION="20" # 日志函数 info() { echo -e "${BLUE}[INFO]${NC} $*"; } success() { echo -e "${GREEN}[SUCCESS]${NC} $*"; } warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } error() { echo -e "${RED}[ERROR]${NC} $*" >&2; } die() { error "$*"; exit 1; } # 显示欢迎信息 show_banner() { cat << 'EOF' ╔════════════════════════════════════════════════════════════╗ ║ ║ ║ hwcloud-console 一键部署脚本 ║ ║ ║ ║ 华为云账号管理 & 自动化任务平台 ║ ║ ║ ╚════════════════════════════════════════════════════════════╝ EOF echo "" } # 检查是否为 root 用户 check_root() { if [ "$EUID" -ne 0 ]; then die "请使用 root 用户运行此脚本,或使用 sudo" fi } # 检测操作系统 detect_os() { info "检测操作系统..." if [ -f /etc/os-release ]; then . /etc/os-release OS=$ID OS_VERSION=$VERSION_ID else die "无法检测操作系统类型" fi case $OS in ubuntu|debian) PKG_MANAGER="apt-get" success "检测到系统: $OS $OS_VERSION" ;; centos|rhel|rocky|almalinux) PKG_MANAGER="yum" success "检测到系统: $OS $OS_VERSION" ;; *) die "不支持的操作系统: $OS" ;; esac } # 检查系统要求 check_requirements() { info "检查系统要求..." # 检查内存(至少 2GB) total_mem=$(free -m | awk '/^Mem:/{print $2}') if [ "$total_mem" -lt 1800 ]; then warn "系统内存较低 (${total_mem}MB),建议至少 2GB" else success "内存检查通过 (${total_mem}MB)" fi # 检查磁盘空间(至少 5GB) free_space=$(df -BG / | awk 'NR==2 {print $4}' | sed 's/G//') if [ "$free_space" -lt 5 ]; then die "磁盘空间不足,需要至少 5GB 可用空间" else success "磁盘空间检查通过 (${free_space}GB 可用)" fi } # 安装基础依赖 install_base_deps() { info "更新系统包管理器..." if [ "$PKG_MANAGER" = "apt-get" ]; then apt-get update -qq apt-get install -y -qq curl wget git build-essential \ python3 python3-pip python3-venv \ xvfb x11vnc supervisor \ fonts-wqy-zenhei fonts-noto-cjk \ ca-certificates gnupg lsb-release else yum update -y -q yum install -y -q curl wget git gcc gcc-c++ make \ python3 python3-pip \ xorg-x11-server-Xvfb x11vnc supervisor \ wqy-zenhei-fonts \ ca-certificates fi success "基础依赖安装完成" } # 安装 Go install_go() { if command -v go &> /dev/null; then local current_version=$(go version | awk '{print $3}' | sed 's/go//') info "Go 已安装 (版本: $current_version)" return fi info "安装 Go $GO_VERSION..." local arch=$(uname -m) case $arch in x86_64) arch="amd64" ;; aarch64) arch="arm64" ;; armv7l) arch="armv6l" ;; *) die "不支持的架构: $arch" ;; esac wget -q "https://go.dev/dl/go${GO_VERSION}.linux-${arch}.tar.gz" -O /tmp/go.tar.gz rm -rf /usr/local/go tar -C /usr/local -xzf /tmp/go.tar.gz rm /tmp/go.tar.gz # 添加到 PATH if ! grep -q "/usr/local/go/bin" /etc/profile; then echo 'export PATH=$PATH:/usr/local/go/bin' >> /etc/profile fi export PATH=$PATH:/usr/local/go/bin success "Go 安装完成 (版本: $(go version | awk '{print $3}'))" } # 安装 Node.js (用于某些任务脚本) install_nodejs() { if command -v node &> /dev/null; then local current_version=$(node -v) info "Node.js 已安装 (版本: $current_version)" return fi info "安装 Node.js $NODE_VERSION..." if [ "$PKG_MANAGER" = "apt-get" ]; then curl -fsSL https://deb.nodesource.com/setup_${NODE_VERSION}.x | bash - apt-get install -y nodejs else curl -fsSL https://rpm.nodesource.com/setup_${NODE_VERSION}.x | bash - yum install -y nodejs fi success "Node.js 安装完成 (版本: $(node -v))" } # 安装 Playwright 和浏览器 install_playwright() { info "安装 Playwright 和 Chromium 浏览器..." # 创建 Python 虚拟环境 python3 -m venv "$INSTALL_DIR/venv" source "$INSTALL_DIR/venv/bin/activate" # 安装 playwright pip3 install --quiet playwright camoufox # 安装浏览器 playwright install chromium playwright install-deps success "Playwright 和浏览器安装完成" } # 克隆项目代码 clone_repository() { info "克隆项目代码..." if [ -d "$INSTALL_DIR/.git" ]; then warn "项目目录已存在,拉取最新代码..." cd "$INSTALL_DIR" git pull origin main else rm -rf "$INSTALL_DIR" git clone "$REPO_URL" "$INSTALL_DIR" fi cd "$INSTALL_DIR" success "代码克隆完成" } # 编译程序 build_project() { info "编译 hwcloud 程序..." cd "$INSTALL_DIR/go" # 下载 Go 依赖 /usr/local/go/bin/go mod download # 编译 /usr/local/go/bin/go build -trimpath -ldflags="-s -w" \ -o "$INSTALL_DIR/go/bin/hwcloud" ./cmd/hwcloud chmod +x "$INSTALL_DIR/go/bin/hwcloud" local file_size=$(du -h "$INSTALL_DIR/go/bin/hwcloud" | cut -f1) success "程序编译完成 (大小: $file_size)" } # 配置 Xvfb (虚拟显示) setup_xvfb() { info "配置虚拟显示 (Xvfb)..." cat > /etc/systemd/system/hwcloud-xvfb.service << 'EOF' [Unit] Description=Xvfb virtual display for hwcloud After=network.target [Service] Type=simple ExecStart=/usr/bin/Xvfb :99 -screen 0 1920x1080x24 -ac +extension GLX +render -noreset Restart=always RestartSec=10 [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable hwcloud-xvfb.service systemctl start hwcloud-xvfb.service success "Xvfb 配置完成" } # 配置 VNC (用于验证码处理) setup_vnc() { info "配置 VNC 服务..." # 创建 VNC 密码 mkdir -p ~/.vnc x11vnc -storepasswd hwcloud ~/.vnc/passwd cat > /etc/systemd/system/hwcloud-vnc.service << 'EOF' [Unit] Description=x11vnc for hwcloud manual captcha solving After=hwcloud-xvfb.service Requires=hwcloud-xvfb.service [Service] Type=simple Environment=DISPLAY=:99 ExecStart=/usr/bin/x11vnc -display :99 -forever -shared -rfbauth /root/.vnc/passwd -rfbport 5901 Restart=always RestartSec=10 [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable hwcloud-vnc.service systemctl start hwcloud-vnc.service success "VNC 配置完成 (端口: 5901, 密码: hwcloud)" } # 配置 Camoufox (浏览器指纹伪装) setup_camoufox() { info "配置 Camoufox 服务..." cat > /etc/systemd/system/hwcloud-camoufox.service << EOF [Unit] Description=Camoufox login service for hwcloud-console After=network.target hwcloud-xvfb.service Requires=hwcloud-xvfb.service [Service] Type=simple WorkingDirectory=$INSTALL_DIR Environment=DISPLAY=:99 Environment=PYTHONUNBUFFERED=1 ExecStart=$INSTALL_DIR/venv/bin/python $INSTALL_DIR/camoufox_server.py Restart=always RestartSec=10 StandardOutput=append:$INSTALL_DIR/logs/camoufox.log StandardError=append:$INSTALL_DIR/logs/camoufox.log [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable hwcloud-camoufox.service systemctl start hwcloud-camoufox.service success "Camoufox 配置完成" } # 配置主服务 setup_main_service() { info "配置 hwcloud-console 主服务..." mkdir -p "$INSTALL_DIR/logs" mkdir -p "$INSTALL_DIR/state" cat > /etc/systemd/system/hwcloud-console.service << EOF [Unit] Description=hwcloud web console After=network.target hwcloud-xvfb.service hwcloud-camoufox.service Requires=hwcloud-xvfb.service [Service] Type=simple WorkingDirectory=$INSTALL_DIR Environment=DISPLAY=:99 Environment=HWCLOUD_HOME=$INSTALL_DIR Environment=PLAYWRIGHT_DRIVER_PATH=$INSTALL_DIR/playwright-driver Environment=PLAYWRIGHT_BROWSERS_PATH=$INSTALL_DIR/browsers ExecStart=$INSTALL_DIR/go/bin/hwcloud serve --addr :$SERVICE_PORT Restart=always RestartSec=10 StandardOutput=append:$INSTALL_DIR/logs/service.log StandardError=append:$INSTALL_DIR/logs/service.log [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable hwcloud-console.service systemctl start hwcloud-console.service success "主服务配置完成" } # 配置防火墙 setup_firewall() { info "配置防火墙..." if command -v ufw &> /dev/null; then ufw allow "$SERVICE_PORT/tcp" comment "hwcloud-console" success "UFW 防火墙规则已添加" elif command -v firewall-cmd &> /dev/null; then firewall-cmd --permanent --add-port="$SERVICE_PORT/tcp" firewall-cmd --reload success "firewalld 防火墙规则已添加" else warn "未检测到防火墙,请手动开放端口 $SERVICE_PORT" fi } # 检查服务状态 check_services() { info "检查服务状态..." echo "" local services=( "hwcloud-xvfb" "hwcloud-vnc" "hwcloud-camoufox" "hwcloud-console" ) local all_ok=true for service in "${services[@]}"; do if systemctl is-active --quiet "$service.service"; then echo -e " ${GREEN}✓${NC} $service.service - ${GREEN}运行中${NC}" else echo -e " ${RED}✗${NC} $service.service - ${RED}已停止${NC}" all_ok=false fi done echo "" if $all_ok; then success "所有服务运行正常" else warn "部分服务未正常启动,请检查日志" fi } # 显示安装完成信息 show_completion() { local server_ip=$(hostname -I | awk '{print $1}') cat << EOF ╔════════════════════════════════════════════════════════════╗ ║ ║ ║ 🎉 安装完成!🎉 ║ ║ ║ ╚════════════════════════════════════════════════════════════╝ 📦 安装目录: $INSTALL_DIR 🌐 访问地址: • Web 控制台: http://${server_ip}:${SERVICE_PORT} • VNC 查看器: vnc://${server_ip}:5901 (密码: hwcloud) 📋 服务管理命令: • 查看状态: systemctl status hwcloud-console • 重启服务: systemctl restart hwcloud-console • 查看日志: tail -f $INSTALL_DIR/logs/service.log 📂 重要目录: • 日志目录: $INSTALL_DIR/logs/ • 数据目录: $INSTALL_DIR/state/ • 账号数据: $INSTALL_DIR/accounts/ ⚙️ 下一步操作: 1. 在浏览器中访问 http://${server_ip}:${SERVICE_PORT} 2. 首次访问会要求设置管理员账号 3. 添加华为云账号开始使用 📖 文档地址: • GitHub: https://github.com/qiandali1999/hwcloud-automation • 更新文档: $INSTALL_DIR/UI-UPDATE.md 💡 提示: • 使用 Ctrl+Shift+R 强制刷新浏览器查看最新 UI • VNC 可用于手动处理验证码 • 所有服务都已配置开机自启 EOF } # 主安装流程 main() { show_banner info "开始安装 hwcloud-console..." echo "" check_root detect_os check_requirements echo "" info "开始安装依赖包,这可能需要几分钟..." echo "" install_base_deps install_go install_nodejs echo "" clone_repository echo "" install_playwright echo "" build_project echo "" info "配置系统服务..." echo "" setup_xvfb setup_vnc setup_camoufox setup_main_service echo "" setup_firewall echo "" sleep 3 # 等待服务启动 check_services show_completion success "安装完成!" } # 错误处理 trap 'error "安装过程中发生错误"; exit 1' ERR # 执行主流程 main "$@"