02wms8-prod.sh 6.8 KB

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