Skip to contentSkip to conversation

fencing: Refactor access to 3rd-party binaries

PATCH New Marek 'marx' Grac 1 participant 1 message Apr 9, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fencing: Refactor access to 3rd-party binaries

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(-)
fence/agents/amt/fence_amt.py +5 -47
index fdf2db3..b70743a 100644
@@ -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):
fence/agents/ipmilan/fence_ipmilan.py +7 -50

80 lines not on this page · raw message

fence/agents/lib/fencing.py.py +20 -0
index f243202..7d2994f 100644
@@ -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)
Signature · 1 line
1.9.0