From dcbd01e55a399cfc78de754d5b06305d9b44a549 Mon Sep 17 00:00:00 2001 From: Alexander Gordeev Date: Wed, 11 Mar 2015 15:50:33 +0300 Subject: [PATCH] IBP: improve exception handling in fuel-agent This patch resolves the issue with exception's message carrying and adds very high level try/except clause to fuel-agent entrypoint. Closes-Bug: #1422448 Change-Id: Ie0d326b3b5db4ce05e688a63b817971b4fd77080 --- fuel_agent/cmd/agent.py | 36 ++++++++++++++++------- fuel_agent/drivers/ks_spaces_validator.py | 4 +++ fuel_agent/errors.py | 12 +++----- fuel_agent/utils/md_utils.py | 3 +- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/fuel_agent/cmd/agent.py b/fuel_agent/cmd/agent.py index eb025c33..c161aec5 100644 --- a/fuel_agent/cmd/agent.py +++ b/fuel_agent/cmd/agent.py @@ -16,6 +16,7 @@ import json import sys from oslo.config import cfg +import six from fuel_agent import manager as manager from fuel_agent.openstack.common import log @@ -31,6 +32,10 @@ opts = [ CONF = cfg.CONF CONF.register_opts(opts) +CONF(sys.argv[1:], project='fuel-agent', + version=version.version_info.release_string()) +log.setup('fuel-agent') +LOG = log.getLogger(__name__) def provision(): @@ -53,18 +58,29 @@ def bootloader(): main(['do_parsing', 'do_bootloader']) +def print_err(line): + sys.stderr.write(six.text_type(line)) + sys.stderr.write('\n') + + +def handle_exception(exc): + LOG.exception(exc) + print_err('Unexpected error') + print_err(exc) + sys.exit(-1) + + def main(actions=None): - CONF(sys.argv[1:], project='fuel-agent', - version=version.version_info.release_string()) - log.setup('fuel-agent') + try: + with open(CONF.provision_data_file) as f: + data = json.load(f) - with open(CONF.provision_data_file) as f: - data = json.load(f) - - mgr = manager.Manager(data) - if actions: - for action in actions: - getattr(mgr, action)() + mgr = manager.Manager(data) + if actions: + for action in actions: + getattr(mgr, action)() + except Exception as exc: + handle_exception(exc) if __name__ == '__main__': main() diff --git a/fuel_agent/drivers/ks_spaces_validator.py b/fuel_agent/drivers/ks_spaces_validator.py index c44af9a7..9274e718 100644 --- a/fuel_agent/drivers/ks_spaces_validator.py +++ b/fuel_agent/drivers/ks_spaces_validator.py @@ -15,6 +15,9 @@ import jsonschema from fuel_agent import errors +from fuel_agent.openstack.common import log as logging + +LOG = logging.getLogger(__name__) KS_SPACES_SCHEMA = { @@ -128,6 +131,7 @@ def validate(scheme): jsonschema.validate(scheme, KS_SPACES_SCHEMA, format_checker=checker) except Exception as exc: + LOG.exception(exc) raise errors.WrongPartitionSchemeError(str(exc)) # scheme is not valid if the number of disks is 0 diff --git a/fuel_agent/errors.py b/fuel_agent/errors.py index 4b15c73c..59c12868 100644 --- a/fuel_agent/errors.py +++ b/fuel_agent/errors.py @@ -12,15 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -from fuel_agent.openstack.common import log as logging - -LOG = logging.getLogger(__name__) - class BaseError(Exception): - def __init__(self, *args, **kwargs): - super(BaseException, self).__init__(*args, **kwargs) - LOG.error(self.message) + def __init__(self, message, *args, **kwargs): + self.message = message + super(BaseError, self).__init__(message, *args, **kwargs) class WrongPartitionSchemeError(BaseError): @@ -103,7 +99,7 @@ class TemplateWriteError(BaseError): pass -class ProcessExecutionError(Exception): +class ProcessExecutionError(BaseError): def __init__(self, stdout=None, stderr=None, exit_code=None, cmd=None, description=None): self.exit_code = exit_code diff --git a/fuel_agent/utils/md_utils.py b/fuel_agent/utils/md_utils.py index 0b4aef82..e07284db 100644 --- a/fuel_agent/utils/md_utils.py +++ b/fuel_agent/utils/md_utils.py @@ -61,7 +61,8 @@ def mddisplay(names=None): output = utils.execute('mdadm', '--detail', mdname, check_exit_code=[0])[0] md.update(mddetail_parse(output)) - except errors.ProcessExecutionError: + except errors.ProcessExecutionError as exc: + LOG.debug(exc) continue finally: mds.append(md)