fix: 更新Web-AMR项目部署脚本,增强用户提示信息,简化选择和验证流程,支持自定义服务器配置
This commit is contained in:
parent
b0f7620626
commit
ec0b182671
745
自动部署脚本.bat
745
自动部署脚本.bat
@ -5,9 +5,19 @@ title Web-AMR项目部署脚本
|
||||
|
||||
echo ==================== Web-AMR项目部署开始 ====================
|
||||
|
||||
echo [重要提醒]-
|
||||
echo 如果遇到文件解压成功但服务器文件未更新的情况,可能的原因-
|
||||
echo 1. 浏览器缓存-请清除浏览器缓存或使用Ctrl+F5强制刷新-
|
||||
echo 2. CDN缓存-如果使用CDN,需要清除CDN缓存-
|
||||
echo 3. 服务器权限-确保部署用户有写入权限-
|
||||
echo 4. 文件系统缓存-脚本已包含sync命令强制刷新-
|
||||
echo 5. 服务进程缓存-如果是SPA应用,可能需要重启Web服务-
|
||||
echo 使用选项8可以验证服务器文件是否正确更新-
|
||||
echo.
|
||||
|
||||
:: 检查package.json是否存在
|
||||
if not exist "package.json" (
|
||||
echo [错误] 当前目录下未找到package.json文件,请确保在项目根目录下执行此脚本
|
||||
echo [错误]- 当前目录下未找到package.json文件,请确保在项目根目录下执行此脚本-
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
@ -15,38 +25,38 @@ if not exist "package.json" (
|
||||
:: 询问是否需要打包
|
||||
:ask_build
|
||||
echo.
|
||||
echo [选择] 是否需要重新打包项目?
|
||||
echo 1. 是,执行 npm run build
|
||||
echo 2. 否,使用现有的 dist 文件夹
|
||||
echo [选择]- 是否需要重新打包项目?
|
||||
echo 1. 是,执行 npm run build-
|
||||
echo 2. 否,使用现有的 dist 文件夹-
|
||||
echo.
|
||||
set /p build_choice="请输入选择 (1/2): "
|
||||
|
||||
if "%build_choice%"=="1" goto do_build
|
||||
if "%build_choice%"=="2" goto check_dist
|
||||
echo [错误] 请输入有效选择 (1 或 2)
|
||||
echo [错误]- 请输入有效选择 (1 或 2)-
|
||||
goto ask_build
|
||||
|
||||
:do_build
|
||||
echo.
|
||||
echo [步骤1] 开始构建项目...
|
||||
echo 执行命令:npm run build
|
||||
echo [步骤1]- 开始构建项目...
|
||||
echo 执行命令:npm run build-
|
||||
call npm run build
|
||||
|
||||
if %errorlevel% neq 0 (
|
||||
echo [错误] 构建失败,退出部署
|
||||
echo [错误]- 构建失败,退出部署-
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
echo [成功] 项目构建完成
|
||||
echo [成功]- 项目构建完成-
|
||||
goto choose_server
|
||||
|
||||
:check_dist
|
||||
:: 检查dist文件夹是否存在
|
||||
if not exist "dist" (
|
||||
echo [错误] 未找到dist文件夹,请选择重新打包
|
||||
echo [错误]- 未找到dist文件夹,请选择重新打包-
|
||||
goto ask_build
|
||||
)
|
||||
echo [信息] 使用现有的 dist 文件夹
|
||||
echo [信息]- 使用现有的 dist 文件夹-
|
||||
|
||||
:choose_server
|
||||
:: 服务器配置
|
||||
@ -69,19 +79,22 @@ set "server3_path=/var/www/web-map"
|
||||
set "server3_user=ma"
|
||||
set "server3_pass=2213@Aiit"
|
||||
set "server3_name=开发服务器80"
|
||||
set "server3_use_key=true"
|
||||
set "server3_use_key=false"
|
||||
|
||||
:: 选择部署方式
|
||||
:ask_deploy_mode
|
||||
echo.
|
||||
echo [选择] 请选择部署方式:
|
||||
echo 1. 单个部署 - %server1_name% (%server1_host%)
|
||||
echo 2. 单个部署 - %server2_name% (%server2_host%)
|
||||
echo 3. 单个部署 - %server3_name% (%server3_host%)
|
||||
echo 4. 批量部署 - 部署到所有服务器
|
||||
echo 5. 自定义批量部署
|
||||
echo [选择]- 请选择部署方式:
|
||||
echo 1. 单个部署-%server1_name% ^(%server1_host%^)-
|
||||
echo 2. 单个部署-%server2_name% ^(%server2_host%^)-
|
||||
echo 3. 单个部署-%server3_name% ^(%server3_host%^)-
|
||||
echo 4. 批量部署-部署到所有预设服务器-
|
||||
echo 5. 自定义批量部署^(从预设服务器中选择^)-
|
||||
echo 6. 添加并部署到自定义服务器-
|
||||
echo 7. 批量部署到自定义服务器-
|
||||
echo 8. 验证部署状态^(检查服务器文件是否更新^)-
|
||||
echo.
|
||||
set /p deploy_choice="请输入选择 (1-5): "
|
||||
set /p deploy_choice="请输入选择 (1-8): "
|
||||
|
||||
if "%deploy_choice%"=="1" (
|
||||
set deploy_servers=1
|
||||
@ -100,41 +113,277 @@ if "%deploy_choice%"=="4" (
|
||||
goto start_deploy
|
||||
)
|
||||
if "%deploy_choice%"=="5" goto custom_deploy
|
||||
echo [错误] 请输入有效选择 (1-5)
|
||||
if "%deploy_choice%"=="6" goto add_custom_server
|
||||
if "%deploy_choice%"=="7" goto deploy_custom_servers
|
||||
if "%deploy_choice%"=="8" goto verify_deployment
|
||||
echo [错误]- 请输入有效选择 (1-8)-
|
||||
goto ask_deploy_mode
|
||||
|
||||
:custom_deploy
|
||||
echo.
|
||||
echo [自定义批量部署] 请选择要部署的服务器(多选用逗号分隔,如:1,3)
|
||||
echo 1. %server1_name% (%server1_host%)
|
||||
echo 2. %server2_name% (%server2_host%)
|
||||
echo 3. %server3_name% (%server3_host%)
|
||||
echo [自定义批量部署] 请选择要部署的预设服务器^(多选用逗号分隔,如:1,3^)
|
||||
echo 1. %server1_name% ^(%server1_host%^)
|
||||
echo 2. %server2_name% ^(%server2_host%^)
|
||||
echo 3. %server3_name% ^(%server3_host%^)
|
||||
echo.
|
||||
set /p deploy_servers="请输入服务器编号: "
|
||||
goto start_deploy
|
||||
|
||||
:add_custom_server
|
||||
echo.
|
||||
echo ==================== 添加自定义服务器 ====================
|
||||
echo [信息] 请输入自定义服务器配置:
|
||||
echo.
|
||||
|
||||
set /p custom_name="服务器名称: "
|
||||
if "%custom_name%"=="" set custom_name=自定义服务器
|
||||
|
||||
set /p custom_host="服务器IP地址: "
|
||||
if "%custom_host%"=="" (
|
||||
echo [错误] IP地址不能为空
|
||||
pause
|
||||
goto ask_deploy_mode
|
||||
)
|
||||
|
||||
set /p custom_user="用户名: "
|
||||
if "%custom_user%"=="" (
|
||||
echo [错误] 用户名不能为空
|
||||
pause
|
||||
goto ask_deploy_mode
|
||||
)
|
||||
|
||||
set /p custom_pass="密码: "
|
||||
if "%custom_pass%"=="" (
|
||||
echo [错误] 密码不能为空
|
||||
pause
|
||||
goto ask_deploy_mode
|
||||
)
|
||||
|
||||
set /p custom_path="部署路径 (默认: /var/www/web): "
|
||||
if "%custom_path%"=="" set custom_path=/var/www/web
|
||||
|
||||
set /p custom_port="SSH端口 (默认: 22): "
|
||||
if "%custom_port%"=="" set custom_port=22
|
||||
|
||||
echo.
|
||||
echo [选择] 认证方式:
|
||||
echo 1. SSH密钥认证^(免密^)
|
||||
echo 2. 密码认证
|
||||
set /p auth_choice="请选择 (1/2): "
|
||||
|
||||
if "%auth_choice%"=="1" (
|
||||
set custom_use_key=true
|
||||
echo [信息] 将使用SSH密钥认证
|
||||
) else (
|
||||
set custom_use_key=false
|
||||
echo [信息] 将使用密码认证
|
||||
)
|
||||
|
||||
echo.
|
||||
echo ==================== 自定义服务器配置确认 ====================
|
||||
echo 服务器名称: %custom_name%
|
||||
echo IP地址: %custom_host%
|
||||
echo 用户名: %custom_user%
|
||||
echo 部署路径: %custom_path%
|
||||
echo SSH端口: %custom_port%
|
||||
if "%custom_use_key%"=="true" (
|
||||
echo 认证方式: SSH密钥认证^(免密^)
|
||||
) else (
|
||||
echo 认证方式: 密码认证
|
||||
)
|
||||
echo.
|
||||
set /p confirm="确认部署到此服务器?(y/n): "
|
||||
|
||||
if /i "%confirm%"=="y" (
|
||||
echo [信息] 开始部署到自定义服务器...
|
||||
|
||||
:: 检查dist文件夹
|
||||
if not exist "dist" (
|
||||
echo [错误] 未找到dist文件夹,请先构建项目
|
||||
pause
|
||||
goto ask_deploy_mode
|
||||
)
|
||||
|
||||
:: 创建临时压缩包
|
||||
echo [信息] 正在创建临时压缩包...
|
||||
powershell -Command "Compress-Archive -Path 'dist\*' -DestinationPath 'dist_temp.zip' -Force"
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误] 创建压缩包失败
|
||||
pause
|
||||
goto ask_deploy_mode
|
||||
)
|
||||
echo [成功] 临时压缩包创建完成
|
||||
|
||||
call :deploy_to_custom_server
|
||||
if !errorlevel! equ 0 (
|
||||
echo [成功] 自定义服务器部署完成!
|
||||
) else (
|
||||
echo [错误] 自定义服务器部署失败!
|
||||
)
|
||||
|
||||
:: 清理本地临时文件
|
||||
if exist "dist_temp.zip" del dist_temp.zip
|
||||
echo [信息] 本地临时文件清理完成
|
||||
) else (
|
||||
echo [取消] 取消部署
|
||||
)
|
||||
|
||||
echo.
|
||||
echo 按任意键返回主菜单...
|
||||
pause >nul
|
||||
goto ask_deploy_mode
|
||||
|
||||
:deploy_custom_servers
|
||||
echo.
|
||||
echo ==================== 批量部署到自定义服务器 ====================
|
||||
echo [信息] 此功能将指导您配置多个自定义服务器并批量部署
|
||||
echo.
|
||||
|
||||
set /p custom_count="请输入要配置的自定义服务器数量: "
|
||||
if "%custom_count%"=="" (
|
||||
echo [错误] 数量不能为空
|
||||
pause
|
||||
goto ask_deploy_mode
|
||||
)
|
||||
|
||||
:: 验证输入是否为数字
|
||||
echo %custom_count%| findstr /r "^[1-9][0-9]*$" >nul
|
||||
if errorlevel 1 (
|
||||
echo [错误] 请输入有效的数字
|
||||
pause
|
||||
goto ask_deploy_mode
|
||||
)
|
||||
|
||||
if %custom_count% gtr 10 (
|
||||
echo [错误] 服务器数量不能超过10个
|
||||
pause
|
||||
goto ask_deploy_mode
|
||||
)
|
||||
|
||||
echo [信息] 将配置 %custom_count% 个自定义服务器
|
||||
echo.
|
||||
|
||||
:: 检查dist文件夹
|
||||
if not exist "dist" (
|
||||
echo [错误] 未找到dist文件夹,请先构建项目
|
||||
pause
|
||||
goto ask_deploy_mode
|
||||
)
|
||||
|
||||
:: 创建临时压缩包
|
||||
echo [信息] 正在创建临时压缩包...
|
||||
powershell -Command "Compress-Archive -Path 'dist\*' -DestinationPath 'dist_temp.zip' -Force"
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误] 创建压缩包失败
|
||||
pause
|
||||
goto ask_deploy_mode
|
||||
)
|
||||
echo [成功] 临时压缩包创建完成
|
||||
echo.
|
||||
|
||||
set success_count=0
|
||||
set total_count=%custom_count%
|
||||
|
||||
for /l %%i in (1,1,%custom_count%) do (
|
||||
echo ==================== 配置第 %%i 个服务器 ====================
|
||||
call :config_single_custom_server %%i
|
||||
if !errorlevel! equ 0 (
|
||||
set /a success_count+=1
|
||||
)
|
||||
echo.
|
||||
)
|
||||
|
||||
:: 清理本地临时文件
|
||||
if exist "dist_temp.zip" del dist_temp.zip
|
||||
echo [信息] 本地临时文件清理完成
|
||||
|
||||
echo.
|
||||
echo ==================== 批量自定义服务器部署结果 ====================
|
||||
echo 总计配置服务器数:%total_count%
|
||||
echo 成功部署数:%success_count%
|
||||
if %success_count% equ %total_count% (
|
||||
echo [成功] 所有自定义服务器部署完成!
|
||||
) else (
|
||||
echo [警告] 部分自定义服务器部署失败,请检查错误信息
|
||||
)
|
||||
|
||||
echo.
|
||||
echo 按任意键返回主菜单...
|
||||
pause >nul
|
||||
goto ask_deploy_mode
|
||||
|
||||
:config_single_custom_server
|
||||
setlocal enabledelayedexpansion
|
||||
set server_index=%1
|
||||
|
||||
echo [配置] 第 %server_index% 个服务器:
|
||||
|
||||
set /p temp_name="服务器名称 (默认: 自定义服务器%server_index%): "
|
||||
if "%temp_name%"=="" set temp_name=自定义服务器%server_index%
|
||||
|
||||
set /p temp_host="服务器IP地址: "
|
||||
if "%temp_host%"=="" (
|
||||
echo [错误] IP地址不能为空,跳过此服务器
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
set /p temp_user="用户名: "
|
||||
if "%temp_user%"=="" (
|
||||
echo [错误] 用户名不能为空,跳过此服务器
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
set /p temp_pass="密码: "
|
||||
if "%temp_pass%"=="" (
|
||||
echo [错误] 密码不能为空,跳过此服务器
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
set /p temp_path="部署路径 (默认: /var/www/web): "
|
||||
if "%temp_path%"=="" set temp_path=/var/www/web
|
||||
|
||||
set /p temp_port="SSH端口 (默认: 22): "
|
||||
if "%temp_port%"=="" set temp_port=22
|
||||
|
||||
echo [选择] 认证方式:
|
||||
echo 1. SSH密钥认证^(免密^)
|
||||
echo 2. 密码认证
|
||||
set /p auth_choice="请选择 (1/2): "
|
||||
|
||||
if "%auth_choice%"=="1" (
|
||||
set temp_use_key=true
|
||||
) else (
|
||||
set temp_use_key=false
|
||||
)
|
||||
|
||||
echo [信息] 开始部署到 %temp_name% (%temp_host%)...
|
||||
call :deploy_to_temp_server "%temp_name%" "%temp_host%" "%temp_user%" "%temp_pass%" "%temp_path%" "%temp_port%" "%temp_use_key%"
|
||||
exit /b %errorlevel%
|
||||
|
||||
:start_deploy
|
||||
:: 检查scp命令是否可用
|
||||
where scp >nul 2>&1
|
||||
if %errorlevel% neq 0 (
|
||||
echo [错误] 未找到scp命令,请安装OpenSSH客户端
|
||||
echo Windows 10用户可以通过以下方式安装:
|
||||
echo 设置 -^> 应用 -^> 可选功能 -^> 添加功能 -^> OpenSSH客户端
|
||||
echo [错误]- 未找到scp命令,请安装OpenSSH客户端-
|
||||
echo Windows 10用户可以通过以下方式安装-
|
||||
echo 设置 -^> 应用 -^> 可选功能 -^> 添加功能 -^> OpenSSH客户端-
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
:: 创建临时压缩包
|
||||
echo.
|
||||
echo [信息] 正在创建临时压缩包...
|
||||
echo [信息]- 正在创建临时压缩包...
|
||||
powershell -Command "Compress-Archive -Path 'dist\*' -DestinationPath 'dist_temp.zip' -Force"
|
||||
|
||||
if %errorlevel% neq 0 (
|
||||
echo [错误] 创建压缩包失败
|
||||
echo [错误]- 创建压缩包失败-
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
echo [成功] 临时压缩包创建完成
|
||||
echo [成功]- 临时压缩包创建完成-
|
||||
|
||||
:: 部署到选定的服务器
|
||||
set success_count=0
|
||||
@ -150,19 +399,16 @@ for %%s in (%deploy_servers%) do (
|
||||
|
||||
:: 清理本地临时文件
|
||||
if exist "dist_temp.zip" del dist_temp.zip
|
||||
echo [成功] 本地临时文件清理完成
|
||||
echo [成功]- 本地临时文件清理完成-
|
||||
|
||||
echo.
|
||||
echo ==================== 部署结果汇总 ====================
|
||||
echo 总计部署服务器数:%total_count%
|
||||
echo 成功部署数:%success_count%
|
||||
echo 总计部署服务器数:%total_count%-
|
||||
echo 成功部署数:%success_count%-
|
||||
if %success_count% equ %total_count% (
|
||||
echo [成功] 所有服务器部署完成!
|
||||
echo.
|
||||
echo [访问提示] Web-AMR项目部署路径:/var/www/web-map
|
||||
echo 请确认nginx或apache配置指向正确的路径
|
||||
echo [成功]- 所有服务器部署完成!
|
||||
) else (
|
||||
echo [警告] 部分服务器部署失败,请检查错误信息
|
||||
echo [警告]- 部分服务器部署失败,请检查错误信息-
|
||||
)
|
||||
echo.
|
||||
echo 脚本执行完成!
|
||||
@ -196,54 +442,447 @@ if "%server_num%"=="1" (
|
||||
set current_name=!server3_name!
|
||||
set current_use_key=!server3_use_key!
|
||||
) else (
|
||||
echo [错误] 无效的服务器编号:%server_num%
|
||||
echo [错误]- 无效的服务器编号:%server_num%-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo.
|
||||
echo [部署] 开始部署到 !current_name! (!current_host!)...
|
||||
echo 目标路径:!current_path!
|
||||
echo 用户名:!current_user!
|
||||
echo [部署]- 开始部署到 !current_name! (!current_host!)...
|
||||
echo 目标路径:!current_path!-
|
||||
echo 用户名:!current_user!-
|
||||
|
||||
:: 根据认证方式设置命令前缀
|
||||
if "!current_use_key!"=="true" (
|
||||
set scp_cmd=scp -o StrictHostKeyChecking=no
|
||||
set ssh_cmd=ssh -o StrictHostKeyChecking=no
|
||||
echo [信息] 使用SSH密钥认证(免密登录)
|
||||
echo [信息]- 使用SSH密钥认证(免密登录)-
|
||||
) else (
|
||||
:: 检查是否有sshpass工具
|
||||
where sshpass >nul 2>&1
|
||||
if !errorlevel! equ 0 (
|
||||
set scp_cmd=sshpass -p "!current_pass!" scp -o StrictHostKeyChecking=no
|
||||
set ssh_cmd=sshpass -p "!current_pass!" ssh -o StrictHostKeyChecking=no
|
||||
echo [信息] 使用sshpass自动输入密码
|
||||
echo [信息]- 使用sshpass自动输入密码-
|
||||
) else (
|
||||
set scp_cmd=scp -o StrictHostKeyChecking=no
|
||||
set ssh_cmd=ssh -o StrictHostKeyChecking=no
|
||||
echo [提示] 如果提示输入密码,请输入:!current_pass!
|
||||
echo !current_pass! | clip
|
||||
echo [提示]- 如果提示输入密码,请输入:!current_pass!
|
||||
echo [自动]- 密码已复制到剪贴板,可直接粘贴 ^(Ctrl+V^)
|
||||
echo [建议]- 安装sshpass工具可以实现自动输入密码-
|
||||
)
|
||||
)
|
||||
|
||||
:: 上传文件到远程服务器
|
||||
echo [信息]- 正在上传文件...
|
||||
!scp_cmd! dist_temp.zip !current_user!@!current_host!:/tmp/
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误]- 文件上传到 !current_name! 失败-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo [成功]- 文件上传到 !current_name! 完成-
|
||||
|
||||
:: 在远程服务器上解压并部署
|
||||
echo [信息]- 正在远程服务器上解压部署...
|
||||
!ssh_cmd! !current_user!@!current_host! "mkdir -p !current_path!"
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误]- 创建目录失败-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo [信息]- 创建备份...
|
||||
!ssh_cmd! !current_user!@!current_host! "if [ -d '!current_path!' ] && [ -n \"\$(ls -A !current_path! 2>/dev/null)\" ]; then cp -r !current_path! !current_path!_backup_\$(date +%%Y%%m%%d_%%H%%M%%S) 2>/dev/null || true; fi"
|
||||
|
||||
echo [信息]- 清理旧文件...
|
||||
!ssh_cmd! !current_user!@!current_host! "rm -rf !current_path!/* 2>/dev/null || true"
|
||||
!ssh_cmd! !current_user!@!current_host! "rm -rf !current_path!/.* 2>/dev/null || true"
|
||||
|
||||
echo [信息]- 解压新文件...
|
||||
!ssh_cmd! !current_user!@!current_host! "cd !current_path! && unzip -o /tmp/dist_temp.zip"
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误]- 解压文件失败-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo [信息]- 设置文件权限...
|
||||
!ssh_cmd! !current_user!@!current_host! "chown -R \$(whoami):\$(whoami) !current_path!/* 2>/dev/null || true"
|
||||
!ssh_cmd! !current_user!@!current_host! "chmod -R 755 !current_path!/* 2>/dev/null || true"
|
||||
|
||||
echo [信息]- 清理临时文件...
|
||||
!ssh_cmd! !current_user!@!current_host! "rm -f /tmp/dist_temp.zip"
|
||||
!ssh_cmd! !current_user!@!current_host! "sync"
|
||||
|
||||
echo [信息]- 显示部署结果...
|
||||
!ssh_cmd! !current_user!@!current_host! "echo '[服务器]- 部署完成,文件列表:' && ls -la !current_path!"
|
||||
!ssh_cmd! !current_user!@!current_host! "echo '[服务器]- 文件总数:' && find !current_path! -type f | wc -l"
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误]- !current_name! 远程部署失败-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo [成功]- !current_name! 部署完成!
|
||||
exit /b 0
|
||||
|
||||
:: 部署到自定义服务器的函数
|
||||
:deploy_to_custom_server
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
echo.
|
||||
echo [部署] 开始部署到 %custom_name% (%custom_host%)...
|
||||
echo 目标路径:%custom_path%
|
||||
echo 用户名:%custom_user%
|
||||
echo SSH端口:%custom_port%
|
||||
|
||||
:: 根据认证方式设置命令前缀
|
||||
if "%custom_use_key%"=="true" (
|
||||
set scp_cmd=scp -o StrictHostKeyChecking=no -P %custom_port%
|
||||
set ssh_cmd=ssh -o StrictHostKeyChecking=no -p %custom_port%
|
||||
echo [信息] 使用SSH密钥认证(免密登录)
|
||||
) else (
|
||||
:: 检查是否有sshpass工具
|
||||
where sshpass >nul 2>&1
|
||||
if !errorlevel! equ 0 (
|
||||
set scp_cmd=sshpass -p "%custom_pass%" scp -o StrictHostKeyChecking=no -P %custom_port%
|
||||
set ssh_cmd=sshpass -p "%custom_pass%" ssh -o StrictHostKeyChecking=no -p %custom_port%
|
||||
echo [信息] 使用sshpass自动输入密码
|
||||
) else (
|
||||
set scp_cmd=scp -o StrictHostKeyChecking=no -P %custom_port%
|
||||
set ssh_cmd=ssh -o StrictHostKeyChecking=no -p %custom_port%
|
||||
echo %custom_pass% | clip
|
||||
echo [提示] 如果提示输入密码,请输入:%custom_pass%
|
||||
echo [自动] 密码已复制到剪贴板,可直接粘贴 ^(Ctrl+V^)
|
||||
echo [建议] 安装sshpass工具可以实现自动输入密码
|
||||
)
|
||||
)
|
||||
|
||||
:: 上传文件到远程服务器
|
||||
echo [信息] 正在上传文件...
|
||||
!scp_cmd! dist_temp.zip !current_user!@!current_host!:/tmp/
|
||||
!scp_cmd! dist_temp.zip %custom_user%@%custom_host%:/tmp/
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误] 文件上传到 !current_name! 失败
|
||||
echo [错误] 文件上传到 %custom_name% 失败
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo [成功] 文件上传到 !current_name! 完成
|
||||
echo [成功] 文件上传到 %custom_name% 完成
|
||||
|
||||
:: 在远程服务器上解压并部署
|
||||
echo [信息] 正在远程服务器上解压部署...
|
||||
!ssh_cmd! !current_user!@!current_host! "mkdir -p !current_path! && rm -r !current_path!/* 2>/dev/null || true && cd !current_path! && unzip -o /tmp/dist_temp.zip && rm /tmp/dist_temp.zip && echo '部署完成,文件列表:' && ls -la !current_path! || echo '文件列表显示失败,但部署成功'"
|
||||
echo [信息]- 正在远程服务器上解压部署...
|
||||
!ssh_cmd! %custom_user%@%custom_host% "mkdir -p %custom_path%"
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误] !current_name! 远程部署失败
|
||||
echo [错误]- 创建目录失败-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo [成功] !current_name! 部署完成!
|
||||
exit /b 0
|
||||
echo [信息]- 创建备份...
|
||||
!ssh_cmd! %custom_user%@%custom_host% "if [ -d '%custom_path%' ] && [ -n \"\$(ls -A %custom_path% 2>/dev/null)\" ]; then cp -r %custom_path% %custom_path%_backup_\$(date +%%Y%%m%%d_%%H%%M%%S) 2>/dev/null || true; fi"
|
||||
|
||||
echo [信息]- 清理旧文件...
|
||||
!ssh_cmd! %custom_user%@%custom_host% "rm -rf %custom_path%/* 2>/dev/null || true"
|
||||
!ssh_cmd! %custom_user%@%custom_host% "rm -rf %custom_path%/.* 2>/dev/null || true"
|
||||
|
||||
echo [信息]- 解压新文件...
|
||||
!ssh_cmd! %custom_user%@%custom_host% "cd %custom_path% && unzip -o /tmp/dist_temp.zip"
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误]- 解压文件失败-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo [信息]- 设置文件权限...
|
||||
!ssh_cmd! %custom_user%@%custom_host% "chown -R \$(whoami):\$(whoami) %custom_path%/* 2>/dev/null || true"
|
||||
!ssh_cmd! %custom_user%@%custom_host% "chmod -R 755 %custom_path%/* 2>/dev/null || true"
|
||||
|
||||
echo [信息]- 清理临时文件...
|
||||
!ssh_cmd! %custom_user%@%custom_host% "rm -f /tmp/dist_temp.zip"
|
||||
!ssh_cmd! %custom_user%@%custom_host% "sync"
|
||||
|
||||
echo [信息]- 显示部署结果...
|
||||
!ssh_cmd! %custom_user%@%custom_host% "echo '[服务器]- 部署完成,文件列表:' && ls -la %custom_path%"
|
||||
!ssh_cmd! %custom_user%@%custom_host% "echo '[服务器]- 文件总数:' && find %custom_path% -type f | wc -l"
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误] %custom_name% 远程部署失败
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo [成功] %custom_name% 部署完成!
|
||||
exit /b 0
|
||||
|
||||
:: 部署到临时服务器的函数(用于批量自定义服务器)
|
||||
:deploy_to_temp_server
|
||||
setlocal enabledelayedexpansion
|
||||
set temp_name=%~1
|
||||
set temp_host=%~2
|
||||
set temp_user=%~3
|
||||
set temp_pass=%~4
|
||||
set temp_path=%~5
|
||||
set temp_port=%~6
|
||||
set temp_use_key=%~7
|
||||
|
||||
echo.
|
||||
echo [部署] 开始部署到 %temp_name% (%temp_host%)...
|
||||
echo 目标路径:%temp_path%
|
||||
echo 用户名:%temp_user%
|
||||
echo SSH端口:%temp_port%
|
||||
|
||||
:: 根据认证方式设置命令前缀
|
||||
if "%temp_use_key%"=="true" (
|
||||
set scp_cmd=scp -o StrictHostKeyChecking=no -P %temp_port%
|
||||
set ssh_cmd=ssh -o StrictHostKeyChecking=no -p %temp_port%
|
||||
echo [信息] 使用SSH密钥认证(免密登录)
|
||||
) else (
|
||||
:: 检查是否有sshpass工具
|
||||
where sshpass >nul 2>&1
|
||||
if !errorlevel! equ 0 (
|
||||
set scp_cmd=sshpass -p "%temp_pass%" scp -o StrictHostKeyChecking=no -P %temp_port%
|
||||
set ssh_cmd=sshpass -p "%temp_pass%" ssh -o StrictHostKeyChecking=no -p %temp_port%
|
||||
echo [信息] 使用sshpass自动输入密码
|
||||
) else (
|
||||
set scp_cmd=scp -o StrictHostKeyChecking=no -P %temp_port%
|
||||
set ssh_cmd=ssh -o StrictHostKeyChecking=no -p %temp_port%
|
||||
echo %temp_pass% | clip
|
||||
echo [提示] 如果提示输入密码,请输入:%temp_pass%
|
||||
echo [自动] 密码已复制到剪贴板,可直接粘贴 ^(Ctrl+V^)
|
||||
echo [建议] 安装sshpass工具可以实现自动输入密码
|
||||
)
|
||||
)
|
||||
|
||||
:: 上传文件到远程服务器
|
||||
echo [信息] 正在上传文件...
|
||||
!scp_cmd! dist_temp.zip %temp_user%@%temp_host%:/tmp/
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误] 文件上传到 %temp_name% 失败
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo [成功] 文件上传到 %temp_name% 完成
|
||||
|
||||
:: 在远程服务器上解压并部署
|
||||
echo [信息]- 正在远程服务器上解压部署...
|
||||
!ssh_cmd! %temp_user%@%temp_host% "mkdir -p %temp_path%"
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误]- 创建目录失败-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo [信息]- 创建备份...
|
||||
!ssh_cmd! %temp_user%@%temp_host% "if [ -d '%temp_path%' ] && [ -n \"\$(ls -A %temp_path% 2>/dev/null)\" ]; then cp -r %temp_path% %temp_path%_backup_\$(date +%%Y%%m%%d_%%H%%M%%S) 2>/dev/null || true; fi"
|
||||
|
||||
echo [信息]- 清理旧文件...
|
||||
!ssh_cmd! %temp_user%@%temp_host% "rm -rf %temp_path%/* 2>/dev/null || true"
|
||||
!ssh_cmd! %temp_user%@%temp_host% "rm -rf %temp_path%/.* 2>/dev/null || true"
|
||||
|
||||
echo [信息]- 解压新文件...
|
||||
!ssh_cmd! %temp_user%@%temp_host% "cd %temp_path% && unzip -o /tmp/dist_temp.zip"
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误]- 解压文件失败-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo [信息]- 设置文件权限...
|
||||
!ssh_cmd! %temp_user%@%temp_host% "chown -R \$(whoami):\$(whoami) %temp_path%/* 2>/dev/null || true"
|
||||
!ssh_cmd! %temp_user%@%temp_host% "chmod -R 755 %temp_path%/* 2>/dev/null || true"
|
||||
|
||||
echo [信息]- 清理临时文件...
|
||||
!ssh_cmd! %temp_user%@%temp_host% "rm -f /tmp/dist_temp.zip"
|
||||
!ssh_cmd! %temp_user%@%temp_host% "sync"
|
||||
|
||||
echo [信息]- 显示部署结果...
|
||||
!ssh_cmd! %temp_user%@%temp_host% "echo '[服务器]- 部署完成,文件列表:' && ls -la %temp_path%"
|
||||
!ssh_cmd! %temp_user%@%temp_host% "echo '[服务器]- 文件总数:' && find %temp_path% -type f | wc -l"
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误] %temp_name% 远程部署失败
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo [成功] %temp_name% 部署完成!
|
||||
exit /b 0
|
||||
|
||||
:verify_deployment
|
||||
echo.
|
||||
echo ==================== 验证部署状态 ====================
|
||||
echo [选择]- 请选择要验证的服务器:
|
||||
echo 1. %server1_name% ^(%server1_host%^)
|
||||
echo 2. %server2_name% ^(%server2_host%^)
|
||||
echo 3. %server3_name% ^(%server3_host%^)
|
||||
echo 4. 验证所有预设服务器-
|
||||
echo 5. 验证自定义服务器-
|
||||
echo.
|
||||
set /p verify_choice="请输入选择 (1-5): "
|
||||
|
||||
if "%verify_choice%"=="1" (
|
||||
call :verify_single_server 1
|
||||
goto verify_end
|
||||
)
|
||||
if "%verify_choice%"=="2" (
|
||||
call :verify_single_server 2
|
||||
goto verify_end
|
||||
)
|
||||
if "%verify_choice%"=="3" (
|
||||
call :verify_single_server 3
|
||||
goto verify_end
|
||||
)
|
||||
if "%verify_choice%"=="4" (
|
||||
call :verify_single_server 1
|
||||
call :verify_single_server 2
|
||||
call :verify_single_server 3
|
||||
goto verify_end
|
||||
)
|
||||
if "%verify_choice%"=="5" (
|
||||
call :verify_custom_server
|
||||
goto verify_end
|
||||
)
|
||||
echo [错误]- 请输入有效选择 (1-5)-
|
||||
goto verify_deployment
|
||||
|
||||
:verify_end
|
||||
echo.
|
||||
echo 按任意键返回主菜单...
|
||||
pause >nul
|
||||
goto ask_deploy_mode
|
||||
|
||||
:verify_single_server
|
||||
setlocal enabledelayedexpansion
|
||||
set server_num=%1
|
||||
|
||||
if "%server_num%"=="1" (
|
||||
set current_host=!server1_host!
|
||||
set current_path=!server1_path!
|
||||
set current_user=!server1_user!
|
||||
set current_pass=!server1_pass!
|
||||
set current_name=!server1_name!
|
||||
set current_use_key=!server1_use_key!
|
||||
) else if "%server_num%"=="2" (
|
||||
set current_host=!server2_host!
|
||||
set current_path=!server2_path!
|
||||
set current_user=!server2_user!
|
||||
set current_pass=!server2_pass!
|
||||
set current_name=!server2_name!
|
||||
set current_use_key=!server2_use_key!
|
||||
) else if "%server_num%"=="3" (
|
||||
set current_host=!server3_host!
|
||||
set current_path=!server3_path!
|
||||
set current_user=!server3_user!
|
||||
set current_pass=!server3_pass!
|
||||
set current_name=!server3_name!
|
||||
set current_use_key=!server3_use_key!
|
||||
) else (
|
||||
echo [错误]- 无效的服务器编号:%server_num%-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo.
|
||||
echo [验证]- 正在验证 !current_name! (!current_host!) 的部署状态...
|
||||
|
||||
:: 根据认证方式设置命令前缀
|
||||
if "!current_use_key!"=="true" (
|
||||
set ssh_cmd=ssh -o StrictHostKeyChecking=no
|
||||
) else (
|
||||
where sshpass >nul 2>&1
|
||||
if !errorlevel! equ 0 (
|
||||
set ssh_cmd=sshpass -p "!current_pass!" ssh -o StrictHostKeyChecking=no
|
||||
) else (
|
||||
set ssh_cmd=ssh -o StrictHostKeyChecking=no
|
||||
echo !current_pass! | clip
|
||||
echo [提示]- 如果提示输入密码,请输入:!current_pass!-
|
||||
echo [自动]- 密码已复制到剪贴板,可直接粘贴 ^(Ctrl+V^)
|
||||
)
|
||||
)
|
||||
|
||||
!ssh_cmd! !current_user!@!current_host! "echo '[验证]- 检查部署目录:!current_path!'"
|
||||
!ssh_cmd! !current_user!@!current_host! "if [ -d '!current_path!' ]; then echo '[验证]- 目录存在'; else echo '[验证]- 目录不存在!'; fi"
|
||||
!ssh_cmd! !current_user!@!current_host! "if [ -d '!current_path!' ]; then echo '[验证]- 文件总数:' && find !current_path! -type f | wc -l; fi"
|
||||
!ssh_cmd! !current_user!@!current_host! "if [ -d '!current_path!' ]; then echo '[验证]- 目录大小:' && du -sh !current_path!; fi"
|
||||
!ssh_cmd! !current_user!@!current_host! "if [ -d '!current_path!' ]; then echo '[验证]- 最近修改的文件:' && find !current_path! -type f -printf '%T+ %p\n' | sort -r | head -5; fi"
|
||||
!ssh_cmd! !current_user!@!current_host! "if [ -f '!current_path!/index.html' ]; then echo ' ✓ index.html 存在'; else echo ' ✗ index.html 不存在'; fi"
|
||||
!ssh_cmd! !current_user!@!current_host! "if [ -d '!current_path!/assets' ]; then echo ' ✓ assets 目录存在'; else echo ' ✗ assets 目录不存在'; fi"
|
||||
!ssh_cmd! !current_user!@!current_host! "echo '[验证]- 检查完成'"
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误]- 验证 !current_name! 时发生错误-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo [成功]- !current_name! 验证完成-
|
||||
exit /b 0
|
||||
|
||||
:verify_custom_server
|
||||
echo.
|
||||
echo ==================== 验证自定义服务器 ====================
|
||||
echo [信息]- 请输入要验证的自定义服务器信息:
|
||||
echo.
|
||||
|
||||
set /p verify_host="服务器IP地址: "
|
||||
if "%verify_host%"=="" (
|
||||
echo [错误]- IP地址不能为空-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
set /p verify_user="用户名: "
|
||||
if "%verify_user%"=="" (
|
||||
echo [错误]- 用户名不能为空-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
set /p verify_pass="密码: "
|
||||
if "%verify_pass%"=="" (
|
||||
echo [错误]- 密码不能为空-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
set /p verify_path="部署路径 (默认: /var/www/web): "
|
||||
if "%verify_path%"=="" set verify_path=/var/www/web
|
||||
|
||||
set /p verify_port="SSH端口 (默认: 22): "
|
||||
if "%verify_port%"=="" set verify_port=22
|
||||
|
||||
echo.
|
||||
echo [选择]- 认证方式:
|
||||
echo 1. SSH密钥认证^(免密^)-
|
||||
echo 2. 密码认证-
|
||||
set /p auth_choice="请选择 (1/2): "
|
||||
|
||||
if "%auth_choice%"=="1" (
|
||||
set ssh_cmd=ssh -o StrictHostKeyChecking=no -p %verify_port%
|
||||
) else (
|
||||
where sshpass >nul 2>&1
|
||||
if !errorlevel! equ 0 (
|
||||
set ssh_cmd=sshpass -p "%verify_pass%" ssh -o StrictHostKeyChecking=no -p %verify_port%
|
||||
) else (
|
||||
set ssh_cmd=ssh -o StrictHostKeyChecking=no -p %verify_port%
|
||||
echo %verify_pass% | clip
|
||||
echo [提示]- 如果提示输入密码,请输入:%verify_pass%-
|
||||
echo [自动]- 密码已复制到剪贴板,可直接粘贴 ^(Ctrl+V^)
|
||||
)
|
||||
)
|
||||
|
||||
echo.
|
||||
echo [验证]- 正在验证自定义服务器 (%verify_host%) 的部署状态...
|
||||
|
||||
!ssh_cmd! %verify_user%@%verify_host% "echo '[验证]- 检查部署目录:%verify_path%'"
|
||||
!ssh_cmd! %verify_user%@%verify_host% "if [ -d '%verify_path%' ]; then echo '[验证]- 目录存在'; else echo '[验证]- 目录不存在!'; fi"
|
||||
!ssh_cmd! %verify_user%@%verify_host% "if [ -d '%verify_path%' ]; then echo '[验证]- 文件总数:' && find %verify_path% -type f | wc -l; fi"
|
||||
!ssh_cmd! %verify_user%@%verify_host% "if [ -d '%verify_path%' ]; then echo '[验证]- 目录大小:' && du -sh %verify_path%; fi"
|
||||
!ssh_cmd! %verify_user%@%verify_host% "if [ -d '%verify_path%' ]; then echo '[验证]- 最近修改的文件:' && find %verify_path% -type f -printf '%T+ %p\n' | sort -r | head -5; fi"
|
||||
!ssh_cmd! %verify_user%@%verify_host% "if [ -f '%verify_path%/index.html' ]; then echo ' ✓ index.html 存在'; else echo ' ✗ index.html 不存在'; fi"
|
||||
!ssh_cmd! %verify_user%@%verify_host% "if [ -d '%verify_path%/assets' ]; then echo ' ✓ assets 目录存在'; else echo ' ✗ assets 目录不存在'; fi"
|
||||
!ssh_cmd! %verify_user%@%verify_host% "echo '[验证]- 检查完成'"
|
||||
|
||||
if !errorlevel! neq 0 (
|
||||
echo [错误]- 验证自定义服务器时发生错误-
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo [成功]- 自定义服务器验证完成-
|
||||
exit /b 0
|
Loading…
x
Reference in New Issue
Block a user