04wms-ztf-test.sh 7.3 KB

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