06wms-ztf-filemanager-test.sh 6.2 KB

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