From: Marek 'marx' Grac <mgrac@redhat.com>
To: cluster-devel.redhat.com
Subject: [Cluster-devel] [PATCH] fencing: Refactor access to 3rd-party binaries
Date: Wed, 9 Apr 2014 17:52:04 +0200 [thread overview]
Message-ID: <1397058724-8528-1-git-send-email-mgrac@redhat.com> (raw)
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 other threads:[~2014-04-09 15:52 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1397058724-8528-1-git-send-email-mgrac@redhat.com \
--to=mgrac@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).