#!/bin/bash
#
# A script for upgrading from OpenVZ 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 trial license
#

# Just for the case, check if we have openvz-release
if [ ! -f /etc/openvz-release ]; then
	echo "/etc/openvz-release file not found! Have you already updated your system from OpenVZ 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-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}

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*`
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/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 -Uvh ${release_pkg_path} 2>&1 | tee -a ${LOG}

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

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

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

