Previously, we had just one fence agent of that type that was ported to
fencing library. Now, there are fence_amt and fence_ipmilan so it is possible
to create a more generic version of such access.
---
 fence/agents/amt/fence_amt.py         | 52 +++-----------------------------
 fence/agents/ipmilan/fence_ipmilan.py | 57 +++++------------------------------
 fence/agents/lib/fencing.py.py        | 20 ++++++++++++
 3 files changed, 32 insertions(+), 97 deletions(-)

diff --git a/fence/agents/amt/fence_amt.py b/fence/agents/amt/fence_amt.py
index fdf2db3..b70743a 100644
--- a/fence/agents/amt/fence_amt.py
+++ b/fence/agents/amt/fence_amt.py
@@ -6,7 +6,7 @@ import atexit
 from pipes import quote
 sys.path.append("@FENCEAGENTSLIBDIR@")
 from fencing import *
-from fencing import fail_usage, is_executable, SUDO_PATH
+from fencing import fail_usage, is_executable, SUDO_PATH, run_command
 
 #BEGIN_VERSION_GENERATION
 RELEASE_VERSION="Fence agent for Intel AMT"
@@ -15,22 +15,8 @@ BUILD_DATE=""
 #END_VERSION_GENERATION
 
 def get_power_status(_, options):
-       cmd = create_command(options, "status")
-
-       try:
-               logging.debug("Running: %s" % cmd)
-               process = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
stderr=subprocess.PIPE, shell=True)
-       except OSError:
-               fail_usage("Amttool not found or not accessible")
-
-       process.wait()
-
-       out = process.communicate()
-       process.stdout.close()
-       process.stderr.close()
-       logging.debug("%s\n" % str(out))
-
-       match = re.search('Powerstate:[\\s]*(..)', str(out))
+       output = run_command(options, create_command(options, "status"))
+       match = re.search('Powerstate:[\\s]*(..)', str(output))
        status = match.group(1) if match else None
 
        if (status == None):
@@ -41,39 +27,11 @@ def get_power_status(_, options):
                return "off"
 
 def set_power_status(_, options):
-       cmd = create_command(options, options["--action"])
-
-       try:
-               logging.debug("Running: %s" % cmd)
-               process = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
stderr=subprocess.PIPE, shell=True)
-       except OSError:
-               fail_usage("Amttool not found or not accessible")
-
-       process.wait()
-
-       out = process.communicate()
-       process.stdout.close()
-       process.stderr.close()
-       logging.debug("%s\n" % str(out))
-
+       run_command(options, create_command(options, options["--action"]))
        return
 
 def reboot_cycle(_, options):
-       cmd = create_command(options, "cycle")
-
-       try:
-               logging.debug("Running: %s" % cmd)
-               process = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
stderr=subprocess.PIPE, shell=True)
-       except OSError:
-               fail_usage("Amttool not found or not accessible")
-
-       status = process.wait()
-
-       out = process.communicate()
-       process.stdout.close()
-       process.stderr.close()
-       logging.debug("%s\n" % str(out))
-    
+       (status, _, _) = run_command(options, create_command(options, "cycle"))
        return not bool(status)
 
 def create_command(options, action):
diff --git a/fence/agents/ipmilan/fence_ipmilan.py 
b/fence/agents/ipmilan/fence_ipmilan.py
index a57efb7..a3d490f 100644
--- a/fence/agents/ipmilan/fence_ipmilan.py
+++ b/fence/agents/ipmilan/fence_ipmilan.py
@@ -1,12 +1,12 @@
 #!/usr/bin/python -tt
 
-import sys, shlex, subprocess, re, os
+import sys, shlex, re, os
 import logging
 import atexit
 from pipes import quote
 sys.path.append("@FENCEAGENTSLIBDIR@")
 from fencing import *
-from fencing import SUDO_PATH, fail_usage, is_executable
+from fencing import SUDO_PATH, fail_usage, is_executable, run_command
 
 #BEGIN_VERSION_GENERATION
 RELEASE_VERSION=""
@@ -15,61 +15,18 @@ BUILD_DATE=""
 #END_VERSION_GENERATION
 
 def get_power_status(_, options):
-       cmd = create_command(options, "status")
-
-       try:
-               logging.info("Executing: %s\n" % cmd)
-               process = subprocess.Popen(shlex.split(cmd), 
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-       except OSError:
-               fail_usage("Ipmitool not found or not accessible")
-
-       process.wait()
-
-       out = process.communicate()
-       process.stdout.close()
-       process.stderr.close()
-       logging.debug("%s\n" % str(out))
-
-       match = re.search('[Cc]hassis [Pp]ower is [\\s]*([a-zA-Z]{2,3})', 
str(out))
+       output = run_command(options, create_command(options, "status"))
+       match = re.search('[Cc]hassis [Pp]ower is [\\s]*([a-zA-Z]{2,3})', 
str(output))
        status = match.group(1) if match else None
-
        return status
 
 def set_power_status(_, options):
-       cmd = create_command(options, options["--action"])
-
-       try:
-               logging.debug("Executing: %s\n" % cmd)
-               process = subprocess.Popen(shlex.split(cmd), 
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-       except OSError:
-               fail_usage("Ipmitool not found or not accessible")
-
-       process.wait()
-
-       out = process.communicate()
-       process.stdout.close()
-       process.stderr.close()
-       logging.debug("%s\n" % str(out))
-
+       run_command(options, create_command(options, options["--action"]))
        return
 
 def reboot_cycle(_, options):
-       cmd = create_command(options, "cycle")
-
-       try:
-               logging.debug("Executing: %s\n" % cmd)
-               process = subprocess.Popen(shlex.split(cmd), 
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-       except OSError:
-               fail_usage("Ipmitool not found or not accessible")
-
-       process.wait()
-
-       out = process.communicate()
-       process.stdout.close()
-       process.stderr.close()
-       logging.debug("%s\n" % str(out))
-
-       return bool(re.search('chassis power control: cycle', str(out).lower()))
+       output = run_command(options, create_command(options, "cycle"))
+       return bool(re.search('chassis power control: cycle', 
str(output).lower()))
 
 def create_command(options, action):
        cmd = options["--ipmitool-path"]
diff --git a/fence/agents/lib/fencing.py.py b/fence/agents/lib/fencing.py.py
index f243202..7d2994f 100644
--- a/fence/agents/lib/fencing.py.py
+++ b/fence/agents/lib/fencing.py.py
@@ -3,6 +3,8 @@
 import sys, getopt, time, os, uuid, pycurl, stat
 import pexpect, re, atexit, syslog
 import logging
+import subprocess
+import shlex
 import __main__
 
 ## do not add code here.
@@ -1091,3 +1093,21 @@ def is_executable(path):
                if stat.S_ISREG(stats.st_mode) and os.access(path, os.X_OK):
                        return True
        return False
+
+def run_command(options, command):
+       # @todo: Use timeouts from options[]
+       logging.info("Executing: %s\n" % command)
+
+       try:
+               process = subprocess.Popen(shlex.split(command), 
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+       except OSError, ex:
+               fail_usage("Unable to run %s\n" % command)
+
+       status = process.wait()
+       (pipe_stdout, pipe_stderr) = process.communicate()
+       process.stdout.close()
+       process.stderr.close()
+
+       logging.debug("%s %s %s\n" % str(status), str(pipe_stdout), 
str(pipe_stderr))
+
+       return (status, pipe_stdout, pipe_stderr)
-- 
1.9.0

Reply via email to