#!/usr/bin/env python
import csha

csha.log.debug('HA ha_start_daemon')

nodeid = csha.get_current_host_node_id()
my_host_uuid = csha.util.get_current_host_uuid()

sf = csha.Statefile()
sf.set_host(nodeid, csha.Host(my_host_uuid, True))

# Start monitoring xapi's health
csha.touch_signal_file()

# We assume that xapi is the only one calling this script, therefore it must be
# alive at this point, and we indicate that xapi is alive on this host by
# joining the "xapi HA" lockspace. If xapi dies, the host will self-fence and
# reboot, because we have started monitoring xapi's health above. If the host
# does not reboot within the token timeout the other hosts will notice it and
# fence it, and it will be removed from this lockspace. Even if it reboots
# within the token timeout, it will remove itself from the old lockspaces at
# startup. Thus the host will always leave the "xapi HA" lockspace, regardless
# of how the host fails or how long it takes to reboot.
csha.join_xapi_ha_lockspace()
