11kpi-admin.sh 6.4 KB

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