WuYongJie ba98bf22cc feat: 添加许可证系统、Agent部署模块和Harbor镜像管理
- 新增许可证服务器 (license-server/)
- 新增 Agent 部署模块 (flnxi-agent/)
- 添加许可证验证和激活页面
- 添加 Harbor 镜像同步脚本
- 更新素材系统支持许可证验证
2025-12-15 21:19:46 +08:00

130 lines
5.1 KiB
PowerShell

# 许可证服务器部署脚本
# 部署到 45.249.208.118
param(
[string]$Server = "45.249.208.118",
[string]$User = "root",
[string]$SshKey = "$env:USERPROFILE\.ssh\youyidian_prod_ed25519",
[string]$RemotePath = "/opt/license-server",
[switch]$SkipBuild = $false
)
$ErrorActionPreference = "Stop"
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "许可证服务器部署" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "目标服务器: $User@$Server" -ForegroundColor Yellow
Write-Host "远程路径: $RemotePath" -ForegroundColor Yellow
Write-Host ""
# SSH 命令封装
function Invoke-RemoteCommand {
param([string]$Command)
ssh -i $SshKey "$User@$Server" $Command
if ($LASTEXITCODE -ne 0) { throw "远程命令执行失败: $Command" }
}
# SCP 上传
function Upload-File {
param([string]$Local, [string]$Remote)
scp -i $SshKey -r $Local "$User@$Server`:$Remote"
if ($LASTEXITCODE -ne 0) { throw "文件上传失败: $Local" }
}
# 1. 检查 SSH 连接
Write-Host "1. 检查 SSH 连接..." -ForegroundColor White
try {
Invoke-RemoteCommand "echo 'SSH 连接成功'"
Write-Host " SSH 连接成功 ✓" -ForegroundColor Green
} catch {
Write-Host " SSH 连接失败,请检查密钥和网络" -ForegroundColor Red
exit 1
}
# 2. 创建远程目录
Write-Host "2. 创建远程目录..." -ForegroundColor White
Invoke-RemoteCommand "mkdir -p $RemotePath"
Write-Host " 目录创建成功 ✓" -ForegroundColor Green
# 3. 上传文件
Write-Host "3. 上传项目文件..." -ForegroundColor White
# 创建临时目录存放要上传的文件
$tempDir = Join-Path $env:TEMP "license-server-deploy"
if (Test-Path $tempDir) { Remove-Item -Recurse -Force $tempDir }
New-Item -ItemType Directory -Path $tempDir | Out-Null
# 复制必要文件
Copy-Item -Path "docker-compose.yml" -Destination $tempDir
Copy-Item -Path "Dockerfile" -Destination $tempDir
Copy-Item -Path "package*.json" -Destination $tempDir
Copy-Item -Path "next.config.js" -Destination $tempDir
Copy-Item -Path "tsconfig.json" -Destination $tempDir
Copy-Item -Path "tailwind.config.ts" -Destination $tempDir
Copy-Item -Path "postcss.config.js" -Destination $tempDir
Copy-Item -Path "prisma" -Destination $tempDir -Recurse
Copy-Item -Path "app" -Destination $tempDir -Recurse
Copy-Item -Path "lib" -Destination $tempDir -Recurse
Copy-Item -Path "components" -Destination $tempDir -Recurse -ErrorAction SilentlyContinue
if (Test-Path "public") { Copy-Item -Path "public" -Destination $tempDir -Recurse }
# 上传
Upload-File "$tempDir/*" $RemotePath
Write-Host " 文件上传成功 ✓" -ForegroundColor Green
# 清理临时目录
Remove-Item -Recurse -Force $tempDir
# 4. 创建 .env 文件(如果不存在)
Write-Host "4. 检查环境变量配置..." -ForegroundColor White
$envExists = ssh -i $SshKey "$User@$Server" "test -f $RemotePath/.env && echo 'exists'"
if ($envExists -ne "exists") {
Write-Host " 创建 .env 文件模板..." -ForegroundColor Yellow
$envContent = @"
# 许可证服务器环境变量
JWT_SECRET=your-jwt-secret-change-this
RSA_PRIVATE_KEY=your-rsa-private-key
"@
ssh -i $SshKey "$User@$Server" "cat > $RemotePath/.env << 'EOF'
$envContent
EOF"
Write-Host " 请编辑 $RemotePath/.env 配置 RSA 私钥" -ForegroundColor Yellow
} else {
Write-Host " .env 文件已存在 ✓" -ForegroundColor Green
}
# 5. 构建和启动
Write-Host "5. 构建和启动服务..." -ForegroundColor White
Invoke-RemoteCommand "cd $RemotePath && docker compose down 2>/dev/null || true"
Invoke-RemoteCommand "cd $RemotePath && docker compose build --no-cache"
Invoke-RemoteCommand "cd $RemotePath && docker compose up -d"
Write-Host " 服务启动成功 ✓" -ForegroundColor Green
# 6. 等待服务就绪
Write-Host "6. 等待服务就绪..." -ForegroundColor White
Start-Sleep -Seconds 10
# 7. 运行数据库迁移
Write-Host "7. 运行数据库迁移..." -ForegroundColor White
Invoke-RemoteCommand "cd $RemotePath && docker compose exec -T license-server npx prisma db push"
Write-Host " 数据库迁移成功 ✓" -ForegroundColor Green
# 8. 检查服务状态
Write-Host "8. 检查服务状态..." -ForegroundColor White
$status = ssh -i $SshKey "$User@$Server" "docker compose -f $RemotePath/docker-compose.yml ps --format 'table {{.Name}}\t{{.Status}}'"
Write-Host $status
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "部署完成!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "许可证服务器地址: http://$Server`:3001" -ForegroundColor Yellow
Write-Host ""
Write-Host "后续步骤:" -ForegroundColor White
Write-Host "1. 配置 RSA 私钥: ssh -i $SshKey $User@$Server 'vim $RemotePath/.env'" -ForegroundColor Gray
Write-Host "2. 重启服务: ssh -i $SshKey $User@$Server 'cd $RemotePath && docker compose restart'" -ForegroundColor Gray
Write-Host "3. 初始化管理员: ssh -i $SshKey $User@$Server 'cd $RemotePath && docker compose exec license-server npx ts-node scripts/init-admin.ts'" -ForegroundColor Gray