#!/bin/bash
#Nifty: Upgrades puppet and migrates to new cluster.
curl -s https://nifty.sh/lib/universal/niftylib.sh > /tmp/lib.nifty.sh
. /tmp/lib.nifty.sh
rm -f /tmp/lib.nifty.sh

# Requirements
require_root
require_64bit

# Require OS
OS=`detect_os`
if [[ "$OS" != "centos6" && "$OS" != "centos7" && "$OS" != "ubuntu1404" && "$OS" != "ubuntu1604" && "$OS" != "ubuntu1804" ]]; then
  err "This script supports only CentOS 6, 7 and Ubuntu 14.04, 16.04 and 18.04. Sorry"
  curl -s "https://puppetman1.zitcom.dk/upgrade/posterror" -d "error=`hostname` is not CentOS 6, 7 or Ubuntu 14, 16 or 18 LTS. Upgrade failed"
  exit 1
else
  ce "Detected $OS - continuing"
fi

# Check required endpoints and repos exist before continuing.
curl --silent --output /dev/null -m 2 https://puppetman1.zitcom.dk
if [ $? != 0 ]
then
  ce "puppetman not reachable. Check networking"
  exit 1
fi

#CentOS Repo
if [[ "$OS" == "centos6" || "$OS" == "centos7" ]]; then
  if wget --spider https://yum.puppet.com/puppet7-release-el-7.noarch.rpm 2>/dev/null; then
    ce "Puppetlabs repo accessible"
  else
    ce "Can't reach puppetlabs repo, aborting"
    exit 1
  fi
fi

#Ubuntu Repo
if [[ "$OS" == "ubuntu1404" || "$OS" == "ubuntu1604" || "$OS" == "ubuntu1804" ]]; then
  if wget --spider https://apt.puppetlabs.com/puppet7-release-bionic.deb 2>/dev/null; then
    ce "Puppetlabs repo accessible"
  else
    ce "Can't reach puppetlabs repo, aborting"
    exit 1
  fi
fi

# Ensure that this server uses old puppet
ce "Checking puppet.conf"
PUPPETSERVER=$(cat /etc/puppet/puppet.conf  |grep server |awk -F'=' '{print $2}')

if [[ "${PUPPETSERVER//[[:space:]]/}" != "puppetmaster1.zitcom.dk" && "${PUPPETSERVER//[[:space:]]/}" != "puppetmaster2.zitcom.dk" ]]; then
  err "Not using old puppetmaster"
  exit 1
fi

ce "Getting path for my certificate"
CERTFILE=$(puppet config print hostcert)

ce "Getting path for my key"
PRIVKEY=$(puppet config print hostprivkey)

ce "Getting my certname"
MYCERT=$(puppet config print certname)


# Create a file with our certname in it
ce "Generating signature based on our key"
SIGNATURE=$(openssl dgst -sha256 -sign $PRIVKEY $CERTFILE  |openssl base64)

# Now call the API and send the files
ce "Calling PuppetMan API"
PREPARE=$(curl -s -F "certificate=@$CERTFILE" -F "signature=$SIGNATURE" https://puppetman1.zitcom.dk/upgrade/prepare/$MYCERT)
PREPARESTATE=$(echo $PREPARE |awk '{print $1}')
UUID=$(echo $PREPARE |awk '{print $2}')

if [ "$PREPARESTATE" != "UUID" ]; then
  err $PREPARE
  exit 1
fi

ce "Got UUID: $UUID"
ce "Removing old puppet agent and puppetlabs-release"

if [[ "$OS" == "centos6" || "$OS" == "centos7" ]]; then
  yum remove -y puppet puppetlabs-release

  ce "Installing puppet repository"
  if [ "$OS" = "centos6" ]; then
    rpm -Uhv https://yum.puppetlabs.com/puppet7-release-el-6.noarch.rpm
  else
    rpm -Uhv https://yum.puppet.com/puppet7-release-el-7.noarch.rpm
  fi

  ce "Installing puppet agent"
  yum install puppet-agent -y
fi

if [[ "$OS" == "ubuntu1404" || "$OS" == "ubuntu1604" || "$OS" == "ubuntu1804" ]]; then
  apt-get purge --auto-remove puppet -y

  ce "Installing puppet repository"
  if [ "$OS" = "ubuntu1404" ]; then
    wget https://apt.puppetlabs.com/puppet7-release-trusty.deb
    dpkg -i puppet7-release-trusty.deb
  elif [ "$OS" = "ubuntu1604" ]; then
    wget https://apt.puppetlabs.com/puppet7-release-xenial.deb
    dpkg -i puppet7-release-xenial.deb
  else
    wget https://apt.puppetlabs.com/puppet7-release-bionic.deb
    dpkg -i puppet7-release-bionic.deb
  fi

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

ce "Configuring puppet.conf"
curl -s https://mirror.zitcom.dk/internal/nifty.sh/puppetcluster/puppet.conf > /etc/puppetlabs/puppet/puppet.conf

sed -i "s/INSERT-SERVER-UUID-HERE/$UUID/" /etc/puppetlabs/puppet/puppet.conf

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

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

ce "If all is good, this server is now in new Puppet!"
curl -s "https://puppetman1.zitcom.dk/upgrade/posterror" -d "error=`hostname` has finished upgrade"

ce "Calling finish-api"
PREPARE=$(curl -s -X PUT https://puppetman1.zitcom.dk/upgrade/finish/$UUID)
