04wms-ztf-test.sh 6.6 KB

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