136 lines
4.8 KiB
PowerShell
136 lines
4.8 KiB
PowerShell
# 在现有服务器上快速搭建 Docker Registry
|
||
# 使用方式:.\scripts\deploy\setup-registry.ps1 -Server 45.249.208.118 -Username root -Password "your-password"
|
||
|
||
[CmdletBinding()]
|
||
param(
|
||
[Parameter(Mandatory = $true)]
|
||
[string]$Server,
|
||
|
||
[string]$Username = "root",
|
||
[string]$Password,
|
||
|
||
[switch]$EnableAuth,
|
||
[string]$AuthUser = "admin",
|
||
[string]$AuthPassword
|
||
)
|
||
|
||
# 导入认证函数(从 local-build-and-upload.ps1)
|
||
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||
$localBuildScript = Join-Path $scriptPath "local-build-and-upload.ps1"
|
||
|
||
# 简化的 SSH 命令执行
|
||
function Invoke-SSH {
|
||
param([string]$Command)
|
||
$sshTarget = "${Username}@${Server}"
|
||
if ($Password) {
|
||
# 使用 plink 或 sshpass(如果可用)
|
||
if (Get-Command plink -ErrorAction SilentlyContinue) {
|
||
& plink -batch -ssh -pw $Password $sshTarget $Command
|
||
} else {
|
||
Write-Host "请手动执行: ssh $sshTarget '$Command'" -ForegroundColor Yellow
|
||
return $false
|
||
}
|
||
} else {
|
||
ssh $sshTarget $Command
|
||
}
|
||
return $LASTEXITCODE -eq 0
|
||
}
|
||
|
||
Write-Host "===> 在服务器上搭建 Docker Registry" -ForegroundColor Cyan
|
||
Write-Host "服务器: $Server" -ForegroundColor Gray
|
||
|
||
# 1. 上传 docker-compose.registry.yml
|
||
Write-Host "===> 上传 Registry 配置文件" -ForegroundColor Cyan
|
||
$registryCompose = Join-Path (Split-Path -Parent $scriptPath) "..\docker-compose.registry.yml"
|
||
$registryCompose = Resolve-Path $registryCompose
|
||
|
||
if (Get-Command scp -ErrorAction SilentlyContinue) {
|
||
if ($Password) {
|
||
# 使用 pscp 或 scp with sshpass
|
||
Write-Host "上传配置文件..." -ForegroundColor Gray
|
||
# 这里需要根据实际情况调整上传方式
|
||
} else {
|
||
scp $registryCompose "${Username}@${Server}:/root/deploy/docker-compose.registry.yml"
|
||
}
|
||
} else {
|
||
Write-Host "请手动上传 docker-compose.registry.yml 到服务器 /root/deploy/" -ForegroundColor Yellow
|
||
}
|
||
|
||
# 2. 在服务器上启动 Registry
|
||
Write-Host "===> 启动 Registry 服务" -ForegroundColor Cyan
|
||
$startCmd = "cd /root/deploy && docker compose -f docker-compose.registry.yml up -d"
|
||
if (Invoke-SSH $startCmd) {
|
||
Write-Host "✓ Registry 启动成功" -ForegroundColor Green
|
||
} else {
|
||
Write-Host "✗ Registry 启动失败" -ForegroundColor Red
|
||
exit 1
|
||
}
|
||
|
||
# 3. 配置认证(可选)
|
||
if ($EnableAuth) {
|
||
Write-Host "===> 配置 Registry 认证" -ForegroundColor Cyan
|
||
|
||
if (-not $AuthPassword) {
|
||
$AuthPassword = Read-Host "请输入 Registry 密码" -AsSecureString
|
||
$AuthPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
|
||
[Runtime.InteropServices.Marshal]::SecureStringToBSTR($AuthPassword)
|
||
)
|
||
}
|
||
|
||
# 在服务器上创建 htpasswd
|
||
$authCmd = @"
|
||
cd /root/deploy && \
|
||
mkdir -p registry/config && \
|
||
docker run --rm --entrypoint htpasswd httpd:2 -Bbn $AuthUser $AuthPassword > registry/config/htpasswd
|
||
"@
|
||
|
||
if (Invoke-SSH $authCmd) {
|
||
Write-Host "✓ 认证配置成功" -ForegroundColor Green
|
||
Write-Host "请编辑 docker-compose.registry.yml 启用认证配置" -ForegroundColor Yellow
|
||
}
|
||
}
|
||
|
||
# 4. 配置 Docker 允许不安全的 Registry(如果使用 HTTP)
|
||
Write-Host "===> 配置 Docker 允许不安全的 Registry" -ForegroundColor Cyan
|
||
$dockerConfigCmd = @"
|
||
mkdir -p /etc/docker && \
|
||
if [ ! -f /etc/docker/daemon.json ]; then echo '{}' > /etc/docker/daemon.json; fi && \
|
||
python3 -c "
|
||
import json
|
||
with open('/etc/docker/daemon.json', 'r') as f:
|
||
config = json.load(f)
|
||
if 'insecure-registries' not in config:
|
||
config['insecure-registries'] = []
|
||
if '$Server:5000' not in config['insecure-registries']:
|
||
config['insecure-registries'].append('$Server:5000')
|
||
with open('/etc/docker/daemon.json', 'w') as f:
|
||
json.dump(config, f, indent=2)
|
||
" && \
|
||
systemctl restart docker
|
||
"@
|
||
|
||
if (Invoke-SSH $dockerConfigCmd) {
|
||
Write-Host "✓ Docker 配置成功" -ForegroundColor Green
|
||
} else {
|
||
Write-Host "⚠ Docker 配置失败,请手动配置" -ForegroundColor Yellow
|
||
}
|
||
|
||
# 5. 测试 Registry
|
||
Write-Host "===> 测试 Registry" -ForegroundColor Cyan
|
||
$testCmd = "curl -s http://localhost:5000/v2/ | head -1"
|
||
if (Invoke-SSH $testCmd) {
|
||
Write-Host "✓ Registry 运行正常" -ForegroundColor Green
|
||
} else {
|
||
Write-Host "⚠ Registry 测试失败,请检查日志" -ForegroundColor Yellow
|
||
}
|
||
|
||
Write-Host ""
|
||
Write-Host "===> Registry 搭建完成" -ForegroundColor Green
|
||
Write-Host "Registry 地址: http://$Server:5000" -ForegroundColor Gray
|
||
Write-Host ""
|
||
Write-Host "下一步:" -ForegroundColor Cyan
|
||
Write-Host "1. 在本地配置 Docker 允许不安全的 Registry" -ForegroundColor Gray
|
||
Write-Host "2. 测试推送镜像: docker tag hello-world:latest $Server`:5000/hello-world:latest" -ForegroundColor Gray
|
||
Write-Host "3. 修改部署脚本使用 Registry" -ForegroundColor Gray
|
||
|