hue_impala_post.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #parse command line arguments
  18. parse_arguments()
  19. {
  20. # Test that we're using compatible getopt version.
  21. getopt -T > /dev/null
  22. if [[ $? -ne 4 ]]; then
  23. echo "Incompatible getopt version."
  24. exit 1
  25. fi
  26. HUE_AUTH_SERVER="LDAP"
  27. GETOPT=`getopt -n $0 -o u:,w:,s:,p:,e,v,h \
  28. -l user:,password:,server:,port:,enablessl,verbose,help \
  29. -- "$@"`
  30. eval set -- "$GETOPT"
  31. while true;
  32. do
  33. case "$1" in
  34. -u|--user)
  35. HUE_USER=$2
  36. shift 2
  37. ;;
  38. -w|--password)
  39. HUE_PASSWORD=$2
  40. shift 2
  41. ;;
  42. -s|--server)
  43. HUE_SERVER=$2
  44. shift 2
  45. ;;
  46. -p|--port)
  47. HUE_PORT=$2
  48. shift 2
  49. ;;
  50. -e|--enablessl)
  51. ENABLESSL=1
  52. shift
  53. ;;
  54. -v|--verbose)
  55. VERBOSE=1
  56. shift
  57. ;;
  58. --)
  59. shift
  60. break
  61. ;;
  62. *)
  63. usage
  64. exit 1
  65. ;;
  66. esac
  67. done
  68. #
  69. if [[ -z ${HUE_USER} ]]
  70. then
  71. HUE_USER="admin"
  72. fi
  73. if [[ -z ${HUE_PASSWORD} ]]
  74. then
  75. HUE_PASSWORD="admin"
  76. fi
  77. if [[ -z ${HUE_SERVER} ]]
  78. then
  79. HUE_SERVER="localhost"
  80. fi
  81. if [[ -z ${HUE_PORT} ]]
  82. then
  83. HUE_PORT="8888"
  84. fi
  85. }
  86. usage()
  87. {
  88. cat << EOF
  89. usage: $0 [options]
  90. Collects troubleshooting data from Hue server:
  91. OPTIONS
  92. -u|--user Hue username - default admin.
  93. -w|--password Hue password - default admin.
  94. -s|--server Hue server host - localhost.
  95. -p|--port Hue server port - 8888.
  96. -h|--help Show this message.
  97. EOF
  98. }
  99. main()
  100. {
  101. parse_arguments "$@"
  102. if [[ -z ${ENABLESSL} ]]
  103. then
  104. HUE_HTTP="http"
  105. else
  106. HUE_HTTP="https"
  107. fi
  108. HUE_PASS_URL="${HUE_HTTP}://${HUE_SERVER}:${HUE_PORT}/accounts/login/"
  109. HUE_IMPALA_PARAMS_URL="${HUE_HTTP}://${HUE_SERVER}:${HUE_PORT}/impala/api/query/parameters"
  110. POST_STRING="query-query=drop+table+test1%3B&query-database=default&settings-next_form_id=0&file_resources-next_form_id=0&functions-next_form_id=0&query-email_notify=false&query-is_parameterized=true"
  111. hue_login
  112. echo "Posting Impala Params:"
  113. do_curl \
  114. POST \
  115. "${HUE_IMPALA_PARAMS_URL}" \
  116. -H "content-type:application/x-www-form-urlencoded" -d "${POST_STRING}"
  117. }
  118. function do_curl() {
  119. METHOD=$1
  120. shift
  121. URL=$1
  122. shift
  123. ARGS=$@
  124. CURL=$(which curl)
  125. if [ -z ${COOKIE_JAR} ]
  126. then
  127. COOKIE_JAR=/tmp/cookie.jar
  128. fi
  129. if [ -f ${COOKIE_JAR} ]
  130. then
  131. CSRF_TOKEN=`grep ${HUE_SERVER} ${COOKIE_JAR} | grep csrftoken | cut -f 7`
  132. fi
  133. if [ ! -f ${CURL} ]
  134. then
  135. echo "curl not found, unable to run any curl commands"
  136. else
  137. debug "Connecting to ${URL}"
  138. debug "${CURL} \
  139. ${CURL_OPTS} \
  140. -k \
  141. -e \"${HUE_HTTP}://${HUE_SERVER}:${HUE_PORT}/\" \
  142. -b @${COOKIE_JAR} \
  143. -c ${COOKIE_JAR} \
  144. -H \"X-CSRFToken: ${CSRF_TOKEN}\" \
  145. -X ${METHOD} \
  146. -f \
  147. ${URL} \
  148. ${ARGS}"
  149. ${CURL} \
  150. ${CURL_OPTS} \
  151. -k \
  152. -e "${HUE_HTTP}://${HUE_SERVER}:${HUE_PORT}/" \
  153. -b @${COOKIE_JAR} \
  154. -c ${COOKIE_JAR} \
  155. -H "X-CSRFToken: ${CSRF_TOKEN}" \
  156. -X ${METHOD} \
  157. -f \
  158. ${URL} \
  159. ${ARGS}
  160. fi
  161. }
  162. function hue_login() {
  163. echo "Login to Hue to get Cookie:"
  164. do_curl \
  165. GET \
  166. "${HUE_PASS_URL}" \
  167. -L 2>&1 > /dev/null
  168. do_curl \
  169. POST \
  170. "${HUE_PASS_URL}" \
  171. -F username=${HUE_USER} -F password="${HUE_PASSWORD}" -F server="${HUE_AUTH_SERVER}" 2>&1 > /dev/null
  172. }
  173. debug()
  174. {
  175. if [[ ! -z $VERBOSE ]]
  176. then
  177. echo "$1"
  178. fi
  179. }
  180. main "$@"