title: 风控发包脚本 #标题
tags: work #标签
categories: 工作大杂烩 # 分类
password: jianzhao87.
abstract: 有东西被加密了, 请输入密码查看。
message: 您好, 这里需要密码。
date: 2020-11-19
wrong_pass_message: 抱歉, 这个密码看着不太对, 请再试试。


找个地方备份下此更新部署脚本。

部署脚本

  1. $ cat fk_deploy_jar.sh # 脚本如下
  2. #!/usr/bin/env bash
  3. set -e
  4. user=lvjianzhao
  5. tmp_dir=/tmp/"${user}"/
  6. jar_dir='/apps/var/'
  7. backup_dir='/apps/var/jakarta/backup/'
  8. item=0
  9. cur_date=$(date +%F_%H%M)
  10. h2() {
  11. printf "\n${underline}${bold}${white}%s${reset}\n" "$@"
  12. }
  13. USAGE(){
  14. echo -e "\033[33m
  15. $0 Script usage:
  16. -n: Specify the app name to deploy(Such as /apps/var/customermanager/ app_name for that:customermanager)
  17. -f: Specify the jar_name to deploy
  18. -h: Output help information
  19. \033[0m"
  20. }
  21. if [[ $1 == '--help' || $1 == '-h' ]]
  22. then
  23. USAGE
  24. exit 0
  25. fi
  26. h2 "[Step $item]: Assign a value to a variable..."; let item+=1
  27. while getopts ":f:n:" optname;do
  28. case "$optname" in
  29. "f")
  30. jar_package=$OPTARG
  31. ;;
  32. "n")
  33. app_name=$OPTARG
  34. ;;
  35. ":")
  36. echo "This option has no value!"
  37. ;;
  38. "*")
  39. echo "error message"
  40. ;;
  41. "?")
  42. echo "Not sure about this option..."
  43. ;;
  44. esac
  45. done
  46. echo -e "\033[33m
  47. The jar_name to be deployed is: ${jar_package}
  48. The app_name to be deployed is: ${app_name}
  49. After successful deployment, be sure to execute the instructions ( ps -ef |grep ${jar_package} | grep -v grep ) to ensure that the new process exists...
  50. \033[0m"
  51. read -p "Press enter to confirm and continue execution. To terminate, press 'CTRL + C' to terminate the script..."
  52. h2 "[Step $item]: Synchronous jars..."; let item+=1
  53. if [[ ! -d ${tmp_dir} ]]
  54. then
  55. mkdir ${tmp_dir}
  56. cd ${tmp_dir}
  57. else
  58. cd ${tmp_dir}
  59. fi
  60. rsync -avs 10.252.201.131::NRPE/${user}/${jar_package} ${tmp_dir}
  61. h2 "[Step $item]: Modify full permissions and backup the original jar package..."; let item+=1
  62. chmod 777 ${tmp_dir}${jar_package}
  63. chown llyxpt ${tmp_dir}${jar_package}
  64. cp -rp ${jar_dir}${app_name}/${jar_package} ${backup_dir}${jar_package}_${cur_date}
  65. ls ${backup_dir}${jar_package}_${cur_date}
  66. if [[ $? -eq 0 ]]
  67. then
  68. echo "Backup file exists "
  69. else
  70. echo -e "\033[31m The Backup failed,Script stop running.....
  71. Exiting............
  72. \033[0m"
  73. exit 1
  74. fi
  75. cp -rp ${tmp_dir}${jar_package} ${jar_dir}${app_name}/
  76. h2 "[Step $item]:Confirm the current pid number..."; let item+=1
  77. set +e;ps -ef | grep ${jar_package} | egrep -v 'deploy_jar.sh|grep';set -e
  78. app_pid=$(ps -ef | grep ${jar_package} | egrep -v 'deploy_jar.sh|grep' | awk '{print $2}')
  79. read -p "The pid of ${app_pid} is about to be killed, press enter to confirm":
  80. if [[ -n ${app_pid} ]];then
  81. kill -9 ${app_pid}
  82. fi
  83. h2 "[Step $item]: Start the deployment..."; let item+=1
  84. cd ${jar_dir}${app_name}
  85. while true
  86. do
  87. read -p "Please enter the application startup command(Don't add &): " start_cmd
  88. if [[ -z ${start_cmd} ]];then
  89. echo "Please enter the correct startup command!"
  90. else
  91. break
  92. fi
  93. done
  94. ${start_cmd} &
  95. ls ${jar_dir}${app_name}/${jar_package}
  96. rm -rf ${tmp_dir}${jar_package}
  97. echo -e "\033[33m Successful deployment, exit normally...... \033[0m"

脚本使用说明

注:在执行该脚本前,要保证以下两点:

  • 要部署的jar包已经上传到10.252.201.131主机的/tmp/你的名字/ 目录下,并且其他人具有可读权限
  • 脚本所在主机已经切换至 llyxpt 用户

脚本可定义的选项如下:
风控发包脚本 - 图1

提示:脚本在执行过程中,只要遇到错误就会停止,不会继续往下执行。

命令示例:

  1. ./fk_deploy_jar.sh -n fed -f risk-mgt-manager-fed-0.0.2-SNAPSHOT.jar

脚本执行过程:

风控发包脚本 - 图2

风控发包脚本 - 图3

输出下面这个,就表示执行成功。手动执行下开头的ps提示指令,确认进程存在即可。
风控发包脚本 - 图4

风控发包脚本 - 图5

更改应用启动方式为supervisord后,脚本更新如下

注: 相比上面脚本,无需再使用“-f”选项指定 jar包名称、人工确认旧程序的 pid 号及手动输入应用启动命令

  1. #!/usr/bin/env bash
  2. set -e
  3. # user=lvjianzhao
  4. rsync_source_dir=10.252.201.131::NRPE/lvjianzhao/
  5. #tmp_dir=/tmp/"${user}"/
  6. jar_dir='/apps/var/'
  7. backup_dir='/apps/var/jakarta/backup/'
  8. item=0
  9. cur_date=$(date +%F_%H%M)
  10. h2() {
  11. printf "\n${underline}${bold}${white}%s${reset}\n" "$@"
  12. }
  13. USAGE(){
  14. echo -e "\033[33m
  15. $0 Script usage:
  16. -n: Specify the app name to deploy(Such as /apps/var/customermanager/ app_name for that:customermanager)
  17. -h: Output help information
  18. \033[0m"
  19. }
  20. if [[ $1 == '--help' || $1 == '-h' ]]
  21. then
  22. USAGE
  23. exit 0
  24. fi
  25. h2 "[Step $item]: Assign a value to a variable..."; let item+=1
  26. while getopts ":n:" optname;do
  27. case "$optname" in
  28. "n")
  29. app_name=$OPTARG
  30. ;;
  31. ":")
  32. echo "This option has no value!"
  33. ;;
  34. "*")
  35. echo "error message"
  36. ;;
  37. "?")
  38. echo "Not sure about this option..."
  39. ;;
  40. esac
  41. done
  42. jar_package=$(basename $(ls /apps/var/${app_name}/*.jar))
  43. echo -e "\033[33m
  44. The jar_name to be deployed is: ${jar_package}
  45. The app_name to be deployed is: ${app_name}
  46. After successful deployment, be sure to execute the instructions ( ps -ef |grep ${jar_package} | grep -v grep ) to ensure that the new process exists...
  47. \033[0m"
  48. read -p "Press enter to confirm and continue execution. To terminate, press 'CTRL + C' to terminate the script..."
  49. h2 "[Step $item]: Synchronous jars..."; let item+=1
  50. if [[ ! -d ${backup_dir} ]];then
  51. mkdir ${backup_dir} -p
  52. fi
  53. h2 "[Step $item]: Backup the original jar package and Synchronous jars..."; let item+=1
  54. cd /apps/var/
  55. mv ${jar_dir}${app_name}/${jar_package} ${backup_dir}${jar_package}_${cur_date}
  56. ls ${backup_dir}${jar_package}_${cur_date}
  57. rsync -avz ${rsync_source_dir}${jar_package} ${jar_dir}${app_name}/
  58. chmod 755 -R ${jar_dir}${app_name}
  59. chown llyxpt -R ${jar_dir}${app_name}
  60. h2 "[Step $item]: Start the deployment..."; let item+=1
  61. supervisorctl restart ${app_name}
  62. sleep 3
  63. ps -ef | grep ${jar_name} | grep -v grep
  64. echo -e "\033[33m Successful deployment, exit normally...... \033[0m"