replaceAndStart.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/bash
  2. echo "开始运行Demo服务..."
  3. PARAM=$1;
  4. echo -e "Commit log: "$PARAM;
  5. #----------------------- 基本参数配置 start -----------------------
  6. # JAVA安装目录
  7. JAVA_PATH="/opt/jdk1.8.0_191/bin/java";
  8. # 运行环境配置
  9. JAVA_RUN_ENV="test";
  10. # drone部署的jar包所在路径
  11. DRONE_JAR_PATH="/home/drone/data/demoParant";
  12. # API工程
  13. APP_API_NAME="apiService-0.0.1-SNAPSHOT.jar"
  14. APP_API_PATH="/home/demoPatent/apiService"
  15. APP_API_PING_URL="http://127.0.0.1:18091/api/open/ping"
  16. APP_API_RUN=
  17. # 用户端工程
  18. APP_USER_NAME="userService-0.0.1-SNAPSHOT.jar"
  19. APP_USER_PATH="/home/demoPatent/userService"
  20. APP_USER_RUN=
  21. APP_USER_PING_URL="http://127.0.0.1:18071/user/open/ping"
  22. # 后台服务工程
  23. APP_BACK_NAME="backService-0.0.1-SNAPSHOT.jar"
  24. APP_BACK_PATH="/home/demoPatent/backService"
  25. APP_BACK_RUN=
  26. APP_BACK_PING_URL="http://127.0.0.1:18081/back/open/ping"
  27. # 记录是否有启动失败的服务
  28. APP_START_FAIL=
  29. APP_START_RESULT=
  30. #----------------------- 基本参数配置 end -----------------------
  31. echo "程序运行环境:$JAVA_RUN_ENV"
  32. #----------------------- 定义启动函数 start-----------------------
  33. startAPP() {
  34. APP_NAME=$1;
  35. APP_PATH=$2;
  36. PING_URL=$3;
  37. # echo "APP_NAME:$APP_NAME"
  38. # echo "APP_PATH:$APP_PATH"
  39. # echo "PING_URL:$APP_NAME"
  40. # 部署新jar包到程序运行目录
  41. if [ -f "$DRONE_JAR_PATH/$APP_NAME" ];then
  42. echo "Move the new jar package to the deployment directory ..."
  43. mv -f $DRONE_JAR_PATH/$APP_NAME $APP_PATH/$APP_NAME
  44. else
  45. echo "The drone original jar file not exist, skip move."
  46. fi
  47. # 获取程序PID
  48. getPid() {
  49. APP_PID=`ps -ef | grep -v grep | grep $APP_NAME | awk '{print $2}'`
  50. }
  51. getPid
  52. # 启动前检查应用是否启动,如果已经启动则先停止再重新启动
  53. while [ ${APP_PID} ]
  54. do
  55. echo -e "App ${APP_NAME} is still RUNNING! PID:$APP_PID";
  56. echo "stop ${APP_NAME} ...";
  57. kill -9 ${APP_PID};
  58. sleep 2;
  59. getPid
  60. done
  61. # 运行jar包
  62. echo "start ${APP_NAME} ...."
  63. cd ${APP_PATH}
  64. nohup ${JAVA_PATH} -server -Xms256m -Xmx512m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=768m -Xss256k -jar ./${APP_NAME} --spring.profiles.active=$JAVA_RUN_ENV > /dev/null & 2>&1 &
  65. # 请求测试接口,判断服务是否正常启动
  66. getHttpCode () {
  67. http_code=`curl -Is -m 10 -w %{http_code} -o /dev/null $PING_URL`;
  68. }
  69. # 检查服务启动状态
  70. sleep 2s;
  71. count=0;
  72. # 获取接口状态码
  73. getHttpCode
  74. while [ $http_code -ne 200 ]
  75. do
  76. if (($count == 8));then
  77. echo "${APP_NAME}: $(expr $count \* 5)秒内未启动,请检查!";
  78. msg="start ${APP_NAME} failed!\n";
  79. echo -e $msg;
  80. APP_START_RESULT=$APP_START_RESULT$msg;
  81. APP_START_FAIL="YES";
  82. return;
  83. fi
  84. count=$(($count+1));
  85. echo "${APP_NAME} waiting to start ...";
  86. # sleep 1s;
  87. sleep 5s;
  88. # 获取接口状态码
  89. getHttpCode
  90. echo "http_code --> $http_code";
  91. done
  92. getPid
  93. msg="start ${APP_NAME} success! PID=$APP_PID\n";
  94. echo -e $msg;
  95. APP_START_RESULT=$APP_START_RESULT$msg;
  96. }
  97. #----------------------- 函数定义结束 end -----------------------
  98. #----------------------- commit log 判断 start-----------------------
  99. # 根据commit日志参数判断启动哪个工程
  100. if [[ $PARAM == *'[CI API]'* ]]; then
  101. echo -e "Commit参数指定启动API工程...\n";
  102. APP_API_RUN="true"
  103. fi
  104. if [[ $PARAM == *'[CI USER]'* ]]; then
  105. echo -e "Commit参数指定启动用户端工程...\n";
  106. APP_USER_RUN="true"
  107. fi
  108. if [[ $PARAM == *'[CI BACK]'* ]]; then
  109. echo -e "Commit参数指定启动BACK工程...\n";
  110. APP_BACK_RUN="true"
  111. fi
  112. if [[ -z "$APP_API_RUN" ]] && [[ -z "$APP_USER_RUN" ]] && [[ -z "$APP_BACK_RUN" ]]; then
  113. echo -e "Commit未指定参数,准备启动所有工程...\n";
  114. APP_API_RUN="true"
  115. APP_USER_RUN="true"
  116. APP_BACK_RUN="true"
  117. fi
  118. #----------------------- commit log 判断结束 -----------------------
  119. # 启动运营端服务
  120. if [ $APP_API_RUN ]; then
  121. echo "Run api service ..."
  122. startAPP $APP_API_NAME $APP_API_PATH $APP_API_PING_URL
  123. fi
  124. # 启动用户端服务
  125. if [ $APP_USER_RUN ]; then
  126. echo "Run user service"
  127. startAPP $APP_USER_NAME $APP_USER_PATH $APP_USER_PING_URL
  128. fi
  129. # 启动商家端服务
  130. if [ $APP_BACK_RUN ]; then
  131. echo "Run back service"
  132. startAPP $APP_BACK_NAME $APP_BACK_PATH $APP_BACK_PING_URL
  133. fi
  134. # 删除drone构建缓存
  135. rm -rf $DRONE_JAR_PATH/*.jar
  136. # 打印运行结果
  137. echo "------------ service start status ------------"
  138. echo -e $APP_START_RESULT
  139. echo "----------------------------------------------"
  140. # 如果有启动失败的应用,则退出状态码为1,用于drone标记构建失败
  141. if [ $APP_START_FAIL ]; then
  142. exit 1;
  143. fi