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