remote-install-engine.sh.in 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. if [ "$2" == "install" ]; then
  56. PKGCMD="dpkg -i"
  57. elif [ "$2" == "upgrade" ]; then
  58. PKGCMD="dpkg -i"
  59. else
  60. echo "Bad option type."
  61. exit 1
  62. fi
  63. elif [ "$1" == "rpm" ]; then
  64. if [ "$2" == "install" ]; then
  65. PKGCMD="rpm -ivh"
  66. elif [ "$2" == "upgrade" ]; then
  67. PKGCMD="rpm -Uvh"
  68. else
  69. echo "Bad option type."
  70. exit 1
  71. fi
  72. [ $WITH_PLUGINS -eq 1 ] && PKGCMD="$PKGCMD --nodeps"
  73. else
  74. echo "BAD Package type."
  75. exit 1
  76. fi
  77. }
  78. checkInstall(){
  79. _FILE=`ls ${INSTALL_DIR}${CONFIG_DIR}/version 2>&1 1>/dev/null; echo $?`
  80. checkUser "hpcc"
  81. _USER=$?
  82. if [ "${_FILE}" == 0 ] && [ ${_USER} -eq 1 ]; then
  83. _INSTALLED=1
  84. checkUpgrade
  85. else
  86. _INSTALLED=0
  87. fi
  88. }
  89. checkUpgrade(){
  90. if [ "${_INSTALLED}" == "1" ]; then
  91. _VERSION=`cat ${INSTALL_DIR}${CONFIG_DIR}/version`
  92. _PKG=`echo $PKG | grep -c "${_VERSION}"`
  93. if [ "${_PKG}" == "1" ]; then
  94. _UPGRADE=0
  95. else
  96. _UPGRADE=1
  97. fi
  98. fi
  99. }
  100. installPkg(){
  101. checkInstall
  102. if [ "${_INSTALLED}" == "0" ]; then
  103. pkgCmd $OSPKG "install"
  104. $PKGCMD $PKG 1>/dev/null 2>&1;
  105. elif [ "${_UPGRADE}" == "1" ]; then
  106. pkgCmd $OSPKG "upgrade"
  107. $PKGCMD $PKG 1>/dev/null 2>&1;
  108. fi
  109. checkInstall
  110. if [ "${_INSTALLED}" == "0" ]; then
  111. echo "Failed to install ${PKG} (missing dependencies?)"
  112. exit 1
  113. fi
  114. }
  115. generateMD5(){
  116. if [ -e $2/$3 ]; then
  117. /usr/bin/md5sum $2/$3 > $1/$3.md5
  118. fi
  119. }
  120. checkMD5(){
  121. if [ -e $1.md5 ] && [ -e $2.md5 ]; then
  122. _K1=`cat $1.md5 | awk '{print $1}'`
  123. _K2=`cat $2.md5 | awk '{print $1}'`
  124. if [ "${_K1}" == "${_K2}" ]; then
  125. KM="1"
  126. else
  127. KM="0"
  128. fi
  129. fi
  130. }
  131. checkKeys(){
  132. CNT=0
  133. if [ -e ~hpcc/.ssh/$1 ] && [ -e ~hpcc/.ssh/$1.pub ]; then
  134. mkdir -p ${MD5}
  135. mkdir -p ${CURR}
  136. cp ~hpcc/.ssh/$1 ${CURR}/${1}-inst
  137. cp ~hpcc/.ssh/$1.pub ${CURR}/${1}-inst.pub
  138. generateMD5 ${MD5} ${CURR} ${1}-inst
  139. generateMD5 ${MD5} ${CURR} ${1}-inst.pub
  140. if [ -e ${NEW}/$1 ]; then
  141. generateMD5 ${MD5} ${NEW} ${1}
  142. generateMD5 ${MD5} ${NEW} ${1}.pub
  143. fi
  144. checkMD5 ${MD5}/$1 ${MD5}/${1}-inst
  145. if [ "$KM" == "0" ]; then
  146. CNT=1
  147. fi
  148. checkMD5 ${MD5}/$1.pub ${MD5}/${1}-inst.pub
  149. if [ "$KM" == "0" ]; then
  150. CNT=1
  151. fi
  152. rm -rf ${MD5}
  153. rm -rf ${CURR}
  154. else
  155. CNT=1
  156. fi
  157. if [ "$CNT" == "1" ]; then
  158. installKeys ${NEW} $1
  159. fi
  160. }
  161. installKeys(){
  162. mkdir -p ~hpcc/.ssh
  163. rm -f ~hpcc/.ssh/authorized_keys
  164. /bin/cp $1/$2 ~hpcc/.ssh/$2
  165. /bin/cp $1/$2.pub ~hpcc/.ssh/$2.pub
  166. /bin/cp $1/$2.pub ~hpcc/.ssh/authorized_keys
  167. chown -R hpcc:hpcc ~hpcc/.ssh
  168. chmod 700 ~hpcc/.ssh
  169. chmod 600 ~hpcc/.ssh/$2
  170. chmod 644 ~hpcc/.ssh/$2.pub
  171. chmod 644 ~hpcc/.ssh/authorized_keys
  172. }
  173. if [ $# -eq 0 ]; then
  174. print_usage
  175. fi
  176. PKG=$1
  177. pkgtype=`echo "$PKG" | grep -i rpm`
  178. if [ -z $pkgtype ]; then
  179. OSPKG="deb"
  180. else
  181. OSPKG="rpm"
  182. WITH_PLUGINS=0
  183. basename $PKG | grep -i with-plugins
  184. [ $? -eq 0 ] && WITH_PLUGINS=1
  185. fi
  186. installPkg
  187. checkUser "hpcc"
  188. _USER=$?
  189. if [ ${_USER} -eq 1 ]; then
  190. checkKeys id_rsa
  191. if [ -e ${REMOTE_INSTALL}/${ENV_XML_FILE} ]; then
  192. cp -r ${REMOTE_INSTALL}/${ENV_XML_FILE} ${CONFIG_DIR}/${ENV_XML_FILE}
  193. chown hpcc:hpcc ${CONFIG_DIR}/${ENV_XML_FILE}
  194. fi
  195. if [ -e ${REMOTE_INSTALL}/${ENV_CONF_FILE} ]; then
  196. cp -r ${REMOTE_INSTALL}/${ENV_CONF_FILE} ${CONFIG_DIR}/${ENV_CONF_FILE}
  197. chown hpcc:hpcc ${CONFIG_DIR}/${ENV_CONF_FILE}
  198. fi
  199. fi
  200. rm -rf ${REMOTE_INSTALL}
  201. rm -rf ~/remote_install.tgz
  202. exit 0