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

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