deploy-to-server.ps1 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # MQTT项目部署脚本
  2. # 将项目部署到树莓派服务器 (192.168.1.17)
  3. param(
  4. [string]$ServerIP = "192.168.1.17",
  5. [string]$Username = "yangfei",
  6. [string]$Password = "yangfei",
  7. [string]$ProjectPath = "/home/yangfei/mqtt-vue-dashboard"
  8. )
  9. # 颜色定义
  10. $Green = "\u001b[32m"
  11. $Red = "\u001b[31m"
  12. $Yellow = "\u001b[33m"
  13. $Reset = "\u001b[0m"
  14. function Write-Color {
  15. param([string]$Color, [string]$Message)
  16. Write-Host "$Color$Message$Reset"
  17. }
  18. function Write-Success { param([string]$Message) Write-Color -Color $Green -Message $Message }
  19. function Write-Error { param([string]$Message) Write-Color -Color $Red -Message $Message }
  20. function Write-Warning { param([string]$Message) Write-Color -Color $Yellow -Message $Message }
  21. function Test-SSHConnection {
  22. Write-Host "测试SSH连接..."
  23. try {
  24. $result = ssh ${Username}@${ServerIP} "echo '连接成功'"
  25. if ($LASTEXITCODE -eq 0) {
  26. Write-Success "SSH连接测试成功"
  27. return $true
  28. } else {
  29. Write-Error "SSH连接失败"
  30. return $false
  31. }
  32. } catch {
  33. Write-Error "SSH连接异常: $_"
  34. return $false
  35. }
  36. }
  37. function Prepare-Server {
  38. Write-Host "准备服务器环境..."
  39. # 在服务器上创建项目目录
  40. $commands = @(
  41. "mkdir -p $ProjectPath",
  42. "mkdir -p $ProjectPath/backend",
  43. "mkdir -p $ProjectPath/frontend",
  44. "mkdir -p $ProjectPath/logs"
  45. )
  46. foreach ($cmd in $commands) {
  47. ssh ${Username}@${ServerIP} $cmd
  48. if ($LASTEXITCODE -ne 0) {
  49. Write-Error "服务器准备失败: $cmd"
  50. return $false
  51. }
  52. }
  53. Write-Success "服务器环境准备完成"
  54. return $true
  55. }
  56. function Deploy-Backend {
  57. Write-Host "部署后端代码..."
  58. # 构建后端
  59. Set-Location "mqtt-vue-dashboard\server"
  60. Write-Host "安装后端依赖..."
  61. npm install
  62. if ($LASTEXITCODE -ne 0) {
  63. Write-Error "后端依赖安装失败"
  64. return $false
  65. }
  66. Write-Host "构建后端..."
  67. npm run build
  68. if ($LASTEXITCODE -ne 0) {
  69. Write-Error "后端构建失败"
  70. return $false
  71. }
  72. # 复制后端文件到服务器
  73. Write-Host "上传后端文件到服务器..."
  74. $backendFiles = @(
  75. "dist",
  76. "package.json",
  77. "package-lock.json",
  78. ".env",
  79. ".env.production"
  80. )
  81. foreach ($file in $backendFiles) {
  82. if (Test-Path $file) {
  83. scp -r $file ${Username}@${ServerIP}:${ProjectPath}/backend/
  84. if ($LASTEXITCODE -ne 0) {
  85. Write-Error "后端文件上传失败: $file"
  86. return $false
  87. }
  88. }
  89. }
  90. Set-Location "..\.."
  91. Write-Success "后端部署完成"
  92. return $true
  93. }
  94. function Deploy-Frontend {
  95. Write-Host "部署前端代码..."
  96. # 构建前端
  97. Set-Location "mqtt-vue-dashboard"
  98. Write-Host "安装前端依赖..."
  99. npm install
  100. if ($LASTEXITCODE -ne 0) {
  101. Write-Error "前端依赖安装失败"
  102. return $false
  103. }
  104. Write-Host "构建前端..."
  105. npm run build
  106. if ($LASTEXITCODE -ne 0) {
  107. Write-Error "前端构建失败"
  108. return $false
  109. }
  110. # 复制前端文件到服务器
  111. Write-Host "上传前端文件到服务器..."
  112. if (Test-Path "dist") {
  113. scp -r dist ${Username}@${ServerIP}:${ProjectPath}/frontend/
  114. if ($LASTEXITCODE -ne 0) {
  115. Write-Error "前端文件上传失败"
  116. return $false
  117. }
  118. }
  119. Set-Location ".."
  120. Write-Success "前端部署完成"
  121. return $true
  122. }
  123. function Setup-Server {
  124. Write-Host "在服务器上设置项目..."
  125. # 在服务器上安装依赖和启动服务
  126. $setupScript = @"
  127. cd $ProjectPath/backend
  128. npm install --production
  129. # 创建启动脚本
  130. cat > $ProjectPath/start-backend.sh << 'EOF'
  131. #!/bin/bash
  132. cd $ProjectPath/backend
  133. npm start
  134. EOF
  135. chmod +x $ProjectPath/start-backend.sh
  136. # 创建nginx配置(如果需要)
  137. cat > $ProjectPath/nginx.conf << 'EOF'
  138. server {
  139. listen 80;
  140. server_name localhost;
  141. # 前端静态文件
  142. location / {
  143. root $ProjectPath/frontend/dist;
  144. index index.html;
  145. try_files \$uri \$uri/ /index.html;
  146. }
  147. # 后端API代理
  148. location /api {
  149. proxy_pass http://localhost:3002;
  150. proxy_http_version 1.1;
  151. proxy_set_header Upgrade \$http_upgrade;
  152. proxy_set_header Connection 'upgrade';
  153. proxy_set_header Host \$host;
  154. proxy_cache_bypass \$http_upgrade;
  155. }
  156. # WebSocket代理
  157. location /socket.io {
  158. proxy_pass http://localhost:3002;
  159. proxy_http_version 1.1;
  160. proxy_set_header Upgrade \$http_upgrade;
  161. proxy_set_header Connection 'upgrade';
  162. proxy_set_header Host \$host;
  163. proxy_cache_bypass \$http_upgrade;
  164. }
  165. }
  166. EOF
  167. "@
  168. # 执行设置脚本
  169. ssh ${Username}@${ServerIP} "bash -c '$setupScript'"
  170. if ($LASTEXITCODE -ne 0) {
  171. Write-Error "服务器设置失败"
  172. return $false
  173. }
  174. Write-Success "服务器设置完成"
  175. return $true
  176. }
  177. function Start-Services {
  178. Write-Host "启动服务..."
  179. # 启动后端服务
  180. $startScript = @"
  181. # 检查是否已有进程在运行
  182. pkill -f "node.*backend"
  183. # 启动后端服务
  184. cd $ProjectPath/backend
  185. nohup npm start > $ProjectPath/logs/backend.log 2>&1 &
  186. # 等待服务启动
  187. sleep 5
  188. # 检查服务是否正常启动
  189. if curl -s http://localhost:3002/api/health > /dev/null; then
  190. echo "后端服务启动成功"
  191. else
  192. echo "后端服务启动失败"
  193. exit 1
  194. fi
  195. "@
  196. ssh ${Username}@${ServerIP} "bash -c '$startScript'"
  197. if ($LASTEXITCODE -ne 0) {
  198. Write-Error "服务启动失败"
  199. return $false
  200. }
  201. Write-Success "服务启动完成"
  202. return $true
  203. }
  204. function Verify-Deployment {
  205. Write-Host "验证部署结果..."
  206. # 检查后端服务
  207. $healthCheck = ssh ${Username}@${ServerIP} "curl -s http://localhost:3002/api/health || echo '服务未启动'"
  208. if ($healthCheck -match "服务未启动") {
  209. Write-Error "后端服务未正常运行"
  210. return $false
  211. }
  212. Write-Success "后端服务运行正常"
  213. # 检查前端文件
  214. $frontendCheck = ssh ${Username}@${ServerIP} "ls -la $ProjectPath/frontend/dist/"
  215. if ($LASTEXITCODE -ne 0) {
  216. Write-Error "前端文件部署失败"
  217. return $false
  218. }
  219. Write-Success "前端文件部署成功"
  220. Write-Success "部署验证完成"
  221. return $true
  222. }
  223. # 主部署流程
  224. Write-Host "开始部署MQTT项目到服务器 $ServerIP" -ForegroundColor Cyan
  225. # 1. 测试连接
  226. if (-not (Test-SSHConnection)) {
  227. Write-Error "部署失败:无法连接到服务器"
  228. exit 1
  229. }
  230. # 2. 准备服务器环境
  231. if (-not (Prepare-Server)) {
  232. Write-Error "部署失败:服务器环境准备失败"
  233. exit 1
  234. }
  235. # 3. 部署后端
  236. if (-not (Deploy-Backend)) {
  237. Write-Error "部署失败:后端部署失败"
  238. exit 1
  239. }
  240. # 4. 部署前端
  241. if (-not (Deploy-Frontend)) {
  242. Write-Error "部署失败:前端部署失败"
  243. exit 1
  244. }
  245. # 5. 服务器设置
  246. if (-not (Setup-Server)) {
  247. Write-Error "部署失败:服务器设置失败"
  248. exit 1
  249. }
  250. # 6. 启动服务
  251. if (-not (Start-Services)) {
  252. Write-Error "部署失败:服务启动失败"
  253. exit 1
  254. }
  255. # 7. 验证部署
  256. if (-not (Verify-Deployment)) {
  257. Write-Error "部署失败:验证失败"
  258. exit 1
  259. }
  260. Write-Success "部署完成!"
  261. Write-Host ""
  262. Write-Host "访问地址:" -ForegroundColor Yellow
  263. Write-Host " 后端API: http://$ServerIP`:3002" -ForegroundColor White
  264. Write-Host " 前端应用: http://$ServerIP`" -ForegroundColor White
  265. Write-Host ""
  266. Write-Host "查看日志:" -ForegroundColor Yellow
  267. Write-Host " 后端日志: ssh $Username@$ServerIP 'tail -f $ProjectPath/logs/backend.log'" -ForegroundColor White