distributePKI.in 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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>./distributePKI.log
  19. ###<REPLACE>###
  20. progname=distributePKI
  21. source ${INSTALL_DIR}/etc/init.d/hpcc_common
  22. source ${INSTALL_DIR}/etc/init.d/init-functions
  23. source ${INSTALL_DIR}/etc/init.d/export-path
  24. HPCC_CONFIG=${HPCC_CONFIG:-${CONFIG_DIR}/${ENV_CONF_FILE}}
  25. SECTION=${1:-DEFAULT}
  26. PATH_PREFIX=`cat ${HPCC_CONFIG} | sed -n "/\[${SECTION}\]/,/\[/p" | grep "^home *= *" | sed -e 's/^home *= *//'`
  27. USER_NAME=`cat ${HPCC_CONFIG} | sed -n "/\[${SECTION}\]/,/\[/p" | grep "^user *= *" | sed -e 's/^user *= *//'`
  28. homePath=${PATH_PREFIX}/${USER_NAME}
  29. certPath=${homePath}/certificate
  30. sbin_path="${INSTALL_DIR}/sbin"
  31. envfile="${CONFIG_DIR}/${ENV_XML_FILE}"
  32. source_file=certificates.tgz
  33. source_tar=${homePath}/${source_file}
  34. if [ "$(whoami)" != "root" ] && [ "$(whoami)" != "${USER_NAME}" ]; then
  35. echo ""
  36. echo "The script must run as root, $USER_NAME or sudo."
  37. echo ""
  38. exit 1
  39. fi
  40. createScriptFile() {
  41. cat > $SCRIPT_FILE <<SCRIPTFILE
  42. #!/bin/bash
  43. IP=\$1
  44. if ping -c 1 -w 5 -n \$IP > /dev/null 2>&1; then
  45. echo "\$IP: Host is alive."
  46. CAN_SSH="\`ssh -i $homePath/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER_NAME@\$IP exit > /dev/null 2>&1; echo \$?\`"
  47. if [ "\$CAN_SSH" -eq 255 ]; then
  48. echo "\$IP: Cannot SSH to host.";
  49. fi
  50. IS_LOCAL="\`ssh -i $homePath/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER_NAME@\$IP ls $SCRIPT_FILE > /dev/null 2>&1; echo \$?\`"
  51. if [ "\$IS_LOCAL" -ne 0 ]; then
  52. echo "\$IP: Fetching complist for \$IP"
  53. else
  54. echo "\$IP: running on local machine, nothing to do"
  55. exit 0
  56. fi
  57. ## get list of components on remote machine
  58. OIFS=\$IFS
  59. IFS=\$'\\n'
  60. complist=(\$($sbin_path/configgen -env $envfile -ip \$IP -list | awk 'BEGIN { FS="=";} {print \$1;}'))
  61. IFS=\$OIFS
  62. ## ensure certPath exists
  63. ssh -i $homePath/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER_NAME@\$IP mkdir -p $certPath > /dev/null 2>&1
  64. ssh -i $homePath/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER_NAME@\$IP rm -f $certPath/public.key.pem $certPath/key.pem $certPath/certificate.pem > /dev/null 2>&1
  65. scp -i $homePath/.ssh/id_rsa $certPath/public.key.pem $certPath/key.pem $certPath/certificate.pem $USER_NAME@\$IP:$certPath
  66. for i in \${complist[@]} ; do
  67. ## allow for overwrite
  68. ssh -i $homePath/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER_NAME@\$IP rm -rf $certPath/\$i > /dev/null 2>&1
  69. echo "\$IP: scp -r $certPath/\$i $certPath";
  70. scp -r -i $homePath/.ssh/id_rsa $certPath/\$i $USER_NAME@\$IP:$certPath
  71. done
  72. else
  73. echo "\$IP: Cannot Ping host? (Host Alive?)"
  74. exit 1
  75. fi
  76. SCRIPTFILE
  77. chmod +x ${SCRIPT_FILE}
  78. }
  79. createRemoteToRemoteScriptFile() {
  80. cat > $SCRIPT_FILE <<REMOTE_TO_REMOTE_SCRIPTFILE
  81. #!/bin/bash
  82. IP=\$1
  83. if ping -c 1 -w 5 -n \$IP > /dev/null 2>&1; then
  84. echo "\$IP: Host is alive."
  85. CAN_SSH="\`ssh -i $homePath/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER_NAME@\$IP exit > /dev/null 2>&1; echo \$?\`"
  86. if [ "\$CAN_SSH" -eq 255 ]; then
  87. echo "\$IP: Cannot SSH to host.";
  88. fi
  89. if [ "$dali_ip" == "\$IP" ]; then
  90. echo "\$IP: On admin node, nothing to do"
  91. exit 0
  92. fi
  93. ## get list of components on remote machine
  94. OIFS=\$IFS
  95. IFS=\$'\\n'
  96. complist=(\$($sbin_path/configgen -env $envfile -ip \$IP -list | awk 'BEGIN { FS="=";} {print \$1;}'))
  97. IFS=\$OIFS
  98. ## ensure certPath exists
  99. ssh -i $homePath/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER_NAME@\$IP mkdir -p $certPath > /dev/null 2>&1
  100. ssh -i $homePath/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER_NAME@\$IP rm -f $certPath/public.key.pem $certPath/key.pem $certPath/certificate.pem > /dev/null 2>&1
  101. scp -i $homePath/.ssh/id_rsa $USER_NAME@$dali_ip:$certPath/public.key.pem $USER_NAME@\$IP:$certPath
  102. scp -i $homePath/.ssh/id_rsa $USER_NAME@$dali_ip:$certPath/key.pem $USER_NAME@\$IP:$certPath
  103. scp -i $homePath/.ssh/id_rsa $USER_NAME@$dali_ip:$certPath/certificate.pem $USER_NAME@\$IP:$certPath
  104. for i in \${complist[@]} ; do
  105. ## allow for overwrite
  106. ssh -i $homePath/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER_NAME@\$IP rm -rf $certPath/\$i > /dev/null 2>&1
  107. echo "\$IP: scp -r $certPath/\$i $certPath";
  108. scp -r -i $homePath/.ssh/id_rsa $USER_NAME@$dali_ip:$certPath/\$i $USER_NAME@\$IP:$certPath
  109. done
  110. else
  111. echo "\$IP: Cannot Ping host? (Host Alive?)"
  112. exit 1
  113. fi
  114. REMOTE_TO_REMOTE_SCRIPTFILE
  115. chmod +x ${SCRIPT_FILE}
  116. }
  117. is_dali=$($sbin_path/configgen -env $envfile -list | grep dali )
  118. if [ -n "$is_dali" ]; then
  119. SCRIPT_FILE=/tmp/distributePKI_$$
  120. createScriptFile
  121. ${INSTALL_DIR}/etc/init.d/setupPKI
  122. OPTION="-e ${CONFIG_DIR}/${ENV_CONF_FILE} -s ${SECTION:-DEFAULT} -x"
  123. eval ${INSTALL_DIR}/sbin/cluster_script.py -f ${SCRIPT_FILE} $OPTION
  124. rm -rf ${SCRIPT_FILE}
  125. else
  126. SCRIPT_FILE=/tmp/distributePKI_$$
  127. dali_ip=$($sbin_path/configgen -env $envfile -listall | awk 'BEGIN {FS=",";} /^DaliServerProcess/ {print $3;}')
  128. createRemoteToRemoteScriptFile
  129. ssh -i $homePath/.ssh/id_rsa -o BatchMode=yes -o LogLevel=QUIET -o StrictHostKeyChecking=no $USER_NAME@$dali_ip $INSTALL_DIR/etc/init.d/setupPKI
  130. OPTION="-e ${CONFIG_DIR}/${ENV_CONF_FILE} -s ${SECTION:-DEFAULT}"
  131. eval ${INSTALL_DIR}/sbin/cluster_script.py -f ${SCRIPT_FILE} $OPTION
  132. rm -rf ${SCRIPT_FILE}
  133. fi
  134. set +x