#!/usr/bin/env bash
# Install Puppet Agent bootstrap script

# ---------------------------------------------------------------------------
#                                                                 usage
# ---------------------------------------------------------------------------

script=$(basename $0)

USAGE="\
Usage:  $script -h
Where:  -h|--help Invokes help text
"

usage()
{
  echo "$USAGE"
  exit 0
}


# ---------------------------------------------------------------------------
#                                                                 functions
# ---------------------------------------------------------------------------

# Display current status
--()
{
  echo "$@"
}

# Fatal error; bail
fatal()
{
  echo "$1" >> /dev/stderr
  exit 1
}

# This is for catching unexpected errors.  Bash's implementation of this is
# problematic, but it's a last-ditch catchall for errors.
trap_err()
{
  rc=$1
  lineno=$2
  func=$3
  cat <<EOF >>/dev/stderr
Unexpected failure of subshell command.  Preceeding lines may offer some clue.
Please consider updating the script with adequate testing.

Exit status: $rc
Line:        $lineno
Function:    $func
EOF
}

# Ubuntu is a little more complicated than RH variants because apt and dpkg
# do not accept URLs as packages
ubuntu_install()
{
  url=$1

  tmpf=$(mktemp) || fatal "Could not create temporary file for package download"
  curl $url -o $tmpf || fatal "Could not download Puppet repo package from '$url' to '$tmpf'"
  dpkg -i $tmpf || fatal "Could not install Puppet repo package"
  rm $tmpf
  apt update
}

# ---------------------------------------------------------------------------
#                                                                 init
# ---------------------------------------------------------------------------

# exit immediately if a pipeline, subshell command or command enclosed in 
# braces fails
set -e

# and should that happen, print out a message with any available information
trap 'trap_err $? $LINENO ${FUNCNAME}' ERR

# interpret parameters
if [ "$1" == "-h" -o "$1" == "--help" ]
then
  usage
fi

# ---------------------------------------------------------------------------
#                                                          preflight checks
# ---------------------------------------------------------------------------

# get OS information if available
OSINFO=/etc/os-release
if [ -f $OSINFO ]
then
  . $OSINFO
else
  fatal "Could not find $OSINFO; what are you running?"
fi

echo "Puppet 7 selected"
YUM_REPO_URL="https://yum.puppet.com/puppet7/puppet7-release-el-${VERSION_ID}.noarch.rpm"
APT_REPO_URL="https://apt.puppet.com/puppet7-release-${VERSION_CODENAME}.deb"
 
# determine what repo we should be using and commands to run
case "$ID" in
  centos|rhel)
    REPO_URL=$YUM_REPO_URL
    INSTALL_REPO="yum -y install ${REPO_URL}"
    INSTALL_PUPPET="yum -y install puppet-agent"
    REMOVE_PUPPET="yum -yq remove puppet6-release"
    ;;
  ubuntu)
    REPO_URL=$APT_REPO_URL
    INSTALL_REPO="ubuntu_install ${REPO_URL}"
    INSTALL_PUPPET="apt install -y puppet-agent"
    REMOVE_PUPPET="apt remove -yq puppet6-release"
    ;;
  *)
    fatal " $ID :: Unrecognized Linux distribution: no configured Puppet repository"
    ;;
esac

# Make sure that they are root
if [ "$EUID" -ne "0" ]
then
  fatal "You must be root to install Puppet."
fi

# ---------------------------------------------------------------------------
#                                                                 main
# ---------------------------------------------------------------------------

-- Removing old release package for Puppet repository
if ! $REMOVE_PUPPET
then
 fatal "Unable to remove old release package puppet6-release\*"
fi

-- Installing release package for Puppet repository
if ! $INSTALL_REPO
then
  fatal "Unable to install release package from $REPO_URL"
fi

-- Installing Puppet Agent
if ! $INSTALL_PUPPET
then
  fatal "Unable to install Puppet"
fi


-- Updating path to include Puppet
export PATH=/opt/puppetlabs/bin:$PATH

cat <<EOF

This host has been upgraded to Puppet 7.x

EOF

