gongxiangp2/setup-production-server.ps1

317 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素材 - 生产服务器配置脚本
# 功能:配置 insecure-registry,允许从构建机拉取镜像
# 目标:192.168.58.20(生产服务器)
# 注意:只修改配置,不安装任何东西,不影响现有服务
param(
[string]$ProdServerIP = '192.168.58.20',
[string]$ProdServerUser = 'root',
[string]$ProdServerPassword = $env:PRODUCTION_SERVER_PASSWORD,
[string]$BuildServerIP = '192.168.40.206',
[switch]$DryRun, # 只检查,不修改
[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-ProdServer {
param([string]$Command, [switch]$Silent)
$result = & "C:\Program Files\PuTTY\plink.exe" -batch -pw $ProdServerPassword "$ProdServerUser@$ProdServerIP" $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-production-server.ps1 # 配置生产服务器
.\setup-production-server.ps1 -DryRun # 只检查,不修改
功能:
1. 备份现有 Docker 配置(如果存在)
2. 配置 insecure-registry(允许从构建机拉取镜像)
3. 重启 Docker 服务
4. 验证配置
目标服务器:
IP:$ProdServerIP(生产服务器)
用户:$ProdServerUser
构建机:
IP:$BuildServerIP
注意:
- 此脚本只修改 Docker 配置,不安装任何东西
- 会备份现有配置到 /etc/docker/daemon.json.backup
- 重启 Docker 时会短暂中断服务(约 5-10 秒)
- 所有容器会自动重启(restart: always)
"@
}
# ==================== 主逻辑 ====================
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($ProdServerPassword)) {
throw '缺少生产服务器密码,请先设置 PRODUCTION_SERVER_PASSWORD 环境变量'
}
Write-Host "📋 配置信息" -ForegroundColor Magenta
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Info "目标服务器:$ProdServerIP(生产服务器)"
Write-Info "构建机:$BuildServerIP"
Write-Info "操作:配置 insecure-registry"
if ($DryRun) {
Write-Warning-Custom "模式:只检查,不修改(DryRun)"
}
Write-Host ""
Write-Warning-Custom "⚠️ 重要提示:"
Write-Info " - 此脚本只修改 Docker 配置,不安装任何东西"
Write-Info " - 会备份现有配置"
Write-Info " - 重启 Docker 时会短暂中断服务(约 5-10 秒)"
Write-Info " - 所有容器会自动重启"
Write-Host ""
# ==================== 步骤 0:检查现有服务 ====================
Write-Step 0 "检查生产服务器状态"
Write-SubStep "测试 SSH 连接..."
$testResult = Invoke-ProdServer -Command "echo 'OK'" -Silent
if ($testResult -notmatch "OK") {
Write-Error-Custom "无法连接生产服务器"
return
}
Write-Success "SSH 连接正常"
Write-SubStep "检查运行中的服务..."
$containers = Invoke-ProdServer -Command "docker ps --format '{{.Names}}'" -Silent
$containerList = $containers -split "`n" | Where-Object { $_ -match '\S' }
Write-Info " 当前运行的容器:"
foreach ($container in $containerList) {
Write-Host " - $container" -ForegroundColor Gray
}
Write-SubStep "检查现有 Docker 配置..."
$existingConfig = Invoke-ProdServer -Command "cat /etc/docker/daemon.json 2>/dev/null || echo 'NOT_EXISTS'" -Silent
if ($existingConfig -match "NOT_EXISTS") {
Write-Info " /etc/docker/daemon.json 不存在(将创建新配置)"
$hasExistingConfig = $false
} else {
Write-Warning-Custom " /etc/docker/daemon.json 已存在"
Write-Host " 当前配置:" -ForegroundColor Gray
$existingConfig | ForEach-Object { Write-Host " $_" -ForegroundColor Gray }
$hasExistingConfig = $true
}
Write-SubStep "检查当前 insecure-registry 配置..."
$currentInsecure = Invoke-ProdServer -Command "docker info 2>&1 | grep -A 3 'Insecure Registries'" -Silent
Write-Host " 当前配置:" -ForegroundColor Gray
$currentInsecure | ForEach-Object { Write-Host " $_" -ForegroundColor Gray }
if ($DryRun) {
Write-Host ""
Write-Info "DryRun 模式:检查完成,未进行任何修改"
return
}
Write-Host ""
Write-Warning-Custom "⚠️ 即将修改 Docker 配置并重启 Docker 服务"
Write-Info " - 会短暂中断服务(约 5-10 秒)"
Write-Info " - 所有容器会自动重启"
Write-Host ""
$confirm = Read-Host "确认继续?(y/n)"
if ($confirm -ne 'y') {
Write-Info "已取消配置"
return
}
# ==================== 步骤 1:备份现有配置 ====================
Write-Step 1 "备份现有配置"
if ($hasExistingConfig) {
Write-SubStep "备份 /etc/docker/daemon.json..."
$backupCmd = "cp /etc/docker/daemon.json /etc/docker/daemon.json.backup.`$(date +%Y%m%d_%H%M%S)"
Invoke-ProdServer -Command $backupCmd -Silent | Out-Null
Write-Success "配置已备份"
} else {
Write-Info "无需备份(配置文件不存在)"
}
# ==================== 步骤 2:创建新配置 ====================
Write-Step 2 "配置 insecure-registry"
Write-SubStep "创建新的 Docker 配置..."
$newConfig = @"
{
"insecure-registries": ["$BuildServerIP`:5000"]
}
"@
$createConfigCmd = @"
cat > /etc/docker/daemon.json << 'EOFCONFIG'
$newConfig
EOFCONFIG
"@
Invoke-ProdServer -Command $createConfigCmd -Silent | Out-Null
Write-Success "配置已创建"
Write-SubStep "验证配置文件..."
$verifyConfig = Invoke-ProdServer -Command "cat /etc/docker/daemon.json"
Write-Success "配置文件内容正确"
# ==================== 步骤 3:重启 Docker ====================
Write-Step 3 "重启 Docker 服务"
Write-Warning-Custom "⚠️ 即将重启 Docker,服务会短暂中断(约 5-10 秒)"
Start-Sleep -Seconds 2
Write-SubStep "重启 Docker 服务..."
Invoke-ProdServer -Command "systemctl restart docker" -Silent | Out-Null
Write-SubStep "等待 Docker 启动..."
Start-Sleep -Seconds 5
Write-SubStep "检查 Docker 状态..."
$dockerStatus = Invoke-ProdServer -Command "systemctl is-active docker" -Silent
if ($dockerStatus -match "active") {
Write-Success "Docker 服务已启动"
} else {
Write-Error-Custom "Docker 服务启动失败"
Write-Info "请手动检查:ssh root@$ProdServerIP 'systemctl status docker'"
return
}
Write-SubStep "等待容器自动重启..."
Start-Sleep -Seconds 5
Write-SubStep "检查容器状态..."
$containersAfter = Invoke-ProdServer -Command "docker ps --format 'table {{.Names}}\t{{.Status}}'"
$runningCount = ($containersAfter -split "`n" | Where-Object { $_ -match "Up" }).Count
if ($runningCount -ge $containerList.Count) {
Write-Success "所有容器已恢复运行"
} else {
Write-Warning-Custom "部分容器可能未启动,请检查"
}
# ==================== 步骤 4:验证配置 ====================
Write-Step 4 "验证配置"
Write-SubStep "检查 insecure-registry 配置..."
$newInsecure = Invoke-ProdServer -Command "docker info 2>&1 | grep -A 5 'Insecure Registries'"
if ($newInsecure -match $BuildServerIP) {
Write-Success "insecure-registry 配置成功"
} else {
Write-Warning-Custom "配置可能未生效,请检查"
}
Write-SubStep "测试连接构建机 Registry..."
$registryTest = Invoke-ProdServer -Command "curl -s http://$BuildServerIP`:5000/v2/_catalog" -Silent
if ($registryTest -match "repositories") {
Write-Success "可以访问构建机 Registry"
} else {
Write-Warning-Custom "无法访问构建机 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 "生产服务器:$ProdServerIP"
Write-Info "构建机 Registry:$BuildServerIP`:5000"
Write-Info "配置文件:/etc/docker/daemon.json"
if ($hasExistingConfig) {
Write-Info "备份文件:/etc/docker/daemon.json.backup.*"
}
Write-Host ""
Write-Host "🎯 下一步操作" -ForegroundColor Magenta
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Info "1. 先做 Git 增量部署预检:"
Write-Host " .\scripts\git-incremental-deploy.ps1 -DryRun" -ForegroundColor Yellow
Write-Info "2. 测试部署到影子系统:"
Write-Host " .\scripts\git-incremental-deploy.ps1 -Target shadow" -ForegroundColor Yellow
Write-Info "3. 如果测试成功,部署到正式系统:"
Write-Host " .\scripts\git-incremental-deploy.ps1" -ForegroundColor Yellow
Write-Host ""
}
# 执行
Main