#!/bin/sh

## centos2cl INFO
# Short Description	:upgrades CentOS or RHEL distro to CloudLinux
# Description      	:removes yum repo configuration related to CentOS/RHEL,
#	  		as well as release related packages. Installs cloudlinux 		
#		        release related packages, new kernel and lve tools
## centos2cl INFO


function check_kernel_update_permission {
    if [ -e /etc/yum.conf ] && [ -n "$(grep exclude /etc/yum.conf | grep kernel | grep -v '^#')" ]; then
	echo Kernel update is prohibited on your system. 
	echo Remove "exclude kernel" option from /etc/yum.conf
	echo and run this script again.
	exit 1;
    fi;
    if uname -r | grep -v  $KERNEL_VERSION ; then
	echo "Your server has non-standard kernel installed (see above)"
	echo "To resolve the issue, we can unregister this kernel RPM from RPM database."
	echo -n "Do you want to do that [Y/N]: "
	read YN
	case $YN in
	    [yY]|[yY][eE][sS])
               echo "Removing non-standard kernels from RPM database"
               rpm -e --justdb `rpm -qa | grep kernel | grep -v $KERNEL_VERSION`
               ;;
	    *)
		echo "Please remove non-standard kernels manually and run this script again."
		exit 1
		;;
	esac
    fi
}

function check_yum {
    if [ ! -n "$(cat /etc/yum.conf | sed -e 's/ //g' | grep -i ^"plugins=1")" ]; then
	echo "Yum error: Plugins are disabled"
	echo "Please enable yum-plugins: add string \"plugins=1\" to the /etc/yum.conf"
	exit 1;
    fi;
}

function check_release {
    if [ "$OS_VERSION" != 5 ] && [ "$OS_VERSION" != 6 ]; then
	echo "This script is for Version 5 or 6 only"
	exit 1;
    elif [ -f /proc/vz/vestat ]; then
	echo "You are running OpenVZ/Virtuozzo, which is incompatible with CloudLinux."
	echo "For more info please go to: http://www.cloudlinux.com/vz-compat.html."
	exit 1;
    fi;
}

function backup {
    BACKUP=/etc/cl-convert-saved
    mkdir -p $BACKUP    
    cp /etc/redhat-release $BACKUP
#disable redhat
    mv /etc/yum.repos.d/RedHat-Base.repo $BACKUP 2> /dev/null #test if that is the rate path
    yum -y erase rhnlib > /dev/null 2>&1
    rpm -e --nodeps redhat-release-notes redhat-release redhat-logos 2> /dev/null
    rpm -e --nodeps redhat-release-server 2> /dev/null
    if [ -f /etc/sysconfig/rhn/systemid ]; then mv /etc/sysconfig/rhn/systemid $BACKUP ; fi
    if [ -f /etc/yum/pluginconf.d/rhnplugin.conf ]; then mv /etc/yum/pluginconf.d/rhnplugin.conf $BACKUP ; fi

#disable centos
    mv /etc/yum.repos.d/CentOS-Media.repo $BACKUP 2> /dev/null
    mv /etc/yum.repos.d/CentOS-Base.repo $BACKUP 2> /dev/null
    rpm -e --nodeps centos-release centos-release-notes redhat-logos 2> /dev/null ;
}

function prep {
    yum clean all
    rpm --import http://repo.cloudlinux.com/cloudlinux/security/RPM-GPG-KEY-CloudLinux
    rpm -Uvh $CLOUDLINUX_RELEASE $CLOUDLINUX_LOGOS --force ;
}

function check_cpanel {
if [ -f /usr/local/cpanel/cpanel ] ; then
    CPANEL_MAJOR=`/usr/local/cpanel/cpanel -V | cut -f 1 -d "."`
    CPANEL_MINOR=`/usr/local/cpanel/cpanel -V | cut -f 2 -d "."`
	if [ $CPANEL_MAJOR -eq 11 ] && [ $CPANEL_MINOR -lt 30 ]; then
	CPANEL_OLD=YES
	else
	CPANEL_OLD=NO
	fi
fi
}

function check_cloudlinux_repos {
    ENABLED_REPOS="--disablerepo=* --enablerepo=cloudlinux-x86_64-server-5-h2e"
    for repo in `yum repolist|grep cloudlinux|grep enabled|cut -d' ' -f1`; do
    ENABLED_REPOS="$ENABLED_REPOS --enablerepo=$repo"
    done
}

function check_source {
NEED_SOURCE="NO"
if [ -x /sbin/lspci ] ; then
    if $(/sbin/lspci -n | grep -q 1414) || $(/sbin/lspci -n | grep -q 15ad) ; then
    NEED_SOURCE="YES"
    fi
fi
}


ARCH=$(uname -i)
CPU=$(uname -p)
OS_VERSION="$(rpm -q --qf %{version} `rpm -q --whatprovides redhat-release` | cut -c 1)"
    if [ "$OS_VERSION" -eq "5" ]; then
    KERNEL_VERSION=2.6.18
    CLOUDLINUX_RELEASE=http://repo.cloudlinux.com/cloudlinux/migrate/$ARCH/CloudLinux/cloudlinux-release-5-6.noarch.rpm
    CLOUDLINUX_LOGOS=http://repo.cloudlinux.com/cloudlinux/migrate/x86_64/CloudLinux/cloudlinux-logos-0.3-1.el5.1.noarch.rpm
    CENTOS_RELEASE=http://mirror.centos.org/centos/5/os/${ARCH}/CentOS/centos-release-5-6.el5.centos.1.${ARCH}.rpm
    elif [ "$OS_VERSION" -eq "6" ]; then
    KERNEL_VERSION=2.6.32
    CLOUDLINUX_RELEASE=http://repo.cloudlinux.com/cloudlinux/6.1/migrate/${ARCH}/RPMS/cloudlinux-release-6-6.1.1.${CPU}.rpm
    CLOUDLINUX_LOGOS=http://repo.cloudlinux.com/cloudlinux/6.1/migrate/x86_64/RPMS/redhat-logos-60.0.14-1.el6.cloudlinux.noarch.rpm
    CENTOS_RELEASE=http://mirror.yandex.ru/centos/6.0/os/${ARCH}/Packages/centos-release-6-0.el6.centos.5.${CPU}.rpm
    fi

