safe_copyPKI.in 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/bash
  2. ################################################################################
  3. # HPCC SYSTEMS software Copyright (C) 2019 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. set -x
  18. exec 3>&2 2>./safe_copyPKI.log
  19. INSTALL_DIR=/opt/HPCCSystems
  20. CONFIG_DIR=/etc/HPCCSystems
  21. ENV_XML_FILE=environment.xml
  22. ENV_CONF_FILE=environment.conf
  23. PID_DIR=/var/run/HPCCSystems
  24. LOCK_DIR=/var/lock/HPCCSystems
  25. LOG_DIR=/var/log/HPCCSystems
  26. INIT_PATH=/etc/init.d
  27. progname=safe_copyPKI
  28. source ${INSTALL_DIR}/etc/init.d/hpcc_common
  29. source ${INSTALL_DIR}/etc/init.d/init-functions
  30. source ${INSTALL_DIR}/etc/init.d/export-path
  31. HPCC_CONFIG=${HPCC_CONFIG:-${CONFIG_DIR}/${ENV_CONF_FILE}}
  32. SECTION=${1:-DEFAULT}
  33. PATH_PREFIX=`cat ${HPCC_CONFIG} | sed -n "/\[${SECTION}\]/,/\[/p" | grep "^home *= *" | sed -e 's/^home *= *//'`
  34. USER_NAME=`cat ${HPCC_CONFIG} | sed -n "/\[${SECTION}\]/,/\[/p" | grep "^user *= *" | sed -e 's/^user *= *//'`
  35. homePath=${PATH_PREFIX}/${USER_NAME}
  36. certPath=${homePath}/certificate
  37. sbin_path="${INSTALL_DIR}/sbin"
  38. envfile="${CONFIG_DIR}/${ENV_XML_FILE}"
  39. if [ "$(whoami)" != "${USER}" ]; then
  40. echo ""
  41. echo "The script must run as $USER."
  42. echo ""
  43. exit 1
  44. fi
  45. createScriptFile() {
  46. cat > $SCRIPT_FILE <<SCRIPTFILE
  47. #!/bin/bash
  48. IP=\$1
  49. set -x
  50. exec 3>&2 2>./safe-\$(date -Ins).log
  51. if ping -c 1 -w 5 -n \$IP > /dev/null 2>&1; then
  52. echo "\$IP: Host is alive."
  53. CAN_SSH="\`ssh -i $HOME/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER@\$IP exit > /dev/null 2>&1; echo \$?\`"
  54. if [ "\$CAN_SSH" -eq 255 ]; then
  55. echo "\$IP: Cannot SSH to host.";
  56. fi
  57. IS_LOCAL="\`ssh -i $HOME/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER@\$IP ls $SCRIPT_FILE > /dev/null 2>&1; echo \$?\`"
  58. if [ "\$IS_LOCAL" -ne 0 ]; then
  59. echo "\$IP: Fetching complist for \$IP"
  60. else
  61. echo "\$IP: running on local machine, nothing to do"
  62. exit 0
  63. fi
  64. ## get list of components on remote machine
  65. OIFS=\$IFS
  66. IFS=\$'\\n'
  67. complist=(\$($sbin_path/configgen -env $envfile -ip \$IP -list | awk 'BEGIN { FS="=";} {print \$1;}'))
  68. IFS=\$OIFS
  69. mkdir -p $HOME/tmp_certs
  70. for i in "" \${complist[@]} ; do
  71. compName=""
  72. if [ "\$i" != "" ]; then
  73. compName=""\${i#*_}
  74. fi
  75. if [ -n "\$compName" ]; then
  76. ## allow for overwrite
  77. ssh -i $HOME/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER@\$IP sudo rm -rf $certPath/\$compName > /dev/null 2>&1
  78. sudo cp -R $certPath/\$compName $HOME/tmp_certs
  79. fi
  80. done
  81. sudo chown -R $USER:$USER $HOME/tmp_certs
  82. scp -r -i $HOME/.ssh/id_rsa $HOME/tmp_certs $USER@\$IP:$HOME
  83. ## copy full directory over
  84. ssh -i $HOME/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER@\$IP sudo cp -R $HOME/tmp_certs/ $certPath > /dev/null 2>&1
  85. ## ensure $USER_NAME is owner
  86. ssh -i $HOME/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER@\$IP sudo chown -R $USER_NAME:$USER_NAME $certPath > /dev/null 2>&1
  87. ## cleanup
  88. rm -rf $HOME/tmp_certs
  89. ssh -i $HOME/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER@\$IP rm -rf $HOME/tmp_certs > /dev/null 2>&1
  90. else
  91. echo "\$IP: Cannot Ping host? (Host Alive?)"
  92. exit 1
  93. fi
  94. SCRIPTFILE
  95. chmod +x ${SCRIPT_FILE}
  96. }
  97. SCRIPT_FILE=/tmp/distributePKI_$$
  98. createScriptFile
  99. sudo ${INSTALL_DIR}/etc/init.d/setupPKI
  100. OPTION="-e ${CONFIG_DIR}/${ENV_CONF_FILE} -s ${SECTION:-DEFAULT} -x"
  101. eval sudo ${INSTALL_DIR}/sbin/cluster_script.py -f ${SCRIPT_FILE} $OPTION
  102. rm -rf ${SCRIPT_FILE}
  103. set +x