Commit 04a8e89d authored by Marco Mariani's avatar Marco Mariani

grid: extracted method check_promise()

parent 9c61e87e
...@@ -196,7 +196,7 @@ def create_slapgrid_object(options, logger): ...@@ -196,7 +196,7 @@ def create_slapgrid_object(options, logger):
supervisord_configuration_path=op['supervisord_configuration_path'], supervisord_configuration_path=op['supervisord_configuration_path'],
buildout=op.get('buildout'), buildout=op.get('buildout'),
logger=logger, logger=logger,
maximum_periodicity = op.get('maximum_periodicity', 86400), maximum_periodicity=op.get('maximum_periodicity', 86400),
key_file=op.get('key_file'), key_file=op.get('key_file'),
cert_file=op.get('cert_file'), cert_file=op.get('cert_file'),
signature_private_key_file=op.get('signature_private_key_file'), signature_private_key_file=op.get('signature_private_key_file'),
...@@ -375,7 +375,7 @@ class Slapgrid(object): ...@@ -375,7 +375,7 @@ class Slapgrid(object):
try: try:
return self.computer.getComputerPartitionList() return self.computer.getComputerPartitionList()
except socket.error as exc: except socket.error as exc:
self.logger.fatal(exc) self.logger.critical(exc)
raise raise
def processSoftwareReleaseList(self): def processSoftwareReleaseList(self):
...@@ -472,56 +472,56 @@ class Slapgrid(object): ...@@ -472,56 +472,56 @@ class Slapgrid(object):
self.supervisord_configuration_path, self.supervisord_configuration_path,
logger=self.logger) logger=self.logger)
def _checkPromises(self, computer_partition): def check_promise(self, instance_path, promise_path):
self.logger.info("Checking promises...") promise = os.path.basename(promise_path)
instance_path = os.path.join(self.instance_root, computer_partition.getId()) self.logger.info('Checking promise %r.', promise)
uid, gid = None, None
stat_info = os.stat(instance_path) stat_info = os.stat(instance_path)
#stat sys call to get statistics informations
uid = stat_info.st_uid uid = stat_info.st_uid
gid = stat_info.st_gid gid = stat_info.st_gid
promise_present = False p = subprocess.Popen([promise_path],
# Get the list of promises
promise_dir = os.path.join(instance_path, 'etc', 'promise')
if os.path.exists(promise_dir) and os.path.isdir(promise_dir):
# Check whether every promise is kept
for promise in os.listdir(promise_dir):
promise_present = True
command = [os.path.join(promise_dir, promise)]
promise = os.path.basename(command[0])
self.logger.info("Checking promise %r.", promise)
process_handler = subprocess.Popen(command,
preexec_fn=lambda: dropPrivileges(uid, gid, logger=self.logger), preexec_fn=lambda: dropPrivileges(uid, gid, logger=self.logger),
cwd=instance_path, cwd=instance_path,
env=None if sys.platform == 'cygwin' else {}, env=None if sys.platform == 'cygwin' else {},
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE) stdin=subprocess.PIPE)
process_handler.stdin.flush() p.stdin.flush()
process_handler.stdin.close() p.stdin.close()
process_handler.stdin = None p.stdin = None
time.sleep(self.promise_timeout) time.sleep(self.promise_timeout)
if process_handler.poll() is None: if p.poll() is None:
process_handler.terminate() p.terminate()
raise Slapgrid.PromiseError("The promise %r timed out" % promise) raise Slapgrid.PromiseError('The promise %r timed out' % promise)
elif process_handler.poll() != 0: elif p.poll() != 0:
stderr = process_handler.communicate()[1] stderr = p.communicate()[1]
if stderr is None: if stderr is None:
stderr = 'No error output from %r.' % promise message = 'No error output from %r.' % promise
else: else:
stderr = 'Promise %r:' % promise + stderr message = 'Promise %r:%s' % (promise, stderr)
raise Slapgrid.PromiseError(stderr) raise Slapgrid.PromiseError(message)
if not promise_present: def check_all_promises(self, computer_partition):
self.logger.info("No promise.") self.logger.info("Checking promises...")
instance_path = os.path.join(self.instance_root, computer_partition.getId())
promise_dir = os.path.join(instance_path, 'etc', 'promise')
if os.path.isdir(promise_dir):
promises = os.listdir(promise_dir)
else:
promises = []
if not promises:
self.logger.info('No promise.')
return
for promise_filename in promises:
promise_path = os.path.join(promise_dir, promise_filename)
self.check_promise(instance_path, promise_path)
def processComputerPartition(self, computer_partition): def processComputerPartition(self, computer_partition):
""" """
...@@ -536,7 +536,7 @@ class Slapgrid(object): ...@@ -536,7 +536,7 @@ class Slapgrid(object):
# Check if we defined explicit list of partitions to process. # Check if we defined explicit list of partitions to process.
# If so, if current partition not in this list, skip. # If so, if current partition not in this list, skip.
if len(self.computer_partition_filter_list) > 0 and \ if self.computer_partition_filter_list and \
(computer_partition_id not in self.computer_partition_filter_list): (computer_partition_id not in self.computer_partition_filter_list):
return return
...@@ -561,6 +561,7 @@ class Slapgrid(object): ...@@ -561,6 +561,7 @@ class Slapgrid(object):
# Problem with instance: SR URI not set. # Problem with instance: SR URI not set.
# Try to process it anyway, it may need to be deleted. # Try to process it anyway, it may need to be deleted.
software_url = None software_url = None
try: try:
software_path = os.path.join(self.software_root, md5digest(software_url)) software_path = os.path.join(self.software_root, md5digest(software_url))
except TypeError: except TypeError:
...@@ -631,7 +632,7 @@ class Slapgrid(object): ...@@ -631,7 +632,7 @@ class Slapgrid(object):
local_partition.install() local_partition.install()
computer_partition.available() computer_partition.available()
local_partition.start() local_partition.start()
self._checkPromises(computer_partition) self.check_all_promises(computer_partition)
computer_partition.started() computer_partition.started()
elif computer_partition_state == COMPUTER_PARTITION_STOPPED_STATE: elif computer_partition_state == COMPUTER_PARTITION_STOPPED_STATE:
try: try:
......
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