#!/sbin/sh
####################################################################
#  Copyright 2004-2008 Hewlett-Packard Development Company, L.P.  
# Confidential commercial computer software.  Valid license required.
#
# This is the hpvm serviceguard package online migration move script
#####################################################################
# @(#) $Id: hpvmsg_move 8307 2008-11-20 00:28:08Z tharple $
#set -x
PATH=${PATH}:/usr/sbin

function log
{
    typeset stat=0
    if [[ ${1} = stop ]] ; then
	exec 5>&- 4>&- 3>&- 2>&- 1>&-
	sleep 1
    else
	exec 3>&1 4>&2 5>>${LogFile} || stat=1
	tee -a ${LogFile} 1>&3 |&
	exec 1>&p || stat=1
	tee -a ${LogFile} 1>&4 |&
	exec 2>&p || stat=1
    fi
    return $stat
}

function rx
{
    typeset rtn=1
    typeset node=${1}
    typeset line
    typeset nline=fIrSt
    typeset answer
    shift
    # Is this a local node (hostname) do
    if [[ `hostname | sed 's/\..*//'` = ${node} ]] ; then
	${*}
	echo ${?} 
    else
    # Do we have a remote method set
	if [[ -z ${RCMD} ]] ; then
	    if ssh ${node} -q -o batchmode=yes -o PasswordAuthentication=no true ; then
		RCMD=ssh
	    else
		echo "\nThe hpvmsg_package command cannot ssh in batch mode to: ${node}" 1>&2
		exit 4
	    fi
	fi
	ssh ${node} -q -o batchmode=yes -o PasswordAuthentication=no "${*} ; echo \${?}" 
    fi | 
    while read line ; do
	if [[ ${nline} != fIrSt ]] ; then
	    echo ${nline}
	fi
	nline=${line}
    done
    if [[ ${nline} = +([0-9]) ]] ; then
	rtn=${nline}
    fi
    return ${rtn}
}

function rxh
{
    typeset host
    for host in ${hosts:-${allhosts:-`hostname`}} ; do
	if [[ ${lh} = ${host} ]] ; then
	    ${noaction} && eval print ${line} || eval ${*}
	else
	    ${noaction} && print rx ${host} ${line} || rx ${host} ${*}
	fi
    done
}
function usage 
{
    cat <<EOF

Usage:

cmmovepakg  -[vdf] -n target package

This script initiates an online migration (move) of a HPVM
Serviceguard guest packages from the current running node to the node
resolved by the target reference specified.  This script is a
prototype for the future Serviceguard cmmovecl command.

Where:
  -v verbose
  -d debug
  -f force
  target-node is the Serviceguard node where the package should migrate to
  package is the name of the guest/package to be migrated

Assumption:
  HPVM installed
  Serviceguard Cluster created
  Multi-server Environment is properly configured
  guests are created and packaged
  ssh root works across all hosts

EOF
exit 1
}

function pkgenable
{
    /usr/sbin/cmmodpkg -e -n ${tnode} ${package} &&
    /usr/sbin/cmrunpkg -n  ${tnode} ${package} &&
    /usr/sbin/cmmodpkg -e  ${package}
    /usr/sbin/cmmodpkg -e ${nodeargs} ${package}
}


function doexit
{
if ${reenable} ; then
    pkgenable
fi
log stop
}
##############################################################################
# ALIASES

alias Error="print -u2 \\\nERROR : ${0} : "

##############################################################################
# MAIN
# Start logging
log
trap "doexit" exit

typeset RCMD
typeset OK=false
typeset list=false
typeset verbose=false
typeset debug=false
typeset force=false
typeset localhost=`hostname`
typeset nodeinfo=`cmviewcl -l node | awk '/NODE/{n=1;next};n{print $1,$2,$3}' OFS=:` 
typeset nodeargs=`echo $nodeinfo | sed 's/:[^[:space:]*]*//g;s/[^[:space:]][^[:space:]]*/-n &/g'`
typeset pkginfo
typeset target
typeset tnode
typeset node
typeset status
typeset state
typeset auto
typeset hosts
typeset failretry=20
typeset reenable=false
typeset CMMOVEPKG=/usr/sbin/cmmovepkg

while getopts fvdn: opt ; do
    case ${opt} in
	(n)
	target=${OPTARG}
	;;
	(f)
	force=true
	;;
	(d)
	${debug} && set -v
	debug=true
	set -x
	;;
	(v)
	verbose=true
	;;
	(*) Usage
	usage
	;;
    esac
done
shift OPTIND-1
if [[ -x ${CMMOVEPKG}  ]] ; then
    Error This command has been deprecated by cmmovepkg
     cat 1>&2 <<EOF
Please refer to the appropriate Serviceguard and HPVM documentation
for additional information related to Online Guest Migration of
Integrity Virtual Machines in a Serviceguard Environment.
EOF
    if ! ${force} ; then
	exit -1
    fi
fi
package=${1}
if [[ ${#} -gt 0 ]] ; then 
    shift
else
    usage
fi
if [[ -n ${package} && -n ${target} ]] ; then
    if ! tnode=`rx ${target} hostname` ; then
	exit ${?}
    fi
    if [[ ${nodeinfo} != @(|*[[:space:]])${tnode}:up:running@(|[[:space:]]*) ]] ; then
	Error node is not up and running : ${target}
	cmviewcl -l node
	usage
    fi
    cmviewcl -l package -p ${package} | tail -1 | read pack status state auto node
    if [[ ${package} != ${pack} || ${status} != up || ${state} != running ]] ; then
	Error package is not up and running : ${package}
	cmviewcl -l package
	usage
    fi
    if [[ ${nodeinfo} != @(|*[[:space:]])${node}:up:running@(|[[:space:]]*) ]] ; then
	Error node is not up and running : ${node}
	cmviewcl -l node
	usage
    fi
    if [[ ${node} != ${localhost} ]] ; then
	${verbose} && Error package is up and running but not on the local hosts : ${1}
    fi
    if [[ ${node} != ${tnode} ]] ; then
	print "Moving (online) Serviceguard package ${package} from node ${node} to node ${tnode}"
	${verbose} && print "Disabling package failover for package : ${package}"
	/usr/sbin/cmmodpkg -d ${package} && 
	reenable=true &&
	rx ${node} /opt/hpvm/bin/hpvmmigrate -S -P ${package} -h ${target} 2>&1
	status=$?
	if [ status -ne 0 ] ; then
	    cat <<EOF
--------------------------
   The Online Migration did not succeed, 
   the virtual machine ${package} is probably still running on source host

   Package failover is disabled, you can reenable it manually by typing :
   cmmodpkg -e ${nodeargs} ${package}
   cmmodpkg -e ${package}
---------------------------
EOF
	    sleep 20
	    exit $status
	fi
	print Waiting for Serviceguard to detect node change....
	while [[ $(cmviewcl -p ${package} | awk 'END{print $2}') != down ]] ; do
	    sleep 5
	    let failretry-=1 || exit 1
	done
	sleep 5
	${verbose} && print  "Re-integrating ${package} in Serviceguard on ${tnode}"
	if pkgenable ; then
	    reenable=false
	    ${verbose} && cmviewcl -v -p ${package}
	    exit 0
	else
	    Error Re-integrating ${package} in Serviceguard on ${tnode}
	    exit 1
	fi
    else
	print "Serviceguard package ${package} already running node ${node}"
    fi
else
    usage
fi
