#!/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=""

# 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; break ;;
    "--key") KEY="$1"; break ;;
  esac
done

if ! $SKIP_LIC && [ -z ${KEY} ] ; then
    echo "Usage: $0 --skip-license|--key <PRODUCT_KEY>"
    exit 1
fi

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

# Get the current version of virtuozzo-release package available on server
release_pkg=`curl ${SERVER_REPO}/${PKG_PATH}/ 2>/dev/null | grep virtuozzo-release | cut -f8 -d\"`
echo "Installing ${release_pkg} ..." | tee ${LOG}
rpm -Uvh $SERVER_REPO/${PKG_PATH}/${release_pkg} 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
