11kpi-admin.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 ""
  88. else
  89. echo "wms-ui-pc 文件未变化"
  90. fi
  91. # 构建 java
  92. echo -e "$echoPrefix cd $codePath $echoSuffix"
  93. cd $codePath
  94. echo -e "$echoPrefix gradle build -x test $echoSuffix"
  95. gradle build -x test
  96. echo ""
  97. }
  98. logPath=/data/logs/kpi_admin/server.log
  99. dirPath=/home/www/deploy/kpi_admin/wms-modules/wms-system/build/libs/
  100. serverName=kpi_admin
  101. cmd=$1
  102. profiles=${2:-"test"}
  103. #-------------------------------------------------------------------
  104. JAVA_MEM_OPTS=" -DappName=${serverName} -server -Xmx2g -Xms2g"
  105. DATABASE_OPTS=" -Ddatabase.codeset=ISO-8859-1 -Ddatabase.logging=false"
  106. JAVA_OPTS_EXT=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dapplication.codeset=UTF-8 -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai"
  107. #-------------------------------------------------------------------
  108. startServer() {
  109. pid=$1
  110. if [ -z $pid ];then
  111. echo -e "$echoPrefix cd $dirPath../runtime/ $echoSuffix"
  112. cd $dirPath../runtime/
  113. 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"
  114. 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 &
  115. echo "$serverName 启动成功!"
  116. else
  117. echo "$serverName 正在运行..."
  118. fi
  119. echo "查看日志: tail -F $logPath -n 100"
  120. }
  121. stopServer() {
  122. pid=$1
  123. if [ -z $pid ];then
  124. echo "$serverName 未运行"
  125. else
  126. echo -e "$echoPrefix ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print \$2}' | xargs kill $echoSuffix"
  127. ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print $2}' | xargs kill
  128. echo "$serverName 已停止!"
  129. fi
  130. }
  131. deployPull() {
  132. for ((idx=0; idx<${#gitUrlArr[@]}; idx++)); do
  133. pullCode $codePath ${gitUrlArr[idx]} ${branchArr[idx]} ${positionArr[idx]}
  134. if [ "${needBuild[idx]}" == "-1" ]; then
  135. needBuild[idx]=$isChange
  136. fi
  137. done
  138. echo "###--代码更新完成--------------------------------------------------------------------------###"
  139. echo ""
  140. }
  141. deployServer() {
  142. pid=$1
  143. deployPull
  144. buildCode
  145. echo "###--代码构建完成--------------------------------------------------------------------------###"
  146. # 重启服务
  147. if [ ! -z $pid ];then
  148. stopServer $pid
  149. sleep 8s
  150. fi
  151. # 移动编译后的文件
  152. echo -e "$echoPrefix cd $dirPath $echoSuffix"
  153. cd $dirPath
  154. echo -e "$echoPrefix rm -rf ../runtime $echoSuffix"
  155. rm -rf ../runtime
  156. echo -e "$echoPrefix cp -r ./ ../runtime $echoSuffix"
  157. cp -r ./ ../runtime
  158. echo ""
  159. startServer
  160. }
  161. restartServer() {
  162. pid=$1
  163. if [ ! -z $pid ];then
  164. stopServer $pid
  165. sleep 3s
  166. fi
  167. startServer
  168. }
  169. logs() {
  170. tail -F $logPath -n 100
  171. }
  172. pid=`ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print $2}'`
  173. # 操作参数: pull deploy restart start stop kill log logs
  174. if [ "$cmd" == "pull" ];then
  175. deployPull
  176. if [ -z $pid ];then
  177. echo "$serverName 未运行 | 输入操作参数: pull deploy restart start stop kill log logs"
  178. fi
  179. elif [ "$cmd" == "deploy" ];then
  180. deployServer $pid
  181. elif [ "$cmd" == "restart" ];then
  182. restartServer $pid
  183. elif [ "$cmd" == "start" ];then
  184. startServer $pid
  185. elif [ "$cmd" == "stop" ] || [ "$cmd" == "kill" ];then
  186. stopServer $pid
  187. elif [ "$cmd" == "log" ] || [ "$cmd" == "logs" ];then
  188. logs
  189. else
  190. if [ -z $pid ];then
  191. echo "$serverName 未运行 | 输入操作参数: pull deploy restart start stop kill log logs"
  192. else
  193. echo "输入操作参数: pull deploy restart start stop kill log logs"
  194. echo "pid=$pid | $serverName 正在运行... | 查看日志: tail -F $logPath -n 100"
  195. fi
  196. fi
  197. # kpi-admin测试
  198. # bash <(curl -s http://git.yvanui.com/lizhiwei/jztd-deploy/raw/master/hua_wei_yun/11kpi-admin.sh) [cmd profiles]
  199. #nohup node server.js >>/dev/null 2>&1 &