puppet_agent_install.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. "
  38. }
  39. OPTS=$(getopt \
  40. -n $0 \
  41. -o '' \
  42. -l 'puppet-master:' \
  43. -l 'repo-file:' \
  44. -l 'gpg-key-files:' \
  45. -l 'help' \
  46. -- "$@")
  47. if [ $? != 0 ] ; then
  48. usage
  49. echo "Invalid args" >&2
  50. exit 3
  51. fi
  52. echo "DEBUG: opts ${OPTS}"
  53. eval set -- "${OPTS}"
  54. while true ; do
  55. case "$1" in
  56. --puppet-master)
  57. MASTER=$2 ; shift 2
  58. ;;
  59. --repo-file)
  60. REPOFILE=$2 ; shift 2
  61. ;;
  62. --gpg-key-files)
  63. GPGKEYFILESTR=$2 ; shift 2
  64. ;;
  65. --help)
  66. usage ;
  67. exit 0
  68. ;;
  69. --)
  70. shift ; break
  71. ;;
  72. *)
  73. echo "Unknown option: $1" >&2
  74. usage
  75. exit 1
  76. ;;
  77. esac
  78. done
  79. if [[ "x" == "x${MASTER}" ]]; then
  80. echo "Error: Puppet master not specified" >&2
  81. exit 3
  82. fi
  83. if [[ "x" == "x${REPOFILE}" ]]; then
  84. echo "Error: Repo file not specified" >&2
  85. exit 3
  86. fi
  87. if [[ "x" != "x${GPGKEYFILESTR}" ]]; then
  88. GPGKEYFILES=$(echo ${GPGKEYFILESTR} | tr "," " ")
  89. fi
  90. master=${MASTER}
  91. repoFile=${REPOFILE}
  92. gpgKeyFiles=${GPGKEYFILES}
  93. echo "DEBUG: Puppet Master: ${master}"
  94. echo "DEBUG: Repo File: ${repoFile}"
  95. echo "DEBUG: GPG Key File Locations: ${gpgKeyFiles}"
  96. if [[ ! -f ${repoFile} ]]; then
  97. echo "Error: Repo file ${repoFile} does not exist" >&2
  98. exit 3
  99. else
  100. echo "Copying $repoFile to /etc/yum.repos.d/"
  101. cp -f $repoFile /etc/yum.repos.d/
  102. fi
  103. repoFileName=`basename $repoFile`
  104. if [[ ! -f "/etc/yum.repos.d/${repoFileName}" ]]; then
  105. echo "Error: Repo file ${repoFile} not copied over to /etc/yum.repos.d/" >&2
  106. exit 3
  107. fi
  108. for gpgKeyFile in ${gpgKeyFiles}
  109. do
  110. if [[ ! -f ${gpgKeyFile} ]]; then
  111. echo "Error: Specified GPG key file ${gpgKeyFile} does not exist" >&2
  112. exit 3
  113. fi
  114. echo "Copying ${gpgKeyFile} to /etc/pki/rpm-gpg/"
  115. cp -f ${gpgKeyFile} /etc/pki/rpm-gpg/
  116. gpgKeyFileName=`basename ${gpgKeyFile}`
  117. if [[ ! -f "/etc/pki/rpm-gpg/${gpgKeyFileName}" ]]; then
  118. echo "Error: GPG key file ${gpgKeyFile} not copied over to /etc/pki/rpm-gpg/" >&2
  119. exit 3
  120. fi
  121. done
  122. host=`hostname -f | tr '[:upper:]' '[:lower:]'`
  123. out=`/etc/init.d/iptables stop 1>/dev/null`
  124. echo "Installing puppet using yum"
  125. out=`yum install -y hmc-agent`
  126. ret=$?
  127. if [[ "$ret" != "0" ]]; then
  128. echo "$host:_ERROR_:retcode:[$ret], CMD:[$pp_cmd]: OUT:[$out]" >&2
  129. exit 1
  130. fi
  131. #Install ruby
  132. out=`yum install -y ruby-devel rubygems`
  133. ret=$?
  134. if [[ "$ret" != "0" ]]; then
  135. echo "$host:_ERROR_:retcode:[$ret], CMD:[$pp_cmd]: OUT:[$out]" >&2
  136. exit 1
  137. fi
  138. out=`echo $master > /etc/hmc/hmc-agent.conf`
  139. out=`mkdir -p /etc/puppet/agent 2>&1`
  140. agent_auth_conf="path /run\nauth any\nallow $master\n\npath /\nauth any"
  141. out=`echo -e $agent_auth_conf > /etc/puppet/agent/auth.conf`
  142. out=`touch /etc/puppet/agent/namespaceauth.conf`
  143. out=`cp -f /etc/puppet/puppet.conf /etc/puppet/agent/ 2>&1`
  144. ret=$?
  145. if [[ "$ret" != "0" ]]; then
  146. echo "$host:_ERROR_:retcode:[$ret], CMD:[$pp_cmd]: OUT:[$out]" >&2
  147. exit 1
  148. fi
  149. #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.
  150. echo "Stopping puppet agent using service stop command"
  151. out=`service hmc-agent stop`
  152. ret=$?
  153. echo "Starting puppet agent for HMC"
  154. out=`service hmc-agent start`
  155. ret=$?
  156. if [[ "$ret" != "0" ]]; then
  157. echo "$host:_ERROR_:retcode:[$ret], CMD:[$pp_cmd]: OUT:[$out]" >&2
  158. exit 1
  159. fi
  160. echo "Setting chkconfig for HMC"
  161. out=`chkconfig --add hmc-agent`
  162. ret=$?
  163. #if [[ "$ret" != "0" ]]; then
  164. # echo "$host:_ERROR_:retcode:[$ret], CMD:[$pp_cmd]: OUT:[$out]" >&2
  165. # exit 1
  166. #fi
  167. exit 0