doracoin revisó este gist . Ir a la revisión
1 file changed, 95 insertions
start-podman-container.ps1(archivo creado)
| @@ -0,0 +1,95 @@ | |||
| 1 | + | <# | |
| 2 | + | .SYNOPSIS | |
| 3 | + | 启动 Windows 上的 Podman 容器 | |
| 4 | + | ||
| 5 | + | .DESCRIPTION | |
| 6 | + | 此脚本检查 Podman machine 状态,必要时启动它,然后启动指定的容器列表。 | |
| 7 | + | #> | |
| 8 | + | ||
| 9 | + | # 配置参数 | |
| 10 | + | $PodmanWaitTime = 3 | |
| 11 | + | $PodmanContainerWaitTime = 3 | |
| 12 | + | $ContainerList = @("filecodebox") | |
| 13 | + | ||
| 14 | + | function Write-ColorOutput { | |
| 15 | + | param( | |
| 16 | + | [string]$Message, | |
| 17 | + | [string]$Color = "White" | |
| 18 | + | ) | |
| 19 | + | Write-Host $Message -ForegroundColor $Color | |
| 20 | + | } | |
| 21 | + | ||
| 22 | + | try { | |
| 23 | + | # 检查 Podman 是否可用 | |
| 24 | + | if (-not (Get-Command podman -ErrorAction SilentlyContinue)) { | |
| 25 | + | throw "Podman 未安装或未在 PATH 中找到。请确保已安装 Podman。" | |
| 26 | + | } | |
| 27 | + | ||
| 28 | + | Write-ColorOutput "等待 $PodmanWaitTime 秒以确保系统准备就绪..." "Yellow" | |
| 29 | + | Start-Sleep -Seconds $PodmanWaitTime | |
| 30 | + | ||
| 31 | + | # 检查 Podman machine 状态 | |
| 32 | + | Write-ColorOutput "检查 Podman machine 状态..." "Cyan" | |
| 33 | + | $machineInfo = podman machine info 2>&1 | |
| 34 | + | ||
| 35 | + | if ($LASTEXITCODE -ne 0) { | |
| 36 | + | throw "获取 Podman machine 信息时出错: $machineInfo" | |
| 37 | + | } | |
| 38 | + | ||
| 39 | + | $machineState = ($machineInfo | Select-String "machinestate").Line | |
| 40 | + | if (-not $machineState) { | |
| 41 | + | throw "无法从 podman machine info 中解析机器状态" | |
| 42 | + | } | |
| 43 | + | ||
| 44 | + | $machineState = ($machineState -split ":")[1].Trim().ToLower() | |
| 45 | + | ||
| 46 | + | if ($machineState -ne "running") { | |
| 47 | + | Write-ColorOutput "启动 Podman machine..." "Yellow" | |
| 48 | + | $startResult = podman machine start 2>&1 | |
| 49 | + | ||
| 50 | + | if ($LASTEXITCODE -ne 0) { | |
| 51 | + | throw "启动 Podman machine 失败: $startResult" | |
| 52 | + | } | |
| 53 | + | ||
| 54 | + | Write-ColorOutput "等待 Podman machine 启动完成..." "Cyan" | |
| 55 | + | Start-Sleep -Seconds 10 | |
| 56 | + | } else { | |
| 57 | + | Write-ColorOutput "Podman machine 已在运行。" "Green" | |
| 58 | + | } | |
| 59 | + | ||
| 60 | + | # 启动容器 | |
| 61 | + | Write-ColorOutput "启动 Podman 容器..." "Cyan" | |
| 62 | + | Start-Sleep -Seconds $PodmanContainerWaitTime | |
| 63 | + | ||
| 64 | + | foreach ($container in $ContainerList) { | |
| 65 | + | Write-ColorOutput "启动容器: $container" "Yellow" | |
| 66 | + | ||
| 67 | + | # 检查容器是否存在 | |
| 68 | + | $containerExists = podman container inspect $container 2>&1 | |
| 69 | + | if ($LASTEXITCODE -ne 0) { | |
| 70 | + | Write-ColorOutput "警告: 容器 '$container' 不存在或无法访问" "Red" | |
| 71 | + | continue | |
| 72 | + | } | |
| 73 | + | ||
| 74 | + | # 启动容器 | |
| 75 | + | $startResult = podman container start $container 2>&1 | |
| 76 | + | if ($LASTEXITCODE -ne 0) { | |
| 77 | + | Write-ColorOutput "启动容器 '$container' 失败: $startResult" "Red" | |
| 78 | + | } else { | |
| 79 | + | Write-ColorOutput "容器 '$container' 启动成功" "Green" | |
| 80 | + | } | |
| 81 | + | ||
| 82 | + | Start-Sleep -Seconds 2 | |
| 83 | + | } | |
| 84 | + | ||
| 85 | + | Write-ColorOutput "所有操作已完成。" "Green" | |
| 86 | + | ||
| 87 | + | # 显示容器状态摘要 | |
| 88 | + | Write-ColorOutput "`n容器状态摘要:" "Cyan" | |
| 89 | + | podman ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | |
| 90 | + | Pause | |
| 91 | + | } | |
| 92 | + | catch { | |
| 93 | + | Write-ColorOutput "错误: $($_.Exception.Message)" "Red" | |
| 94 | + | exit 1 | |
| 95 | + | } | |
Siguiente
Anterior