#!/bin/bash
#Nifty: Installs puppet agent.

# shellcheck source=../../lib/universal/niftylib.sh
source <(curl -fSs https://nifty.sh/lib/universal/niftylib.sh)

# Requirements
require_root
require_64bit

# Require OS
require_os_distro centos almalinux debian ubuntu
case "$OS_DISTRO" in
  centos|almalinux) require_os_version 6 7 8 9 10 ;;
  debian) require_os_version 10 11 12 ;;
  ubuntu) require_os_version 14 16 18 20 22 24 ;;
esac

# Get list of server IP addresses
IPS=$(ip -br addr | awk '$1 != "lo" { print }' | sed -r -e 's/ +/ /g' -e 's/\/[0-9]+//g' | cut -f3- -d' ')

# Build appendable HTTP GET list of these IPs
IPLIST=""
for ip in $IPS; do
  IPLIST="${IPLIST}&ips\[\]=$ip"
done

# Check if server is in preprod
ce "Looking up server"
NETADMIN_SERVER="$(hostname)"
while :; do
  PUPPET_CONFIG="$(curl -Ss "https://puppetman1.zitcom.dk/utils/install/linuxserver?server=$NETADMIN_SERVER$IPLIST")"
  if [ "$PUPPET_CONFIG" = "none" ]; then
    err "The server could not be found in Netadmin."
  elif [ "$PUPPET_CONFIG" = "multiple" ]; then
    err "There are multiple results in Netadmin for this server's hostname and IP addresses."
  elif [ "$PUPPET_CONFIG" = "notpreprod" ]; then
    err "Server is not in preproduction or test."
    is_interactive || exit 1
    prompt_continue "Press Enter to try again."
    continue
  elif [ -z "$PUPPET_CONFIG" ] || ! grep -q 'puppet.zitcom.dk' <<< "$PUPPET_CONFIG"; then
    err "Recieved empty or invalid puppet configuration:"
    is_interactive || exit 1
    prompt_continue "Press Enter to try again."
    continue
  else
    break
  fi

  is_interactive || exit 1
  err "Please provide the server's Netadmin UUID:"
  read -r NETADMIN_SERVER
done

# Now we should be ready to install
if [ "$OS_FAMILY" = "rhel" ]; then
  ce "Installing puppet repository"
  if compare_versions "$OS_VERSION" "<=" "9"; then
    rpm -Uvh "https://yum.puppet.com/puppet7-release-el-$OS_VERSION.noarch.rpm"
  else
    rpm -Uvh "https://yum.voxpupuli.org/openvox8-release-el-$OS_VERSION.noarch.rpm"
  fi

  ce "Installing puppet agent"
  if compare_versions "$OS_VERSION" "<=" "7"; then
    yum install -y puppet-agent
  elif compare_versions "$OS_VERSION" "<=" "9"; then
    dnf install -y puppet-agent
  else
    dnf install -y openvox-agent
  fi
fi

if [[ "$OS" == "ubuntu"* || "$OS" == "debian"* ]]; then
  [ "$OS" = "ubuntu1404" ] && CODENAME="trusty"
  [ "$OS" = "ubuntu1604" ] && CODENAME="xenial"
  [ "$OS" = "ubuntu1804" ] && CODENAME="bionic"
  [ "$OS" = "ubuntu2004" ] && CODENAME="focal"
  [ "$OS" = "ubuntu2204" ] && CODENAME="jammy"
  [ "$OS" = "ubuntu2404" ] && CODENAME="noble"
  [ "$OS" = "debian10" ] && CODENAME="buster"
  [ "$OS" = "debian11" ] && CODENAME="bullseye"
  [ "$OS" = "debian12" ] && CODENAME="bookworm"

  wget "https://apt.puppetlabs.com/puppet7-release-$CODENAME.deb"
  dpkg -i "puppet7-release-$CODENAME.deb"

  apt-get update
  ce "Installing puppet agent"
  apt-get install -y puppet-agent
fi

ce "Configuring puppet.conf"
tee "/etc/puppetlabs/puppet/puppet.conf" <<< "$PUPPET_CONFIG" &> /dev/null

ce "Enabling puppet service"
if [ "$OS" = "centos6" ]; then
    chkconfig puppet on
elif [ "$OS" = "ubuntu1404" ]; then
    /opt/puppetlabs/bin/puppet agent --enable
else
  systemctl enable puppet
fi

ce "Now running puppet agent to obtain cert"
/opt/puppetlabs/bin/puppet agent -vt --waitforcert 10

echo
echo
echo

ce "Now starting the puppet service"
if [[ "$OS" = "centos6" || "$OS" = "ubuntu1404" ]]; then
    /etc/init.d/puppet start
else
  systemctl start puppet
fi

ce "All set!"

if [ -f "/tmp/puppet_conf" ]; then
  rm -f /tmp/puppet_conf
fi
