flnxi-Wu/scripts/manage-docker-images.ps1
WuYongJie 8705b9846a
Some checks failed
CI Check / check (push) Has been cancelled
feat: 统一弹窗样式系统与分类管理优化
- 创建统一的Modal组件,采用Apple风格设计
- 实现毛玻璃效果、圆角、动画和细边框描边
- 修复弹窗打开时背景滚动问题
- 修复分类管理中重复卡片和数组字符串显示问题
- 优化选项解析逻辑,添加去重和过滤机制
2025-11-25 09:37:00 +08:00

271 lines
9.2 KiB
PowerShell

# Docker 基础镜像管理脚本
# 用于管理项目中的基础 Docker 镜像
param(
[Parameter(Mandatory=$true)]
[ValidateSet("list", "load", "save", "update", "push-to-server")]
[string]$Action,
[string]$ImageName = "",
[string]$ImageTag = "latest",
[string]$Server = "45.249.208.118",
[string]$Username = "root",
[string]$KeyPath = "C:\Users\Mr_Wu01\.ssh\youyidian_prod_ed25519"
)
$ErrorActionPreference = "Stop"
$ImagesDir = Join-Path $PSScriptRoot ".." "docker-images"
function Get-ImagePath {
param([string]$Name, [string]$Tag)
return Join-Path $ImagesDir $Name "$Name-$Tag.tar"
}
function Get-ImageVersionFile {
param([string]$Name)
return Join-Path $ImagesDir $Name "VERSION.txt"
}
function List-Images {
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " 已保存的基础镜像列表" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
if (-not (Test-Path $ImagesDir)) {
Write-Host "镜像目录不存在: $ImagesDir" -ForegroundColor Yellow
return
}
$found = $false
Get-ChildItem $ImagesDir -Directory | ForEach-Object {
$imageName = $_.Name
$tarFiles = Get-ChildItem $_.FullName -Filter "*.tar" -ErrorAction SilentlyContinue
if ($tarFiles) {
$found = $true
Write-Host "📦 $imageName" -ForegroundColor Green
foreach ($tar in $tarFiles) {
$size = [math]::Round($tar.Length / 1MB, 2)
$modified = $tar.LastWriteTime.ToString("yyyy-MM-dd HH:mm")
Write-Host " └─ $($tar.Name) ($size MB, 更新于: $modified)" -ForegroundColor Gray
}
$versionFile = Get-ImageVersionFile -Name $imageName
if (Test-Path $versionFile) {
Write-Host " └─ 版本信息:" -ForegroundColor Gray
Get-Content $versionFile | ForEach-Object {
Write-Host " $_" -ForegroundColor DarkGray
}
}
Write-Host ""
}
}
if (-not $found) {
Write-Host "未找到已保存的镜像" -ForegroundColor Yellow
}
}
function Save-Image {
param([string]$Name, [string]$Tag)
if (-not $Name) {
Write-Host "错误: 请指定镜像名称 (例如: minio-mc)" -ForegroundColor Red
return
}
$imageFullName = if ($Name -match "/") { $Name } else { "minio/mc" }
$imageWithTag = "$imageFullName`:$Tag"
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " 保存镜像: $imageWithTag" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# 检查镜像是否存在
Write-Host "[1/3] 检查本地镜像..." -ForegroundColor Yellow
$imageExists = docker images --format "{{.Repository}}:{{.Tag}}" | Select-String -Pattern "^$([regex]::Escape($imageFullName)):$Tag$"
if (-not $imageExists) {
Write-Host " 镜像不存在,正在拉取..." -ForegroundColor Yellow
docker pull $imageWithTag
if ($LASTEXITCODE -ne 0) {
Write-Host "✗ 镜像拉取失败" -ForegroundColor Red
return
}
} else {
Write-Host "✓ 镜像已存在" -ForegroundColor Green
}
# 创建目录
$imageDir = Join-Path $ImagesDir $Name
if (-not (Test-Path $imageDir)) {
New-Item -ItemType Directory -Path $imageDir -Force | Out-Null
}
# 保存镜像
Write-Host ""
Write-Host "[2/3] 保存镜像到文件..." -ForegroundColor Yellow
$tarPath = Get-ImagePath -Name $Name -Tag $Tag
docker save $imageWithTag -o $tarPath
if ($LASTEXITCODE -eq 0) {
$size = [math]::Round((Get-Item $tarPath).Length / 1MB, 2)
Write-Host "✓ 镜像已保存: $tarPath ($size MB)" -ForegroundColor Green
} else {
Write-Host "✗ 镜像保存失败" -ForegroundColor Red
return
}
# 保存版本信息
Write-Host ""
Write-Host "[3/3] 保存版本信息..." -ForegroundColor Yellow
$versionFile = Get-ImageVersionFile -Name $Name
$digest = docker images --format "{{.ID}}" $imageWithTag | Select-Object -First 1
$versionInfo = @"
$Tag - $(Get-Date -Format 'yyyy-MM-dd')
Image: $imageWithTag
Digest: $digest
Size: ~$size MB
"@
Set-Content -Path $versionFile -Value $versionInfo
Write-Host "✓ 版本信息已保存" -ForegroundColor Green
Write-Host ""
Write-Host "完成!" -ForegroundColor Green
}
function Load-Image {
param([string]$Name, [string]$Tag)
if (-not $Name) {
Write-Host "错误: 请指定镜像名称" -ForegroundColor Red
return
}
$tarPath = Get-ImagePath -Name $Name -Tag $Tag
if (-not (Test-Path $tarPath)) {
Write-Host "错误: 镜像文件不存在: $tarPath" -ForegroundColor Red
Write-Host "提示: 使用 'save' 操作先保存镜像" -ForegroundColor Yellow
return
}
Write-Host "加载镜像: $tarPath" -ForegroundColor Cyan
docker load -i $tarPath
if ($LASTEXITCODE -eq 0) {
Write-Host "✓ 镜像加载成功" -ForegroundColor Green
} else {
Write-Host "✗ 镜像加载失败" -ForegroundColor Red
}
}
function Update-Image {
param([string]$Name, [string]$Tag)
if (-not $Name) {
Write-Host "错误: 请指定镜像名称" -ForegroundColor Red
return
}
$imageFullName = if ($Name -match "/") { $Name } else { "minio/mc" }
$imageWithTag = "$imageFullName`:$Tag"
Write-Host "更新镜像: $imageWithTag" -ForegroundColor Cyan
Write-Host ""
# 拉取最新版本
Write-Host "[1/2] 拉取最新镜像..." -ForegroundColor Yellow
docker pull $imageWithTag
if ($LASTEXITCODE -ne 0) {
Write-Host "✗ 镜像拉取失败" -ForegroundColor Red
return
}
Write-Host "✓ 镜像拉取成功" -ForegroundColor Green
# 保存
Write-Host ""
Write-Host "[2/2] 保存镜像..." -ForegroundColor Yellow
Save-Image -Name $Name -Tag $Tag
}
function Push-ToServer {
param([string]$Name, [string]$Tag, [string]$Server, [string]$Username, [string]$KeyPath)
if (-not $Name) {
Write-Host "错误: 请指定镜像名称" -ForegroundColor Red
return
}
$tarPath = Get-ImagePath -Name $Name -Tag $Tag
if (-not (Test-Path $tarPath)) {
Write-Host "错误: 镜像文件不存在: $tarPath" -ForegroundColor Red
return
}
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " 推送镜像到服务器" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "镜像: $Name`:$Tag" -ForegroundColor Gray
Write-Host "服务器: ${Username}@${Server}" -ForegroundColor Gray
Write-Host ""
# 压缩文件
Write-Host "[1/3] 压缩镜像文件..." -ForegroundColor Yellow
$tarGzPath = "$tarPath.gz"
Compress-Archive -Path $tarPath -DestinationPath $tarGzPath -Force
$compressedSize = [math]::Round((Get-Item $tarGzPath).Length / 1MB, 2)
Write-Host "✓ 已压缩: $compressedSize MB" -ForegroundColor Green
# 传输
Write-Host ""
Write-Host "[2/3] 传输到服务器..." -ForegroundColor Yellow
$scpArgs = @("-i", $KeyPath, $tarGzPath, "${Username}@${Server}:/root/")
$scpProcess = Start-Process -FilePath "scp" -ArgumentList $scpArgs -NoNewWindow -PassThru -Wait
if ($scpProcess.ExitCode -eq 0) {
Write-Host "✓ 传输成功" -ForegroundColor Green
} else {
Write-Host "✗ 传输失败" -ForegroundColor Red
Remove-Item $tarGzPath -ErrorAction SilentlyContinue
return
}
# 在服务器上加载
Write-Host ""
Write-Host "[3/3] 在服务器上加载镜像..." -ForegroundColor Yellow
$tarGzFileName = Split-Path $tarGzPath -Leaf
$tarFileName = Split-Path $tarPath -Leaf
$sshCmd = "cd /root && gunzip -c $tarGzFileName > $tarFileName && docker load -i $tarFileName && rm -f $tarFileName $tarGzFileName && docker images | grep $(if ($Name -match '/') { $Name } else { 'minio/mc' })"
$sshArgs = @("-i", $KeyPath, "${Username}@${Server}", $sshCmd)
$sshProcess = Start-Process -FilePath "ssh" -ArgumentList $sshArgs -NoNewWindow -PassThru -Wait
if ($sshProcess.ExitCode -eq 0) {
Write-Host "✓ 镜像已加载到服务器" -ForegroundColor Green
} else {
Write-Host "✗ 服务器加载失败" -ForegroundColor Red
}
# 清理本地压缩文件
Remove-Item $tarGzPath -ErrorAction SilentlyContinue
Write-Host ""
Write-Host "完成!" -ForegroundColor Green
}
# 主逻辑
switch ($Action) {
"list" {
List-Images
}
"save" {
Save-Image -Name $ImageName -Tag $ImageTag
}
"load" {
Load-Image -Name $ImageName -Tag $ImageTag
}
"update" {
Update-Image -Name $ImageName -Tag $ImageTag
}
"push-to-server" {
Push-ToServer -Name $ImageName -Tag $ImageTag -Server $Server -Username $Username -KeyPath $KeyPath
}
}