Commit 8e0369a2 authored by Antoine Catton's avatar Antoine Catton

Code factorization of report running and promise checking

parent 65494a3a
...@@ -472,6 +472,23 @@ class Slapgrid(object): ...@@ -472,6 +472,23 @@ class Slapgrid(object):
return False return False
def _runCommandsAsUserAndYieldPopen(self, commands_list, user, cwd):
uid, gid = user
for command in commands_list:
kw = dict()
if not self.console:
kw.update(stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process_handler = SlapPopen(command,
preexec_fn=lambda: dropPrivileges(uid, gid),
cwd=cwd,
env=None, **kw)
yield (process_handler, command)
def agregateAndSendUsage(self): def agregateAndSendUsage(self):
"""Will agregate usage from each Computer Partition. """Will agregate usage from each Computer Partition.
""" """
...@@ -494,6 +511,13 @@ class Slapgrid(object): ...@@ -494,6 +511,13 @@ class Slapgrid(object):
else: else:
script_list_to_run = [] script_list_to_run = []
uid, gid = None, None
stat_info = os.stat(instance_path)
#stat sys call to get statistics informations
uid = stat_info.st_uid
gid = stat_info.st_gid
#We now generate the pseudorandom name for the xml file #We now generate the pseudorandom name for the xml file
# and we add it in the invocation_list # and we add it in the invocation_list
f = tempfile.NamedTemporaryFile() f = tempfile.NamedTemporaryFile()
...@@ -502,30 +526,18 @@ class Slapgrid(object): ...@@ -502,30 +526,18 @@ class Slapgrid(object):
name_xml) name_xml)
failed_script_list = [] failed_script_list = []
for script in script_list_to_run:
commands_to_run = [ (os.path.join(instance_path, 'etc', 'report', script),
invocation_list = [] path_to_slapreport,
invocation_list.append(os.path.join(instance_path, 'etc', 'report', )
script)) for script in script_list_to_run ]
#We add the xml_file name in the invocation_list
#f = tempfile.NamedTemporaryFile() cwd = os.path.join(instance_path, 'etc', 'report')
#name_xml = '%s.%s' % ('slapreport', os.path.basename(f.name))
#path_to_slapreport = os.path.join(instance_path, 'var', name_xml) for process_handler, command in self._runCommandAsUserAndYieldPopen(commands_to_run,
(uid, gid),
invocation_list.append(path_to_slapreport) cwd):
#Dropping privileges
uid, gid = None, None
stat_info = os.stat(instance_path)
#stat sys call to get statistics informations
uid = stat_info.st_uid
gid = stat_info.st_gid
kw = dict()
if not self.console:
kw.update(stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process_handler = SlapPopen(invocation_list,
preexec_fn=lambda: dropPrivileges(uid, gid),
cwd=os.path.join(instance_path, 'etc', 'report'),
env=None, **kw)
result = process_handler.communicate()[0] result = process_handler.communicate()[0]
if self.console: if self.console:
result = 'Please consult messages above' result = 'Please consult messages above'
...@@ -535,39 +547,27 @@ class Slapgrid(object): ...@@ -535,39 +547,27 @@ class Slapgrid(object):
clean_run = False clean_run = False
failed_script_list.append("Script %r failed with %s." % (script, result)) failed_script_list.append("Script %r failed with %s." % (script, result))
logger.warning("Failed to run %r, the result was. \n%s" % logger.warning("Failed to run %r, the result was. \n%s" %
(invocation_list, result)) (command, result))
if len(failed_script_list): if len(failed_script_list):
computer_partition.error('\n'.join(failed_script_list)) computer_partition.error('\n'.join(failed_script_list))
# #
# Checking if the promises are kept # Checking if the promises are kept
# #
# XXX-Antoine: I thought about factorizing this code with
# the above one. But it seemed like a lot to get through.
# Get the list of promises # Get the list of promises
promise_dir = os.join(instance_path, 'etc', 'promise') promise_dir = os.join(instance_path, 'etc', 'promise')
promises_list = os.listdir(promise_dir) commands_to_run = os.listdir(promise_dir)
cwd = instance_path
# Check whether every promise is kept # Check whether every promise is kept
for promise in promises_list: for process_handler, command in self._runCommandAsUserAndYieldPopen(commands_to_run,
command_line = [promise] (uid, gid),
cwd):
uid, gid = None, None
stat_info = os.stat(instance_path)
uid, gid = stat_info.st_uid, stat_info.st_gid
kw = dict()
if not self.console:
kw.update(stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process_handler = SlapPopen(command_line,
preexec_fn=lambda: dropPrivileges(uid, gid),
cwd=instance_path,
env=None, **kw)
time.sleep(3) # 3 seconds timeout time.sleep(3) # 3 seconds timeout
promise = os.path.basename(command)
if process_handler.poll() is None: if process_handler.poll() is None:
process_handler.kill() process_handler.kill()
computer_partition.error("The promise '%s' timed out" % promise) computer_partition.error("The promise '%s' timed out" % promise)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment