#!/usr/bin/bash
#
# A script for upgrading from Virtuozzo Linux to commercial Virtuozzo.
# Upgrade steps are like the following:
# 1. Install virtuozzo-release package
# 2. Install a group of packages specific to commercial Virtuozzo
# 3. Activate license (if provided by user)
#

# Just for the case, check if we have openvz-release
if [ -f /etc/openvz-release ]; then
    echo "/etc/openvz-release file found! Have you already updated your system to OpenVZ or to Virtuozzo?"
    exit 1
fi

SERVER_REPO="http://repo.virtuozzo.com"
PKG_PATH="vz/releases/9.0/x86_64/os/Packages/v"
LOG="/var/log/do-upgrade-vzlin-vz9.log"
SKIP_LIC=false
KEY=""
ACCEPT_EULA=false

# User should either provide a key or explicitly specify that he doesn't need it
for arg in "$@"; do
  shift
  case "$arg" in
    "--skip-license") SKIP_LIC=true;;
    "--key") KEY="$1";;
    "--boot") BOOT="$1";;
    "--accept-eula") ACCEPT_EULA=true;;
  esac
done

if ([[ ${SKIP_LIC} == true ]] && [[ -n ${KEY} ]]) || ([[ ${SKIP_LIC} == false ]] && [[ -z ${KEY} ]]) ; then
    echo "Usage: $0 --skip-license|--key <PRODUCT_KEY> [--accept-eula]"
    exit 1
fi

echo "Switched to virtuozzo-release on `date`" > ${LOG}

if [ ! -f /usr/bin/wget ]; then
    echo "wget not found! Installing it first..." | tee -a ${LOG}
    yum install -y wget
fi

echo "Downloading virtuozzo-release RPM from ${SERVER_REPO}/${PKG_PATH}" | tee -a ${LOG}

# Download the current version of virtuozzo-release package available on server
# to a temp location to extract EULA from it
tmpdir=`mktemp -d`
if [ $? -ne 0 ]; then
    echo "Failed to create a temporary directory to download virtuozzo-release package" | tee -a ${LOG}
    exit 1
fi
wget ${WGET_OPTS} -r -l1 --no-parent --no-directories -A"*virtuozzo-release*" ${SERVER_REPO}/${PKG_PATH} -P ${tmpdir} 2>/dev/null
if [ $? -ne 0 ]; then
    echo "Failed to download virtuozzo-release package" | tee -a ${LOG}
    rm -rf ${tmpdir}
    exit 1
fi
release_pkg_path=`ls ${tmpdir}/*virtuozzo-release*`
if [ -z ${release_pkg_path} ] ; then
    echo "Failed to download virtuozzo-release package" | tee -a ${LOG}
    rm -rf ${tmpdir}
    exit 1
fi
release_pkg=`basename ${release_pkg_path}`
pushd ${tmpdir} > /dev/null
rpm2cpio ${release_pkg_path} | cpio -idm 2>/dev/null
popd > /dev/null
EULA=${tmpdir}/usr/share/licenses/virtuozzo-release/EULA

if [ ! -f ${EULA} ] ; then
    echo "Cannot find EULA in virtuozzo-release package" | tee -a ${LOG}
    rm -rf ${tmpdir}
    exit 1
fi

if [[ ${ACCEPT_EULA} == false ]] ; then
    rpm -q python3-unidecode >/dev/null 2>&1 || dnf install -y python3-unidecode 2>&1 | tee -a ${LOG}
    if /usr/libexec/vzlinux/show-eula.py ${EULA}; then
        ACCEPT_EULA=true
    else
        echo "Rejected Virtuozzo EULA." | tee -a ${LOG}
        rm -rf ${tmpdir}
        exit 1
    fi
fi

if [ -z ${KEY} ] ; then
    echo "WARNING: No license key is provided. Without valid key, you will not be able to run any VE." | tee -a ${LOG}
fi

echo "Installing ${release_pkg} ..." | tee -a ${LOG}
rpm -q vzlinux-cep-config >/dev/null 2>&1 && rpm -e --nodeps vzlinux-cep-config
rpm -i --nodeps ${release_pkg_path} 2>&1 | tee -a ${LOG}

# Cleanup temp location with no longer needed downloaded virtuozzo-release package
rm -rf ${tmpdir}

# Drop qemu packages; sometimes Vz qemu doesn't obsolete all packages that it should
rpm -qa --qf='%{NAME} ' qemu* | xargs rpm -e --nodeps

# We'll replace this with vzlicutils
rpm -q vllicutils >/dev/null 2>&1 && rpm -e --nodeps vllicutils

# Install Vz-specific packages
yum groupinstall -y -x grub2-theme-openvz templates core ps base vz qemu vz-comm-upgrade 2>&1 | tee -a ${LOG}
dnf install -y kernel 2>&1 | tee -a ${LOG}
grub2-mkconfig -o /boot/grub2/grub.cfg
if [[ ! -z "${BOOT}" ]]; then
    grub2-install "${BOOT}" 2>&1 | tee -a ${LOG}
fi

yum reinstall -y virtuozzo-release
# Some package in VzLinux can intersect with Vz ones.
# We want Vz and distrosync will bring them due to repo priorities
dnf distrosync -y 2>&1 | tee -a ${LOG}

# Activate license
if ! $SKIP_LIC ; then
    vzlicload -p "${KEY}" 2>&1 | tee -a ${LOG}
fi

# Adjust services
systemctl enable vz
systemctl enable libvirtd

# We need working libvirt in order to create bridged network,
# but we can't use vzct until we reboot into Vz kernel
mv /usr/lib64/libvirt/connection-driver/libvirt_driver_vzct.so /tmp/
systemctl start libvirtd

if [ -f /usr/libexec/create_bridges.py ]; then
    /usr/libexec/create_bridges.py
else
    /usr/libexec/vz9_create_bridges
fi

mv /tmp/libvirt_driver_vzct.so  /usr/lib64/libvirt/connection-driver/

[ -d /vz ] || mkdir /vz
chmod 755 /vz

grep '^enable\\|disable' /lib/systemd/system-preset/01-virtuozzo.preset | while read service; do
    systemctl preset $service > /dev/null 2>&1
done
systemctl mask wpa_supplicant.service > /dev/null 2>&1

echo "Conversion complete. Please reboot to VHS kernel to start working"

#systemctl start prl-disp 2>&1 | tee -a ${LOG}

