Commit 076cf903 authored by Marco Mariani's avatar Marco Mariani

Explicitly provide logger to SoftwareRelease.error and

ComputerPartition.error. Also use a fallback logger if not provided.
parent 128cc007
...@@ -579,14 +579,14 @@ class Slapgrid(object): ...@@ -579,14 +579,14 @@ class Slapgrid(object):
self.logger.info('Destroyed %r.' % software_release_uri) self.logger.info('Destroyed %r.' % software_release_uri)
# Send log before exiting # Send log before exiting
except (SystemExit, KeyboardInterrupt): except (SystemExit, KeyboardInterrupt):
software_release.error(traceback.format_exc()) software_release.error(traceback.format_exc(), logger=self.logger)
raise raise
# Buildout failed: send log but don't print it to output (already done) # Buildout failed: send log but don't print it to output (already done)
except BuildoutFailedError as exc: except BuildoutFailedError as exc:
clean_run = False clean_run = False
try: try:
software_release.error(exc) software_release.error(exc, logger=self.logger)
except (SystemExit, KeyboardInterrupt): except (SystemExit, KeyboardInterrupt):
raise raise
except Exception: except Exception:
...@@ -597,7 +597,7 @@ class Slapgrid(object): ...@@ -597,7 +597,7 @@ class Slapgrid(object):
except Exception: except Exception:
exc = traceback.format_exc() exc = traceback.format_exc()
self.logger.error(exc) self.logger.error(exc)
software_release.error(exc) software_release.error(exc, logger=self.logger)
clean_run = False clean_run = False
else: else:
if state == 'available': if state == 'available':
...@@ -798,14 +798,14 @@ class Slapgrid(object): ...@@ -798,14 +798,14 @@ class Slapgrid(object):
try: try:
computer_partition.stopped() computer_partition.stopped()
except (SystemExit, KeyboardInterrupt): except (SystemExit, KeyboardInterrupt):
computer_partition.error(traceback.format_exc()) computer_partition.error(traceback.format_exc(), logger=self.logger)
raise raise
except Exception: except Exception:
pass pass
else: else:
error_string = "Computer Partition %r has unsupported state: %s" % \ error_string = "Computer Partition %r has unsupported state: %s" % \
(computer_partition_id, computer_partition_state) (computer_partition_id, computer_partition_state)
computer_partition.error(error_string) computer_partition.error(error_string, logger=self.logger)
raise NotImplementedError(error_string) raise NotImplementedError(error_string)
# If partition has been successfully processed, write timestamp # If partition has been successfully processed, write timestamp
...@@ -850,13 +850,13 @@ class Slapgrid(object): ...@@ -850,13 +850,13 @@ class Slapgrid(object):
# Send log before exiting # Send log before exiting
except (SystemExit, KeyboardInterrupt): except (SystemExit, KeyboardInterrupt):
computer_partition.error(traceback.format_exc()) computer_partition.error(traceback.format_exc(), logger=self.logger)
raise raise
# Buildout failed: send log but don't print it to output (already done) # Buildout failed: send log but don't print it to output (already done)
except BuildoutFailedError as exc: except BuildoutFailedError as exc:
try: try:
computer_partition.error(exc) computer_partition.error(exc, logger=self.logger)
except (SystemExit, KeyboardInterrupt): except (SystemExit, KeyboardInterrupt):
raise raise
except Exception: except Exception:
...@@ -867,7 +867,7 @@ class Slapgrid(object): ...@@ -867,7 +867,7 @@ class Slapgrid(object):
except Exception as exc: except Exception as exc:
self.logger.error(traceback.format_exc()) self.logger.error(traceback.format_exc())
try: try:
computer_partition.error(exc) computer_partition.error(exc, logger=self.logger)
except (SystemExit, KeyboardInterrupt): except (SystemExit, KeyboardInterrupt):
raise raise
except Exception: except Exception:
...@@ -904,14 +904,14 @@ class Slapgrid(object): ...@@ -904,14 +904,14 @@ class Slapgrid(object):
# Send log before exiting # Send log before exiting
except (SystemExit, KeyboardInterrupt): except (SystemExit, KeyboardInterrupt):
computer_partition.error(traceback.format_exc()) computer_partition.error(traceback.format_exc(), logger=self.logger)
raise raise
except Slapgrid.PromiseError as exc: except Slapgrid.PromiseError as exc:
clean_run_promise = False clean_run_promise = False
try: try:
self.logger.error(exc) self.logger.error(exc)
computer_partition.error(exc) computer_partition.error(exc, logger=self.logger)
except (SystemExit, KeyboardInterrupt): except (SystemExit, KeyboardInterrupt):
raise raise
except Exception: except Exception:
...@@ -922,7 +922,7 @@ class Slapgrid(object): ...@@ -922,7 +922,7 @@ class Slapgrid(object):
except BuildoutFailedError as exc: except BuildoutFailedError as exc:
clean_run = False clean_run = False
try: try:
computer_partition.error(exc) computer_partition.error(exc, logger=self.logger)
except (SystemExit, KeyboardInterrupt): except (SystemExit, KeyboardInterrupt):
raise raise
except Exception: except Exception:
...@@ -934,7 +934,7 @@ class Slapgrid(object): ...@@ -934,7 +934,7 @@ class Slapgrid(object):
clean_run = False clean_run = False
self.logger.error(traceback.format_exc()) self.logger.error(traceback.format_exc())
try: try:
computer_partition.error(exc) computer_partition.error(exc, logger=self.logger)
except (SystemExit, KeyboardInterrupt): except (SystemExit, KeyboardInterrupt):
raise raise
except Exception: except Exception:
...@@ -1106,7 +1106,7 @@ class Slapgrid(object): ...@@ -1106,7 +1106,7 @@ class Slapgrid(object):
failed_script_list.append("Script %r failed." % script) failed_script_list.append("Script %r failed." % script)
self.logger.warning('Failed to run %r' % invocation_list) self.logger.warning('Failed to run %r' % invocation_list)
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), logger=self.logger)
# Whatever happens, don't stop processing other instances # Whatever happens, don't stop processing other instances
except Exception: except Exception:
self.logger.info('Cannot run usage script(s) for %r: %s' % ( self.logger.info('Cannot run usage script(s) for %r: %s' % (
...@@ -1186,7 +1186,7 @@ class Slapgrid(object): ...@@ -1186,7 +1186,7 @@ class Slapgrid(object):
computer_partition.getId(), computer_partition.getId(),
traceback.format_exc()) traceback.format_exc())
self.logger.info(issue) self.logger.info(issue)
computer_partition.error(issue) computer_partition.error(issue, logger=self.logger)
report_usage_issue_cp_list.append(computer_partition_id) report_usage_issue_cp_list.append(computer_partition_id)
for computer_partition in computer_partition_list: for computer_partition in computer_partition_list:
...@@ -1220,7 +1220,7 @@ class Slapgrid(object): ...@@ -1220,7 +1220,7 @@ class Slapgrid(object):
try: try:
computer_partition.stopped() computer_partition.stopped()
except (SystemExit, KeyboardInterrupt): except (SystemExit, KeyboardInterrupt):
computer_partition.error(traceback.format_exc()) computer_partition.error(traceback.format_exc(), logger=self.logger)
raise raise
except Exception: except Exception:
pass pass
...@@ -1230,12 +1230,12 @@ class Slapgrid(object): ...@@ -1230,12 +1230,12 @@ class Slapgrid(object):
continue continue
local_partition.destroy() local_partition.destroy()
except (SystemExit, KeyboardInterrupt): except (SystemExit, KeyboardInterrupt):
computer_partition.error(traceback.format_exc()) computer_partition.error(traceback.format_exc(), logger=self.logger)
raise raise
except Exception: except Exception:
clean_run = False clean_run = False
exc = traceback.format_exc() exc = traceback.format_exc()
computer_partition.error(exc) computer_partition.error(exc, logger=self.logger)
self.logger.error(exc) self.logger.error(exc)
try: try:
computer_partition.destroyed() computer_partition.destroyed()
......
...@@ -273,8 +273,8 @@ def bootstrapBuildout(path, logger, buildout=None, ...@@ -273,8 +273,8 @@ def bootstrapBuildout(path, logger, buildout=None,
message = 'Failed to run buildout profile in directory %r' % (path) message = 'Failed to run buildout profile in directory %r' % (path)
logger.error(message) logger.error(message)
raise BuildoutFailedError('%s:\n%s\n' % (message, process_handler.output)) raise BuildoutFailedError('%s:\n%s\n' % (message, process_handler.output))
except OSError as error: except OSError as exc:
raise BuildoutFailedError(error) raise BuildoutFailedError(exc)
finally: finally:
old_umask = os.umask(umask) old_umask = os.umask(umask)
logger.debug('Restore umask from %03o to %03o' % (old_umask, umask)) logger.debug('Restore umask from %03o to %03o' % (old_umask, umask))
...@@ -316,8 +316,8 @@ def launchBuildout(path, buildout_binary, logger, ...@@ -316,8 +316,8 @@ def launchBuildout(path, buildout_binary, logger,
message = 'Failed to run buildout profile in directory %r' % (path) message = 'Failed to run buildout profile in directory %r' % (path)
logger.error(message) logger.error(message)
raise BuildoutFailedError('%s:\n%s\n' % (message, process_handler.output)) raise BuildoutFailedError('%s:\n%s\n' % (message, process_handler.output))
except OSError as error: except OSError as exc:
raise BuildoutFailedError(error) raise BuildoutFailedError(exc)
finally: finally:
old_umask = os.umask(umask) old_umask = os.umask(umask)
logger.debug('Restore umask from %03o to %03o' % (old_umask, umask)) logger.debug('Restore umask from %03o to %03o' % (old_umask, umask))
......
...@@ -34,8 +34,6 @@ __all__ = ["slap", "ComputerPartition", "Computer", "SoftwareRelease", ...@@ -34,8 +34,6 @@ __all__ = ["slap", "ComputerPartition", "Computer", "SoftwareRelease",
"Supply", "OpenOrder", "NotFoundError", "Unauthorized", "Supply", "OpenOrder", "NotFoundError", "Unauthorized",
"ResourceNotReady", "ServerError"] "ResourceNotReady", "ServerError"]
from interface import slap as interface
from xml_marshaller import xml_marshaller
import httplib import httplib
import logging import logging
import socket import socket
...@@ -43,9 +41,16 @@ import ssl ...@@ -43,9 +41,16 @@ import ssl
import traceback import traceback
import urllib import urllib
import urlparse import urlparse
import zope.interface import zope.interface
from interface import slap as interface
from xml_marshaller import xml_marshaller
fallback_logger = logging.getLogger()
fallback_handler = logging.StreamHandler()
fallback_logger.setLevel(logging.INFO)
fallback_logger.addHandler(fallback_handler)
log = logging.getLogger(__name__)
DEFAULT_SOFTWARE_TYPE = 'RootSoftwareInstance' DEFAULT_SOFTWARE_TYPE = 'RootSoftwareInstance'
...@@ -112,7 +117,7 @@ class SoftwareRelease(SlapDocument): ...@@ -112,7 +117,7 @@ class SoftwareRelease(SlapDocument):
else: else:
return self._software_release return self._software_release
def error(self, error_log): def error(self, error_log, logger=None):
try: try:
# Does not follow interface # Does not follow interface
self._connection_helper.POST('/softwareReleaseError', { self._connection_helper.POST('/softwareReleaseError', {
...@@ -120,8 +125,7 @@ class SoftwareRelease(SlapDocument): ...@@ -120,8 +125,7 @@ class SoftwareRelease(SlapDocument):
'computer_id' : self.getComputerId(), 'computer_id' : self.getComputerId(),
'error_log': error_log}) 'error_log': error_log})
except Exception: except Exception:
exception = traceback.format_exc() (logger or fallback_logger).error(traceback.format_exc())
log.error(exception)
def available(self): def available(self):
self._connection_helper.POST('/availableSoftwareRelease', { self._connection_helper.POST('/availableSoftwareRelease', {
...@@ -476,15 +480,14 @@ class ComputerPartition(SlapDocument): ...@@ -476,15 +480,14 @@ class ComputerPartition(SlapDocument):
'computer_partition_id': self.getId(), 'computer_partition_id': self.getId(),
}) })
def error(self, error_log): def error(self, error_log, logger=None):
try: try:
self._connection_helper.POST('/softwareInstanceError', { self._connection_helper.POST('/softwareInstanceError', {
'computer_id': self._computer_id, 'computer_id': self._computer_id,
'computer_partition_id': self.getId(), 'computer_partition_id': self.getId(),
'error_log': error_log}) 'error_log': error_log})
except Exception: except Exception:
exception = traceback.format_exc() (logger or fallback_logger).error(traceback.format_exc())
log.error(exception)
def bang(self, message): def bang(self, message):
self._connection_helper.POST('/softwareInstanceBang', { self._connection_helper.POST('/softwareInstanceBang', {
...@@ -726,7 +729,7 @@ class slap: ...@@ -726,7 +729,7 @@ class slap:
returns SoftwareRelease class object returns SoftwareRelease class object
""" """
return SoftwareRelease(software_release=software_release, return SoftwareRelease(software_release=software_release,
connection_helper=self._connection_helper, connection_helper=self._connection_helper
) )
def registerComputer(self, computer_guid): def registerComputer(self, computer_guid):
......
...@@ -24,12 +24,15 @@ ...@@ -24,12 +24,15 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# #
############################################################################## ##############################################################################
import xml_marshaller
import httplib
import os
import unittest import unittest
import urlparse import urlparse
import httplib
import slapos.slap import slapos.slap
import os import xml_marshaller
class UndefinedYetException(Exception): class UndefinedYetException(Exception):
"""To catch exceptions which are not yet defined""" """To catch exceptions which are not yet defined"""
......
...@@ -1831,7 +1831,7 @@ exit 127""" % {'worked_file': worked_file}) ...@@ -1831,7 +1831,7 @@ exit 127""" % {'worked_file': worked_file})
slapos.grid.slapgrid.SLAPGRID_PROMISE_FAIL) slapos.grid.slapgrid.SLAPGRID_PROMISE_FAIL)
self.assertTrue(os.path.isfile(worked_file)) self.assertTrue(os.path.isfile(worked_file))
self.assertEqual(instance.error_log.split('\n')[-1], 'Error') self.assertEqual(instance.error_log[-5:], 'Error')
self.assertTrue(instance.error) self.assertTrue(instance.error)
self.assertIsNone(instance.state) self.assertIsNone(instance.state)
......
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