07wms-ztf-print-test.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/bin/bash
  2. #set -x
  3. set -e
  4. codePath=/home/www/deploy/wms8_ztf_print_2
  5. gitUrlArr=(
  6. "http://git.yvanui.com/jztd/wms.git"
  7. "http://git.yvanui.com/jztd/wms-ui-pc.git"
  8. "http://git.yvanui.com/lizhiwei/yvan-framework.git"
  9. )
  10. branchArr=(
  11. "feature_0912"
  12. "feature_0912"
  13. "8.4"
  14. )
  15. positionArr=(
  16. "."
  17. "wms-ui-pc"
  18. "yvan-framework"
  19. )
  20. # 是否需要构建,-1:自动;0:不需要;1:需要
  21. needBuild=(
  22. "1"
  23. "1"
  24. "1"
  25. )
  26. echoPrefix="\033[36m+"
  27. echoSuffix="\033[0m"
  28. # 强制覆盖本地的代码 git fetch --all && git reset --hard origin/master
  29. isChange="1" # 仓库是否发生变化,0:未变化;非0:变化
  30. pullCode() {
  31. # Usage: pullCode "codePath" "gitUrl" "branch" "position"
  32. path=$1 # 代码保存路径
  33. gitUrl=$2 # git仓库地址
  34. branch=${3:-"master"} # git分支
  35. position=${4:-""} # 使用“.”clone到当前文件夹
  36. isChange="1"
  37. # 创建文件夹
  38. if [ ! -d "$path" ]; then
  39. echo -e "$echoPrefix 请检查服务器是否正确,再执行: mkdir -p $path $echoSuffix"
  40. exit
  41. fi
  42. # git clone
  43. echo -e "$echoPrefix cd $path $echoSuffix"
  44. cd $path
  45. if [ "`ls -A $path`" == "" ] || [ ! -d "$path/$position" ] || [ "`ls -A $path/$position`" = "" ]; then
  46. echo -e "$echoPrefix git clone $gitUrl $position $echoSuffix"
  47. git clone $gitUrl $position
  48. isChange="2"
  49. fi
  50. # 进入文件夹,切换分支,git pull
  51. if [ "$position" != "" ]; then
  52. echo -e "$echoPrefix cd $position $echoSuffix"
  53. cd $position
  54. fi
  55. echo -e "$echoPrefix git checkout $branch $echoSuffix"
  56. git checkout $branch
  57. echo -e "$echoPrefix git checkout . $echoSuffix"
  58. git checkout .
  59. echo -e "$echoPrefix git pull $echoSuffix"
  60. if [ "`git pull`" == "Already up-to-date." ] && [ $isChange == "1" ]; then
  61. isChange="0"
  62. fi
  63. echo ""
  64. }
  65. buildCode() {
  66. # 构建 java
  67. echo -e "$echoPrefix cd $codePath $echoSuffix"
  68. cd $codePath
  69. echo -e "$echoPrefix gradle build -x test $echoSuffix"
  70. gradle build -x test
  71. echo ""
  72. }
  73. logPath=/data/logs/wms8_ztf_print_2/server.log
  74. dirPath=/home/www/deploy/wms8_ztf_print_2/wms-modules/wms-print/build/libs/
  75. serverName=2_wms8_ztf_print_2
  76. cmd=$1
  77. profiles=${2:-"test"}
  78. #-------------------------------------------------------------------
  79. JAVA_MEM_OPTS=" -DappName=${serverName} -server -Xmx2g -Xms2g"
  80. DATABASE_OPTS=" -Ddatabase.codeset=ISO-8859-1 -Ddatabase.logging=false"
  81. JAVA_OPTS_EXT=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dapplication.codeset=UTF-8 -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai"
  82. #-------------------------------------------------------------------
  83. moveBuildFile() {
  84. # 移动编译后的文件
  85. echo -e "$echoPrefix cd $dirPath $echoSuffix"
  86. cd $dirPath
  87. echo -e "$echoPrefix rm -rf ../runtime $echoSuffix"
  88. rm -rf ../runtime
  89. echo -e "$echoPrefix cp -r ./ ../runtime $echoSuffix"
  90. cp -r ./ ../runtime
  91. echo ""
  92. }
  93. startServer() {
  94. pid=$1
  95. if [ -z $pid ];then
  96. echo -e "$echoPrefix cd $dirPath../runtime/ $echoSuffix"
  97. cd $dirPath../runtime/
  98. echo -e "$echoPrefix java $JAVA_MEM_OPTS $DATABASE_OPTS $JAVA_OPTS_EXT -jar ./wms-print-0.0.0-SNAPSHOT.jar --spring.profiles.active=global,$profiles --server.port=8084 >>/dev/null 2>&1 & $echoSuffix"
  99. java $JAVA_MEM_OPTS $DATABASE_OPTS $JAVA_OPTS_EXT -jar ./wms-print-0.0.0-SNAPSHOT.jar --spring.profiles.active=global,$profiles --server.port=9085 >>/dev/null 2>&1 &
  100. echo "$serverName 启动成功!"
  101. else
  102. echo "$serverName 正在运行..."
  103. fi
  104. echo "查看日志: tail -F $logPath -n 100"
  105. }
  106. stopServer() {
  107. pid=$1
  108. if [ -z $pid ];then
  109. echo "$serverName 未运行"
  110. else
  111. echo -e "$echoPrefix ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print \$2}' | xargs kill $echoSuffix"
  112. ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print $2}' | xargs kill
  113. echo "$serverName 已停止!"
  114. fi
  115. }
  116. deployPull() {
  117. for ((idx=0; idx<${#gitUrlArr[@]}; idx++)); do
  118. pullCode $codePath ${gitUrlArr[idx]} ${branchArr[idx]} ${positionArr[idx]}
  119. if [ "${needBuild[idx]}" == "-1" ]; then
  120. needBuild[idx]=$isChange
  121. fi
  122. done
  123. # 设置 require_config.js 前端版本
  124. # version=$(date +_%Y_%m_%d_%H_%M_%S)
  125. # echo -e "$echoPrefix sed -i 's/_[0-9]\{4\}_[0-9]\{2\}_[0-9]\{2\}_[0-9]\{2\}_[0-9]\{2\}_[0-9]\{2\}/$version/g' $codePath/public/require_config.js $echoSuffix"
  126. # sed -i "s/_[0-9]\{4\}_[0-9]\{2\}_[0-9]\{2\}_[0-9]\{2\}_[0-9]\{2\}_[0-9]\{2\}/$version/g" $codePath/public/require_config.js
  127. echo "###--代码更新完成--------------------------------------------------------------------------###"
  128. echo ""
  129. }
  130. deployServer() {
  131. pid=$1
  132. deployPull
  133. buildCode
  134. echo "###--代码构建完成--------------------------------------------------------------------------###"
  135. # 重启服务
  136. if [ ! -z $pid ];then
  137. stopServer $pid
  138. sleep 8s
  139. fi
  140. # 移动编译后的文件
  141. moveBuildFile
  142. startServer
  143. }
  144. restartServer() {
  145. pid=$1
  146. if [ ! -z $pid ];then
  147. stopServer $pid
  148. sleep 3s
  149. fi
  150. moveBuildFile
  151. startServer
  152. }
  153. logs() {
  154. tail -F $logPath -n 100
  155. }
  156. pid=`ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print $2}'`
  157. # 操作参数: pull build deploy restart start stop kill log logs
  158. if [ "$cmd" == "pull" ];then
  159. deployPull
  160. if [ -z $pid ];then
  161. echo "$serverName 未运行 | 输入操作参数: pull build deploy restart start stop kill log logs"
  162. fi
  163. elif [ "$cmd" == "build" ];then
  164. deployPull
  165. buildCode
  166. echo "###--代码构建完成--------------------------------------------------------------------------###"
  167. elif [ "$cmd" == "deploy" ];then
  168. deployServer $pid
  169. elif [ "$cmd" == "restart" ];then
  170. restartServer $pid
  171. elif [ "$cmd" == "start" ];then
  172. startServer $pid
  173. elif [ "$cmd" == "stop" ] || [ "$cmd" == "kill" ];then
  174. stopServer $pid
  175. elif [ "$cmd" == "log" ] || [ "$cmd" == "logs" ];then
  176. logs
  177. else
  178. if [ -z $pid ];then
  179. echo "$serverName 未运行 | 输入操作参数: pull build deploy restart start stop kill log logs"
  180. echo "查看日志: tail -F $logPath -n 100"
  181. else
  182. echo "输入操作参数: pull build deploy restart start stop kill log logs"
  183. echo "pid=$pid | $serverName 正在运行... | 查看日志: tail -F $logPath -n 100"
  184. fi
  185. fi
  186. # 中通服API-华为云-打印服务测试
  187. # bash <(curl -s http://git.yvanui.com/lizhiwei/jztd-deploy/raw/master/hua_wei_yun/07wms-ztf-print-test.sh) [cmd profiles]