gongxiangp2/setup-build-server.ps1

330 lines
13 KiB
PowerShell
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 由壹点3D素材 - 构建机一键配置脚本
# 功能:安装 Docker Compose + Registry + UI
# 目标:192.168.40.206(构建机)
param(
[string]$BuildServerIP = '192.168.40.206',
[string]$BuildServerUser = 'root',
[string]$BuildServerPassword = $env:BUILD_SERVER_PASSWORD,
[switch]$SkipDockerCompose, # 跳过 Docker Compose 安装
[switch]$SkipRegistry, # 跳过 Registry 安装
[switch]$Help
)
# 设置编码
try {
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
chcp 65001 | Out-Null
} catch {}
# ==================== 辅助函数 ====================
function Write-Success {
param([string]$Message)
Write-Host "✅ $Message" -ForegroundColor Green
}
function Write-Error-Custom {
param([string]$Message)
Write-Host "❌ $Message" -ForegroundColor Red
}
function Write-Info {
param([string]$Message)
Write-Host "ℹ️ $Message" -ForegroundColor Cyan
}
function Write-Warning-Custom {
param([string]$Message)
Write-Host "⚠️ $Message" -ForegroundColor Yellow
}
function Write-Step {
param([int]$Step, [string]$Message)
Write-Host ""
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Host "📦 步骤 $Step`: $Message" -ForegroundColor Magenta
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
}
function Write-SubStep {
param([string]$Message)
Write-Host " ▶ $Message" -ForegroundColor Yellow
}
function Invoke-BuildServer {
param([string]$Command, [switch]$Silent)
$result = & "C:\Program Files\PuTTY\plink.exe" -batch -pw $BuildServerPassword "$BuildServerUser@$BuildServerIP" $Command 2>&1
if (-not $Silent) {
$result | ForEach-Object {
if ($_ -match "error|failed|fatal") {
Write-Host " $_" -ForegroundColor Red
} elseif ($_ -match "warning|warn") {
Write-Host " $_" -ForegroundColor Yellow
} elseif ($_ -match "success|complete|done|✓|Successfully") {
Write-Host " $_" -ForegroundColor Green
} else {
Write-Host " $_" -ForegroundColor Gray
}
}
}
return $result
}
function Show-Help {
Write-Host @"
由壹点3D素材 - 构建机一键配置脚本
用法:
.\setup-build-server.ps1 # 完整配置
.\setup-build-server.ps1 -SkipDockerCompose # 跳过 Docker Compose 安装
.\setup-build-server.ps1 -SkipRegistry # 跳过 Registry 安装
功能:
1. 安装 Docker Compose
2. 安装 Docker Registry(端口 5000)
3. 安装 Registry UI(端口 5001)
4. 验证安装
目标服务器:
IP:$BuildServerIP
用户:$BuildServerUser
注意:
- 此脚本只配置构建机,不会影响生产服务器
- 所有操作都是安全的,不会影响现有服务
"@
}
# ==================== 主逻辑 ====================
function Main {
Write-Host ""
Write-Host "╔════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ ║" -ForegroundColor Cyan
Write-Host "║ 由壹点3D素材 - 构建机一键配置 ║" -ForegroundColor Cyan
Write-Host "║ ║" -ForegroundColor Cyan
Write-Host "╚════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""
if ($Help) { Show-Help; return }
if ([string]::IsNullOrWhiteSpace($BuildServerPassword)) {
throw '缺少构建机密码,请先设置 BUILD_SERVER_PASSWORD 环境变量'
}
Write-Host "📋 配置信息" -ForegroundColor Magenta
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Info "目标服务器:$BuildServerIP(构建机)"
Write-Info "用户:$BuildServerUser"
Write-Info "操作:安装 Docker Compose + Registry + UI"
Write-Host ""
Write-Warning-Custom "⚠️ 重要提示:"
Write-Info " - 此脚本只配置构建机($BuildServerIP)"
Write-Info " - 不会影响生产服务器(192.168.58.20)"
Write-Info " - 不会影响任何现有服务"
Write-Host ""
$confirm = Read-Host "确认开始配置?(y/n)"
if ($confirm -ne 'y') {
Write-Info "已取消配置"
return
}
# ==================== 步骤 0:检查连接 ====================
Write-Step 0 "检查构建机连接"
Write-SubStep "测试 SSH 连接..."
$testResult = Invoke-BuildServer -Command "echo 'OK'" -Silent
if ($testResult -notmatch "OK") {
Write-Error-Custom "无法连接构建机"
return
}
Write-Success "SSH 连接正常"
Write-SubStep "检查 Docker 版本..."
$dockerVersion = Invoke-BuildServer -Command "docker --version" -Silent
if ($dockerVersion -match "Docker version") {
Write-Success "Docker 已安装:$($dockerVersion -replace 'Docker version ', '')"
} else {
Write-Error-Custom "Docker 未安装,请先安装 Docker"
return
}
# ==================== 步骤 1:安装 Docker Compose ====================
if (-not $SkipDockerCompose) {
Write-Step 1 "安装 Docker Compose"
Write-SubStep "检查是否已安装..."
$composeCheck = Invoke-BuildServer -Command "docker-compose --version 2>&1" -Silent
if ($composeCheck -match "docker-compose version" -or $composeCheck -match "Docker Compose version") {
Write-Success "Docker Compose 已安装:$composeCheck"
} else {
Write-SubStep "开始下载 Docker Compose..."
Write-Info " 下载地址:GitHub(可能需要 1-2 分钟)"
$installCmd = @"
curl -L "https://github.com/docker/compose/releases/download/v2.24.0/docker-compose-`$(uname -s)-`$(uname -m)" -o /usr/local/bin/docker-compose && \
chmod +x /usr/local/bin/docker-compose && \
docker-compose --version
"@
$installResult = Invoke-BuildServer -Command $installCmd
if ($installResult -match "Docker Compose version") {
Write-Success "Docker Compose 安装成功"
} else {
Write-Error-Custom "Docker Compose 安装失败"
Write-Info "请检查网络连接或手动安装"
return
}
}
} else {
Write-Step 1 "跳过 Docker Compose 安装"
}
# ==================== 步骤 2:创建 Registry 配置 ====================
if (-not $SkipRegistry) {
Write-Step 2 "配置 Docker Registry"
Write-SubStep "创建 Registry 目录..."
Invoke-BuildServer -Command "mkdir -p /opt/docker-registry" -Silent | Out-Null
Write-Success "目录已创建:/opt/docker-registry"
Write-SubStep "创建 docker-compose.yml 配置文件..."
$composeYml = @"
version: '3.8'
services:
registry:
image: registry:2
container_name: docker-registry
restart: always
ports:
- "5000:5000"
environment:
REGISTRY_STORAGE_DELETE_ENABLED: "true"
volumes:
- ./data:/var/lib/registry
networks:
- registry-net
ui:
image: joxit/docker-registry-ui:latest
container_name: docker-registry-ui
restart: always
ports:
- "5001:80"
environment:
- SINGLE_REGISTRY=true
- REGISTRY_TITLE=由壹点3D素材 - 镜像仓库
- DELETE_IMAGES=true
- SHOW_CONTENT_DIGEST=true
- NGINX_PROXY_PASS_URL=http://registry:5000
- SHOW_CATALOG_NB_TAGS=true
- CATALOG_MIN_BRANCHES=1
- CATALOG_MAX_BRANCHES=1
- TAGLIST_PAGE_SIZE=100
- REGISTRY_SECURED=false
- CATALOG_ELEMENTS_LIMIT=1000
depends_on:
- registry
networks:
- registry-net
networks:
registry-net:
driver: bridge
"@
# 将配置写入远程服务器
$createConfigCmd = @"
cat > /opt/docker-registry/docker-compose.yml << 'EOFCONFIG'
$composeYml
EOFCONFIG
"@
Invoke-BuildServer -Command $createConfigCmd -Silent | Out-Null
Write-Success "配置文件已创建"
# ==================== 步骤 3:启动 Registry ====================
Write-Step 3 "启动 Docker Registry"
Write-SubStep "拉取镜像(可能需要 1-2 分钟)..."
Write-Info " 正在拉取 registry:2 和 joxit/docker-registry-ui:latest"
$startCmd = "cd /opt/docker-registry && docker-compose up -d"
$startResult = Invoke-BuildServer -Command $startCmd
Write-SubStep "等待服务启动..."
Start-Sleep -Seconds 5
Write-SubStep "检查服务状态..."
$statusResult = Invoke-BuildServer -Command "cd /opt/docker-registry && docker-compose ps"
if ($statusResult -match "docker-registry.*Up" -and $statusResult -match "docker-registry-ui.*Up") {
Write-Success "Registry 服务启动成功"
} else {
Write-Warning-Custom "服务可能未完全启动,请检查日志"
}
# ==================== 步骤 4:验证安装 ====================
Write-Step 4 "验证安装"
Write-SubStep "测试 Registry API..."
$apiTest = Invoke-BuildServer -Command "curl -s http://localhost:5000/v2/_catalog" -Silent
if ($apiTest -match "repositories") {
Write-Success "Registry API 正常:http://$BuildServerIP`:5000"
} else {
Write-Warning-Custom "Registry API 测试失败"
}
Write-SubStep "测试 Registry UI..."
$uiTest = Invoke-BuildServer -Command "curl -s http://localhost:5001 | head -5" -Silent
if ($uiTest -match "html" -or $uiTest -match "DOCTYPE") {
Write-Success "Registry UI 正常:http://$BuildServerIP`:5001"
} else {
Write-Warning-Custom "Registry UI 测试失败"
}
Write-SubStep "查看运行中的容器..."
Invoke-BuildServer -Command "docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' | grep registry"
} else {
Write-Step 2 "跳过 Registry 安装"
}
# ==================== 完成 ====================
Write-Host ""
Write-Host "╔════════════════════════════════════════════════════════════╗" -ForegroundColor Green
Write-Host "║ ║" -ForegroundColor Green
Write-Host "║ ✅ 配置完成! ║" -ForegroundColor Green
Write-Host "║ ║" -ForegroundColor Green
Write-Host "╚════════════════════════════════════════════════════════════╝" -ForegroundColor Green
Write-Host ""
Write-Host "📊 配置结果" -ForegroundColor Magenta
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Info "构建机:$BuildServerIP"
Write-Info "Registry API:http://$BuildServerIP`:5000"
Write-Info "Registry UI:http://$BuildServerIP`:5001"
Write-Host ""
Write-Host "🎯 下一步操作" -ForegroundColor Magenta
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Info "1. 在浏览器访问 Registry UI:http://$BuildServerIP`:5001"
Write-Info "2. 配置生产服务器(运行下面的命令):"
Write-Host " .\setup-production-server.ps1" -ForegroundColor Yellow
Write-Info "3. 测试 Git 增量部署:"
Write-Host " .\scripts\git-incremental-deploy.ps1 -DryRun" -ForegroundColor Yellow
Write-Host " .\scripts\git-incremental-deploy.ps1 -Target shadow" -ForegroundColor Yellow
Write-Host ""
}
# 执行
Main