hue_dump_user.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #Test to search for doc1 and doc2
  18. #parse command line arguments
  19. parse_arguments()
  20. {
  21. # Test that we're using compatible getopt version.
  22. getopt -T > /dev/null
  23. if [[ $? -ne 4 ]]; then
  24. echo "Incompatible getopt version."
  25. exit 1
  26. fi
  27. # Parse short and long option parameters.
  28. OVERRIDE=
  29. USERNAME=
  30. VERBOSE=
  31. DESKTOP_DEBUG=false
  32. GETOPT=`getopt -n $0 -o o,u:,v,h \
  33. -l override,username:,verbose,help \
  34. -- "$@"`
  35. eval set -- "$GETOPT"
  36. while true;
  37. do
  38. case "$1" in
  39. -o|--override)
  40. OVERRIDE=true
  41. shift
  42. ;;
  43. -u|--username)
  44. USERNAME=$2
  45. shift 2
  46. ;;
  47. -v|--verbose)
  48. VERBOSE=true
  49. DESKTOP_DEBUG=true
  50. shift
  51. ;;
  52. --)
  53. shift
  54. break
  55. ;;
  56. *)
  57. usage
  58. exit 1
  59. ;;
  60. esac
  61. done
  62. #
  63. }
  64. usage()
  65. {
  66. cat << EOF
  67. usage: $0 [options]
  68. Exports all user objects:
  69. OPTIONS
  70. -o|--override Allow script to run as non-root, must set HUE_CONF_DIR manually before running
  71. -u|--username <user> User to export objects from.
  72. -v|--verbose Verbose logging, off by default
  73. -h|--help Show this message.
  74. EOF
  75. }
  76. main()
  77. {
  78. parse_arguments "$@"
  79. SCRIPT_DIR="$( cd -P "$( dirname "$0" )" && pwd )"
  80. PYTHONPATH=${SCRIPT_DIR}/lib:${PYTHONPATH}
  81. export SCRIPT_DIR PYTHONPATH
  82. #SET IMPORTANT ENV VARS
  83. if [[ -z ${HUE_CONF_DIR} ]]
  84. then
  85. if [ -d "/var/run/cloudera-scm-agent/process" ]
  86. then
  87. HUE_CONF_DIR="/var/run/cloudera-scm-agent/process/`ls -1 /var/run/cloudera-scm-agent/process | grep HUE_SERVER | sort -n | tail -1 `"
  88. else
  89. HUE_CONF_DIR="/etc/hue/conf"
  90. fi
  91. export HUE_CONF_DIR
  92. fi
  93. if [[ ! ${USER} =~ .*root* ]]
  94. then
  95. if [[ -z ${OVERRIDE} ]]
  96. then
  97. echo "Script must be run as root: exiting"
  98. exit 1
  99. fi
  100. else
  101. if [[ $(ps -ef | grep "[h]ue runc" | awk '{print }') ]]
  102. then
  103. DESKTOP_LOG_DIR=$(strings /proc/$(ps -ef | grep "[h]ue runc" | awk '{print }' | awk '{print $2}')/environ | grep DESKTOP_LOG_DIR | awk -F\= '{print $2}')
  104. fi
  105. fi
  106. if [[ -z ${DESKTOP_LOG_DIR} ]]
  107. then
  108. DESKTOP_LOG_DIR=${HUE_CONF_DIR}/logs
  109. fi
  110. if [[ ! -f ${DESKTOP_LOG_DIR} ]]
  111. then
  112. mkdir -p ${DESKTOP_LOG_DIR}
  113. fi
  114. LOG_FILE=${DESKTOP_LOG_DIR}/`basename "$0" | awk -F\. '{print $1}'`.log
  115. LOG_ROTATE_SIZE=10 #MB before rotating, size in MB before rotating log to .1
  116. LOG_ROTATE_COUNT=5 #number of log files, so 20MB max
  117. PARCEL_DIR=/opt/cloudera/parcels/CDH
  118. if [ ! -d "/usr/lib/hadoop" ]
  119. then
  120. CDH_HOME=$PARCEL_DIR
  121. else
  122. CDH_HOME=/usr
  123. fi
  124. if [ -d "${CDH_HOME}/lib/hue/build/env/bin" ]
  125. then
  126. COMMAND="${CDH_HOME}/lib/hue/build/env/bin/hue shell"
  127. TEST_COMMAND="${CDH_HOME}/lib/hue/build/env/bin/hue dbshell"
  128. else
  129. COMMAND="${CDH_HOME}/share/hue/build/env/bin/hue shell"
  130. TEST_COMMAND="${CDH_HOME}/share/hue/build/env/bin/hue dbshell"
  131. fi
  132. ORACLE_ENGINE_CHECK=$(grep engine ${HUE_CONF_DIR}/hue* | grep -i oracle)
  133. if [[ ! -z ${ORACLE_ENGINE_CHECK} ]]
  134. then
  135. if [[ -z ${ORACLE_HOME} ]]
  136. then
  137. ORACLE_PARCEL=/opt/cloudera/parcels/ORACLE_INSTANT_CLIENT/instantclient_11_2
  138. if [[ -d ${ORACLE_PARCEL} ]]
  139. then
  140. ORACLE_HOME=${ORACLE_PARCEL}
  141. LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${ORACLE_HOME}
  142. export LD_LIBRARY_PATH ORACLE_HOME
  143. fi
  144. fi
  145. if [[ -z ${ORACLE_HOME} ]]
  146. then
  147. echo "It looks like you are using Oracle as your backend"
  148. echo "ORACLE_HOME must be set to the correct Oracle client"
  149. echo "before running this script"
  150. exit 1
  151. fi
  152. fi
  153. HUE_IGNORE_PASSWORD_SCRIPT_ERRORS=1
  154. if [[ -z ${HUE_DATABASE_PASSWORD} ]]
  155. then
  156. echo "CDH 5.5 and above requires that you set the environment variable:"
  157. echo "HUE_DATABASE_PASSWORD=<dbpassword>"
  158. exit 1
  159. fi
  160. export CDH_HOME COMMAND HUE_IGNORE_PASSWORD_SCRIPT_ERRORS
  161. # echo "COMMAND: echo \"from django.db import connection; cursor = connection.cursor(); cursor.execute('select count(*) from auth_user')\" | ${TEST_COMMAND}" | tee -a ${LOG_FILE}
  162. # echo "from django.db import connection; cursor = connection.cursor(); cursor.execute('select count(*) from auth_user')" | ${TEST_COMMAND} | tee -a ${LOG_FILE}
  163. if [[ $? -ne 0 ]]
  164. then
  165. echo "DB connect test did not work, HUE_DATABASE_PASSWORD may not be correct"
  166. echo "If the next query test fails check password in CM: http://<cmhostname>:7180/api/v5/cm/deployment and search for HUE_SERVER and database to find correct password"
  167. fi
  168. # ${COMMAND} <<EOF 2>&1 > /dev/null
  169. ${COMMAND} <<EOF
  170. username = "${USERNAME}"
  171. LOGFILE = "${LOG_FILE}"
  172. logrotatesize=${LOG_ROTATE_SIZE}
  173. backupcount=${LOG_ROTATE_COUNT}
  174. import logging
  175. import logging.handlers
  176. from django.contrib.auth.models import User
  177. import customdumpdata
  178. LOG = logging.getLogger()
  179. format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
  180. fh = logging.handlers.RotatingFileHandler(LOGFILE, maxBytes = (1048576*logrotatesize), backupCount = backupcount)
  181. fh.setFormatter(format)
  182. LOG.addHandler(fh)
  183. LOG.setLevel(logging.INFO)
  184. user = User.objects.get(username = username)
  185. print ""
  186. print ""
  187. customdumpdata.Command().handle_noargs('desktop.Document', 'desktop.Document2', 'beeswax.SavedQuery', 'beeswax.QueryHistory', format='json', indent=2, exclude=[], use_natural_keys=True, use_base_manager=False, user=user)
  188. EOF
  189. }
  190. main "$@"