Skip to content

Instantly share code, notes, and snippets.

@jbd
Created July 19, 2012 08:47
Show Gist options
  • Save jbd/3141655 to your computer and use it in GitHub Desktop.
Save jbd/3141655 to your computer and use it in GitHub Desktop.
Quick'n dirty supermicro smcipmitool fence agent
#!/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
import os
import subprocess
import shlex
sys.path.append("/usr/share/fence")
from fencing import *
#BEGIN_VERSION_GENERATION
RELEASE_VERSION="3.1.5"
BUILD_DATE="(built Fri Jun 22 11:20:18 UTC 2012)"
REDHAT_COPYRIGHT="Copyright (C) Red Hat, Inc. 2004-2010 All rights reserved."
#END_VERSION_GENERATION
SMCIPMITOOLJAR = "/opt/SMCIPMITool/SMCIPMITool.jar"
if not os.path.exists(SMCIPMITOOLJAR):
fail_usage("File %s does not exists, aborting." % SMCIPMITOOLJAR)
# Jay, from StackOverflow proposed this
# http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
JAVA_BIN = which("java")
if not JAVA_BIN:
fail_usage("Cannot find java, aborting.")
# Quote string for proper existence in quoted string used for pexpect.run function
# Ex. test'this will return test'\''this. So pexpect run will really pass ' to argument
def quote_for_run(str):
return str.replace(r"'", "'\\''")
# Log message if user set verbose option
def smcipmitool_log(options, message):
if options["log"] >= LOG_MODE_VERBOSE:
options["debug_fh"].write(message+"\n")
def smcipmitool_prepare_command(options):
# Command line example
# java -jar SMCIPMITool.jar 157.99.69.134 ADMIN PASSWORD blade 10 power reset
ipaddr = options["-a"]
login = options["-l"]
power_action = options["-o"]
bladeid = options["-n"]
password = options["-p"]
if power_action == "on":
power_action = "up"
elif power_action == "off":
power_action = "down"
command_line = "%s -jar %s %s %s %s blade %s power %s" % (quote_for_run(JAVA_BIN),
quote_for_run(SMCIPMITOOLJAR),
quote_for_run(ipaddr),
quote_for_run(login),
quote_for_run(password),
bladeid, power_action)
return command_line
# Run command with timeout and parameters. Returns string
# with output from the java -jar SMCIPMITool. If something fails (command not found, exit code is not 0), fail_usage
# function is called (and never return).
def smcipmitool_run_command(options):
command = smcipmitool_prepare_command(options)
return subprocess.call(shlex.split(command))
def smcipmitool_get_outlets(conn, options):
pass
def get_outlets_status(conn, options):
pass
def get_power_status(conn, options):
pass
def set_power_status(conn, options):
pass
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" ]
options = check_input(device_opt, process_input(device_opt))
# Default is secure connection
options["-x"] = 1
docs = { }
docs["shortdesc"] = "Fence agent for supermicro officeblade"
docs["longdesc"] = "fence_officeblade is an I/O Fencing agent \
which can be used with the supermicro officeblade chassis. \
It use the SMCIPMITool.jar provided on the supermicro website, which does \
all the work. "
show_docs(options, docs)
#result = fence_action(None, options, set_power_status, get_power_status)
result = smcipmitool_run_command(options)
sys.exit(result)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment