fence-agents: master - fencing: added timeout and env parameters to run_command function

Marek GrĂ¡c marx at fedoraproject.org
Mon Apr 14 11:56:14 UTC 2014


Gitweb:        http://git.fedorahosted.org/git/?p=fence-agents.git;a=commitdiff;h=95c1e64c75c9ae0410febd9653d6e65b88ff0d88
Commit:        95c1e64c75c9ae0410febd9653d6e65b88ff0d88
Parent:        665b26cd5c98877a85d69bc00fa6860b511b6f14
Author:        Ondrej Mular <omular at redhat.com>
AuthorDate:    Fri Apr 11 07:41:38 2014 -0400
Committer:     Marek 'marx' Grac <mgrac at redhat.com>
CommitterDate: Mon Apr 14 13:54:59 2014 +0200

fencing: added timeout and env parameters to run_command function

Now, we can set enviromant variables and maximum execution time for subprocess.
---
 fence/agents/lib/fencing.py.py |   21 +++++++++++++++++----
 1 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/fence/agents/lib/fencing.py.py b/fence/agents/lib/fencing.py.py
index 32040db..31155ac 100644
--- a/fence/agents/lib/fencing.py.py
+++ b/fence/agents/lib/fencing.py.py
@@ -4,6 +4,7 @@ import sys, getopt, time, os, uuid, pycurl, stat
 import pexpect, re, atexit, syslog
 import logging
 import subprocess
+import threading
 import shlex
 import __main__
 
@@ -1094,20 +1095,32 @@ def is_executable(path):
 			return True
 	return False
 
-def run_command(options, command):
-	# @todo: Use timeouts from options[]
+def run_command(options, command, timeout = None, env = None):
+	if timeout is None and "--power-timeout" in options:
+		timeout = options["--power-timeout"]
+	if timeout is not None:
+		timeout = float(timeout)
+
 	logging.info("Executing: %s\n" % command)
 
 	try:
-		process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+		process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
 	except OSError, ex:
 		fail_usage("Unable to run %s\n" % command)
 
+	thread = threading.Thread(target = process.wait)
+	thread.start()
+	thread.join(timeout)
+	if (thread.is_alive()):
+		process.kill()
+		fail(EC_TIMED_OUT)
+
 	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))
+	logging.debug("%s %s %s\n" % (str(status), str(pipe_stdout), str(pipe_stderr)))
 
 	return (status, pipe_stdout, pipe_stderr)


More information about the cluster-commits mailing list