app.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/bin/bash
  2. set -e
  3. source /etc/profile
  4. export JAVA_HOME=/usr/java/latest
  5. export PATH=$JAVA_HOME/bin:$PATH
  6. touch /tmp/start.log
  7. chown admin: /tmp/start.log
  8. chown -R admin: /home/admin/canal-server
  9. host=`hostname -i`
  10. # waitterm
  11. # wait TERM/INT signal.
  12. # see: http://veithen.github.io/2014/11/16/sigterm-propagation.html
  13. waitterm() {
  14. local PID
  15. # any process to block
  16. tail -f /dev/null &
  17. PID="$!"
  18. # setup trap, could do nothing, or just kill the blocker
  19. trap "kill -TERM ${PID}" TERM INT
  20. # wait for signal, ignore wait exit code
  21. wait "${PID}" || true
  22. # clear trap
  23. trap - TERM INT
  24. # wait blocker, ignore blocker exit code
  25. wait "${PID}" 2>/dev/null || true
  26. }
  27. # waittermpid "${PIDFILE}".
  28. # monitor process by pidfile && wait TERM/INT signal.
  29. # if the process disappeared, return 1, means exit with ERROR.
  30. # if TERM or INT signal received, return 0, means OK to exit.
  31. waittermpid() {
  32. local PIDFILE PID do_run error
  33. PIDFILE="${1?}"
  34. do_run=true
  35. error=0
  36. trap "do_run=false" TERM INT
  37. while "${do_run}" ; do
  38. PID="$(cat "${PIDFILE}")"
  39. if ! ps -p "${PID}" >/dev/null 2>&1 ; then
  40. do_run=false
  41. error=1
  42. else
  43. sleep 1
  44. fi
  45. done
  46. trap - TERM INT
  47. return "${error}"
  48. }
  49. function checkStart() {
  50. local name=$1
  51. local cmd=$2
  52. local timeout=$3
  53. cost=5
  54. while [ $timeout -gt 0 ]; do
  55. ST=`eval $cmd`
  56. if [ "$ST" == "0" ]; then
  57. sleep 1
  58. let timeout=timeout-1
  59. let cost=cost+1
  60. elif [ "$ST" == "" ]; then
  61. sleep 1
  62. let timeout=timeout-1
  63. let cost=cost+1
  64. else
  65. break
  66. fi
  67. done
  68. echo "start $name successful"
  69. }
  70. function start_canal() {
  71. echo "start canal ..."
  72. managerAddress=`perl -le 'print $ENV{"canal.admin.manager"}'`
  73. if [ ! -z "$managerAddress" ] ; then
  74. # canal_local.properties mode
  75. adminPort=`perl -le 'print $ENV{"canal.admin.port"}'`
  76. if [ -z "$adminPort" ] ; then
  77. adminPort=11110
  78. fi
  79. su admin -c 'cd /home/admin/canal-server/bin/ && sh restart.sh local 1>>/tmp/start.log 2>&1'
  80. sleep 5
  81. #check start
  82. checkStart "canal" "nc 127.0.0.1 $adminPort -w 1 -zv 2> /tmp/nc.out && cat /tmp/nc.out | grep -c Connected" 30
  83. else
  84. metricsPort=`perl -le 'print $ENV{"canal.metrics.pull.port"}'`
  85. if [ -z "$metricsPort" ] ; then
  86. metricsPort=11112
  87. fi
  88. destination=`perl -le 'print $ENV{"canal.destinations"}'`
  89. destinationExpr=`perl -le 'print $ENV{"canal.destinations.expr"}'`
  90. multistream=`perl -le 'print $ENV{"canal.instance.multi.stream.on"}'`
  91. if [[ "$destination" =~ ',' ]] || [[ -n "$destinationExpr" ]]; then
  92. if [[ "$multistream" = 'true' ]] ; then
  93. if [[ -n "$destinationExpr" ]] ; then
  94. splitDestinations '1' $destinationExpr
  95. else
  96. splitDestinations '2' $destination
  97. fi
  98. else
  99. echo "multi destination is not support, destinationExpr:$destinationExpr, destinations:$destination"
  100. exit 1;
  101. fi
  102. else
  103. if [ "$destination" != "" ] && [ "$destination" != "example" ] ; then
  104. if [ -d /home/admin/canal-server/conf/example ]; then
  105. mv /home/admin/canal-server/conf/example /home/admin/canal-server/conf/$destination
  106. fi
  107. fi
  108. fi
  109. su admin -c 'cd /home/admin/canal-server/bin/ && sh restart.sh 1>>/tmp/start.log 2>&1'
  110. sleep 5
  111. #check start
  112. checkStart "canal" "nc 127.0.0.1 $metricsPort -w 1 -zv 2> /tmp/nc.out && cat /tmp/nc.out | grep -c Connected" 30
  113. fi
  114. }
  115. function splitDestinations() {
  116. holdExample="false"
  117. prefix=''
  118. array=()
  119. if [[ "$1" == '1' ]] ; then
  120. echo "split destinations expr "$2
  121. prefix=$(echo $2 | sed 's/{.*//')
  122. num=$(echo $2 | sed 's/.*{//;s/}//;s/-/ /')
  123. array=($(seq $num))
  124. else
  125. echo "split destinations "$2
  126. array=(${2//,/ })
  127. fi
  128. for var in ${array[@]}
  129. do
  130. cp -r /home/admin/canal-server/conf/example /home/admin/canal-server/conf/$prefix$var
  131. chown admin:admin -R /home/admin/canal-server/conf/$prefix$var
  132. if [[ "$prefix$var" = 'example' ]] ; then
  133. holdExample="true"
  134. fi
  135. done
  136. if [[ "$holdExample" != 'true' ]] ; then
  137. rm -rf /home/admin/canal-server/conf/example
  138. fi
  139. }
  140. function stop_canal() {
  141. echo "stop canal"
  142. su admin -c 'cd /home/admin/canal-server/bin/ && sh stop.sh 1>>/tmp/start.log 2>&1'
  143. echo "stop canal successful ..."
  144. }
  145. function start_exporter() {
  146. su admin -c 'cd /home/admin/node_exporter && ./node_exporter 1>>/tmp/start.log 2>&1 &'
  147. }
  148. function stop_exporter() {
  149. su admin -c 'killall node_exporter'
  150. }
  151. echo "==> START ..."
  152. start_exporter
  153. start_canal
  154. echo "==> START SUCCESSFUL ..."
  155. tail -f /dev/null &
  156. # wait TERM signal
  157. waitterm
  158. echo "==> STOP"
  159. stop_canal
  160. stop_exporter
  161. echo "==> STOP SUCCESSFUL ..."