remote-install-engine.sh.in 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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: remote-install-engine.sh
  19. #
  20. # This script is used as a remote engine for a cluster installation.
  21. #
  22. # Flow:
  23. #
  24. # 1. SSH Keys Generated.
  25. # 2. Script copied to node.
  26. # 3. Install package copied to node.
  27. # 4. SSH Keys copied to node.
  28. # 5. Run CheckInstall to determine if package is installed.
  29. # 6. Install/Upgrade if needed.
  30. # 7. Run CheckKeys to determine if sshkeys are installed.
  31. # 8. Install keys if different then installed or no keys installed.
  32. # 9. Return.
  33. #
  34. ###<REPLACE>###
  35. REMOTE_INSTALL="/tmp/remote_install"
  36. CURR="${REMOTE_INSTALL}/curr_keys"
  37. NEW="${REMOTE_INSTALL}/new_keys"
  38. MD5="${REMOTE_INSTALL}/md5"
  39. print_usage(){
  40. echo "usage: remote-install-engine.sh <package file>";
  41. exit 1;
  42. }
  43. checkUser(){
  44. USER=$1
  45. id ${USER} 2>&1 > /dev/null
  46. if [ $? -eq 0 ];
  47. then
  48. return 1
  49. else
  50. return 0
  51. fi
  52. }
  53. pkgCmd(){
  54. if [ "$1" == "deb" ]; then
  55. PKGCMD="dpkg -i $PKG &> $outputFile; apt-get install -f -y &> $outputFile;"
  56. elif [ "$1" == "rpm" ]; then
  57. if [ "$2" == "install" ] || [[ "$2" != "reinstall" ]]; then
  58. PKGCMD="yum install --nogpgcheck -y $PKG &> $outputFile"
  59. elif [ "$2" == "reinstall" ]; then
  60. PKGCMD="yum reinstall --nogpgcheck -y $PKG &> $outputFile"
  61. fi
  62. else
  63. echo "BAD Package type."
  64. exit 1
  65. fi
  66. }
  67. checkInstall(){
  68. _FILE=`ls ${INSTALL_DIR}${CONFIG_DIR}/version 2>&1 1>/dev/null; echo $?`
  69. checkUser "hpcc"
  70. _USER=$?
  71. if [ "${_FILE}" == 0 ] && [ ${_USER} -eq 1 ]; then
  72. _INSTALLED=1
  73. checkReinstall
  74. else
  75. _INSTALLED=0
  76. fi
  77. }
  78. checkReinstall(){
  79. if [ "${OSPKG}" == "rpm" ]; then
  80. local current=$( yum info hpccsystems-platform | grep "^Release" | awk '{print $3}' )
  81. local new=$( rpm -qp --info ${PKG} | grep "^Release" | awk '{print $3}' )
  82. if [[ $current == $new ]]; then
  83. _REINSTALL=1
  84. else
  85. _REINSTALL=0
  86. fi
  87. fi
  88. }
  89. installPkg(){
  90. checkInstall
  91. if [ "${_INSTALLED}" == "0" ]; then
  92. pkgCmd $OSPKG "install"
  93. elif [ "${_REINSTALL}" == "1" ]; then
  94. pkgCmd $OSPKG "reinstall"
  95. fi
  96. eval $PKGCMD
  97. if [ $? -ne 0 ]; then
  98. echo "FAILED on Package Command: ${PKGCMD}"
  99. exit 1
  100. fi
  101. }
  102. generateMD5(){
  103. if [ -e $2/$3 ]; then
  104. /usr/bin/md5sum $2/$3 > $1/$3.md5
  105. fi
  106. }
  107. checkMD5(){
  108. if [ -e $1.md5 ] && [ -e $2.md5 ]; then
  109. _K1=`cat $1.md5 | awk '{print $1}'`
  110. _K2=`cat $2.md5 | awk '{print $1}'`
  111. if [ "${_K1}" == "${_K2}" ]; then
  112. KM="1"
  113. else
  114. KM="0"
  115. fi
  116. fi
  117. }
  118. checkKeys(){
  119. CNT=0
  120. if [ -e ~hpcc/.ssh/$1 ] && [ -e ~hpcc/.ssh/$1.pub ]; then
  121. mkdir -p ${MD5}
  122. mkdir -p ${CURR}
  123. cp ~hpcc/.ssh/$1 ${CURR}/${1}-inst
  124. cp ~hpcc/.ssh/$1.pub ${CURR}/${1}-inst.pub
  125. generateMD5 ${MD5} ${CURR} ${1}-inst
  126. generateMD5 ${MD5} ${CURR} ${1}-inst.pub
  127. if [ -e ${NEW}/$1 ]; then
  128. generateMD5 ${MD5} ${NEW} ${1}
  129. generateMD5 ${MD5} ${NEW} ${1}.pub
  130. fi
  131. checkMD5 ${MD5}/$1 ${MD5}/${1}-inst
  132. if [ "$KM" == "0" ]; then
  133. CNT=1
  134. fi
  135. checkMD5 ${MD5}/$1.pub ${MD5}/${1}-inst.pub
  136. if [ "$KM" == "0" ]; then
  137. CNT=1
  138. fi
  139. rm -rf ${MD5}
  140. rm -rf ${CURR}
  141. else
  142. CNT=1
  143. fi
  144. if [ "$CNT" == "1" ]; then
  145. installKeys ${NEW} $1
  146. fi
  147. }
  148. installKeys(){
  149. mkdir -p ~hpcc/.ssh
  150. rm -f ~hpcc/.ssh/authorized_keys
  151. /bin/cp $1/$2 ~hpcc/.ssh/$2
  152. /bin/cp $1/$2.pub ~hpcc/.ssh/$2.pub
  153. /bin/cp $1/$2.pub ~hpcc/.ssh/authorized_keys
  154. chown -R hpcc:hpcc ~hpcc/.ssh
  155. chmod 700 ~hpcc/.ssh
  156. chmod 600 ~hpcc/.ssh/$2
  157. chmod 644 ~hpcc/.ssh/$2.pub
  158. chmod 644 ~hpcc/.ssh/authorized_keys
  159. }
  160. if [ $# -eq 0 ]; then
  161. print_usage
  162. fi
  163. PKG=$1
  164. outputFile="${REMOTE_INSTALL}/remote-install.log"
  165. if [ -e ${outputFile} ]; then
  166. rm -rf ${outputFile}
  167. fi
  168. pkgtype=`echo "$PKG" | grep -i rpm`
  169. if [ -z $pkgtype ]; then
  170. OSPKG="deb"
  171. else
  172. OSPKG="rpm"
  173. WITH_PLUGINS=0
  174. basename $PKG | grep -i with-plugins
  175. [ $? -eq 0 ] && WITH_PLUGINS=1
  176. fi
  177. installPkg
  178. checkUser "hpcc"
  179. _USER=$?
  180. if [ ${_USER} -eq 1 ]; then
  181. checkKeys id_rsa
  182. if [ -e ${REMOTE_INSTALL}/${ENV_XML_FILE} ]; then
  183. cp -r ${REMOTE_INSTALL}/${ENV_XML_FILE} ${CONFIG_DIR}/${ENV_XML_FILE}
  184. chown hpcc:hpcc ${CONFIG_DIR}/${ENV_XML_FILE}
  185. fi
  186. if [ -e ${REMOTE_INSTALL}/${ENV_CONF_FILE} ]; then
  187. cp -r ${REMOTE_INSTALL}/${ENV_CONF_FILE} ${CONFIG_DIR}/${ENV_CONF_FILE}
  188. chown hpcc:hpcc ${CONFIG_DIR}/${ENV_CONF_FILE}
  189. fi
  190. fi
  191. rm -rf ${REMOTE_INSTALL}
  192. exit 0