#!/usr/bin/python

# The Following Agent Has Been Tested On:
#
# Virsh 0.9.13,0.10.2 
# CENTOS 6.4, fence-agents 3.1.5-25.el6_4.2
# Ubuntu 12.10, fence-agents 3.1.5-2ubuntu3
# host system with qemu-kvm-1.2.0+noroms-0ubuntu2
#

import sys, re, pexpect, exceptions
sys.path.append("/usr/share/fence")
from fencing import *

#BEGIN_VERSION_GENERATION
RELEASE_VERSION="3.1.5"
BUILD_DATE="(built Tue Jun 30 14:36:20 UTC 2015)"
REDHAT_COPYRIGHT="Copyright (C) Red Hat, Inc. 2004-2010 All rights reserved."
#END_VERSION_GENERATION

def get_outlets_status(conn, options):
    if options.has_key("-n"):
        try:
                #Restrict virsh list by header and by matched domain name only (if any), assumes it would be unique
		conn.sendline("virsh list --all|awk '/Name|%s/'"%(options["-n"]))
		conn.log_expect(options, options["-c"], int(options["-Y"]))
	except pexpect.EOF:
		fail(EC_CONNECTION_LOST)
	except pexpect.TIMEOUT:
		fail(EC_TIMED_OUT)

	result={}

        #This is status of mini finite automata. 0 = we didn't found Id and Name, 1 = we did
        fa_status=0

        for line in conn.before.splitlines():
	        domain=re.search("^\s*(\S+)\s+(\S+)\s+(\S+).*$",line)
                if (domain!=None):
			if ((fa_status==0) and (domain.group(1).lower()=="id") and (domain.group(2).lower()=="name")):
				fa_status=1
			elif (fa_status==1):
				result[domain.group(2)]=("",(domain.group(3).lower() in ["running","blocked","idle","no state","paused"] and "on" or "off"))
	return result
    elif options.has_key("-U"):
        return get_outlets_status_by_uuid(conn, options)
    else: # other cases , f.e. action list
        try:
                conn.sendline("virsh list --all")
                conn.log_expect(options, options["-c"], int(options["-Y"]))
        except pexpect.EOF:
                fail(EC_CONNECTION_LOST)
        except pexpect.TIMEOUT:
                fail(EC_TIMED_OUT)

        result={}

        #This is status of mini finite automata. 0 = we didn't found Id and Name, 1 = we did
        fa_status=0

        for line in conn.before.splitlines():
                domain=re.search("^\s*(\S+)\s+(\S+)\s+(\S+).*$",line)
                if (domain!=None):   
                        if ((fa_status==0) and (domain.group(1).lower()=="id") and (domain.group(2).lower()=="name")):
                                fa_status=1
                        elif (fa_status==1):
                                result[domain.group(2)]=("",(domain.group(3).lower() in ["running","blocked","idle","no state","paused"] and "on" or "off"))
        return result

#Do the same as get_outlets_status, but using -U <uuid> option
def get_outlets_status_by_uuid(conn, options):
        try:
                #Restrict virsh list by matched domain uuid only (if any), assumes it would be unique
                conn.sendline("virsh list --all --uuid|awk '/%s/'"%(options["-U"].lower()))
                conn.log_expect(options, options["-c"], int(options["-Y"]))
        except pexpect.EOF:
                fail(EC_CONNECTION_LOST)
        except pexpect.TIMEOUT:
                fail(EC_TIMED_OUT)

        result={}

        for line in conn.before.splitlines():
                uuid=re.search(UUID_PATTERN,line)
                if (uuid!=None):
                        result[uuid.group().lower()]=("",get_domstate(conn,options,uuid.group().lower()).lower() in ["running","blocked","idle","no state","paused"] and "on" or "off")
        return result

#Get virsh domstate by uuid
def get_domstate(conn, options, uuid):
        try:
                conn.sendline("virsh domstate %s"%(uuid))
                conn.log_expect(options, options["-c"], int(options["-Y"]))
        except pexpect.EOF:
                fail(EC_CONNECTION_LOST)
        except pexpect.TIMEOUT:
                fail(EC_TIMED_OUT)
        
        for line in conn.before.splitlines():
                #Search for domain state
                state=re.search(STATE_PATTERN,line)
                if (state!=None):
                            return state.group()

#Return domain name/port, if -n was specified, otherwise assume -U was used instead and return domain uuid.
def get_name_or_uuid(options):
        if options.has_key("-n"):
            return options["-n"]
        else:
            return options["-U"].lower()

def get_power_status(conn, options):
	outlets=get_outlets_status(conn,options)
        if (not (get_name_or_uuid(options) in outlets)):
                fail_usage("Failed: You have to enter existing name/uuid of virtual machine!")
        else:
                return outlets[get_name_or_uuid(options)][1]

def set_power_status(conn, options):
	try:
		conn.sendline("virsh %s "%(options["-o"] == "on" and "start" or "destroy")+get_name_or_uuid(options))

		conn.log_expect(options, options["-c"], int(options["-g"]))
                time.sleep(1)

	except pexpect.EOF:
		fail(EC_CONNECTION_LOST)
	except pexpect.TIMEOUT:
		fail(EC_TIMED_OUT)

# Add uuid support
def main():
	device_opt = [  "help", "version", "agent", "quiet", "verbose", "debug",
			"action", "ipaddr", "login", "passwd", "passwd_script",
			"secure", "identity_file", "test", "port", "separator",
			"inet4_only", "inet6_only", "ipport", "uuid",
			"power_timeout", "shell_timeout", "login_timeout", "power_wait" ]

	atexit.register(atexit_handler)

	pinput = process_input(device_opt)
	pinput["-x"] = 1
	options = check_input(device_opt, pinput)

	## Defaults for fence agent
	if 0 == options.has_key("-c"):
		options["-c"] = "\[EXPECT\]#\ "

	options["ssh_options"]="-t '/bin/bash -c \"PS1=\[EXPECT\]#\  /bin/bash --noprofile --norc\"'"

	docs = { }
	docs["shortdesc"] = "Fence agent for virsh"
	docs["longdesc"] = "fence_virsh is an I/O Fencing agent \
which can be used with the virtual machines managed by libvirt. \
It logs via ssh to a dom0 and there run virsh command, which does \
all work. \
\n.P\n\
By default, virsh needs root account to do properly work. So you \
must allow ssh login in your sshd_config."
	show_docs(options, docs)

	## Operate the fencing device
	conn = fence_login(options)
	result = fence_action(conn, options, set_power_status, get_power_status, get_outlets_status)

	## Logout from system
	try:
		conn.sendline("quit")
		conn.close()
	except exceptions.OSError:
		pass
	except pexpect.ExceptionPexpect:
		pass

	sys.exit(result)
if __name__ == "__main__":
	main()

