simple-deploy.ps1 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # 简单部署脚本 - MQTT Vue Dashboard到树莓派服务器
  2. Write-Host "开始部署MQTT项目到服务器 192.168.1.17" -ForegroundColor Cyan
  3. # 1. 检查服务器连接
  4. Write-Host "1. 检查服务器连接..." -ForegroundColor Yellow
  5. ping 192.168.1.17 -n 2 | Out-Null
  6. if ($LASTEXITCODE -eq 0) {
  7. Write-Host " 服务器连接正常" -ForegroundColor Green
  8. } else {
  9. Write-Host " 服务器连接失败" -ForegroundColor Red
  10. exit 1
  11. }
  12. # 2. 在服务器上创建目录结构
  13. Write-Host "2. 创建服务器目录结构..." -ForegroundColor Yellow
  14. try {
  15. $result = ssh yangfei@192.168.1.17 "mkdir -p /home/yangfei/mqtt-vue-dashboard/{backend,frontend,logs}"
  16. Write-Host " 目录创建完成" -ForegroundColor Green
  17. } catch {
  18. Write-Host " 目录创建失败: $_" -ForegroundColor Red
  19. exit 1
  20. }
  21. # 3. 构建前端项目
  22. Write-Host "3. 构建前端项目..." -ForegroundColor Yellow
  23. Set-Location "mqtt-vue-dashboard"
  24. if (Test-Path "node_modules") {
  25. Write-Host " 前端依赖已存在,跳过安装" -ForegroundColor Green
  26. } else {
  27. Write-Host " 安装前端依赖..." -ForegroundColor Yellow
  28. npm install
  29. if ($LASTEXITCODE -ne 0) {
  30. Write-Host " 前端依赖安装失败" -ForegroundColor Red
  31. exit 1
  32. }
  33. }
  34. Write-Host " 构建前端..." -ForegroundColor Yellow
  35. npm run build
  36. if ($LASTEXITCODE -ne 0) {
  37. Write-Host " 前端构建失败" -ForegroundColor Red
  38. exit 1
  39. }
  40. # 4. 上传前端文件到服务器
  41. Write-Host "4. 上传前端文件到服务器..." -ForegroundColor Yellow
  42. if (Test-Path "dist") {
  43. scp -r dist yangfei@192.168.1.17:/home/yangfei/mqtt-vue-dashboard/frontend/
  44. if ($LASTEXITCODE -eq 0) {
  45. Write-Host " 前端文件上传成功" -ForegroundColor Green
  46. } else {
  47. Write-Host " 前端文件上传失败" -ForegroundColor Red
  48. exit 1
  49. }
  50. } else {
  51. Write-Host " 前端构建目录不存在" -ForegroundColor Red
  52. exit 1
  53. }
  54. # 5. 构建后端项目
  55. Write-Host "5. 构建后端项目..." -ForegroundColor Yellow
  56. Set-Location "server"
  57. if (Test-Path "node_modules") {
  58. Write-Host " 后端依赖已存在,跳过安装" -ForegroundColor Green
  59. } else {
  60. Write-Host " 安装后端依赖..." -ForegroundColor Yellow
  61. npm install
  62. if ($LASTEXITCODE -ne 0) {
  63. Write-Host " 后端依赖安装失败" -ForegroundColor Red
  64. exit 1
  65. }
  66. }
  67. Write-Host " 构建后端..." -ForegroundColor Yellow
  68. npm run build
  69. if ($LASTEXITCODE -ne 0) {
  70. Write-Host " 后端构建失败" -ForegroundColor Red
  71. exit 1
  72. }
  73. # 6. 上传后端文件到服务器
  74. Write-Host "6. 上传后端文件到服务器..." -ForegroundColor Yellow
  75. $backendFiles = @("dist", "package.json", "package-lock.json", ".env", ".env.production")
  76. foreach ($file in $backendFiles) {
  77. if (Test-Path $file) {
  78. scp -r $file yangfei@192.168.1.17:/home/yangfei/mqtt-vue-dashboard/backend/
  79. if ($LASTEXITCODE -eq 0) {
  80. Write-Host " 文件 $file 上传成功" -ForegroundColor Green
  81. } else {
  82. Write-Host " 文件 $file 上传失败" -ForegroundColor Red
  83. }
  84. }
  85. }
  86. # 7. 在服务器上设置项目
  87. Write-Host "7. 在服务器上设置项目..." -ForegroundColor Yellow
  88. $setupCommands = @(
  89. "cd /home/yangfei/mqtt-vue-dashboard/backend",
  90. "npm install --production",
  91. "cd /home/yangfei/mqtt-vue-dashboard",
  92. "echo '#!/bin/bash' > start.sh",
  93. "echo 'cd /home/yangfei/mqtt-vue-dashboard/backend' >> start.sh",
  94. "echo 'npm start' >> start.sh",
  95. "chmod +x start.sh"
  96. )
  97. foreach ($cmd in $setupCommands) {
  98. try {
  99. ssh yangfei@192.168.1.17 $cmd
  100. Write-Host " 命令执行成功: $cmd" -ForegroundColor Green
  101. } catch {
  102. Write-Host " 命令执行失败: $cmd" -ForegroundColor Red
  103. }
  104. }
  105. # 8. 启动服务
  106. Write-Host "8. 启动后端服务..." -ForegroundColor Yellow
  107. try {
  108. # 先停止可能正在运行的服务
  109. ssh yangfei@192.168.1.17 "pkill -f 'node.*backend' || true"
  110. # 启动新服务
  111. ssh yangfei@192.168.1.17 "cd /home/yangfei/mqtt-vue-dashboard/backend && nohup npm start > ../logs/backend.log 2>&1 &"
  112. # 等待服务启动
  113. Start-Sleep -Seconds 5
  114. # 检查服务状态
  115. $healthCheck = ssh yangfei@192.168.1.17 "curl -s http://localhost:3002/api/health || echo 'not running'"
  116. if ($healthCheck -ne "not running") {
  117. Write-Host " 后端服务启动成功" -ForegroundColor Green
  118. } else {
  119. Write-Host " 后端服务启动失败" -ForegroundColor Red
  120. }
  121. } catch {
  122. Write-Host " 服务启动失败: $_" -ForegroundColor Red
  123. }
  124. # 9. 验证部署
  125. Write-Host "9. 验证部署结果..." -ForegroundColor Yellow
  126. try {
  127. # 检查前端文件
  128. $frontendCheck = ssh yangfei@192.168.1.17 "ls -la /home/yangfei/mqtt-vue-dashboard/frontend/dist/"
  129. Write-Host " 前端文件验证成功" -ForegroundColor Green
  130. # 检查后端服务
  131. $backendCheck = ssh yangfei@192.168.1.17 "ps aux | grep 'node.*backend' | grep -v grep"
  132. if ($backendCheck) {
  133. Write-Host " 后端服务运行正常" -ForegroundColor Green
  134. } else {
  135. Write-Host " 后端服务未运行" -ForegroundColor Red
  136. }
  137. } catch {
  138. Write-Host " 验证失败: $_" -ForegroundColor Red
  139. }
  140. Write-Host "" -ForegroundColor Cyan
  141. Write-Host "部署完成!" -ForegroundColor Green
  142. Write-Host "" -ForegroundColor Cyan
  143. Write-Host "访问信息:" -ForegroundColor Yellow
  144. Write-Host " 后端API: http://192.168.1.17:3002" -ForegroundColor White
  145. Write-Host " 前端应用: http://192.168.1.17" -ForegroundColor White
  146. Write-Host "" -ForegroundColor Cyan
  147. Write-Host "查看日志:" -ForegroundColor Yellow
  148. Write-Host " ssh yangfei@192.168.1.17 'tail -f /home/yangfei/mqtt-vue-dashboard/logs/backend.log'" -ForegroundColor White
  149. Write-Host "" -ForegroundColor Cyan