buildDockerImage_apq3.ps1 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Cognio 远程构建 Docker 镜像脚本(海外服务器版本,无国内加速)
  2. [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  3. # ============================================================
  4. # 配置区
  5. # ============================================================
  6. # 远程服务器配置
  7. $remoteUser = "root"
  8. $remoteHost = "vps-apq3.zalhb.com"
  9. $remotePort = "22"
  10. # Git 仓库配置
  11. $repoUrl = "https://github.com/0xReLogic/Cognio.git"
  12. $repoDir = "Cognio"
  13. # Docker 镜像配置
  14. $imageName = "amwpfiqvy/cognio"
  15. # tmux 会话名
  16. $tmuxSession = "dbx"
  17. # ============================================================
  18. # 主程序
  19. # ============================================================
  20. Write-Host ""
  21. Write-Host "========================================" -ForegroundColor Cyan
  22. Write-Host " Cognio 远程构建 Docker 镜像" -ForegroundColor Cyan
  23. Write-Host "========================================" -ForegroundColor Cyan
  24. Write-Host ""
  25. # Docker 镜像标签:提示用户输入
  26. $imageTagInput = Read-Host "请输入额外的 Docker 镜像标签 (多个用空格分隔,留空则仅推送 amd64)"
  27. $inputTags = @()
  28. if ($imageTagInput -ne "") {
  29. $inputTags = $imageTagInput -split '\s+' | Where-Object { $_ -ne "" }
  30. }
  31. # 构建最终标签列表
  32. $imageTags = @("amd64") + $inputTags | Select-Object -Unique
  33. # 构建 -t 参数列表
  34. $tagParams = ($imageTags | ForEach-Object { "-t ${imageName}:$_" }) -join " "
  35. Write-Host "镜像标签: $($imageTags -join ', ')" -ForegroundColor Yellow
  36. Write-Host ""
  37. # 支持命令行参数覆盖默认配置
  38. if ($args.Count -ge 1) { $remoteUser = $args[0] }
  39. if ($args.Count -ge 2) { $remoteHost = $args[1] }
  40. if ($args.Count -ge 3) { $remotePort = $args[2] }
  41. Write-Host "连接到: $remoteUser@$remoteHost`:$remotePort" -ForegroundColor Yellow
  42. Write-Host ""
  43. # ============================================================
  44. # 构建远程执行的 Shell 脚本
  45. # ============================================================
  46. # 生成远程脚本内容(写入临时文件执行,避免引号嵌套问题)
  47. $scriptContent = @"
  48. #!/bin/bash
  49. cd ~
  50. if [ -d "$repoDir" ]; then
  51. echo "目录已存在,还原本地修改并拉取最新代码..."
  52. cd $repoDir
  53. git checkout .
  54. git pull
  55. else
  56. echo "目录不存在,正在克隆仓库..."
  57. git clone $repoUrl
  58. cd $repoDir
  59. fi
  60. echo "修复缺失的 openai 依赖..."
  61. sed -i 's/groq = "^0.4.0"/groq = "^0.4.0"\nopenai = "^1.0.0"/' pyproject.toml
  62. echo "正在构建 Docker 镜像(原生 amd64 架构)..."
  63. docker build $tagParams .
  64. echo "正在推送 Docker 镜像..."
  65. docker push ${imageName}:amd64
  66. "@
  67. # 将脚本内容转为 base64,避免特殊字符问题
  68. $scriptBase64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($scriptContent))
  69. # tmux 命令:解码并执行脚本
  70. $remoteCmd = "tmux has-session -t $tmuxSession 2>/dev/null || tmux new-session -d -s $tmuxSession; " +
  71. "tmux send-keys -t $tmuxSession 'echo $scriptBase64 | base64 -d > /tmp/build_cognio.sh && bash /tmp/build_cognio.sh' Enter"
  72. # ============================================================
  73. # 执行远程命令
  74. # ============================================================
  75. Write-Host "正在发送命令到 tmux 会话: $tmuxSession" -ForegroundColor Yellow
  76. Write-Host ""
  77. & ssh -p $remotePort "$remoteUser@$remoteHost" $remoteCmd
  78. if ($LASTEXITCODE -eq 0) {
  79. Write-Host ""
  80. Write-Host "========================================" -ForegroundColor Green
  81. Write-Host " 命令已发送到 tmux 会话: $tmuxSession" -ForegroundColor Green
  82. Write-Host "========================================" -ForegroundColor Green
  83. Write-Host ""
  84. Write-Host "推送目标:" -ForegroundColor Yellow
  85. foreach ($tag in $imageTags) {
  86. Write-Host " - ${imageName}:${tag}" -ForegroundColor Cyan
  87. }
  88. Write-Host ""
  89. Write-Host "查看构建进度:" -ForegroundColor Yellow
  90. Write-Host " ssh -t -p $remotePort $remoteUser@$remoteHost `"tmux attach -t $tmuxSession`"" -ForegroundColor Cyan
  91. } else {
  92. Write-Host ""
  93. Write-Host "部署失败,请检查错误信息" -ForegroundColor Red
  94. }
  95. Write-Host ""
  96. pause