hue_restart_cm.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #Restarts Hue
  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. CM_HOSTNAME="localhost"
  29. CM_PORT="7180"
  30. CM_HTTP="http"
  31. CM_API="v11"
  32. CM_USERNAME="admin"
  33. CM_PASSWORD_INPUT=
  34. ENCODE_LOCATION=/var/lib/hue
  35. GETOPT=`getopt -n $0 -o c:,p:,u:,w:,n,s,l:,h \
  36. -l cmhost:,cmport:,cmuser:,cmpass:,newpass,ssl,encodeloc:,help \
  37. -- "$@"`
  38. eval set -- "$GETOPT"
  39. while true;
  40. do
  41. case "$1" in
  42. -c|--cmhost)
  43. CM_HOSTNAME=$2
  44. shift 2
  45. ;;
  46. -p|--cmport)
  47. CM_PORT=$2
  48. shift 2
  49. ;;
  50. -u|--cmuser)
  51. CM_USERNAME=$2
  52. shift 2
  53. ;;
  54. -w|--cmpass)
  55. CM_PASSWORD_INPUT=$2
  56. shift 2
  57. ;;
  58. -n|--newpass)
  59. NEW_PASS=1
  60. shift
  61. ;;
  62. -s|--ssl)
  63. CM_HTTP="https"
  64. shift
  65. ;;
  66. -l|--encodeloc)
  67. ENCODE_LOCATION=$2
  68. shift 2
  69. ;;
  70. --)
  71. shift
  72. break
  73. ;;
  74. *)
  75. usage
  76. exit 1
  77. ;;
  78. esac
  79. done
  80. #
  81. ENC_PASSWORD_FILE=${ENCODE_LOCATION}/`basename "$0" | awk -F\. '{print $1}'`.enc
  82. }
  83. usage() {
  84. cat << EOF
  85. usage: $0 [options]
  86. Restarts Hue instances with high memory utilization through CM:
  87. OPTIONS
  88. -c|--cmhost <hostname> Host where CM is running - default localhost.
  89. -p|--cmport <port> Port CM is running on - default ${CM_PORT}.
  90. -u|--cmuser <cm_user> Admin User in CM - default admin.
  91. -w|--cmpass <user_pass> Admin User password in CM, required on first run, no default. Will prompt
  92. if not provided through this flag. Future runs will use
  93. encrypted version in <enc_loc>/`basename "$0" | awk -F\. '{print $1}'`.enc
  94. -s|--ssl Enable SSL.
  95. -n|--newpass Prompt for a new password.
  96. -l|--encodeloc <enc_loc> Location to store encoded password in file - default /var/lib/hue.
  97. -v|--verbose Enable verbose logging.
  98. -h|--help Show this message.
  99. EOF
  100. }
  101. main() {
  102. parse_arguments "$@"
  103. if [[ ! ${USER} =~ .*root.* ]]
  104. then
  105. echo "Script must be run as root: exiting"
  106. exit 1
  107. fi
  108. if [[ ! -d ${ENCODE_LOCATION} ]]
  109. then
  110. mkdir -p ${ENCODE_LOCATION}
  111. fi
  112. if [[ ! -z ${CM_PASSWORD_INPUT} ]]
  113. then
  114. echo ${CM_PASSWORD_INPUT} | base64 > ${ENC_PASSWORD_FILE}
  115. chown root:root ${ENC_PASSWORD_FILE}
  116. chmod 600 ${ENC_PASSWORD_FILE}
  117. fi
  118. if [[ -z ${CM_PASSWORD_INPUT} ]]
  119. then
  120. if [[ ! -f ${ENC_PASSWORD_FILE} ]] || [[ ! -z ${NEW_PASS} ]]
  121. then
  122. message "CM Admin user password required on first run"
  123. read -s -p "Please enter password:" CM_PASSWORD_INPUT
  124. echo "New password provided"
  125. echo ${CM_PASSWORD_INPUT} | base64 > ${ENC_PASSWORD_FILE}
  126. chown root:root ${ENC_PASSWORD_FILE}
  127. chmod 600 ${ENC_PASSWORD_FILE}
  128. fi
  129. fi
  130. if [[ ! -f ${ENC_PASSWORD_FILE} ]]
  131. then
  132. message "CM Admin password has not been provided and this is"
  133. message "is first run of the script. Please run again and"
  134. message "provide password."
  135. exit 1
  136. else
  137. CM_PASSWORD=`cat ${ENC_PASSWORD_FILE} | base64 --decode`
  138. fi
  139. if [[ ${CM_HTTP} =~ .*https.* ]]
  140. then
  141. if [[ ${CM_PORT} =~ .*7180.* ]]
  142. then
  143. CM_PORT=7183
  144. fi
  145. fi
  146. CLUSTERNAME=$(urlencode "$(curl -L -s -k -X GET -u ${CM_USERNAME}:${CM_PASSWORD} "${CM_HTTP}://${CM_HOSTNAME}:${CM_PORT}/api/${CM_API}/clusters" | grep '"name" :' | awk -F\" '{print $4}')")
  147. SERVICENAME=$(urlencode "$(curl -L -s -k -X GET -u ${CM_USERNAME}:${CM_PASSWORD} "${CM_HTTP}://${CM_HOSTNAME}:${CM_PORT}/api/${CM_API}/clusters/${CLUSTERNAME}/services" | grep -B1 '"HUE"' | grep '"name" :' | awk -F\" '{print $4}')")
  148. ROLES_JSON="{ \"items\" : [ \""
  149. while read -r ROLE
  150. do
  151. ROLES_JSON="${ROLES_JSON}${ROLE}\",\""
  152. done < <(curl -L -s -k -X GET -u ${CM_USERNAME}:${CM_PASSWORD} "${CM_HTTP}://${CM_HOSTNAME}:${CM_PORT}/api/${CM_API}/clusters/${CLUSTERNAME}/services/${SERVICENAME}/roles" | grep ${SERVICENAME}- | grep '"name" :' | awk -F\" '{print $4}')
  153. ROLES_JSON=$(echo ${ROLES_JSON} | sed "s/,\"$/ ] }/g")
  154. RESTART_API_URL="/api/${CM_API}/clusters/${CLUSTERNAME}/services/${SERVICENAME}/roleCommands/restart"
  155. message "Restarting Hue process -u ${CM_USERNAME}:${CM_PASSWORD}: ${CM_HTTP}://${CM_HOSTNAME}:${CM_PORT}${RESTART_API_URL}: Roles: ${ROLES_JSON}"
  156. RESULTS=`curl -s -X POST -u ${CM_USERNAME}:${CM_PASSWORD} -i -H "content-type:application/json" -d "${ROLES_JSON}" "${CM_HTTP}://${CM_HOSTNAME}:${CM_PORT}${RESTART_API_URL}"`
  157. }
  158. urlencode() {
  159. # urlencode <string>
  160. old_lc_collate=$LC_COLLATE
  161. LC_COLLATE=C
  162. local length="${#1}"
  163. for (( i = 0; i < length; i++ )); do
  164. local c="${1:i:1}"
  165. case $c in
  166. [a-zA-Z0-9.~_-]) printf "$c" ;;
  167. *) printf '%%%02X' "'$c" ;;
  168. esac
  169. done
  170. LC_COLLATE=$old_lc_collate
  171. }
  172. message()
  173. {
  174. echo "$1"
  175. }
  176. main "$@"