05wms-ztf-test.sh 9.6 KB

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