| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- # MQTT项目部署脚本
- # 将项目部署到树莓派服务器 (192.168.1.17)
- param(
- [string]$ServerIP = "192.168.1.17",
- [string]$Username = "yangfei",
- [string]$Password = "yangfei",
- [string]$ProjectPath = "/home/yangfei/mqtt-vue-dashboard"
- )
- # 颜色定义
- $Green = "\u001b[32m"
- $Red = "\u001b[31m"
- $Yellow = "\u001b[33m"
- $Reset = "\u001b[0m"
- function Write-Color {
- param([string]$Color, [string]$Message)
- Write-Host "$Color$Message$Reset"
- }
- function Write-Success { param([string]$Message) Write-Color -Color $Green -Message $Message }
- function Write-Error { param([string]$Message) Write-Color -Color $Red -Message $Message }
- function Write-Warning { param([string]$Message) Write-Color -Color $Yellow -Message $Message }
- function Test-SSHConnection {
- Write-Host "测试SSH连接..."
- try {
- $result = ssh ${Username}@${ServerIP} "echo '连接成功'"
- if ($LASTEXITCODE -eq 0) {
- Write-Success "SSH连接测试成功"
- return $true
- } else {
- Write-Error "SSH连接失败"
- return $false
- }
- } catch {
- Write-Error "SSH连接异常: $_"
- return $false
- }
- }
- function Prepare-Server {
- Write-Host "准备服务器环境..."
-
- # 在服务器上创建项目目录
- $commands = @(
- "mkdir -p $ProjectPath",
- "mkdir -p $ProjectPath/backend",
- "mkdir -p $ProjectPath/frontend",
- "mkdir -p $ProjectPath/logs"
- )
-
- foreach ($cmd in $commands) {
- ssh ${Username}@${ServerIP} $cmd
- if ($LASTEXITCODE -ne 0) {
- Write-Error "服务器准备失败: $cmd"
- return $false
- }
- }
-
- Write-Success "服务器环境准备完成"
- return $true
- }
- function Deploy-Backend {
- Write-Host "部署后端代码..."
-
- # 构建后端
- Set-Location "mqtt-vue-dashboard\server"
-
- Write-Host "安装后端依赖..."
- npm install
- if ($LASTEXITCODE -ne 0) {
- Write-Error "后端依赖安装失败"
- return $false
- }
-
- Write-Host "构建后端..."
- npm run build
- if ($LASTEXITCODE -ne 0) {
- Write-Error "后端构建失败"
- return $false
- }
-
- # 复制后端文件到服务器
- Write-Host "上传后端文件到服务器..."
- $backendFiles = @(
- "dist",
- "package.json",
- "package-lock.json",
- ".env",
- ".env.production"
- )
-
- foreach ($file in $backendFiles) {
- if (Test-Path $file) {
- scp -r $file ${Username}@${ServerIP}:${ProjectPath}/backend/
- if ($LASTEXITCODE -ne 0) {
- Write-Error "后端文件上传失败: $file"
- return $false
- }
- }
- }
-
- Set-Location "..\.."
- Write-Success "后端部署完成"
- return $true
- }
- function Deploy-Frontend {
- Write-Host "部署前端代码..."
-
- # 构建前端
- Set-Location "mqtt-vue-dashboard"
-
- Write-Host "安装前端依赖..."
- npm install
- if ($LASTEXITCODE -ne 0) {
- Write-Error "前端依赖安装失败"
- return $false
- }
-
- Write-Host "构建前端..."
- npm run build
- if ($LASTEXITCODE -ne 0) {
- Write-Error "前端构建失败"
- return $false
- }
-
- # 复制前端文件到服务器
- Write-Host "上传前端文件到服务器..."
- if (Test-Path "dist") {
- scp -r dist ${Username}@${ServerIP}:${ProjectPath}/frontend/
- if ($LASTEXITCODE -ne 0) {
- Write-Error "前端文件上传失败"
- return $false
- }
- }
-
- Set-Location ".."
- Write-Success "前端部署完成"
- return $true
- }
- function Setup-Server {
- Write-Host "在服务器上设置项目..."
-
- # 在服务器上安装依赖和启动服务
- $setupScript = @"
- cd $ProjectPath/backend
- npm install --production
- # 创建启动脚本
- cat > $ProjectPath/start-backend.sh << 'EOF'
- #!/bin/bash
- cd $ProjectPath/backend
- npm start
- EOF
- chmod +x $ProjectPath/start-backend.sh
- # 创建nginx配置(如果需要)
- cat > $ProjectPath/nginx.conf << 'EOF'
- server {
- listen 80;
- server_name localhost;
-
- # 前端静态文件
- location / {
- root $ProjectPath/frontend/dist;
- index index.html;
- try_files \$uri \$uri/ /index.html;
- }
-
- # 后端API代理
- location /api {
- proxy_pass http://localhost:3002;
- proxy_http_version 1.1;
- proxy_set_header Upgrade \$http_upgrade;
- proxy_set_header Connection 'upgrade';
- proxy_set_header Host \$host;
- proxy_cache_bypass \$http_upgrade;
- }
-
- # WebSocket代理
- location /socket.io {
- proxy_pass http://localhost:3002;
- proxy_http_version 1.1;
- proxy_set_header Upgrade \$http_upgrade;
- proxy_set_header Connection 'upgrade';
- proxy_set_header Host \$host;
- proxy_cache_bypass \$http_upgrade;
- }
- }
- EOF
- "@
-
- # 执行设置脚本
- ssh ${Username}@${ServerIP} "bash -c '$setupScript'"
- if ($LASTEXITCODE -ne 0) {
- Write-Error "服务器设置失败"
- return $false
- }
-
- Write-Success "服务器设置完成"
- return $true
- }
- function Start-Services {
- Write-Host "启动服务..."
-
- # 启动后端服务
- $startScript = @"
- # 检查是否已有进程在运行
- pkill -f "node.*backend"
- # 启动后端服务
- cd $ProjectPath/backend
- nohup npm start > $ProjectPath/logs/backend.log 2>&1 &
- # 等待服务启动
- sleep 5
- # 检查服务是否正常启动
- if curl -s http://localhost:3002/api/health > /dev/null; then
- echo "后端服务启动成功"
- else
- echo "后端服务启动失败"
- exit 1
- fi
- "@
-
- ssh ${Username}@${ServerIP} "bash -c '$startScript'"
- if ($LASTEXITCODE -ne 0) {
- Write-Error "服务启动失败"
- return $false
- }
-
- Write-Success "服务启动完成"
- return $true
- }
- function Verify-Deployment {
- Write-Host "验证部署结果..."
-
- # 检查后端服务
- $healthCheck = ssh ${Username}@${ServerIP} "curl -s http://localhost:3002/api/health || echo '服务未启动'"
- if ($healthCheck -match "服务未启动") {
- Write-Error "后端服务未正常运行"
- return $false
- }
-
- Write-Success "后端服务运行正常"
-
- # 检查前端文件
- $frontendCheck = ssh ${Username}@${ServerIP} "ls -la $ProjectPath/frontend/dist/"
- if ($LASTEXITCODE -ne 0) {
- Write-Error "前端文件部署失败"
- return $false
- }
-
- Write-Success "前端文件部署成功"
- Write-Success "部署验证完成"
- return $true
- }
- # 主部署流程
- Write-Host "开始部署MQTT项目到服务器 $ServerIP" -ForegroundColor Cyan
- # 1. 测试连接
- if (-not (Test-SSHConnection)) {
- Write-Error "部署失败:无法连接到服务器"
- exit 1
- }
- # 2. 准备服务器环境
- if (-not (Prepare-Server)) {
- Write-Error "部署失败:服务器环境准备失败"
- exit 1
- }
- # 3. 部署后端
- if (-not (Deploy-Backend)) {
- Write-Error "部署失败:后端部署失败"
- exit 1
- }
- # 4. 部署前端
- if (-not (Deploy-Frontend)) {
- Write-Error "部署失败:前端部署失败"
- exit 1
- }
- # 5. 服务器设置
- if (-not (Setup-Server)) {
- Write-Error "部署失败:服务器设置失败"
- exit 1
- }
- # 6. 启动服务
- if (-not (Start-Services)) {
- Write-Error "部署失败:服务启动失败"
- exit 1
- }
- # 7. 验证部署
- if (-not (Verify-Deployment)) {
- Write-Error "部署失败:验证失败"
- exit 1
- }
- Write-Success "部署完成!"
- Write-Host ""
- Write-Host "访问地址:" -ForegroundColor Yellow
- Write-Host " 后端API: http://$ServerIP`:3002" -ForegroundColor White
- Write-Host " 前端应用: http://$ServerIP`" -ForegroundColor White
- Write-Host ""
- Write-Host "查看日志:" -ForegroundColor Yellow
- Write-Host " 后端日志: ssh $Username@$ServerIP 'tail -f $ProjectPath/logs/backend.log'" -ForegroundColor White
|