From 071551d8f6eb2b08c401a02976f5f416761eb9a0 Mon Sep 17 00:00:00 2001 From: "PC-202302260912\\Administrator" <1769111741@qq.com> Date: Wed, 6 Dec 2023 17:33:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BB=E6=96=87=E4=BB=B6=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=B8=83=E5=B1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.sh | 452 ++++++++++++++++++++++++-------------------------------- 1 file changed, 190 insertions(+), 262 deletions(-) diff --git a/main.sh b/main.sh index c967f79..946525b 100644 --- a/main.sh +++ b/main.sh @@ -42,6 +42,11 @@ show_file(){ find /data/update/"$filer" -maxdepth 1 \( -type f -o -type d \) -printf "%T@\t%-20f\n" | sort -n | awk '{print strftime("%Y-%m-%d %H:%M:%S", $1)"\t"$2}' } +# 获取当前时间,精确到秒,格式年月日时分秒 +get_current_time() { + date +"%Y%m%d%H%M%S" +} + # 发包并解压缩 unzip_package(){ # 服务器地址 @@ -54,7 +59,7 @@ unzip_package(){ local destination_dir=${4} if [ -e $source_tar ]; then echo "正在处理服务器 $ip" - scp "$source_tar/$package" "$ip:$destination_dir" + scp "$source_tar/$package" "$ip:$destination_dir" || { echo "SCP 失败"; exit 1; } ssh "$ip" "tar -zxvf $destination_dir/$package -C $destination_dir" ssh "$ip" "rm -f $destination_dir/$package" echo "处理完成服务器 $ip " @@ -83,9 +88,15 @@ restart_server() { "admin") restart_script="bash /data/jieling/gm/gm_reboot.sh" ;; - "core") + "super") restart_script="supervisorctl restart all" ;; + "coreStop") + restart_script="bash /data/jieling/server/bin/stop.sh core1" + ;; + "coreStart") + restart_script="bash /data/jieling/server/bin/start.sh core1" + ;; *) echo -e "${red}无效的服务器名称${NC}" exit 1 @@ -97,192 +108,43 @@ restart_server() { echo "${server_name} ${ip} 重启完成" } -# 发送包并重启 +# 游戏代码发送包并重启 game_send_package() { - local channel=${1} - local package=${2} - local reboot=${3} + local channel=${1} # ip还是文本更新 + local package=${2} # 包名 + local reboot=${3} # 是否重启(分辨热更) local source_tar="/data/update/package/" local destination_dir="/data/jieling/server" - if is_valid_ip "$channel"; then - unzip_package $channel $package $source_tar $destination_dir - if [[ $reboot -eq 1 ]]; then - restart_server "core" "$channel" + # 停服更新 + if [[ $reboot -eq 1 ]]; then + if is_valid_ip "$channel"; then + unzip_package $channel $package $source_tar $destination_dir + restart_server "coreStop" "$channel" + sleep 5 + restart_server "coreStart" "$channel" + else + for ip in $(awk '{print $2}' "txt/$channel") + do + unzip_package $ip $package $source_tar $destination_dir + restart_server "super" "$ip" + done fi else - for ip in $(awk '{print $2}' "txt/$channel") - do - unzip_package $ip $package $source_tar $destination_dir - if [[ $reboot -eq 1 ]]; then - restart_server "core" "$ip" - fi - done - fi -} - -# 更新gm -gm_send_package() { - local channel=${1} - local package=gm - local source_tar="/data/update/project/miduo_gm/gm" - local destination_dir="/data/jieling/gm" - - if is_valid_ip "$channel"; then - unzip_package $channel $package $source_tar $destination_dir - restart_server "admin" $channel - else - for ip in $(awk '{print $2}' "txt/$channel") - do - unzip_package $ip $package $source_tar $destination_dir - restart_server "admin" $ip - done - fi -} - -# 通用游戏服更新函数 -game_update_option() { - local action=$1 - - case $action in - "update") - local action_desc="打更新包" - local script="update_game.sh" - ;; - "hotfix") - local action_desc="打热更包" - local script="hotfix_game.sh" - ;; - "official_hotfix") - local action_desc="正式服热更" - local script="hotfix_game.sh" - local send_to="txt" - local reboot=0 - ;; - "official_reboot") - local action_desc="正式服停服更新" - local script="hotfix_game.sh" - local send_to="txt" - local reboot=1 - ;; - "test_hotfix") - local action_desc="测试服热更" - local script="hotfix_game.sh" - local send_to="ip" - local reboot=0 - ;; - "test_reboot") - local action_desc="测试服停服更新" - local script="hotfix_game.sh" - local send_to="ip" - local reboot=1 - ;; - *) - echo -e "${red}无效的选项,请重新输入${NC}" - show_menu - handle_input - return - ;; - esac - - echo -e "${yellow}$action_desc${NC}" - echo "==============================" - - # 项目属性 - if [ $action = "update" ] || [ $action = "hotfix" ]; then - show_file project - echo -e "${blue}==============================${NC}" - read -rp "请输入项目地址: " project - if [ -z "$project" ]; then - echo -e "${red}项目地址不能为空,请重新输入${NC}" - return + # 热更新 + if is_valid_ip "$channel"; then + unzip_package $channel $package $source_tar $destination_dir + else + for ip in $(awk '{print $2}' "txt/$channel") + do + unzip_package $ip $package $source_tar $destination_dir + done fi fi - # 包属性 - show_file package - echo -e "${blue}==============================${NC}" - read -rp "请输入包名(不需要带文件后缀):" package - if [ -z "$package" ]; then - echo -e "${red}包名不能为空,请重新输入${NC}" - return - fi - - echo -e "开始执行操作 项目:$project, 包名:$package" - if [ "$send_to" = "txt" ]; then - show_file script/txt - read -rp "请输入文件名称(不需要带文件后缀): " channel - echo -e "开始发送 包名:$package, channel:$channel" - game_send_package "${channel}.txt" "$package" "$reboot" - elif [ "$send_to" = "ip" ]; then - read -rp "请输入游戏服ip: " ip - echo -e "开始发送 包名:$package, ip:$ip" - game_send_package "$ip" "$package" "$reboot" - else - bash "$script" "$project" "$package" - fi -} - -# 通用GM更新函数 -gm_option() { - local action=$1 - - case $action in - "package") - local action_desc="打包" - local script="update_gm.sh" - ;; - "single_update") - local action_desc="单机器更新" - local send_to="ip" - ;; - "batch_update") - local action_desc="批量更新" - local send_to="txt" - ;; -# "single_reboot") -# local action_desc="单机器重启" -# ;; -# "batch_reboot") -# local action_desc="批量重启" -# ;; - *) - echo -e "${red}无效的选项,请重新输入${NC}" - show_menu - handle_input - return - ;; - esac - - echo -e "${yellow}$action_desc${NC}" - if [ "$action" = "package" ]; then - show_file project - echo -e "${blue}==============================${NC}" - read -rp "请输入表所在游戏项目地址: " project - bash "$script" "$project" - echo -e "gm打包完成" - elif [ "$action" = "single_update" ]; then - read -rp "请输入游戏服ip: " ip - echo -e "开始更新gm" - gm_send_package "$ip" - elif [ "$action" = "batch_update" ]; then - echo -e "开始更新gm" - gm_send_package gm_ip.txt - elif [ "$action" = "single_reboot" ]; then - read -rp "请输入游戏服ip: " ip - echo -e "开始重启gm" - restart_server "admin" "$ip" - elif [ "$action" = "batch_reboot" ]; then - local path="/data/update/script/txt/gm_ip.txt" - for ip in $(awk '{print $2}' "${path}") - do - echo -e "开始更新gm $ip" - restart_server "admin" "$ip" - done - fi } # 通用的更新函数 -update_server() { +update_code() { local server_name=$1 local update_script=$2 @@ -318,85 +180,165 @@ exit_script() { exit 0 } -# 游戏服更新子菜单 -game_menu(){ - echo -e "================================" - echo -e "开始更新${green}游戏服${NC}" - echo -e "1. ${yellow}打更新包${NC}" - echo -e "2. ${yellow}打热更包${NC}" - echo -e "3. ${yellow}正式服热更${NC}" - echo -e "4. ${yellow}正式服停服更新${NC}" - echo -e "5. ${yellow}测试服热更${NC}" - echo -e "6. ${yellow}测试服停服更新${NC}" - echo -e "0. ${green}返回主菜单${NC}" - echo -e "*. ${red}其他任意键退出程序${NC}" -} - # 游戏服更新 update_game(){ while true; do - game_menu + echo -e "开始更新${green}游戏服${NC}" + echo -e "================================" + echo -e "1. ${yellow}更新包${NC}" + echo -e "2. ${yellow}正式服更新${NC}" + echo -e "3. ${yellow}测试服更新${NC}" + echo -e "4. ${yellow}热更包${NC}" + echo -e "5. ${yellow}正式服热更${NC}" + echo -e "6. ${yellow}测试服热更${NC}" + echo -e "0. ${green}返回主菜单${NC}" + echo -e "*. ${red}其他任意键退出程序${NC}" read -rp "请输入选项编号: " choice case $choice in - 1) game_update_option "update" ;; # 游戏服更新:打更新包 - 2) game_update_option "hotfix" ;; # 游戏服更新:打热更包 - 3) game_update_option "official_hotfix" ;; # 游戏服更新:正式服热更 - 4) game_update_option "official_reboot" ;; # 游戏服更新:正式服停服更新 - 5) game_update_option "test_hotfix" ;; # 游戏服更新:测试服热更 - 6) game_update_option "test_reboot" ;; # 游戏服更新:测试服停服更新 + 1) game_update_option "create_update_package" ;; # 游戏服更新:打更新包 + 2) game_update_option "official_reboot" ;; # 游戏服更新:正式服停服更新 + 3) game_update_option "test_reboot" ;; # 游戏服更新:测试服停服更新 + 4) game_update_option "create_hotfix_package" ;; # 游戏服更新:打热更包 + 5) game_update_option "official_hotfix" ;; # 游戏服更新:正式服热更 + 6) game_update_option "test_hotfix" ;; # 游戏服更新:测试服热更 0) return ;; *) exit_script ;; esac done } -# gm更新子菜单 -gm_menu(){ - echo -e "================================" - echo -e "开始更新${green}gm服${NC}" - echo -e "1. ${yellow}打包${NC}" - echo -e "2. ${yellow}单机器更新${NC}" - echo -e "3. ${yellow}批量更新${NC}" -# echo -e "4. ${yellow}单机器重启${NC}" -# echo -e "5. ${yellow}批量重启${NC}" - echo -e "0. ${green}返回主菜单${NC}" - echo -e "*. ${red}其他任意键退出程序${NC}" +# 通用游戏服更新函数 +game_update_option() { + echo -e "${yellow}游戏服更新开始...${NC}" + local action=$1 + local current_time=get_current_time + case $action in + "create_update_package") game_code_build "update" "update_game.sh" ;; + "official_reboot") game_code_official "update" "1" ;; + "test_reboot") game_code_test "update" "1" ;; + "create_hotfix_package") game_code_build "hotfix" "hotfix_game.sh" ;; + "official_hotfix") game_code_official "hotfix" "0" ;; + "test_hotfix") game_code_test "hotfix" "0" ;; + *) + echo -e "${red}无效的选项,请重新输入${NC}" + handle_menu + return + ;; + esac +} + +# 游戏代码打包 +game_code_build(){ + local type=$1 + local script=$2 + show_file project | grep master + read -rp "请输入项目地址: " project + bash "${script}" "${project}" "${project}_${type}_${current_time}" +} +# 游戏代码正式更新 +game_code_official(){ + local type=$1 + local reboot=$2 + show_file package | grep "${type}" + read -rp "请输入包名(不需要带文件后缀):" package + show_file script/txt + read -rp "请输入文件名称(不需要带文件后缀): " channel + echo -e "开始发送 包名:$package, 渠道:$channel" + game_send_package "${channel}.txt" "$package" "${reboot}" +} +# 游戏代码测试更新 +game_code_test(){ + local type=$1 + local reboot=$2 + show_file package | grep "${type}" + read -rp "请输入包名(不需要带文件后缀):" package + read -rp "请输入游戏服ip: " ip + echo -e "开始发送 包名:$package, ip:$ip" + game_send_package "$ip" "$package" "${reboot}" } # gm更新 update_gm(){ while true; do - gm_menu + echo -e "================================" + echo -e "开始更新${green}gm服${NC}" + echo -e "1. ${yellow}打包${NC}" + echo -e "2. ${yellow}单机器更新${NC}" + echo -e "3. ${yellow}批量更新${NC}" + echo -e "0. ${green}返回主菜单${NC}" + echo -e "*. ${red}其他任意键退出程序${NC}" read -rp "请输入选项编号: " choice case $choice in 1) gm_option "package" ;; # GM更新:打包 2) gm_option "single_update" ;; # GM更新:单机器更新 3) gm_option "batch_update" ;; # GM更新:批量更新 -# 4) gm_option "single_reboot" ;; # GM更新:单机器重启 -# 5) gm_option "batch_reboot" ;; # GM更新:批量重启 0) return ;; *) exit_script ;; esac done } -# 登陆服更新子菜单 -login_menu(){ - echo -e "================================" - echo -e "开始更新${green}登陆服${NC}" - echo -e "1. ${yellow}更新登陆服代码${NC}" - echo -e "2. ${yellow}重启登陆服${NC}" - echo -e "0. ${green}返回主菜单${NC}" - echo -e "*. ${red}其他任意键退出程序${NC}" +# 通用GM更新函数 +gm_option() { + local action=$1 + case $action in + "package") + show_file project + echo -e "${blue}==============================${NC}" + read -rp "请输入表所在游戏项目地址: " project + bash "update_gm.sh" "$project" + echo -e "gm打包完成" + ;; + "single_update") + read -rp "请输入游戏服ip: " ip + echo -e "开始更新gm" + gm_send_package "$ip" + ;; + "batch_update") + echo -e "开始更新gm" + gm_send_package gmip.txt + ;; + *) + echo -e "${red}无效的选项,请重新输入${NC}" + handle_menu + return + ;; + esac +} + + +# 更新gm +gm_send_package() { + local channel=${1} + local package=gm + local source_tar="/data/update/project/miduo_gm/gm" + local destination_dir="/data/jieling/gm" + + if is_valid_ip "$channel"; then + unzip_package "$channel" $package $source_tar $destination_dir + restart_server "admin" "$channel" + else + # shellcheck disable=SC2013 + for ip in $(awk '{print $2}' "txt/$channel") + do + unzip_package "$ip" $package $source_tar $destination_dir + restart_server "admin" "$ip" + done + fi } # 登陆服更新 -login(){ +update_login(){ while true; do - login_menu + echo -e "开始更新${green}登陆服${NC}" + echo -e "================================" + echo -e "1. ${yellow}更新登陆服代码${NC}" + echo -e "2. ${yellow}重启登陆服${NC}" + echo -e "0. ${green}返回主菜单${NC}" + echo -e "*. ${red}其他任意键退出程序${NC}" read -rp "请输入选项编号: " choice case $choice in - 1) update_server "tomcat" "update_login.sh" ;; + 1) update_code "tomcat" "update_login.sh" ;; 2) restart_bol "tomcat" ;; 0) return ;; *) exit_script ;; @@ -404,23 +346,18 @@ login(){ done } -# 支付服更新子菜单 -pay_menu(){ - echo -e "================================" - echo -e "开始更新${green}支付服${NC}" - echo -e "1. ${yellow}更新支付服代码${NC}" - echo -e "2. ${yellow}重启支付服${NC}" - echo -e "0. ${green}返回主菜单${NC}" - echo -e "*. ${red}其他任意键退出程序${NC}" -} - # 支付服更新 -pay(){ +update_pay(){ while true; do - pay_menu + echo -e "开始更新${green}支付服${NC}" + echo -e "================================" + echo -e "1. ${yellow}更新支付服代码${NC}" + echo -e "2. ${yellow}重启支付服${NC}" + echo -e "0. ${green}返回主菜单${NC}" + echo -e "*. ${red}其他任意键退出程序${NC}" read -rp "请输入选项编号: " choice case $choice in - 1) update_server "delivery" "update_delivery.sh" ;; + 1) update_code "delivery" "update_delivery.sh" ;; 2) restart_bol "delivery" ;; 0) return ;; *) exit_script ;; @@ -428,23 +365,18 @@ pay(){ done } -# 敏感词服菜单 -mingan_menu(){ - echo -e "================================" - echo -e "开始更新${green}敏感词服${NC}" - echo -e "1. ${yellow}更新敏感词服代码${NC}" - echo -e "2. ${yellow}重启敏感词服${NC}" - echo -e "0. ${green}返回主菜单${NC}" - echo -e "*. ${red}其他任意键退出程序${NC}" -} - # 敏感词服更新 -mingan(){ +update_mingan(){ while true; do - mingan_menu + echo -e "开始更新${green}敏感词服${NC}" + echo -e "================================" + echo -e "1. ${yellow}更新敏感词服代码${NC}" + echo -e "2. ${yellow}重启敏感词服${NC}" + echo -e "0. ${green}返回主菜单${NC}" + echo -e "*. ${red}其他任意键退出程序${NC}" read -rp "请输入选项编号: " choice case $choice in - 1) update_server "sensitive" "update_sensitive.sh" ;; + 1) update_code "sensitive" "update_sensitive.sh" ;; 2) restart_bol "sensitive" ;; 0) return ;; *) exit_script ;; @@ -452,27 +384,24 @@ mingan(){ done } -# 展示菜单选项 -show_menu() { +# 处理用户输入 +handle_menu() { + local current_time=get_current_time + echo -e "欢迎使用${blue}戒灵更新脚本-${current_time}${NC}" echo -e "================================" - echo -e "欢迎使用${blue}戒灵更新脚本${NC}" echo -e "1. ${yellow}游戏服更新${NC}" - echo -e "2. ${yellow}gm更新${NC}" + echo -e "2. ${yellow}gm后台更新${NC}" echo -e "3. ${yellow}登陆服更新${NC}" echo -e "4. ${yellow}支付服更新${NC}" echo -e "5. ${yellow}敏感词服更新${NC}" echo -e "0. ${red}退出${NC}" -} - -# 处理用户输入 -handle_input() { read -rp "请输入选项编号: " choice case $choice in 1) update_game ;; 2) update_gm ;; - 3) login ;; - 4) pay ;; - 5) mingan ;; + 3) update_login ;; + 4) update_pay ;; + 5) update_mingan ;; 0) exit_script ;; *) echo -e "${red}无效的选项,请重新输入${NC}" ;; esac @@ -481,8 +410,7 @@ handle_input() { # 主程序入口 main() { while true; do - show_menu - handle_input + handle_menu done }