title: 风控发包脚本 #标题
tags: work #标签
categories: 工作大杂烩 # 分类
password: jianzhao87.
abstract: 有东西被加密了, 请输入密码查看。
message: 您好, 这里需要密码。
date: 2020-11-19
wrong_pass_message: 抱歉, 这个密码看着不太对, 请再试试。
部署脚本
$ cat fk_deploy_jar.sh # 脚本如下#!/usr/bin/env bashset -euser=lvjianzhaotmp_dir=/tmp/"${user}"/jar_dir='/apps/var/'backup_dir='/apps/var/jakarta/backup/'item=0cur_date=$(date +%F_%H%M)h2() {printf "\n${underline}${bold}${white}%s${reset}\n" "$@"}USAGE(){echo -e "\033[33m$0 Script usage:-n: Specify the app name to deploy(Such as /apps/var/customermanager/ app_name for that:customermanager)-f: Specify the jar_name to deploy-h: Output help information\033[0m"}if [[ $1 == '--help' || $1 == '-h' ]]thenUSAGEexit 0fih2 "[Step $item]: Assign a value to a variable..."; let item+=1while getopts ":f:n:" optname;docase "$optname" in"f")jar_package=$OPTARG;;"n")app_name=$OPTARG;;":")echo "This option has no value!";;"*")echo "error message";;"?")echo "Not sure about this option...";;esacdoneecho -e "\033[33mThe jar_name to be deployed is: ${jar_package}The app_name to be deployed is: ${app_name}After successful deployment, be sure to execute the instructions ( ps -ef |grep ${jar_package} | grep -v grep ) to ensure that the new process exists...\033[0m"read -p "Press enter to confirm and continue execution. To terminate, press 'CTRL + C' to terminate the script..."h2 "[Step $item]: Synchronous jars..."; let item+=1if [[ ! -d ${tmp_dir} ]]thenmkdir ${tmp_dir}cd ${tmp_dir}elsecd ${tmp_dir}firsync -avs 10.252.201.131::NRPE/${user}/${jar_package} ${tmp_dir}h2 "[Step $item]: Modify full permissions and backup the original jar package..."; let item+=1chmod 777 ${tmp_dir}${jar_package}chown llyxpt ${tmp_dir}${jar_package}cp -rp ${jar_dir}${app_name}/${jar_package} ${backup_dir}${jar_package}_${cur_date}ls ${backup_dir}${jar_package}_${cur_date}if [[ $? -eq 0 ]]thenecho "Backup file exists "elseecho -e "\033[31m The Backup failed,Script stop running.....Exiting............\033[0m"exit 1ficp -rp ${tmp_dir}${jar_package} ${jar_dir}${app_name}/h2 "[Step $item]:Confirm the current pid number..."; let item+=1set +e;ps -ef | grep ${jar_package} | egrep -v 'deploy_jar.sh|grep';set -eapp_pid=$(ps -ef | grep ${jar_package} | egrep -v 'deploy_jar.sh|grep' | awk '{print $2}')read -p "The pid of ${app_pid} is about to be killed, press enter to confirm":if [[ -n ${app_pid} ]];thenkill -9 ${app_pid}fih2 "[Step $item]: Start the deployment..."; let item+=1cd ${jar_dir}${app_name}while truedoread -p "Please enter the application startup command(Don't add &): " start_cmdif [[ -z ${start_cmd} ]];thenecho "Please enter the correct startup command!"elsebreakfidone${start_cmd} &ls ${jar_dir}${app_name}/${jar_package}rm -rf ${tmp_dir}${jar_package}echo -e "\033[33m Successful deployment, exit normally...... \033[0m"
脚本使用说明
注:在执行该脚本前,要保证以下两点:
- 要部署的jar包已经上传到10.252.201.131主机的/tmp/你的名字/ 目录下,并且其他人具有可读权限
- 脚本所在主机已经切换至 llyxpt 用户
脚本可定义的选项如下:

提示:脚本在执行过程中,只要遇到错误就会停止,不会继续往下执行。
命令示例:
./fk_deploy_jar.sh -n fed -f risk-mgt-manager-fed-0.0.2-SNAPSHOT.jar
脚本执行过程:


输出下面这个,就表示执行成功。手动执行下开头的ps提示指令,确认进程存在即可。


更改应用启动方式为supervisord后,脚本更新如下
注: 相比上面脚本,无需再使用“-f”选项指定 jar包名称、人工确认旧程序的 pid 号及手动输入应用启动命令
#!/usr/bin/env bashset -e# user=lvjianzhaorsync_source_dir=10.252.201.131::NRPE/lvjianzhao/#tmp_dir=/tmp/"${user}"/jar_dir='/apps/var/'backup_dir='/apps/var/jakarta/backup/'item=0cur_date=$(date +%F_%H%M)h2() {printf "\n${underline}${bold}${white}%s${reset}\n" "$@"}USAGE(){echo -e "\033[33m$0 Script usage:-n: Specify the app name to deploy(Such as /apps/var/customermanager/ app_name for that:customermanager)-h: Output help information\033[0m"}if [[ $1 == '--help' || $1 == '-h' ]]thenUSAGEexit 0fih2 "[Step $item]: Assign a value to a variable..."; let item+=1while getopts ":n:" optname;docase "$optname" in"n")app_name=$OPTARG;;":")echo "This option has no value!";;"*")echo "error message";;"?")echo "Not sure about this option...";;esacdonejar_package=$(basename $(ls /apps/var/${app_name}/*.jar))echo -e "\033[33mThe jar_name to be deployed is: ${jar_package}The app_name to be deployed is: ${app_name}After successful deployment, be sure to execute the instructions ( ps -ef |grep ${jar_package} | grep -v grep ) to ensure that the new process exists...\033[0m"read -p "Press enter to confirm and continue execution. To terminate, press 'CTRL + C' to terminate the script..."h2 "[Step $item]: Synchronous jars..."; let item+=1if [[ ! -d ${backup_dir} ]];thenmkdir ${backup_dir} -pfih2 "[Step $item]: Backup the original jar package and Synchronous jars..."; let item+=1cd /apps/var/mv ${jar_dir}${app_name}/${jar_package} ${backup_dir}${jar_package}_${cur_date}ls ${backup_dir}${jar_package}_${cur_date}rsync -avz ${rsync_source_dir}${jar_package} ${jar_dir}${app_name}/chmod 755 -R ${jar_dir}${app_name}chown llyxpt -R ${jar_dir}${app_name}h2 "[Step $item]: Start the deployment..."; let item+=1supervisorctl restart ${app_name}sleep 3ps -ef | grep ${jar_name} | grep -v grepecho -e "\033[33m Successful deployment, exit normally...... \033[0m"
