puppet_agent_install.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #!/bin/bash
  2. #*
  3. #/*
  4. # * Licensed to the Apache Software Foundation (ASF) under one
  5. # * or more contributor license agreements. See the NOTICE file
  6. # * distributed with this work for additional information
  7. # * regarding copyright ownership. The ASF licenses this file
  8. # * to you under the Apache License, Version 2.0 (the
  9. # * "License"); you may not use this file except in compliance
  10. # * with the License. You may obtain a copy of the License at
  11. # *
  12. # * http://www.apache.org/licenses/LICENSE-2.0
  13. # *
  14. # * Unless required by applicable law or agreed to in writing, software
  15. # * distributed under the License is distributed on an "AS IS" BASIS,
  16. # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # * See the License for the specific language governing permissions and
  18. # * limitations under the License.
  19. # */
  20. #
  21. # /* This script takes three arguments,
  22. # * - sshkey: if not specified then ssh w/o key
  23. # * - repository information : to be added to remote node
  24. # * - list of hosts
  25. # */
  26. #set -e
  27. #set -x
  28. trap 'pp_cmd=$ppp_cmd; ppp_cmd=$previous_command; previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG
  29. #trap 'echo "$host: retcode:[$?] command:[$previous_command], out:[$out]"' EXIT
  30. #printf 'Argument is __%s__\n' "$@"
  31. usage() {
  32. echo "
  33. Usage: $0 with the following parameters
  34. --puppet-master Puppet Master
  35. --repo-file Repo File
  36. --gpg-key-files GPG Key files - comma-separated
  37. --using-local-repo Whether local repo is being used
  38. "
  39. }
  40. OPTS=$(getopt \
  41. -n $0 \
  42. -o '' \
  43. -l 'puppet-master:' \
  44. -l 'repo-file:' \
  45. -l 'using-local-repo' \
  46. -l 'gpg-key-files:' \
  47. -l 'help' \
  48. -- "$@")
  49. if [ $? != 0 ] ; then
  50. usage
  51. echo "Invalid args" >&2
  52. exit 3
  53. fi
  54. echo "DEBUG: opts ${OPTS}"
  55. USINGLOCALREPO=0
  56. eval set -- "${OPTS}"
  57. while true ; do
  58. case "$1" in
  59. --puppet-master)
  60. MASTER=$2 ; shift 2
  61. ;;
  62. --repo-file)
  63. REPOFILE=$2 ; shift 2
  64. ;;
  65. --gpg-key-files)
  66. GPGKEYFILESTR=$2 ; shift 2
  67. ;;
  68. --using-local-repo)
  69. USINGLOCALREPO=1; shift
  70. ;;
  71. --help)
  72. usage ;
  73. exit 0
  74. ;;
  75. --)
  76. shift ; break
  77. ;;
  78. *)
  79. echo "Unknown option: $1" >&2
  80. usage
  81. exit 1
  82. ;;
  83. esac
  84. done
  85. if [[ "x" == "x${MASTER}" ]]; then
  86. echo "Error: Puppet master not specified" >&2
  87. exit 3
  88. fi
  89. if [[ "x" == "x${REPOFILE}" ]]; then
  90. echo "Error: Repo file not specified" >&2
  91. exit 3
  92. fi
  93. if [[ "x" != "x${GPGKEYFILESTR}" ]]; then
  94. GPGKEYFILES=$(echo ${GPGKEYFILESTR} | tr "," " ")
  95. fi
  96. master=${MASTER}
  97. repoFile=${REPOFILE}
  98. gpgKeyFiles=${GPGKEYFILES}
  99. usingLocalRepo=${USINGLOCALREPO}
  100. echo "DEBUG: Puppet Master: ${master}"
  101. echo "DEBUG: Repo File: ${repoFile}"
  102. echo "DEBUG: GPG Key File Locations: ${gpgKeyFiles}"
  103. osType=''
  104. osMajorVersion=''
  105. if [ -f /usr/bin/lsb_release ] ; then
  106. osType=$( lsb_release -sd | tr '[:upper:]' '[:lower:]' | tr '"' ' ' | awk '{ for(i=1; i<=NF; i++) { if ( $i ~ /[0-9]+/ ) { cnt=split($i, arr, "."); if ( cnt > 1) { print arr[1] } else { print $i; } break; } print $i; } }' )
  107. osMajorVersion=`lsb_release -sd | tr '[:upper:]' '[:lower:]' | tr '"' ' ' | awk '{ for(i=1; i<=NF; i++) { if ( $i ~ /[0-9]+/ ) { cnt=split($i, arr, "."); if ( cnt > 1) { print arr[1] } else { print $i; } break; } } }'`
  108. else
  109. osType=$( cat `ls /etc/*release | grep "redhat\|SuSE"` | head -1 | awk '{ for(i=1; i<=NF; i++) { if ( $i ~ /[0-9]+/ ) { cnt=split($i, arr, "."); if ( cnt > 1) { print arr[1] } else { print $i; } break; } print $i; } }' | tr '[:upper:]' '[:lower:]' )
  110. osMajorVersion=`cat \`ls /etc/*release | grep "redhat\|SuSE"\` | head -1 | awk '{ for(i=1; i<=NF; i++) { if ( $i ~ /[0-9]+/ ) { cnt=split($i, arr, "."); if ( cnt > 1) { print arr[1] } else { print $i; } break; } } }' | tr '[:upper:]' '[:lower:]'`
  111. fi
  112. osType=`echo ${osType} | sed -e "s/ *//g"`
  113. osArch=`uname -m`
  114. if [[ "xi686" == "x${osArch}" || "xi386" == "x${osArch}" ]]; then
  115. osArch="i386"
  116. fi
  117. if [[ "xx86_64" == "x${osArch}" || "xamd64" == "x${osArch}" ]]; then
  118. osArch="x86_64"
  119. fi
  120. echo "DEBUG: OS Type ${osType}"
  121. echo "DEBUG: OS Arch ${osArch}"
  122. echo "DEBUG: OS Major Version ${osMajorVersion}"
  123. if [[ ! -f ${repoFile} ]]; then
  124. echo "Error: Repo file ${repoFile} does not exist" >&2
  125. exit 3
  126. else
  127. echo "Copying $repoFile to /etc/yum.repos.d/ for ${osType} ${osMajorVersion}"
  128. if [[ "x${usingLocalRepo}" == "x0" ]]; then
  129. osReplaceStr=""
  130. if [[ "x${osMajorVersion}" == "x5" ]]; then
  131. osReplaceStr="centos5"
  132. fi
  133. if [[ "x${osMajorVersion}" == "x6" ]]; then
  134. osReplaceStr="centos6"
  135. fi
  136. if [[ "x${osReplaceStr}" != "x" ]]; then
  137. sed -i -e "s/centos[0-9]/${osReplaceStr}/g" ${repoFile}
  138. fi
  139. fi
  140. cp -f $repoFile /etc/yum.repos.d/
  141. fi
  142. repoFileName=`basename $repoFile`
  143. if [[ ! -f "/etc/yum.repos.d/${repoFileName}" ]]; then
  144. echo "Error: Repo file ${repoFile} not copied over to /etc/yum.repos.d/" >&2
  145. exit 3
  146. fi
  147. for gpgKeyFile in ${gpgKeyFiles}
  148. do
  149. if [[ ! -f ${gpgKeyFile} ]]; then
  150. echo "Error: Specified GPG key file ${gpgKeyFile} does not exist" >&2
  151. exit 3
  152. fi
  153. echo "Copying ${gpgKeyFile} to /etc/pki/rpm-gpg/"
  154. cp -f ${gpgKeyFile} /etc/pki/rpm-gpg/
  155. gpgKeyFileName=`basename ${gpgKeyFile}`
  156. if [[ ! -f "/etc/pki/rpm-gpg/${gpgKeyFileName}" ]]; then
  157. echo "Error: GPG key file ${gpgKeyFile} not copied over to /etc/pki/rpm-gpg/" >&2
  158. exit 3
  159. fi
  160. done
  161. host=`hostname -f | tr '[:upper:]' '[:lower:]'`
  162. out=`/etc/init.d/iptables stop 1>/dev/null`
  163. #check if epel repo is installed if not try installing
  164. #only needed if non-local repo mode
  165. epelVer="";
  166. if [[ "x${osMajorVersion}" == "x5" ]]; then
  167. epelVer="5-4"
  168. fi
  169. if [[ "x${osMajorVersion}" == "x6" ]]; then
  170. epelVer="6-7"
  171. fi
  172. # Assumption that earlier stage is already doing a check on valid os types
  173. # so should not reach here for non-CentOS/RHEL 5/6 hosts
  174. epelRPMUrl="http://download.fedoraproject.org/pub/epel/${osMajorVersion}/${osArch}/epel-release-${epelVer}.noarch.rpm"
  175. echo "Using local repo setting is ${usingLocalRepo}"
  176. if [[ "${usingLocalRepo}" == "0" ]]; then
  177. echo "Checking to see if epel needs to be installed"
  178. epel_installed=`yum repolist enabled | grep epel`
  179. if [[ "x$epel_installed" != "x" ]]; then
  180. echo "Already Installed epel repo"
  181. else
  182. echo "Installing epel-release rpm from ${epelRPMUrl}"
  183. mkdir -p /tmp/HDP-artifacts/
  184. curl -L -f --retry 10 $epelRPMUrl -o /tmp/HDP-artifacts/epel-release-${osMajorVersion}.noarch.rpm
  185. rpm -Uvh /tmp/HDP-artifacts/epel-release-${osMajorVersion}.noarch.rpm
  186. #make sure epel is installed else fail
  187. epel_installed=`yum repolist enabled | grep epel`
  188. if [[ "x$epel_installed" == "x" ]]; then
  189. echo "$host:_ERROR_:retcode:[1], CMD:[rpm -Uvh $epelRPMUrl]: OUT:[Not Installed]" >&2
  190. exit 1
  191. fi
  192. fi
  193. else
  194. echo "Skipping epel check+install as local repo mode is enabled"
  195. fi
  196. echo "Installing puppet using yum"
  197. out=`yum install -y hmc-agent`
  198. ret=$?
  199. if [[ "$ret" != "0" ]]; then
  200. echo "$host:_ERROR_:retcode:[$ret], CMD:[$pp_cmd]: OUT:[$out]" >&2
  201. exit 1
  202. fi
  203. #Install ruby
  204. out=`yum install -y ruby-devel rubygems`
  205. ret=$?
  206. if [[ "$ret" != "0" ]]; then
  207. echo "$host:_ERROR_:retcode:[$ret], CMD:[$pp_cmd]: OUT:[$out]" >&2
  208. exit 1
  209. fi
  210. out=`echo $master > /etc/hmc/hmc-agent.conf`
  211. out=`mkdir -p /etc/puppet/agent 2>&1`
  212. agent_auth_conf="path /run\nauth any\nallow $master\n\npath /\nauth any"
  213. out=`echo -e $agent_auth_conf > /etc/puppet/agent/auth.conf`
  214. out=`touch /etc/puppet/agent/namespaceauth.conf`
  215. out=`cp -f /etc/puppet/puppet.conf /etc/puppet/agent/ 2>&1`
  216. ret=$?
  217. if [[ "$ret" != "0" ]]; then
  218. echo "$host:_ERROR_:retcode:[$ret], CMD:[$pp_cmd]: OUT:[$out]" >&2
  219. exit 1
  220. fi
  221. #TODO clean this up for better fix. For now make sure we stop puppet agent. The issue here is we do not know if we started this puppet agent during our run or not.
  222. echo "Stopping puppet agent using service stop command"
  223. out=`service hmc-agent stop`
  224. ret=$?
  225. echo "Starting puppet agent for HMC"
  226. out=`service hmc-agent start`
  227. ret=$?
  228. if [[ "$ret" != "0" ]]; then
  229. echo "$host:_ERROR_:retcode:[$ret], CMD:[$pp_cmd]: OUT:[$out]" >&2
  230. exit 1
  231. fi
  232. echo "Setting chkconfig for HMC"
  233. out=`chkconfig --add hmc-agent`
  234. ret=$?
  235. #if [[ "$ret" != "0" ]]; then
  236. # echo "$host:_ERROR_:retcode:[$ret], CMD:[$pp_cmd]: OUT:[$out]" >&2
  237. # exit 1
  238. #fi
  239. exit 0