#!/usr/bin/bash
#
# A script for upgrading from VzLinux to OpenVZ.
# Upgrade steps are like the following:
# 1. Install openvz-release package
# 2. Install a group of packages specific to OpenVZ
#

# 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://download.openvz.org"
PKG_PATH="virtuozzo/releases/9.0/x86_64/os/Packages/o"
LOG="/var/log/do-upgrade-openvz9.log"

for arg in "$@"; do
  shift
  case "$arg" in
    "--boot") BOOT="$1";;
  esac
done

echo "Switched to openvz-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 openvz-release RPM from ${SERVER_REPO}/${PKG_PATH}" | tee -a ${LOG}

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

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 no longer needed temp location with downloaded openvz-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

# Install OpenVZ-specific packages
yum groupinstall -y -x grub2-theme-virtuozzo templates core ps base vz qemu 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 openvz-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}

# Adjust services
systemctl enable vz
systemctl enable libvirtd
#systemctl enable prl-disp

# 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

rep '^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}
