#!/bin/bash

########################################################################
#
#PROJECT:      Unified Sessions Manager
#AUTHOR:       Arno-Can Uestuensoez - acue@UnifiedSessionsManager.org
#MAINTAINER:   Arno-Can Uestuensoez - acue_sf1@sourceforge.net
#SHORT:        ctys
#CALLFULLNAME: Commutate To Your Session
#LICENCE:      GPL3
#VERSION:      01_02_007a17
#
########################################################################
#
# Copyright (C) 2007 Arno-Can Uestuensoez (UnifiedSessionsManager.org)
#
########################################################################

_myPKGNAME_STACKER="${BASH_SOURCE}"
_myPKGVERS_STACKER="01.02.002c01"
hookInfoAdd "$_myPKGNAME_STACKER" "$_myPKGVERS_STACKER"




#FUNCBEG###############################################################
#NAME:
#  stackerCancelPropagate
#
#TYPE:
#  bash-function
#
#DESCRIPTION:
#  Workout the execution stack of VMs on current PM. Therefore an 
#  incremented walk-up from the current stack to the topmost and 
#  step-back by incrementally cancelling the stack topdown is performed.
#
#  The most essential functionality to be used here is ctys-vhost in order 
#  to evaluate the MACHINE-TYPE and the TCP/IP address of the instance 
#  to be canceled.
#
#  Once this information is available, the incremental call to that 
#  instance is performed by using ctys itself again.
#
#  Due to the standard encapsulation of CANCEL method, no specific 
#  knowledge about the called plugin is required for the call.
#
#  Thus the only parameter to be passed through is the input parameter
#  which is the generic suboption for the type of CANCEL to be performed.
#
#EXAMPLE:
#
#PARAMETERS:
#  $1: <behaviour>
#      SELF,FORCE|STACK
#
#  $2: <method>[:subarg]
#
#  $3  [<specific-target-list>]
#       - Records: "<machinetype>;<label>;<tcp>"
#       - FS:" "
#
#GLOBALS:
#
#OUTPUT:
#  RETURN:
#  VALUES:
#
#FUNCEND###############################################################
function stackerCancelPropagate () {
    printVerboseDebug ${DBG_LVL_MAINT} $LINENO $BASH_SOURCE "$FUNCTION:\$@=$@"
    local _behaviour=$1;shift
    local _method=${1%:*};shift
    local _margs=${1#*:};shift
    if [ -z "${@}" ];then
	local _allact=`ctys -a list=MAXKEY,SERVER ${CTRL_VERBOSE:+ -d $CTRL_VERBOSE}`
    else
	local _allact="${@}"
    fi

    local _cur=;

    function checkMe () {
	local _t=${1#*;*;*;};_t=${_t%%;*}
	  if [ "${_t}" == "/etc/ctys.d/pm.conf" -o "${_t}" == "/etc/ctys.d/vm.conf" ];then
	      return 0
	  fi
	  return 1
    }

    local _t=;
    local _l=;
    local _mt=;
    for _cur in $_allact;do
	printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "${FUNCNAME}:_cur=${_cur//;/ }"
	checkMe $_cur
	if [ $? -eq 0 ];then
	    printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "${FUNCNAME}:MYSELF-CONTINUE"a
	    continue;
	fi
        #I believe it is just one for results of MAXKEY.
	local _i4=`ctys-vhost ${CTRL_VERBOSE:+ -d $CTRL_VERBOSE} -C macmap -o l,t,mt ${_cur//;/ }`
	_mt=${_i4%%;*};
	_t=${_i4#*;};
	_l=${_t%;*};
	_t=${_t#*;};

	printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "${FUNCNAME}: => _i4=$_i4 - mt=$_mt t=$_t l=$_l"
	if [ -n "$_mt" -a -n "$_t" -a -n "$_l" ];then
	    ping -c 1 ${_t} 2>&1 >/dev/null
	    if [ $? -eq 0 ];then
		printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "${FUNCNAME}:CHK(ping)OK:$_i4"
		ssh ${_t} echo >/dev/null 2>/dev/null;
		if [ $? -eq 0 ];then
		    printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "${FUNCNAME}:CHK(ssh)OK:$_i4"
		    ssh ${_t} ctys -X -V >/dev/null 2>/dev/null;
		    if [ $? -eq 0 ];then
			printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "${FUNCNAME}:CHK(ctys)OK:$_i4"
                        #assume access
			printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "${FUNCNAME}:ctys -t ${_mt} -a cancel=ALL,SELF,${_behaviour} ${_t}"
			ctys -t ${_mt} -a cancel=ALL,SELF,${_behaviour} ${_t}
			continue	
		    else
			printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "${FUNCNAME}:CHK(ctys)NOK"
			ssh ${_t} halt -p >/dev/null 2>/dev/null;
			continue	
		    fi
		else
		    printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "${FUNCNAME}:CHK(ssh)NOK"
		fi
	    else
		printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "${FUNCNAME}:CHK(ping)NOK"
	    fi
	fi

        #so, try the hypervisor
	printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "${FUNCNAME}:use hypervisor:\"$_i4\""
	if [ -n "${_mt}" -a -n "${_l}" ];then
	    printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "${FUNCNAME}:ctys -t ${_mt} -a cancel=l:${_l},FORCE"
	    ctys -t ${_mt} -a cancel=l:${_l},FORCE
	else
	    printVerboseDebug ${DBG_LVL_UID} $LINENO $BASH_SOURCE "${FUNCNAME}:NO-MATCH:\"${_cur//;/ }\""
	fi	  
    done
}





