From 11e8717dba9cde098f8155aa5ca3d324d4b5334e Mon Sep 17 00:00:00 2001
From: Ian Wienand <iwienand@redhat.com>
Date: Tue, 1 Jul 2014 14:06:05 +1000
Subject: [PATCH] Add functions & cleanup install_puppet.sh

A small clean-up of the install_puppet.sh script to enhance
readability and to simplify adding support for extra distributions

 - add distribution check functions
 - move puppet installation for each distribution into its own
   separate function
 - move pip installation into a function
 - add/expand several comments

Change-Id: I5d5f71fde607ace528b7372e2deadccbea58bd2f
---
 install_puppet.sh | 123 +++++++++++++++++++++++++++++++++-------------
 1 file changed, 90 insertions(+), 33 deletions(-)

diff --git a/install_puppet.sh b/install_puppet.sh
index b2b742c644..0d3d49003c 100755
--- a/install_puppet.sh
+++ b/install_puppet.sh
@@ -16,36 +16,36 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
-# Install pip using get-pip
-PIP_GET_PIP_URL=https://bootstrap.pypa.io/get-pip.py
+#
+# Distro identification functions
+#  note, can't rely on lsb_release for these as we're bare-bones and
+#  it may not be installed yet)
+#
 
-ret=1
-if [ -f ./get-pip.py ]; then
-    ret=0
-elif type curl >/dev/null 2>&1; then
-    curl -O $PIP_GET_PIP_URL
-    ret=$?
-elif type wget >/dev/null 2>&1; then
-    wget $PIP_GET_PIP_URL
-    ret=$?
-fi
+function is_fedora {
+    [ -f /usr/bin/yum ] && cat /etc/*release | grep -q -e "Fedora"
+}
 
-if [ $ret -ne 0 ]; then
-    echo "Failed to get get-pip.py"
-    exit 1
-fi
+function is_rhel6 {
+    [ -f /usr/bin/yum ] && \
+        cat /etc/*release | grep -q -e "Red Hat" -e "CentOS" && \
+        cat /etc/*release | grep -q 'release 6'
+}
 
-python get-pip.py
+function is_ubuntu {
+    [ -f /usr/bin/apt-get ]
+}
 
-# Install puppet version 2.7.x from puppetlabs.
-# The repo and preferences files are also managed by puppet, so be sure
-# to keep them in sync with this file.
 
-if cat /etc/*release | grep -e "Fedora" &> /dev/null; then
+#
+# Distro specific puppet installs
+#
 
+function setup_puppet_fedora {
     yum update -y
 
-    # NOTE: we preinstall lsb_release to ensure facter sets lsbdistcodename
+    # NOTE: we preinstall lsb_release to ensure facter sets
+    # lsbdistcodename
     yum install -y redhat-lsb-core git puppet
 
     gem install hiera hiera-puppet
@@ -53,13 +53,31 @@ if cat /etc/*release | grep -e "Fedora" &> /dev/null; then
     mkdir -p /etc/puppet/modules/
     ln -s /usr/local/share/gems/gems/hiera-puppet-* /etc/puppet/modules/
 
-    # Puppet is expecting the command to be pip-python on Fedora
+    # Puppet expects for an expects the command to be pip-python on
+    # Fedora, as per the packaged command name.  However, we're
+    # installing from get-pip.py so it's just 'pip'.  An easy
+    # work-around is to just symlink pip-python to "fool" it.
+    # See upstream issue:
+    #  https://tickets.puppetlabs.com/browse/PUP-1082
     ln -s /usr/bin/pip /usr/bin/pip-python
+}
 
-elif cat /etc/*release | grep -e "CentOS" -e "Red Hat" &> /dev/null; then
-    rpm -qi epel-release &> /dev/null || rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
-    rpm -ivh http://yum.puppetlabs.com/el/6/products/x86_64/puppetlabs-release-6-6.noarch.rpm
+function setup_puppet_rhel6 {
+    local epel_pkg="http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm"
+    local puppet_pkg="http://yum.puppetlabs.com/el/6/products/x86_64/puppetlabs-release-6-6.noarch.rpm"
 
+    # install EPEL
+    rpm -qi epel-release &> /dev/null || rpm -Uvh $epel_pkg
+    # NOTE: for RHEL (not CentOS) enable the optional-rpms channel (if
+    # not already enabled)
+    # yum-config-manager --enable rhel-6-server-optional-rpms
+
+    # NOTE: we preinstall lsb_release to ensure facter sets lsbdistcodename
+    yum install -y redhat-lsb-core git puppet
+
+    rpm -ivh $puppet_pkg
+
+    # ensure we stick to supported puppet 2 versions
     cat > /etc/yum.repos.d/puppetlabs.repo <<"EOF"
 [puppetlabs-products]
 name=Puppet Labs Products El 6 - $basearch
@@ -71,14 +89,9 @@ exclude=puppet-2.8* puppet-2.9* puppet-3* facter-2*
 EOF
 
     yum update -y
-    # NOTE: enable the optional-rpms channel (if not already enabled)
-    # yum-config-manager --enable rhel-6-server-optional-rpms
-
-    # NOTE: we preinstall lsb_release to ensure facter sets lsbdistcodename
-    yum install -y redhat-lsb-core git puppet
-else
-    #defaults to Ubuntu
+}
 
+function setup_puppet_ubuntu {
     lsbdistcodename=`lsb_release -c -s`
     if [ $lsbdistcodename != 'trusty' ] ; then
         # NB: keep in sync with openstack_project/files/00-puppet.pref
@@ -105,4 +118,48 @@ EOF
         --assume-yes dist-upgrade
     DEBIAN_FRONTEND=noninteractive apt-get --option 'Dpkg::Options::=--force-confold' \
         --assume-yes install -y --force-yes puppet git $rubypkg
+}
+
+#
+# pip setup
+#
+
+function setup_pip {
+    # Install pip using get-pip
+    local get_pip_url=https://bootstrap.pypa.io/get-pip.py
+    local ret=1
+
+    if [ -f ./get-pip.py ]; then
+        ret=0
+    elif type curl >/dev/null 2>&1; then
+        curl -O $get_pip_url
+        ret=$?
+    elif type wget >/dev/null 2>&1; then
+        wget $get_pip_url
+        ret=$?
+    fi
+
+    if [ $ret -ne 0 ]; then
+        echo "Failed to get get-pip.py"
+        exit 1
+    fi
+
+    python get-pip.py
+}
+
+#
+# Install pip & puppet
+#
+
+setup_pip
+
+if is_fedora; then
+    setup_puppet_fedora
+elif is_rhel6; then
+    setup_puppet_rhel6
+elif is_ubuntu; then
+    setup_puppet_ubuntu
+else
+    echo "*** Can not setup puppet: distribution not recognized"
+    exit 1
 fi