hpcc-run.sh.in 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #!/bin/bash
  2. ################################################################################
  3. # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. ################################################################################
  17. #
  18. # Usage: hpcc-run.sh [hpcc-init | dafilesrv] [-c component] <cmd>
  19. #
  20. # This is acomplished with a standard ssh command with the use of the
  21. # runtime users id_rsa file.
  22. ###<REPLACE>###
  23. source ${INSTALL_DIR}/etc/init.d/hpcc_common
  24. source ${INSTALL_DIR}/etc/init.d/init-functions
  25. source ${INSTALL_DIR}/etc/init.d/export-path
  26. print_usage(){
  27. echo
  28. echo "usage: hpcc-run.sh [-c component] [-a {hpcc-init|dafilesrv}] [-n concurrent] [-s] [-S] {start|stop|restart|status|setup}"
  29. echo " -a|--action: HPCC service name. Either hpcc-init (default) or dafilesrv."
  30. echo " -c|--comp: HPCC component. For example, mydali, myroxie, mythor, etc."
  31. echo " -n|--concurrent: How many concurrent instances to run. The default is equal to the number of nodes present."
  32. echo " -S|--sequentially: For the command to run sequentially. i.e. one host a time. (overrides -n)"
  33. echo " -s|--save: Save the result to a file named by ip."
  34. echo
  35. end 1
  36. }
  37. getIPS(){
  38. if [ -z "${comp}" ]; then
  39. IPS=`${INSTALL_DIR}/sbin/configgen -env ${envfile} -machines | awk -F, '{print \$1}' | sort | uniq`
  40. else
  41. IPS=`${INSTALL_DIR}/sbin/configgen -env ${envfile} -listall | grep -e "${comp}" | awk -F, '{ print \$3 }' | sort | uniq`
  42. if [ -z "${IPS}" ]; then
  43. log_failure_msg "Component ${comp} not found"
  44. print_usage
  45. end 1
  46. fi
  47. fi
  48. }
  49. getDali(){
  50. DIP=`${INSTALL_DIR}/sbin/configgen -env ${envfile} -listall | grep Dali | awk -F, '{print \$3}' | sort | uniq`
  51. }
  52. createIPListFile(){
  53. _file=$1
  54. echo "$IPS" > $_file
  55. }
  56. createIPListFileExcludeDIP(){
  57. _file=$1
  58. echo "$IPS" | grep -v $DIP > $_file
  59. }
  60. doOneIP(){
  61. _ip=$1
  62. _action=$2
  63. _cmd=$3
  64. _comp=$4
  65. if ping -c 1 -w 5 -n $_ip > /dev/null 2>&1; then
  66. #echo "$_ip: Host is alive."
  67. CAN_SSH="`ssh -i $home/$user/.ssh/id_rsa -o BatchMode=yes -o StrictHostKeyChecking=no $user@$_ip exit > /dev/null 2>&1; echo $?`"
  68. if [ "$CAN_SSH" -eq 255 ]; then
  69. echo "$_ip: Cannot SSH to host.";
  70. return 1
  71. else
  72. hpccStatusFile=/tmp/hpcc_status_$$
  73. if [ -z "${_comp}" ]; then
  74. CMD="sudo /etc/init.d/$_action $_cmd"
  75. else
  76. CMD="sudo /etc/init.d/$_action -c $_comp $_cmd"
  77. fi
  78. echo "$_ip: Running $CMD";
  79. CMD="$CMD | tee ${hpccStatusFile}"
  80. ssh -i $home/$user/.ssh/id_rsa $user@$_ip $CMD;
  81. scp -i $home/$user/.ssh/id_rsa $user@${_ip}:${hpccStatusFile} ${reportDir}/$_ip > /dev/null 2>&1
  82. CMD="rm -rf $hpccStatusFile"
  83. ssh -i $home/$user/.ssh/id_rsa $user@$_ip $CMD
  84. rc=${PIPESTATUS[0]}
  85. echo
  86. return $rc
  87. fi
  88. else
  89. echo "$_ip: Cannot Ping host? (Host Alive?)"
  90. return 1
  91. fi
  92. }
  93. createScript(){
  94. _scriptFile=$1
  95. _action=$2
  96. _cmd=$3
  97. _comp=$4
  98. hpccStatusFile=/tmp/hpcc_status_${dateTime}_$$
  99. cat > $_scriptFile <<SCRIPTFILE
  100. #!/bin/bash
  101. IP=\$1
  102. if ping -c 1 -w 5 -n \$IP > /dev/null 2>&1; then
  103. echo "\$IP: Host is alive."
  104. CAN_SSH="\`ssh -i $home/$user/.ssh/id_rsa -o BatchMode=yes -o StrictHostKeyChecking=no $user@\$IP exit > /dev/null 2>&1; echo \$?\`"
  105. if [ "\$CAN_SSH" -eq 255 ]; then
  106. echo "\$IP: Cannot SSH to host.";
  107. exit 1
  108. else
  109. if [ -z "${_comp}" ]; then
  110. CMD="sudo /etc/init.d/$_action $_cmd"
  111. else
  112. CMD="sudo /etc/init.d/$_action -c $_comp $_cmd"
  113. fi
  114. echo "\$IP: Running \$CMD";
  115. CMD="\$CMD | tee $hpccStatusFile"
  116. ssh -i $home/$user/.ssh/id_rsa $user@\$IP \$CMD;
  117. rc=\${PIPESTATUS[0]}
  118. scp -i $home/$user/.ssh/id_rsa $user@\${IP}:${hpccStatusFile} ${reportDir}/\$IP
  119. CMD="rm -rf $hpccStatusFile"
  120. ssh -i $home/$user/.ssh/id_rsa $user@\$IP \$CMD
  121. exit \$rc
  122. fi
  123. else
  124. echo "\$IP: Cannot Ping host? (Host Alive?)"
  125. exit 1
  126. fi
  127. SCRIPTFILE
  128. chmod +x $_scriptFile
  129. }
  130. runScript() {
  131. if [ "$RUN_CLUSTER_DISPLAY_OUTPUT" = "FALSE" ] && [ $hasPython -eq 1 ]; then
  132. OPTIONS="${OPTIONS:+"$OPTIONS "}-n ${concurrent}"
  133. eval ${INSTALL_DIR}/sbin/cluster_script.py -f ${scriptFile} "$OPTIONS"
  134. rc=$?
  135. else
  136. if [ $hasPython -eq 0 ]; then
  137. echo ""
  138. echo "Cannot detect python version ${expected_python_version}+. Will run on the cluster hosts sequentially."
  139. echo ""
  140. fi
  141. run_cluster ${scriptFile} 0 $1
  142. rc=$?
  143. fi
  144. rm -rf $scriptFile
  145. }
  146. doSetup() {
  147. init setup
  148. scriptFile=/tmp/${action}_setup_$$
  149. createScript $scriptFile $action "setup" $comp
  150. runScript
  151. report "${action} setup"
  152. }
  153. doStatus() {
  154. init status
  155. scriptFile=/tmp/${action}_status_$$
  156. createScript $scriptFile $action "status" $comp
  157. runScript
  158. report "${action} status"
  159. }
  160. doStop() {
  161. echo "$action stop in the cluster ..."
  162. init stop
  163. scriptFile=/tmp/${action}_stop_$$
  164. createScript $scriptFile $action "stop" $comp
  165. if [ -n "${comp}" ]; then
  166. OPTIONS="${OPTIONS:+"$OPTIONS "}-h $IPsFile"
  167. runScript $IPsFile
  168. report "${action} stop"
  169. else
  170. OPTIONS="${OPTIONS:+"$OPTIONS "}-h $IPsExcludeDIP"
  171. runScript $IPsExcludeDIP
  172. report "${action} stop" $DIP
  173. doOneIP $DIP $action "stop" $comp
  174. fi
  175. }
  176. doStart() {
  177. init start
  178. if [ -n "${comp}" ]; then
  179. startFile=$IPsFile
  180. else
  181. doOneIP $DIP $action "start" $comp || end 1
  182. startFile=$IPsExcludeDIP
  183. fi
  184. echo "$action start in the cluster ..."
  185. scriptFile=/tmp/${action}_start_$$
  186. createScript $scriptFile $action "start" $comp
  187. OPTIONS="${OPTIONS:+"$OPTIONS "}-h $startFile"
  188. runScript $startFile
  189. if [ -n "${comp}" ]; then
  190. report "${action} start"
  191. else
  192. report "${action} start" $DIP
  193. fi
  194. [ $rc -ne 0 ] && end $rc
  195. }
  196. init() {
  197. getIPS
  198. getDali
  199. IPsFile=/tmp/ip_list_$$
  200. createIPListFile $IPsFile
  201. IPsExcludeDIP=/tmp/ip_list_exclude_dip_$$
  202. createIPListFileExcludeDIP $IPsExcludeDIP
  203. if [ $concurrent -eq 0 ]; then
  204. concurrent=$( wc -l $IPsFile | awk '{ print $1 }')
  205. fi
  206. dateTime=$(date +"%Y%m%d_%H%M%S")
  207. reportDir=/var/log/HPCCSystems/cluster/$1/${dateTime}
  208. mkdir -p $reportDir
  209. chown -R ${user}:${user} ${reportDir}/..
  210. }
  211. report() {
  212. _title=$1
  213. hostToSkip=$2
  214. if [ "$RUN_CLUSTER_DISPLAY_OUTPUT" = "FALSE" ]; then
  215. ls ${reportDir} | while read _host
  216. do
  217. [ "$_host" = "$hostToSkip" ] && continue
  218. echo "$_host $_title :"
  219. cat ${reportDir}/$_host | grep -v "ervice dafilesrv" | grep -v -e "^[[:space:]]*$"
  220. echo
  221. done
  222. fi
  223. }
  224. end() {
  225. if [ $save -eq 1 ]
  226. then
  227. echo "Cluster status is saved under $reportDir"
  228. echo
  229. else
  230. rm -rf $reportDir
  231. fi
  232. [ -e "${IPsExcludeDIP}" ] && rm -rf ${IPsExcludeDIP}
  233. [ -e "${IPsFile}" ] && rm -rf ${IPsFile}
  234. exit $1
  235. }
  236. ############################################
  237. #
  238. # MAIN
  239. #
  240. ############################################
  241. cluster_tools_init
  242. if [ "${USER}" != "root" ] && [ "${USER}" != "${user}" ]; then
  243. echo ""
  244. echo "The script must run as root, $user or sudo."
  245. echo ""
  246. exit 1
  247. fi
  248. envfile=$configs/$environment
  249. configfile=${CONFIG_DIR}/${ENV_CONF_FILE}
  250. hasPython=0
  251. save=0
  252. expected_python_version=2.6
  253. is_python_installed $expected_python_version
  254. [ $? -eq 0 ] && hasPython=1
  255. concurrent=0
  256. RUN_CLUSTER_DISPLAY_OUTPUT=FALSE
  257. OPTIONS="-e $configfile -s ${SECTION:-DEFAULT}"
  258. TEMP=`/usr/bin/getopt -o a:c:n:sSh --long help,comp:,action:,save,concurrent:,sequentially -n 'hpcc-run' -- "$@"`
  259. if [ $? != 0 ] ; then echo "Failure to parse commandline." >&2 ; end 1 ; fi
  260. eval set -- "$TEMP"
  261. while true ; do
  262. case "$1" in
  263. -c|--comp) comp=$2
  264. shift 2 ;;
  265. -a|--action) action=$2
  266. shift 2 ;;
  267. -n|--concurrent)
  268. if [ -n "$2" ] && [[ $2 =~ ^[1-9][0-9]*$ ]] && [ $concurrent -ne 1 ]; then
  269. concurrent=$2
  270. fi
  271. shift 2 ;;
  272. -S|--sequentially)
  273. concurrent=1
  274. RUN_CLUSTER_DISPLAY_OUTPUT=TRUE
  275. shift ;;
  276. -s|--save) save=1
  277. shift ;;
  278. -h|--help) print_usage
  279. shift ;;
  280. --) shift ; break ;;
  281. *) print_usage ;;
  282. esac
  283. done
  284. case "$action" in
  285. hpcc-init) ;;
  286. dafilesrv) ;;
  287. *) if [ -z $action ]; then
  288. action="hpcc-init"
  289. else
  290. print_usage
  291. fi
  292. ;;
  293. esac
  294. for arg; do
  295. arg=$arg;
  296. case "$arg" in
  297. start)
  298. doStart
  299. ;;
  300. stop)
  301. doStop
  302. ;;
  303. restart)
  304. doStop
  305. doStart
  306. ;;
  307. status)
  308. doStatus
  309. ;;
  310. setup)
  311. doSetup
  312. ;;
  313. *) print_usage;;
  314. esac
  315. done
  316. end 0