hue.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/bin/bash
  2. # Copyright (c) 2019 Cloudera, Inc. All rights reserved.
  3. set -x
  4. # Time marker for both stderr and stdout
  5. date; date 1>&2
  6. export DESKTOP_LOG_DIR="/opt/hive/logs"
  7. export PYTHON_EGG_CACHE=$HUE_CONF_DIR/.python-eggs
  8. export SERVER_SOFTWARE="apache"
  9. function prepare_huedb() {
  10. (
  11. flock -x 124
  12. $HUE_BIN/hue syncdb --noinput
  13. $HUE_BIN/hue makemigrations --noinput --merge
  14. $HUE_BIN/hue migrate
  15. ) 124>$HUE_CONF_DIR/hue.lock
  16. }
  17. function db_connectivity_check() {
  18. i=1
  19. ret="fail"
  20. # perform db connectivity check for at least 60 times,
  21. # before give up
  22. while [[ $i -lt 61 ]]; do
  23. # Run hue dbshell, if successful then one should not see
  24. # psql in the output.
  25. dbshell_output=$(echo "\q"|$HUE_BIN/hue dbshell 2>&1)
  26. error_line=$(echo $dbshell_output|grep psql)
  27. if [[ $error_line -eq 0 ]]; then
  28. echo "Successful db connectivity check"
  29. ret="success"
  30. break
  31. fi
  32. i=$((i+1))
  33. # gradually increase sleep time
  34. sleep $i
  35. echo "Failing db connectivity error: " $i " " $dbshell_output
  36. done
  37. echo "$ret"
  38. }
  39. # In DWX, there are possibilities where
  40. # VW A/Hue Server A may run older version of django-axes-4.5.4 module
  41. # VW B/Hue Server B may run higher version of django-axes-5.13.0 module
  42. # Below function fix axes version issue with column issues
  43. function fix_column_issue() {
  44. i=1
  45. tablename=$1
  46. columnname=$2
  47. while [[ $i -lt 6 ]]; do
  48. echo "Peforming check table \"${tablename}\", column \"${columnname}\""
  49. cexist=$(echo "select column_name from INFORMATION_SCHEMA.COLUMNS \
  50. where table_name='${tablename}';"|$HUE_BIN/hue dbshell 2>&1| \
  51. grep "${columnname}")
  52. if [[ "$cexist" =~ "$columnname" ]]; then
  53. echo "Checked column \"${columnname}\" exist in table \"${tablename}\" seen as \"${cexist}\""
  54. break
  55. else
  56. echo "Altering table \"${tablename}\", adding column \"${columnname}\""
  57. column_add=$(echo "ALTER TABLE ${tablename} ADD COLUMN ${columnname} bool \
  58. NOT NULL DEFAULT false;"|$HUE_BIN/hue dbshell 2>&1)
  59. if [[ "$column_add" == "ALTER TABLE"* ]]; then
  60. echo "Fixed table \"${tablename}\", added column \"${columnname}\""
  61. break
  62. fi
  63. fi
  64. i=$((i+1))
  65. sleep 1
  66. echo "Failing db connectivity error: " $i " " $cexist
  67. done
  68. }
  69. function set_samlcert() {
  70. mkdir -pm 755 $HUE_CONF_DIR/samlcert
  71. cd $HUE_CONF_DIR/samlcert
  72. export RANDFILE=$HUE_CONF_DIR/samlcert/.rnd
  73. CNAME=""
  74. if [[ "$EXTERNAL_HOST" =~ .*"localhost".* ]]; then
  75. CNAME=$EXTERNAL_HOST
  76. else
  77. LASTWORD=$(echo $EXTERNAL_HOST | awk -F'[.=]' '{print $NF}')
  78. SECONDWORD=$(echo $EXTERNAL_HOST | awk -F'[.=]' '{print $(NF-1)}')
  79. if [ ! -z ${LASTWORD} ]; then
  80. CNAME="$SECONDWORD.$LASTWORD"
  81. else
  82. CNAME=$EXTERNAL_HOST
  83. fi
  84. fi
  85. if [ -z ${PASSPHRASE+x} ]; then
  86. openssl genrsa -des3 -passout pass:cloudera -out server.pass.key 2048
  87. openssl rsa -inform PEM -outform PEM -passin pass:cloudera -in server.pass.key -out server.key
  88. openssl req -new -key server.key -out server.csr -subj "/C=US/ST=California/L=Palo Alto/O=Cloudera/OU=Hue/CN=$CNAME"
  89. openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
  90. else
  91. echo "#!/bin/bash" > $HUE_CONF_DIR/samlcert/pass.key
  92. echo "echo \$PASSPHRASE" >> $HUE_CONF_DIR/samlcert/pass.key
  93. chmod a+x $HUE_CONF_DIR/samlcert/pass.key
  94. openssl genrsa -des3 -passout pass:$PASSPHRASE -out server.key 2048
  95. openssl req -new -key server.key -out server.csr -passin pass:$PASSPHRASE -subj "/C=US/ST=California/L=Palo Alto/O=Cloudera/OU=Hue/CN=$CNAME"
  96. openssl x509 -req -days 365 -in server.csr -passin pass:$PASSPHRASE -signkey server.key -out server.crt
  97. fi
  98. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib64
  99. }
  100. # If database connectivity is not set then fail
  101. ret=$(db_connectivity_check)
  102. if [[ $ret == "fail" ]]; then
  103. exit 1
  104. fi
  105. # prepare db schema, run it in single instance flock mode
  106. prepare_huedb
  107. if [[ $1 == kt_renewer ]]; then
  108. if [ -e "/etc/hue/conf/kerberos.ini" ]; then
  109. # The Kerberos ticket renewer role needs to know where kinit is.
  110. KINIT_PATH=`which kinit`
  111. KINIT_PATH=${KINIT_PATH-/usr/bin/kinit}
  112. $HUE_BIN/hue kt_renewer
  113. fi
  114. elif [[ $1 == rungunicornserver ]]; then
  115. if [ -e "/etc/hue/conf/saml.ini" ]; then
  116. set_samlcert
  117. fi
  118. fix_column_issue "axes_accesslog" "trusted"
  119. fix_column_issue "axes_accessattempt" "trusted"
  120. $HUE_BIN/hue rungunicornserver
  121. fi
  122. exit 0