From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4415 invoked by alias); 21 May 2009 12:26:36 -0000 Received: (qmail 4406 invoked by alias); 21 May 2009 12:26:35 -0000 X-SWARE-Spam-Status: No, hits=-2.1 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS X-Spam-Status: No, hits=-2.1 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS X-Spam-Check-By: sourceware.org X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on bastion2.fedora.phx.redhat.com Subject: cluster: RHEL5 - fence_virsh: #496629 Add virsh fencing agent To: cluster-cvs-relay@redhat.com X-Project: Cluster Project X-Git-Module: cluster.git X-Git-Refname: refs/heads/RHEL5 X-Git-Reftype: branch X-Git-Oldrev: dc1798cc8ad2216acc8fb08cf6f4b6381c77f447 X-Git-Newrev: 8f7ef65924e8a48178e72ef359dc5a92091bb9e0 From: =?utf-8?q?Marek_Gr=C3=A1c?= Message-Id: <20090521122609.92F641201FA@lists.fedorahosted.org> Date: Thu, 21 May 2009 12:26:00 -0000 X-Scanned-By: MIMEDefang 2.58 on 172.16.52.254 Mailing-List: contact cluster-cvs-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Post: List-Help: , Sender: cluster-cvs-owner@sourceware.org X-SW-Source: 2009-q2/txt/msg00351.txt.bz2 Gitweb: http://git.fedorahosted.org/git/cluster.git?p=cluster.git;a=commitdiff;h=8f7ef65924e8a48178e72ef359dc5a92091bb9e0 Commit: 8f7ef65924e8a48178e72ef359dc5a92091bb9e0 Parent: dc1798cc8ad2216acc8fb08cf6f4b6381c77f447 Author: Marek 'marx' Grac AuthorDate: Thu May 21 14:19:12 2009 +0200 Committer: Marek 'marx' Grac CommitterDate: Thu May 21 14:19:12 2009 +0200 fence_virsh: #496629 Add virsh fencing agent --- fence/agents/Makefile | 4 ++ fence/agents/virsh/Makefile | 39 ++++++++++++++++ fence/agents/virsh/fence_virsh.py | 87 +++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 0 deletions(-) diff --git a/fence/agents/Makefile b/fence/agents/Makefile index 80655fb..c8ef06a 100644 --- a/fence/agents/Makefile +++ b/fence/agents/Makefile @@ -43,6 +43,7 @@ all: # ${MAKE} -C xcat all [ "${enable_xen}" != "1" ] || ${MAKE} -C xvm all # ${MAKE} -C zvm all + ${MAKE} -C virsh all install: all ${MAKE} -C lib install @@ -73,6 +74,7 @@ install: all # ${MAKE} -C xcat install [ "${enable_xen}" != "1" ] || ${MAKE} -C xvm install # ${MAKE} -C zvm install + ${MAKE} -C virsh install clean: ${MAKE} -C lib clean @@ -103,3 +105,5 @@ clean: # ${MAKE} -C xcat clean [ "${enable_xen}" != "1" ] || ${MAKE} -C xvm clean # ${MAKE} -C zvm clean + ${MAKE} -C virsh clean + diff --git a/fence/agents/virsh/Makefile b/fence/agents/virsh/Makefile new file mode 100644 index 0000000..45ca6bf --- /dev/null +++ b/fence/agents/virsh/Makefile @@ -0,0 +1,39 @@ +############################################################################### +############################################################################### +## +## Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. +## Copyright (C) 2004 Red Hat, Inc. All rights reserved. +## +## This copyrighted material is made available to anyone wishing to use, +## modify, copy, or redistribute it subject to the terms and conditions +## of the GNU General Public License v.2. +## +############################################################################### +############################################################################### + +SOURCE= fence_virsh.py +TARGET= fence_virsh + +top_srcdir=../.. +include ${top_srcdir}/make/defines.mk + +all: $(TARGET) + +fence_virsh: fence_virsh.py + : > $(TARGET) + awk "{print}(\$$1 ~ /#BEGIN_VERSION_GENERATION/){exit 0}" $(SOURCE) >> $(TARGET) + echo "FENCE_RELEASE_NAME=\"${RELEASE}\";" >> $(TARGET) + ${top_srcdir}/scripts/define2var ${top_srcdir}/config/copyright.cf sh REDHAT_COPYRIGHT >> $(TARGET) + echo "BUILD_DATE=\"(built `date`)\";" >> $(TARGET) + awk -v p=0 "(\$$1 ~ /#END_VERSION_GENERATION/){p = 1} {if(p==1)print}" $(SOURCE) >> $(TARGET) + chmod +x $(TARGET) + +install: all + if [ ! -d ${sbindir} ]; then \ + install -d ${sbindir}; \ + fi + install -m755 ${TARGET} ${sbindir} + +clean: + rm -f $(TARGET) + diff --git a/fence/agents/virsh/fence_virsh.py b/fence/agents/virsh/fence_virsh.py new file mode 100644 index 0000000..11c3576 --- /dev/null +++ b/fence/agents/virsh/fence_virsh.py @@ -0,0 +1,87 @@ +#!/usr/bin/python + +## The Following Agent Has Been Tested On: +## +## Virsh 0.3.3 on RHEL 5.2 with xen-3.0.3-51 +## +##### + +import sys, re, pexpect +sys.path.append("/usr/lib/fence") +from fencing import * + +#BEGIN_VERSION_GENERATION +RELEASE_VERSION="Virsh fence agent" +REDHAT_COPYRIGHT="" +BUILD_DATE="" +#END_VERSION_GENERATION + +def get_outlets_status(conn, options): + try: + conn.sendline("virsh list --all") + conn.log_expect(options, options["-c"], SHELL_TIMEOUT) + 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"] and "on" or "off")) + return result + +def get_power_status(conn, options): + outlets=get_outlets_status(conn,options) + + if (not (options["-n"] in outlets)): + fail_usage("Failed: You have to enter existing name of virtual machine!") + else: + return outlets[options["-n"]][1] + +def set_power_status(conn, options): + try: + conn.sendline("virsh %s "%(options["-o"] == "on" and "start" or "destroy")+options["-n"]) + + conn.log_expect(options, options["-c"], POWER_TIMEOUT) + time.sleep(1) + + except pexpect.EOF: + fail(EC_CONNECTION_LOST) + except pexpect.TIMEOUT: + fail(EC_TIMED_OUT) + +def main(): + device_opt = [ "help", "version", "agent", "quiet", "verbose", "debug", + "action", "ipaddr", "login", "passwd", "passwd_script", + "secure", "identity_file", "test", "port" ] + + options = check_input(device_opt, process_input(device_opt)) + + ## Defaults for fence agent + if 0 == options.has_key("-c"): + options["-c"] = "\[EXPECT\]#\ " + + options["-x"]=1 + + options["ssh_options"]="-t '/bin/bash -c \"PS1=\[EXPECT\]#\ /bin/bash --noprofile --norc\"'" + + ## Operate the fencing device + conn = fence_login(options) + fence_action(conn, options, set_power_status, get_power_status) + + ## Logout from system + conn.sendline("quit") + conn.close() + +if __name__ == "__main__": + main()