check_release
check_yum
check_source

NO_ARGS=0
if rpm -qf --queryformat "%{name}" /lib/modules/$(uname -r) > /dev/null 2>&1 ; then
    KERNEL=$(rpm -qf --queryformat "%{name}" /lib/modules/$(uname -r))
else
    KERNEL=kernel
fi

PKGS="$KERNEL lve lve-utils liblve liblve-devel"

if [ $# -eq "$NO_ARGS" ]   # check for no arguments
then
  echo "Usage:"
  echo
  echo "`basename $0` -k activation_key  - upgrade CentOS or RHEL distro to CloudLinux"
  echo "`basename $0` -c                 - convert CloudLinux back to CentOS"
  echo
  echo "For more info see http://www.cloudlinux.com/docs/cln_register.php"
  exit 1
fi



while getopts ":ck:" opt; do
case "$opt" in

    k)
	yum clean all
	check_kernel_update_permission
	check_cpanel
	check_source
	backup
	prep
	if [ $NEED_SOURCE = "YES" ] ; then PKGS="$PKGS $KERNEL-devel gcc make"; fi
	yum -y --disablerepo=* --enablerepo=cloudlinux-base --enablerepo=cloudlinux-updates install yum-rhn-plugin
	echo -n "Registering... "
	/usr/sbin/rhnreg_ks --activationkey $OPTARG
	check_cloudlinux_repos
	if [ $? -ne 0 ]; then echo "Failed"; exit 1; fi; echo "Success"
	echo "Installing lve..."
	if [ -d /scripts ]; then
	    if [ "$CPANEL_OLD" = "YES" ]; then
	    echo "Adding cpanel support modules..."
	    PKGS="$PKGS cpanel-lve cpanel-lvemanager lve-cpanel-plugin"
	    if [ $? -ne 0 ]; then echo "Failed"; exit 1; fi; echo "Success"
	    else
	    PKGS="$PKGS lve-stats"
	    fi
	fi
	yum -y $ENABLED_REPOS install $PKGS
	echo "!!!Important: Memory limits are disabled by default."
	echo "To enable memory limits, please run:"
	echo "# lvectl ubc enable --save"
	echo "Ignore error message that you will receive."
	exit 0;;

    c)
	rpm --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
	rpm -ivh $CENTOS_RELEASE --force --nodeps
	yum install centos-release-notes
	rpm -e cloudlinux-release
	rpm -e rhn-client-tools rhn-check rhnsd rhn-setup rhnlib yum-rhn-plugin
	echo "You converted back to CentOS"
	echo "Now is the time to install kernel."
	echo -e "To delete CloudLinux kernel do \nrpm -e --nodeps $(rpm -qa | grep ^kernel | grep lve | tr -s '\n' ' ')"
	echo "To install new CentOS kernel once you deleted CloudLinux kernel, type yum install kernel"
	echo "If yum say that latest kernel is already installed - it's Ok"
	echo "Please check your bootloader configuration before rebooting the system"
	exit 0;;

    \?)
	echo "Invalid option: -$OPTARG" >&2
	exit 1;;

    :)
	yum clean all
	check_kernel_update_permission
	check_cpanel
	check_source
	backup
	prep
	if [ $NEED_SOURCE = "YES" ] && [ -z "$KERNEL" ]; then PKGS="$PKGS $KERNEL-devel gcc make"; fi
	yum -y --disablerepo=* --enablerepo=cloudlinux-base --enablerepo=cloudlinux-updates install yum-rhn-plugin
	echo -n "Registering... "
	/usr/sbin/clnreg_ks
	check_cloudlinux_repos
	if [ $? -ne 0 ]; then echo "Failed"; exit 1; fi; echo "Success"
	echo "Installing lve..."
	yum -y $ENABLED_REPOS install $PKGS
	if [ -d /scripts ]; then
	    if [ "$CPANEL_OLD" = "YES" ]; then
	    echo "Adding cpanel support modules..."
	    yum -y $ENABLED_REPOS install cpanel-lve cpanel-lvemanager lve-cpanel-plugin
	    if [ $? -ne 0 ]; then echo "Failed"; exit 1; fi; echo "Success"
	    else
	    echo "New cPanel installed, no extra packages needed"
	    fi
	else
	echo "cPanel is not installed, please, install cPanel, and execute:"
	echo "# yum install cpanel-lve cpanel-lvemanager lve-cpanel-plugin"
	fi
	if [ "$OS_VERSION" -eq "5" ]; then
	echo "!!!Important: Memory limits are disabled by default."
	echo "To enable memory limits, please run:"
	echo "# lvectl ubc enable --save"
	echo "Ignore error message that you will receive."
	fi
	exit 0;;

   esac
done
