test-deploy-wms8-jd.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #!/bin/bash
  2. #set -x
  3. set -e
  4. codePath=/home/www/deploy/wms8_test_jd
  5. gitUrlArr=(
  6. "http://git.galaxis.yvanui.com/wms8/wms8-bench.git"
  7. "http://git.yvanui.com/jztd/yvan-ext.git"
  8. "http://git.galaxis.yvanui.com/wms8/wms-core.git"
  9. "http://git.yvanui.com/jztd/yvan-ext-mobile.git"
  10. )
  11. branchArr=(
  12. "master"
  13. "mini-bundle"
  14. "master"
  15. "master"
  16. )
  17. positionArr=(
  18. "."
  19. "yvan-ext"
  20. "wms-core"
  21. "yvan-ext-mobile"
  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. # 构建 yvan-ext
  71. if [ "${needBuild[1]}" != "0" ]; then
  72. echo -e "$echoPrefix cd $codePath/yvan-ext $echoSuffix"
  73. cd "$codePath/yvan-ext"
  74. echo -e "$echoPrefix yarn $echoSuffix"
  75. yarn
  76. echo -e "$echoPrefix yarn link $echoSuffix"
  77. yarn link
  78. echo -e "$echoPrefix yarn build $echoSuffix"
  79. yarn build
  80. echo ""
  81. else
  82. echo "yvan-ext 文件未变化"
  83. fi
  84. # 构建 yvan-ext-mobile
  85. if [ "${needBuild[4]}" != "0" ]; then
  86. echo -e "$echoPrefix cd $codePath/yvan-ext-mobile $echoSuffix"
  87. cd "$codePath/yvan-ext-mobile"
  88. echo -e "$echoPrefix yarn $echoSuffix"
  89. yarn
  90. echo -e "$echoPrefix yarn link $echoSuffix"
  91. yarn link
  92. echo -e "$echoPrefix yarn build $echoSuffix"
  93. yarn build
  94. echo ""
  95. else
  96. echo "yvan-ext-mobile 文件未变化"
  97. fi
  98. # 构建 wms-biz/bundle
  99. if [ "${needBuild[0]}" != "0" ]; then
  100. echo -e "$echoPrefix cd $codePath/wms-biz/bundle $echoSuffix"
  101. cd "$codePath/wms-biz/bundle"
  102. echo -e "$echoPrefix yarn $echoSuffix"
  103. yarn
  104. echo -e "$echoPrefix yarn link yvan-ext $echoSuffix"
  105. yarn link yvan-ext
  106. echo -e "$echoPrefix yarn build $echoSuffix"
  107. yarn build
  108. echo ""
  109. else
  110. echo "wms-biz/bundle 文件未变化"
  111. fi
  112. # 构建 wms-biz/pda-client
  113. if [ "${needBuild[0]}" != "0" ]; then
  114. echo -e "$echoPrefix cd $codePath/wms-biz/pda-client $echoSuffix"
  115. cd "$codePath/wms-biz/pda-client"
  116. echo -e "$echoPrefix yarn $echoSuffix"
  117. yarn
  118. echo -e "$echoPrefix yarn link yvan-ext-mobile $echoSuffix"
  119. yarn link yvan-ext-mobile
  120. echo -e "$echoPrefix yarn build $echoSuffix"
  121. yarn build
  122. echo ""
  123. else
  124. echo "wms-biz/pda-client 文件未变化"
  125. fi
  126. # # 构建 java
  127. # echo -e "$echoPrefix cd $codePath $echoSuffix"
  128. # cd $codePath
  129. # echo -e "$echoPrefix gradle build -x test $echoSuffix"
  130. # gradle build -x test
  131. # echo ""
  132. }
  133. logPath=/data/logs/wms8_test_jd/wms8_test_jd.log
  134. dirPath=/home/www/deploy/wms8_test_jd/wms-biz/servo/build/libs/
  135. serverName=wms8_test_jd
  136. cmd=$1
  137. profiles=${2:-"test-jd"}
  138. #-------------------------------------------------------------------
  139. JAVA_MEM_OPTS=" -DappName=${serverName} -server -Xmx2g -Xms2g"
  140. DATABASE_OPTS=" -Ddatabase.codeset=ISO-8859-1 -Ddatabase.logging=false"
  141. JAVA_OPTS_EXT=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dapplication.codeset=UTF-8 -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai -Duser.name=user"
  142. #-------------------------------------------------------------------
  143. startServer() {
  144. pid=$1
  145. if [ -z $pid ];then
  146. echo -e "$echoPrefix cd $dirPath../runtime/ $echoSuffix"
  147. cd $dirPath../runtime/
  148. echo -e "$echoPrefix java $JAVA_MEM_OPTS $DATABASE_OPTS $JAVA_OPTS_EXT -jar ./servo-0.0.0-SNAPSHOT.jar --spring.profiles.active=global,$profiles --server.port=8095 >>/dev/null 2>&1 & $echoSuffix"
  149. java $JAVA_MEM_OPTS $DATABASE_OPTS $JAVA_OPTS_EXT -jar ./servo-0.0.0-SNAPSHOT.jar --spring.profiles.active=global,$profiles --server.port=8095 >>/dev/null 2>&1 &
  150. echo "$serverName 启动成功!"
  151. else
  152. echo "$serverName 正在运行..."
  153. fi
  154. echo "查看日志: tail -F $logPath -n 100"
  155. }
  156. stopServer() {
  157. pid=$1
  158. if [ -z $pid ];then
  159. echo "$serverName 未运行"
  160. else
  161. echo -e "$echoPrefix ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print \$2}' | xargs kill $echoSuffix"
  162. ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print $2}' | xargs kill
  163. echo "$serverName 已停止!"
  164. fi
  165. }
  166. deployPull() {
  167. for ((idx=0; idx<${#gitUrlArr[@]}; idx++)); do
  168. pullCode $codePath ${gitUrlArr[idx]} ${branchArr[idx]} ${positionArr[idx]}
  169. if [ "${needBuild[idx]}" == "-1" ]; then
  170. needBuild[idx]=$isChange
  171. fi
  172. done
  173. # 设置 require_config.js 前端版本
  174. version=$(date +_%Y_%m_%d_%H_%M_%S)
  175. 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"
  176. 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
  177. echo "###--代码更新完成--------------------------------------------------------------------------###"
  178. echo ""
  179. }
  180. deployServer() {
  181. pid=$1
  182. deployPull
  183. buildCode
  184. echo "###--代码构建完成--------------------------------------------------------------------------###"
  185. # 重启服务
  186. if [ ! -z $pid ];then
  187. stopServer $pid
  188. sleep 3s
  189. fi
  190. # 移动编译后的文件
  191. echo -e "$echoPrefix cd $dirPath $echoSuffix"
  192. cd $dirPath
  193. echo -e "$echoPrefix rm -rf ../runtime $echoSuffix"
  194. rm -rf ../runtime
  195. echo -e "$echoPrefix cp -r ./ ../runtime $echoSuffix"
  196. cp -r ./ ../runtime
  197. echo ""
  198. startServer
  199. }
  200. restartServer() {
  201. pid=$1
  202. if [ ! -z $pid ];then
  203. stopServer $pid
  204. sleep 3s
  205. fi
  206. startServer
  207. }
  208. logs() {
  209. tail -F $logPath -n 100
  210. }
  211. pid=`ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print $2}'`
  212. # 操作参数: pull deploy restart start stop kill log logs
  213. if [ "$cmd" == "pull" ];then
  214. deployPull
  215. if [ -z $pid ];then
  216. echo "$serverName 未运行 | 输入操作参数: pull deploy restart start stop kill log logs"
  217. fi
  218. elif [ "$cmd" == "deploy" ];then
  219. deployServer $pid
  220. elif [ "$cmd" == "restart" ];then
  221. restartServer $pid
  222. elif [ "$cmd" == "start" ];then
  223. startServer $pid
  224. elif [ "$cmd" == "stop" ] || [ "$cmd" == "kill" ];then
  225. stopServer $pid
  226. elif [ "$cmd" == "log" ] || [ "$cmd" == "logs" ];then
  227. logs
  228. else
  229. if [ -z $pid ];then
  230. echo "$serverName 未运行 | 输入操作参数: pull deploy restart start stop kill log logs"
  231. else
  232. echo "输入操作参数: pull deploy restart start stop kill log logs"
  233. echo "pid=$pid | $serverName 正在运行... | 查看日志: tail -F $logPath -n 100"
  234. fi
  235. fi
  236. echo -e "$echoPrefix rm -rf $codePath/yvan-framework $echoSuffix"
  237. rm -rf $codePath/yvan-framework
  238. # bash <(curl -s http://git.yvanui.com/lizhiwei/jztd-deploy/raw/master/yxt-yn/test-deploy-wms8-jd.sh) [cmd profiles]