#!/bin/sh
#
# Copyright (c) Citrix Systems 2008. All rights reserved.
#
# wait for xapi initialisation to complete.  Then, if initialisation
# did complete, and we are not in rolling-pool-upgrade mode, attempt
# to start all vms with "auto_poweron" in their other-config
#

[ -e /proc/xen ] || exit 0

XAPI_START_TIMEOUT_SECONDS=240

# wait for xapi to complete initialisation for a max of XAPI_START_TIMEOUT_SECONDS
/opt/xensource/bin/xapi-wait-init-complete ${XAPI_START_TIMEOUT_SECONDS}

if [ $? -eq 0 ]; then

    # check for rolling-pool-upgrade mode
    pool=$(xe pool-list params=uuid --minimal 2> /dev/null)
    upgrade_mode=$(xe pool-param-get uuid=${pool} param-name=other-config param-key=rolling_upgrade_in_progress 2> /dev/null)

    # if the last command succeeds AND returns true, then we're in upgrade mode
    if [ $? -eq 0 ] && [ ${upgrade_mode} = "true" ]; then
        logger "$0 is not supported while the pool is in rolling upgrade mode."
        exit 1
    fi

    # if xapi init completed then start vms (best effort, don't report errors)
    xe vm-start other-config:auto_poweron=true power-state=halted --multiple >/dev/null 2>/dev/null || true

fi
