#!/bin/bash

#DESC Inplace OS upgrade from RH7 to RH8 - RTI.

#check for passport
#ls /dev | grep sd > /tmp/drives.out
#[ "`ls /dev | grep sdb`" != "" ] && mail -s "`hostname` - passport may be connected. upgrade halted." mgreen@teleflora.com,kpugh@teleflora.com,tshilling@teleflora.com < /tmp/drives.out && exit 1 

#Make sure tfethX naming.
[ "`ifconfig | grep tfeth`" == "" ] && echo "Run change_nics first....." && exit 1

echo "---> Upgrading OS to RHEL8 ..." | tee -a /tmp/updateos.log
echo "" | tee -a /tmp/updateos.log

# save current hostname and firewall rules
rm -rf /usr2/upgrade
mkdir /usr2/upgrade
hostname > /usr2/upgrade/hostname
cp /etc/sysconfig/iptables /usr2/upgrade/.

# clear out repos and install current rh7 OS repos
rm -f /etc/yum/repos.d/*
#updateos rtirepo_install
subscription-manager register --activationkey=TelefloraPOS --org=4508688

# save off ups conf file if there.
[ -f /etc/apcupsd/apcupsd.conf ] && cp -f /etc/apcupsd/apcupsd.conf /usr2/upgrade/apcupsd.conf

# add rh7 extras repo.
#cat > /etc/yum.repos.d/tfrhel7extras.repo << EOF
#[RH7extras]
#name=RH7extras
#baseurl=http://tfrhelrepo.centralus.cloudapp.azure.com/rhel-7-server-extras-rpms
#enabled=1
#gpgcheck=0
#EOF

# remove nodesource repo and nodejs
yum -y remove nodejs nodesource-release

# add leapp package
yum -y install leapp-upgrade-el7toel8

# add leapp repos
cat > /etc/leapp/files/leapp_upgrade_repositories.repo << EOF
[RH8AppStream]
name=RH8AppStream
baseurl=http://rhel8repo.centralus.cloudapp.azure.com/rhel-8-for-x86_64-appstream-rpms

[RH8BaseOS]
name=RH8BaseOS
baseurl=http://rhel8repo.centralus.cloudapp.azure.com/rhel-8-for-x86_64-baseos-rpms

EOF

#download leapp metadata
#cd /etc/leapp/files
#wget http://rhel8repo.centralus.cloudapp.azure.com/support/leapp-data-22.tar.gz
#tar xvfz ./leapp-data-22.tar.gz

#create leapp answer file
cat > /var/log/leapp/answerfile << EOF
[remove_pam_pkcs11_module_check]
# Title:              None
# Reason:             Confirmation
# =================== remove_pam_pkcs11_module_check.confirm ==================
# Label:              Disable pam_pkcs11 module in PAM configuration? If no, the upgrade process will be interrupted.
# Description:        PAM module pam_pkcs11 is no longer available in RHEL-8 since it was replaced by SSSD.
# Reason:             Leaving this module in PAM configuration may lock out the system.
# Type:               bool
# Default:            None
# Available choices: True/False
# Unanswered question. Uncomment the following line with your answer
confirm = True
EOF

#add script to run after reboot.
updateos post_install_rh8

#remove rh8 unsupported pam.d modules.
cp -rp /etc/pam.d /tmp
sed -i '/pam_tally2/d' /etc/pam.d/*
sed -i '/pam_pkcs11/d' /etc/pam.d/*

#remove sendmail reference in hosts.allow.
sed -i '/sendmail/d' /etc/hosts.allow

#switch to uuid ids in /etc/fstab
updateos uuid_switch

#remove nodejs from nodesource repo. (conflicts with rh8 packages.)
yum -y remove nodejs nodesource-release

#multipath fix
yum -y install device-mapper-multipath
/usr/sbin/mpathconf --enable

#pata_ahci on vmservers
[ "`lsmod | grep pata_ahci`" != "" ] && rmmod pata_ahci && echo "blacklist pata_ahci" >/etc/modprobe.d/acpi-blacklist.conf

#remove tcp_wrappers no longer supported.
yum -y remove tcp_wrappers
[ -f /etc/hosts.allow ] && mv -f /etc/hosts.allow /tmp
[ -f /etc/hosts.deny ] && mv -f /etc/hosts.deny /tmp

#remove vsftpd.
yum -y remove vsftpd
yum -y remove firewalld
yum -y install NetworkManager
systemctl enable NetworkManager

#updates ostools to 1.16
updateos install_ostools-1.16

updateos rti_email_fix

# run leap upgrade
leapp upgrade --no-rhsm 
reboot
exit 0
