hpcc-push.sh.in 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. # Uusage: hpcc-push.sh -s <source> -t <target> -n <number_concurrent> -x
  19. #
  20. # This is acomplished with a standard scp 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. usage() {
  27. echo ""
  28. echo "usage: hpcc-push.sh -s <source> -t <target> -n <concurrent> -x"
  29. echo " -n: when specified, denotes the number of concurrent executions threads."
  30. echo " The default is 5."
  31. echo " -s: source file or directory."
  32. echo " -t: target file or directory."
  33. echo " -x: when specified, this option excludes execution on the current host."
  34. echo ""
  35. exit 1
  36. }
  37. createScriptFile() {
  38. cat > $SCRIPT_FILE <<SCRIPTFILE
  39. #~/bin/bash
  40. IP=\$1
  41. if ping -c 1 -w 5 -n \$IP > /dev/null 2>&1; then
  42. echo "\$IP: Host is alive."
  43. CAN_SSH="\`ssh -i $home/$user/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $user@\$IP exit > /dev/null 2>&1; echo \$?\`"
  44. if [ "\$CAN_SSH" -eq 255 ]; then
  45. echo "\$IP: Cannot SSH to host.";
  46. else
  47. echo "\$IP: Copying $source to $target on \$IP";
  48. SCP=\$(scp -r -i $home/$user/.ssh/id_rsa $source $user@\$IP:$target; echo \$?)
  49. if [ "\$SCP" -eq 0 ]; then
  50. echo "\$IP: Success";
  51. else
  52. echo "\$IP: Failure";
  53. exit 1
  54. fi
  55. fi
  56. else
  57. echo "\$IP: Cannot Ping host? (Host Alive?)"
  58. exit 1
  59. fi
  60. SCRIPTFILE
  61. chmod +x ${SCRIPT_FILE}
  62. }
  63. ############################################
  64. #
  65. # MAIN
  66. #
  67. ############################################
  68. cluster_tools_init
  69. if [ "$(whoami)" != "root" ] && [ "$(whoami)" != "${user}" ]; then
  70. echo ""
  71. echo "The script must run as root, $user or sudo."
  72. echo ""
  73. exit 1
  74. fi
  75. source=
  76. target=
  77. exclude=0
  78. OPTION="-e ${CONFIG_DIR}/${ENV_CONF_FILE} -s ${SECTION:-DEFAULT}"
  79. TEMP=`/usr/bin/getopt -o n:s:t:hx --long help,conrrent:,source:,target:,exclude -n 'hpcc-push' -- "$@"`
  80. if [ $? != 0 ] ; then echo "Failure to parse commandline." >&2 ; end 1 ; fi
  81. eval set -- "$TEMP"
  82. while true ; do
  83. case "$1" in
  84. -n|--concurrent)
  85. if [ -n "$2" ] && [[ $2 =~ ^[0-9]+$ ]]
  86. then
  87. [ $2 -gt 0 ] && $OPTION="${OPTION:+"$OPTION "}-n $2"
  88. fi
  89. shift 2 ;;
  90. -s|--source) source="$2"
  91. shift 2 ;;
  92. -t|--target) target="$2"
  93. shift 2 ;;
  94. -x|--exclude) OPTION="${OPTION:+"$OPTION "}-x"
  95. exclude=1
  96. shift ;;
  97. -h|--help) usage
  98. shift ;;
  99. --) shift ; break ;;
  100. *) usage ;;
  101. esac
  102. done
  103. if [ -z "$source" ] || [ -z "$target" ]; then
  104. usage
  105. fi
  106. SCRIPT_FILE=~/hpcc-push_$$
  107. createScriptFile
  108. python_expected_version=3.4
  109. is_python_installed ${python_expected_version}
  110. if [ $? -eq 0 ]
  111. then
  112. eval ${INSTALL_DIR}/sbin/cluster_script.py -f ${SCRIPT_FILE} $OPTION
  113. else
  114. echo ""
  115. echo "Cannot detect python version ${python_expected_version}+. Will run on the cluster hosts sequentially."
  116. echo ""
  117. run_cluster ${SCRIPT_FILE} ${exclude}
  118. fi
  119. rm -rf ${SCRIPT_FILE}