
PREINST_HOOKS="${PREINST_HOOKS} add_docker_repo cloudlinux_extras"

add_docker_repo() {
	if [ ! -f /etc/debian_version ]; then
        local centos_ver
        centos_ver=$(rpm -qf /etc/redhat-release --qf '%{version}'  | cut -d. -f1)
        if [ "${centos_ver}" != "8" ]; then
            # Exit if not centos 7
            return
        fi
        # Use docker-ce repo from centos 7
        if [ ! -f /etc/yum.repos.d/docker-ce.repo ]; then
            dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
        fi
        # Check containerd.io version. Must be greater than 1.2.2
        if [ -z "$(rpm -q --qf '%{version}' containerd.io | awk -F. '$1 >= 1 && 2 >= 2 && $3 >= 3')" ]; then
            dnf -y install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
        fi
        return
        fi
        if [ "#$(lsb_release -s -c)" = "#jessie" ] || [ "#$(lsb_release -s -c)" = "#stretch" ] || [ "#$(lsb_release -s -c)" = "#wheezy" ] || [ "#$(lsb_release -s -c)" = "#buster" ]; then
		echo -n "*** "
		date
		echo "*** Adding repo docker"
		apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D || :
		wget -O - https://download.docker.com/linux/debian/gpg | apt-key add -
		if [ ! -f /etc/apt/sources.list.d/docker.list ]; then
                        echo "deb https://download.docker.com/linux/debian $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
		fi
		apt-get -qy update
	fi

}

version_ge() {
    test "$(echo "$@" | tr " " "\n" | sort -V | tail -n 1)" = "$1";
}

cloudlinux_extras() {
	# Exit if not cloudlinux
	test -f /etc/redhat-release || return 0
	rpm -q cloudlinux-release >/dev/null 2>&1 || return 0

	test -n "$(which yum-config-manager 2>/dev/null)" || return 1

	local lve_ver
	lve_ver=$(rpm -q --qf="%{version}-%{release}" lve)
	if ! version_ge "${lve_ver}" "1.4-1.20" ; then
		# update lve
		echo "Updating lve..."
		yum -y update lve liblve || return 1
		echo "lve updated"
	fi

	echo -n "Remounting cgroup with rw..."
	mount -o remount,rw /sys/fs/cgroup || return 1
	echo " done"

	if yum repolist disabled | grep -q extras ; then
		echo -n "Enabling extras repo..."
		yum-config-manager --enable extras >/dev/null || return 1
		echo " done"
	fi
}
