test-deploy-wms8-jd.sh 7.6 KB

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