Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
183 lines (148 sloc) 5.21 KB
#!/usr/bin/python
# The Following Agent Has Been Tested On:
#
# RHEV 3.4.1 with RHEV Hypervisor - 6.5 - 20140725.0.el6ev and RHEL - 6Server - 6.4.0.4.el6
# Author: Marcos Gil David - marcosgildavid.blogspot.com
#
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 Mon Oct 7 16:16:58 EDT 2013)"
REDHAT_COPYRIGHT="Copyright (C) Red Hat, Inc. 2004-2010 All rights reserved."
#END_VERSION_GENERATION
def get_outlets_status(conn, options):
try:
conn.sendline("virsh list --all")
conn.expect('name:')
conn.sendline(options["virtshuser"])
conn.expect('password:')
conn.sendline(options["virtshuser"])
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
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:
# since we are restarting the vm, the power status has to be "faked"
# the 1st time this function is called after restart of the vm
# it will return off
# from then on it will return the correct state
if options["restarted"]==1:
options["restarted"]=0
return 'off'
else:
return outlets[options["-n"]][1]
def check_for_vm(conn,options):
outlets=get_outlets_status(conn,options)
if (not (options["-n"] in outlets)):
return 0 #false
else:
return 1 #true
def set_power_status(conn, options):
try:
if (options["-o"] == "on" and "start" or "destroy"):
conn.sendline("virsh qemu-monitor-command --hmp %s system_reset"%(options["-n"]))
conn.expect('name:')
conn.sendline(options["virtshuser"])
conn.expect('password:')
conn.sendline(options["virtshuser"])
conn.expect('')
options["restarted"]=1
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)
def create_virtsh_user(conn,options):
try:
conn.sendline('saslpasswd2 -p -a libvirt '+options["virtshuser"])
conn.expect('')
conn.sendline(options["virtshuser"])
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)
def delete_virtsh_user(conn,options):
try:
conn.sendline('saslpasswd2 -d -a libvirt '+options["virtshuser"])
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)
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",
"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\"'"
#custom options
options["virtshuser"]="fencevirtsh"
options["restarted"]=0
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)
## check hosts for vm
fenced=0
hosts=options["-a"]
result = None
for host in hosts.split(","):
if (fenced==0):
options["-a"]=host
conn = fence_login(options)
#create temp user
create_virtsh_user(conn,options)
if (check_for_vm(conn,options)==1):
#restart the vm if it is found
result = fence_action(conn, options, set_power_status, get_power_status, get_outlets_status)
#assume fence was sucessful
fenced=1
# delete temp user
delete_virtsh_user(conn,options)
try:
# logout from the hypervisor
conn.sendline("quit")
conn.close()
except exceptions.OSError:
pass
except pexpect.ExceptionPexpect:
pass
sys.exit(result)
if __name__ == "__main__":
main()
You can’t perform that action at this time.