hue.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/bin/bash
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with 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. # wrapper script to identify the latest python and pass the execution
  18. # to the corresponding hue script in the `build` directory.
  19. set -ex
  20. # Time marker for both stderr and stdout
  21. date; date 1>&2
  22. export SCRIPT_DIR=`dirname $0`
  23. export HUE_HOME_DIR=$(dirname $(dirname "$SCRIPT_DIR"))
  24. source $SCRIPT_DIR/python/python_helper.sh
  25. PYTHON_BIN="${HUE_HOME_DIR}/$(latest_venv_bin_path)/python"
  26. HUE="${HUE_HOME_DIR}/$(latest_venv_bin_path)/hue"
  27. HUE_LOGLISTENER="${HUE_HOME_DIR}/desktop/core/src/desktop/loglistener.py"
  28. function set_path() {
  29. etpath=$(dirname $2)
  30. if [ "$1" == "PYTHONPATH" ]; then
  31. if [ -z ${PYTHONPATH:+x} ]; then
  32. # Set only if PYTHONPATH is not set to allow for safety valve
  33. export PYTHONPATH=$etpath
  34. fi
  35. elif [ "$1" == "LD_LIBRARY_PATH" ]; then
  36. if [ -z ${LD_LIBRARY_PATH:+x} ]; then
  37. # Set only if LD_LIBRARY_PATH is not set to allow for safety valve
  38. export LD_LIBRARY_PATH=$etpath
  39. fi
  40. elif [ "$1" == "PATH" ]; then
  41. if [ -z ${PATH:+x} ]; then
  42. # Set only if PATH is not set to allow for safety valve
  43. export PATH=$etpath
  44. else
  45. export PATH=$PATH:$etpath
  46. fi
  47. fi
  48. }
  49. # This function sets the ENVIRONMENT variable, it has 3 args
  50. # 1. target file, 2. environment variable name
  51. # 3. list of possible places where target can be found
  52. # It iterates over list and tries matching list item and target
  53. # on the filesystem.
  54. function set_env_var() {
  55. target=$1
  56. shift
  57. setvar=$1
  58. shift
  59. search=("$@")
  60. targetpath=""
  61. # Dont use "echo" in the function as last line returns value
  62. for spath in ${search[@]}; do
  63. tpath=""
  64. if echo x"$target" | grep '*' > /dev/null || \
  65. echo x"$spath" | grep '*' > /dev/null; then
  66. # sort with newone first
  67. glob_path=$(ls -1td $spath/$target 2>/dev/null|head -1)
  68. if [ -z ${glob_path:+x} ]; then
  69. tpath=""
  70. else
  71. tpath=$glob_path
  72. fi
  73. else
  74. if [ -e "$spath/$target" ]; then
  75. tpath="$spath/$target"
  76. fi
  77. fi
  78. if [ "$tpath" != "" ]; then
  79. targetpath=$tpath
  80. set_path $setvar $targetpath
  81. break
  82. fi
  83. done
  84. echo $targetpath
  85. }
  86. add_postgres_to_pythonpath_for_version() {
  87. local version="$1" # e.g. "3.8", "3.9", "3.10"
  88. # If postgres engine is detected in *.ini files then check for proper psycopg2 version.
  89. if grep -q '^\s*engine\s*=\s*postgres\+' *.ini; then
  90. if [ -z ${LD_LIBRARY_PATH:+x} ]; then
  91. list=("/usr/lib*" "/usr/local/lib/python${version}/*-packages" \
  92. "/usr/lib64/python${version}/*-packages" \
  93. "/opt/rh/rh-postgresql*/root/usr/lib64" \
  94. "/usr/pgsql-*/lib" \
  95. )
  96. set_env_var "libpq.so" "LD_LIBRARY_PATH" "${list[@]}"
  97. else
  98. echo "LD_LIBRARY_PATH is taken from safety valve, $LD_LIBRARY_PATH"
  99. fi
  100. if [ -z ${PYTHONPATH:+x} ]; then
  101. # If we've included a PSYCOPG2 for this platform, override on PYTHONPATH
  102. list=("/usr/bin" "/opt/rh/rh-python${version//./}/root/usr/local/lib64/python${version}/site-packages" \
  103. "/usr/local/lib/python${version}/*-packages" \
  104. "/usr/lib64/python${version}/*-packages" \
  105. )
  106. set_env_var "psycopg2" "PYTHONPATH" "${list[@]}"
  107. else
  108. echo "PYTHONPATH is taken from safety valve, $PYTHONPATH"
  109. fi
  110. fi
  111. }
  112. add_mysql_to_pythonpath_for_version() {
  113. local version="$1" # e.g. "3.8", "3.9", "3.10"
  114. # If mysql engine is detected in *.ini files then check for proper MySQL-python version.
  115. if grep -q '^\s*engine\s*=\s*mysql\+' *.ini; then
  116. if [ -z ${LD_LIBRARY_PATH:+x} ]; then
  117. list=("/usr/lib*" "/usr/local/lib/python${version}/*-packages" \
  118. "/usr/lib64/python${version}/*-packages" \
  119. "/opt/rh/rh-mysql*/root/usr/lib64" \
  120. "/usr/lib64/mysql" \
  121. )
  122. set_env_var "libmysqlclient.so" "LD_LIBRARY_PATH" "${list[@]}"
  123. else
  124. echo "LD_LIBRARY_PATH is taken from safety valve, $LD_LIBRARY_PATH"
  125. fi
  126. if [ -z ${PYTHONPATH:+x} ]; then
  127. # If we've included a MySQL-python for this platform, override on PYTHONPATH
  128. list=("/usr/bin" "/opt/rh/rh-python${version//./}/root/usr/local/lib64/python${version}/site-packages" \
  129. "/usr/local/lib/python${version}/*-packages" \
  130. "/usr/lib64/python${version}/*-packages" \
  131. )
  132. set_env_var "MySQLdb" "PYTHONPATH" "${list[@]}"
  133. else
  134. echo "PYTHONPATH is taken from safety valve, $PYTHONPATH"
  135. fi
  136. fi
  137. }
  138. add_postgres_to_pythonpath_for_version "$(selected_python_version)"
  139. add_mysql_to_pythonpath_for_version "$(selected_python_version)"
  140. function stop_previous_hueprocs() {
  141. for p in $(cat /tmp/hue_${HUE_PORT}.pid); do
  142. if [[ $p -eq $(ps -p $p -ho pid=) ]]; then
  143. kill -9 $p
  144. fi
  145. done
  146. }
  147. function run_syncdb_and_migrate_subcommands() {
  148. "$HUE" makemigrations --noinput
  149. "$HUE" migrate --fake-initial
  150. }
  151. if [[ "dumpdata" == "$1" ]]; then
  152. umask 037
  153. "$HUE" "$1" --indent 2 > "$2"
  154. elif [[ "syncdb" == "$1" ]]; then
  155. run_syncdb_and_migrate_subcommands
  156. elif [[ "ldaptest" == "$1" ]]; then
  157. "$HUE" "$1"
  158. elif [[ "runcpserver" == "$1" ]]; then
  159. run_syncdb_and_migrate_subcommands
  160. exec "$HUE" "runcpserver"
  161. elif [[ "rungunicornserver" == "$1" ]]; then
  162. stop_previous_hueprocs
  163. exec "$PYTHON_BIN" "$HUE_LOGLISTENER" &
  164. echo $! > /tmp/hue_${HUE_PORT}.pid
  165. run_syncdb_and_migrate_subcommands
  166. exec "$HUE" "rungunicornserver"
  167. else
  168. exec "$HUE" "$@"
  169. fi