startup.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/bin/bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. set -e
  19. export DOLPHINSCHEDULER_BIN=${DOLPHINSCHEDULER_HOME}/bin
  20. export MASTER_START_ENABLED=false
  21. export WORKER_START_ENABLED=false
  22. export API_START_ENABLED=false
  23. export ALERT_START_ENABLED=false
  24. export LOGGER_START_ENABLED=false
  25. # wait database
  26. waitDatabase() {
  27. echo "test ${DATABASE_TYPE} service"
  28. while ! nc -z ${DATABASE_HOST} ${DATABASE_PORT}; do
  29. local counter=$((counter+1))
  30. if [ $counter == 30 ]; then
  31. echo "Error: Couldn't connect to ${DATABASE_TYPE}."
  32. exit 1
  33. fi
  34. echo "Trying to connect to ${DATABASE_TYPE} at ${DATABASE_HOST}:${DATABASE_PORT}. Attempt $counter."
  35. sleep 5
  36. done
  37. echo "connect ${DATABASE_TYPE} service"
  38. if [ ${DATABASE_TYPE} = "mysql" ]; then
  39. v=$(mysql -h${DATABASE_HOST} -P${DATABASE_PORT} -u${DATABASE_USERNAME} --password=${DATABASE_PASSWORD} -D ${DATABASE_DATABASE} -e "select 1" 2>&1)
  40. if [ "$(echo ${v} | grep 'ERROR' | wc -l)" -eq 1 ]; then
  41. echo "Error: Can't connect to database...${v}"
  42. exit 1
  43. fi
  44. else
  45. v=$(PGPASSWORD=${DATABASE_PASSWORD} psql -h ${DATABASE_HOST} -p ${DATABASE_PORT} -U ${DATABASE_USERNAME} -d ${DATABASE_DATABASE} -tAc "select 1")
  46. if [ "$(echo ${v} | grep 'FATAL' | wc -l)" -eq 1 ]; then
  47. echo "Error: Can't connect to database...${v}"
  48. exit 1
  49. fi
  50. fi
  51. }
  52. # init database
  53. initDatabase() {
  54. echo "import sql data"
  55. ${DOLPHINSCHEDULER_HOME}/script/create-dolphinscheduler.sh
  56. }
  57. # check ds version
  58. checkDSVersion() {
  59. if [ ${DATABASE_TYPE} = "mysql" ]; then
  60. v=$(mysql -h${DATABASE_HOST} -P${DATABASE_PORT} -u${DATABASE_USERNAME} --password=${DATABASE_PASSWORD} -D ${DATABASE_DATABASE} -e "SELECT * FROM public.t_ds_version" 2>/dev/null)
  61. else
  62. v=$(PGPASSWORD=${DATABASE_PASSWORD} psql -h ${DATABASE_HOST} -p ${DATABASE_PORT} -U ${DATABASE_USERNAME} -d ${DATABASE_DATABASE} -tAc "SELECT * FROM public.t_ds_version" 2>/dev/null)
  63. fi
  64. if [ -n "$v" ]; then
  65. echo "ds version: $v"
  66. return 0
  67. else
  68. return 1
  69. fi
  70. }
  71. # check init database
  72. checkInitDatabase() {
  73. echo "check init database"
  74. while ! checkDSVersion; do
  75. local counter=$((counter+1))
  76. if [ $counter == 30 ]; then
  77. echo "Error: Couldn't check init database."
  78. exit 1
  79. fi
  80. echo "Trying to check init database. Attempt $counter."
  81. sleep 5
  82. done
  83. }
  84. # wait zk
  85. waitZK() {
  86. echo "connect remote zookeeper"
  87. echo "${ZOOKEEPER_QUORUM}" | awk -F ',' 'BEGIN{ i=1 }{ while( i <= NF ){ print $i; i++ } }' | while read line; do
  88. while ! nc -z ${line%:*} ${line#*:}; do
  89. local counter=$((counter+1))
  90. if [ $counter == 30 ]; then
  91. echo "Error: Couldn't connect to zookeeper."
  92. exit 1
  93. fi
  94. echo "Trying to connect to zookeeper at ${line}. Attempt $counter."
  95. sleep 5
  96. done
  97. done
  98. }
  99. # print usage
  100. printUsage() {
  101. echo -e "Dolphin Scheduler is a distributed and easy-to-expand visual DAG workflow scheduling system,"
  102. echo -e "dedicated to solving the complex dependencies in data processing, making the scheduling system out of the box for data processing.\n"
  103. echo -e "Usage: [ all | master-server | worker-server | api-server | alert-server ]\n"
  104. printf "%-13s: %s\n" "all" "Run master-server, worker-server, api-server and alert-server"
  105. printf "%-13s: %s\n" "master-server" "MasterServer is mainly responsible for DAG task split, task submission monitoring."
  106. printf "%-13s: %s\n" "worker-server" "WorkerServer is mainly responsible for task execution and providing log services."
  107. printf "%-13s: %s\n" "api-server" "ApiServer is mainly responsible for processing requests and providing the front-end UI layer."
  108. printf "%-13s: %s\n" "alert-server" "AlertServer mainly include Alarms."
  109. }
  110. # init config file
  111. source /root/startup-init-conf.sh
  112. case "$1" in
  113. (all)
  114. waitZK
  115. waitDatabase
  116. initDatabase
  117. export MASTER_START_ENABLED=true
  118. export WORKER_START_ENABLED=true
  119. export API_START_ENABLED=true
  120. export ALERT_START_ENABLED=true
  121. export LOGGER_START_ENABLED=true
  122. ;;
  123. (master-server)
  124. waitZK
  125. waitDatabase
  126. export MASTER_START_ENABLED=true
  127. ;;
  128. (worker-server)
  129. waitZK
  130. waitDatabase
  131. export WORKER_START_ENABLED=true
  132. export LOGGER_START_ENABLED=true
  133. ;;
  134. (api-server)
  135. waitZK
  136. waitDatabase
  137. initDatabase
  138. export API_START_ENABLED=true
  139. ;;
  140. (alert-server)
  141. waitDatabase
  142. checkInitDatabase
  143. export ALERT_START_ENABLED=true
  144. ;;
  145. (help)
  146. printUsage
  147. exit 1
  148. ;;
  149. (*)
  150. printUsage
  151. exit 1
  152. ;;
  153. esac
  154. # init directories
  155. mkdir -p ${DOLPHINSCHEDULER_HOME}/logs
  156. # start supervisord
  157. supervisord -n -u root