Gitweb:
http://git.fedorahosted.org/git/?p=fence-agents.git;a=commitdiff;h=698691...
Commit: 698691e42f383d971e62915fbe83c3801d1b9266
Parent: b4125861985a9574d9e199821d45a248ec581d49
Author: Marek 'marx' Grac <mgrac(a)redhat.com>
AuthorDate: Fri Apr 11 09:37:36 2014 +0200
Committer: Marek 'marx' Grac <mgrac(a)redhat.com>
CommitterDate: Fri Apr 11 09:37:36 2014 +0200
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 | 55 +++---------------------------
fence/agents/ipmilan/fence_ipmilan.py | 58 ++++-----------------------------
fence/agents/lib/fencing.py.py | 20 +++++++++++
3 files changed, 33 insertions(+), 100 deletions(-)
diff --git a/fence/agents/amt/fence_amt.py b/fence/agents/amt/fence_amt.py
index fdf2db3..dc2fd12 100644
--- a/fence/agents/amt/fence_amt.py
+++ b/fence/agents/amt/fence_amt.py
@@ -1,12 +1,11 @@
#!/usr/bin/python -tt
-import sys, subprocess, re
-import logging
+import sys, re
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 +14,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 +26,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 9357369..d7b0f18 100644
--- a/fence/agents/ipmilan/fence_ipmilan.py
+++ b/fence/agents/ipmilan/fence_ipmilan.py
@@ -1,12 +1,11 @@
#!/usr/bin/python -tt
-import sys, shlex, subprocess, re, os
-import logging
+import sys, re, os
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 +14,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)