#!/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/7.0/x86_64/os/Packages/v"
LOG="/var/log/do-upgrade-vzlin-vz7.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";;
    "--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 -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* | tail -1`
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
    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}
yum install -y ${release_pkg_path} 2>&1 | tee -a ${LOG}

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

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

# Force replacemant of vzlinux  libreport plugin
rpm -e --nodeps libreport-plugin-vzlinux-bugs >/dev/null 2>&1

# If libvirt is installed, force its update before installing Vz packages
# We need our libvirt and in some cases this can't be solved by dependencies
rpm -qa | grep libvirt-daemon-driver | xargs yum remove -y
rpm -qa "libvirt*" --qf="%{NAME}\n" | sort -u | xargs yum distro-sync -y

# Install Vz-specific packages
yum groupinstall -y templates core ps base vz qemu vz-comm-upgrade 2>&1 | tee -a ${LOG}

yum update -y vzkernel 2>&1 | tee -a ${LOG}

yum reinstall -y virtuozzo-release

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

/usr/libexec/create_bridges.py

# Adjust services
systemctl enable vz

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

