08wms-ztf-config-center-test.sh 8.0 KB

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