# 3D 家具安装展示系统 - Windows 部署脚本 # 适用于: Windows 10/11 + Docker Desktop # # 使用方法: # PowerShell -ExecutionPolicy Bypass -File scripts\deploy.ps1 # 设置错误时停止 $ErrorActionPreference = "Stop" # 打印带颜色的消息 function Print-Info { param([string]$Message) Write-Host "[INFO] $Message" -ForegroundColor Blue } function Print-Success { param([string]$Message) Write-Host "[SUCCESS] $Message" -ForegroundColor Green } function Print-Warning { param([string]$Message) Write-Host "[WARNING] $Message" -ForegroundColor Yellow } function Print-Error { param([string]$Message) Write-Host "[ERROR] $Message" -ForegroundColor Red } # 打印欢迎信息 function Print-Welcome { Write-Host "" Write-Host "==========================================" -ForegroundColor Cyan Write-Host " 3D 家具安装展示系统" -ForegroundColor Cyan Write-Host " 自动部署脚本 v1.0" -ForegroundColor Cyan Write-Host "==========================================" -ForegroundColor Cyan Write-Host "" } # 检查系统要求 function Check-Requirements { Print-Info "检查系统要求..." # 检查 Docker try { $dockerVersion = docker --version Print-Success "Docker 已安装: $dockerVersion" } catch { Print-Error "Docker 未安装,请先安装 Docker Desktop" exit 1 } # 检查 Docker Compose try { $composeVersion = docker-compose --version Print-Success "Docker Compose 已安装: $composeVersion" } catch { Print-Error "Docker Compose 未安装" exit 1 } # 检查 Docker 是否运行 try { docker info | Out-Null Print-Success "Docker 服务运行正常" } catch { Print-Error "Docker 服务未运行,请启动 Docker Desktop" exit 1 } } # 检测硬件配置 function Detect-Hardware { Print-Info "检测硬件配置..." # 检测内存 (GB) $totalMemGB = [math]::Round((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1GB) Print-Info "总内存: ${totalMemGB}GB" # 检测 CPU 核心数 $cpuCores = (Get-CimInstance Win32_Processor).NumberOfLogicalProcessors Print-Info "CPU 核心数: $cpuCores" # 检测磁盘空间 (GB) $disk = Get-PSDrive C $diskAvailGB = [math]::Round($disk.Free / 1GB) Print-Info "可用磁盘空间: ${diskAvailGB}GB" # 推荐部署模式 if ($totalMemGB -lt 4) { Print-Error "内存不足 4GB,无法部署系统" exit 1 } elseif ($totalMemGB -lt 8) { $script:DeploymentMode = "lightweight" Print-Warning "内存为 ${totalMemGB}GB,将使用轻量级模式" } else { $script:DeploymentMode = "standard" Print-Success "内存充足,将使用标准模式" } if ($diskAvailGB -lt 50) { Print-Warning "磁盘可用空间不足 50GB,建议清理磁盘" } } # 生成随机密码 function Generate-Password { param([int]$Length = 16) $bytes = New-Object byte[] $Length $rng = [System.Security.Cryptography.RandomNumberGenerator]::Create() $rng.GetBytes($bytes) return [Convert]::ToBase64String($bytes).Substring(0, $Length) -replace '[+/=]', '' } # 生成配置文件 function Generate-Config { Print-Info "生成配置文件..." # 检查 .env 文件是否存在 if (Test-Path ".env") { Print-Warning ".env 文件已存在" $response = Read-Host "是否覆盖现有配置? (y/N)" if ($response -ne "y" -and $response -ne "Y") { Print-Info "保留现有配置" return } } # 复制模板 Copy-Item ".env.example" ".env" -Force # 生成随机密码 $adminPassword = Generate-Password -Length 16 $dbPassword = Generate-Password -Length 32 $minioPassword = Generate-Password -Length 32 # 读取 .env 文件 $envContent = Get-Content ".env" # 更新配置 $envContent = $envContent -replace "DEPLOYMENT_MODE=.*", "DEPLOYMENT_MODE=$script:DeploymentMode" $envContent = $envContent -replace "ADMIN_PASSWORD=.*", "ADMIN_PASSWORD=$adminPassword" $envContent = $envContent -replace "DB_PASSWORD=.*", "DB_PASSWORD=$dbPassword" $envContent = $envContent -replace "MINIO_PASSWORD=.*", "MINIO_PASSWORD=$minioPassword" # 保存文件 $envContent | Set-Content ".env" Print-Success "配置文件已生成" Print-Info "管理员密码: $adminPassword" Print-Warning "请妥善保存以上密码!" # 保存密码到变量供后续使用 $script:AdminPassword = $adminPassword } # 启动服务 function Start-Services { Print-Info "启动 Docker 服务..." $composeFile = "docker-compose.$script:DeploymentMode.yml" if (-not (Test-Path $composeFile)) { Print-Error "配置文件 $composeFile 不存在" exit 1 } # 拉取镜像 Print-Info "拉取 Docker 镜像..." docker-compose -f $composeFile pull # 启动服务 Print-Info "启动服务..." docker-compose -f $composeFile up -d Print-Success "服务已启动" } # 等待服务就绪 function Wait-ForServices { Print-Info "等待服务就绪..." $maxWait = 120 $elapsed = 0 while ($elapsed -lt $maxWait) { $status = docker-compose ps if ($status -match "healthy") { Print-Success "服务已就绪" return } Start-Sleep -Seconds 5 $elapsed += 5 Write-Host "." -NoNewline } Write-Host "" Print-Warning "服务启动超时,请检查日志" } # 显示访问信息 function Show-AccessInfo { Write-Host "" Write-Host "==========================================" -ForegroundColor Cyan Write-Host " 部署完成!" -ForegroundColor Cyan Write-Host "==========================================" -ForegroundColor Cyan Write-Host "" Print-Success "访问地址: http://localhost:3000" Print-Success "管理员账号: admin" Print-Info "管理员密码: $script:AdminPassword" Write-Host "" Print-Info "查看日志: docker-compose -f docker-compose.$script:DeploymentMode.yml logs -f" Print-Info "停止服务: docker-compose -f docker-compose.$script:DeploymentMode.yml down" Write-Host "" } # 主函数 function Main { Print-Welcome Check-Requirements Detect-Hardware Generate-Config Start-Services Wait-ForServices Show-AccessInfo } # 运行主函数 Main