Shell -- 通过AWS cli 和 targets_group 做流量切换部署脚本(长连接服务)

  1 #!/bin/bash
  2 
  3 who am i
  4 # 部署jar包通过cli的形式
  5 set -x
  6 
  7 #获取线上服务端口
  8 PORT=$2
  9 
 10  15 
 16 # 负载均衡目标组ID:register-targets
 17 targets_arn="arn:aws:xxxxxxxx/elbwan-prod-mqtt-001/75dd805801b1fdce"
 18 
 19 # 获取要部署的主机IP
 20 #HOST_LIST=($(echo $HOST))
 21 
 22 #项目名
 23 REMOTE_PROJECT=$1
 24 
 25 #远程JAR包存放目录
 26 REMOTE_JARDIR=/apps/project/${REMOTE_PROJECT}
 27 
 28 
 29 #远程执行用户
 30 REMOTEUSER='co_dev'
 31 
 32 # 服务器列表新增服务器都在这里面添加,因为aws ELB network loadbalance 的目标群组是instance的形式,不能通过IP的形式挂载/摘取机器所以自己建立了对应关系表
 33 list[1]="prod1,i-0dae45bcc2d4f77ad,10.78.5.55"
 34 list[2]="prod2,i-05c517e7856ef6f76,10.78.7.172"
 35 
 36 
 37 # 查看要部署的机器
 38 echo ${list[@]} 
 39 
 40 # 函数获取进程pid
 41 jarpid () {
 42   JAR_pid=`ssh ${REMOTEUSER}@${IP} ps -ef | grep "${REMOTE_JARDIR}/${REMOTE_PROJECT}.jar" | grep -v "grep"|awk '{print $2}'`
 43   if [ -z "$JAR_pid" ] ; then break ;fi
 44 }
 45   ENV=`echo "$name"| awk -F ',' '{print $1}'`
 46   ID=`echo "$name"| awk -F ',' '{print $2}'`
 47   IP=`echo "$name"| awk -F ',' '{print $3}'`
 48 
 49   # 获取编译后的包名
 50   PACKAGE=`find ./ -name ${REMOTE_PROJECT}.jar`
 51 
 52   # 从ELB找出负载均衡机器
 53   /root/.local/bin/aws elbv2 deregister-targets --target-group-arn  $targets_arn --targets Id=$ID
 54   if [ $? -eq 0 ];then
 55         i=1
 56         while [[ $i < 5 ]]
 57         do
 58         jarpid
 59         if [[ $i -eq 4 ]];then
 60                 ssh ${REMOTEUSER}@${IP}  "kill -9 $JAR_pid"
 61                 break
 62         fi
 63 
 64         ssh ${REMOTEUSER}@${IP}  "kill -15 $JAR_pid"
 65         jarpid
 66         sleep 10
 67         let i++
 68         done
 69   else
 70         echo  “ELB 摘除机器失败,请检查”
 71         exit 4
 72   fi
 73 
 74 
 75         # 远程主机备份.jar 包跟新新版本.jar包并启动
 76         scp $PACKAGE ${REMOTEUSER}@${IP}:$REMOTE_JARDIR/${REMOTE_PROJECT}.jar
 77         if [ $? -eq 0 ];then
 78                 jar_number=`ssh ${REMOTEUSER}@${IP}  find /apps/project/${REMOTE_PROJECT} -name ${REMOTE_PROJECT}.jar | wc -l`
 79                 if [ $jar_number -ge 1 ];then
 80                         echo "${REMOTE_PROJECT}.jar 版本包已经cp 到${REMOTE_PROJECT}/bin 目录下,可以正常器启动${REMOTE_PROJECT}项目"
 81                 else
 82                         echo "${REMOTE_PROJECT}.jar No such file or directory 可能maven编译失败,请检查"
 83                         exit 2
 84                 fi
 85         else
 86                 echo "${REMOTE_PROJECT}.jar   No such file or directory 远程更新jar 失败"
 87     exit 1
 88         fi
 89 
 90 
 91         # 启动jar 服务并检查端口
 92         sleep 2
 93         echo ".......................................开始启动$REMOTE_PROJECT 服务"
 94         ssh ${REMOTEUSER}@${IP} "nohup /apps/sofe/jdk1.8.0_131/bin/java -jar -Xms13g -Xmx13g -Xss512k -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=512m -XX:NewSize=11g -XX:MaxNewSize=11g -XX:SurvivorRatio=18 -XX:MaxDirectMemorySize=2g -XX:+UseParNewGC -XX:ParallelGCThreads=4 -XX:MaxTenuringThreshold=15 -XX:+UseConcMarkSweepGC -XX:+UseCMSInitiatingOccupancyOnly -XX:+ScavengeBeforeFullGC -XX:+CMSScavengeBeforeRemark -XX:+CMSParallelRemarkEnabled -XX:CMSInitiatingOccupancyFraction=70 -XX:-CMSClassUnloadingEnabled -XX:SoftRefLRUPolicyMSPerMB=0 -XX:-ReduceInitialCardMarks -XX:+ExplicitGCInvokesConcurrentAndUnloadsClasses -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:-HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/apps/logs/grab_cycle_mqttserver/HeapDumpOnOutOfMemoryError -Dio.netty.allocator.type=pooled -Dspring.profiles.active="$ENV" ${REMOTE_JARDIR}/${REMOTE_PROJECT}.jar  >>/apps/project/${REMOTE_PROJECT}/${REMOTE_PROJECT}.log 2>&1 &"
 95         sleep 10
 96         java_pid=`ssh $REMOTEUSER@$IP ps -ef | grep "${REMOTE_JARDIR}/$REMOTE_PROJECT" | egrep -v "grep"|wc -l`
 97         if [[  $java_pid -ge 1 ]];then
 98                         echo "${REMOTE_PROJECT} startup is successful"
 99                 else
100                         echo "${REMOTE_PROJECT}  startup is filed"
101                         exit 3
102         fi
103 
104  # 从负载均衡器注册目标主机
105  /root/.local/bin/aws elbv2 register-targets --target-group-arn  $targets_arn --targets Id=$ID
106  if [ $? -eq 0 ];then
107       echo "${IP} 服务器 ${REMOTE_PROJECT} 项目部署完毕.等待4分钟注册操作下一台机器..................................."
108       echo -e "\n\n\n"
109       sleep 180
110  else
111       echo "${IP} 主机注册负载均衡失败"
112       exit 5
113  fi
114 
115 done

 

posted @ 2018-11-21 15:37  MR_dy  阅读(544)  评论(0编辑  收藏  举报