From: Walter A. Boring IV Date: Thu, 8 Aug 2013 22:29:54 +0000 (-0700) Subject: Remove Brick iser dependency on cinder X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=d1d6b808afec4a4d74d004dc48832eaaf1b415b1;p=openstack-build%2Fcinder-build.git Remove Brick iser dependency on cinder This patch removes the brick iser.py's dependency on cinder exceptions and volume_utils. This required moving some exceptions out of cinder's exception.py that the iser.py raises. Also had to create a BrickException and refactor existing brick exceptions to use the new BrickException model. Fixes Bug #1210312 Change-Id: I672375807fed4952e5321fbcd9b57b9ef369f68f --- diff --git a/cinder/brick/exception.py b/cinder/brick/exception.py new file mode 100644 index 000000000..b103e16d8 --- /dev/null +++ b/cinder/brick/exception.py @@ -0,0 +1,114 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# (c) Copyright 2013 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Exceptions for the Brick library.""" + +import sys + +from oslo.config import cfg + +from cinder.openstack.common import log as logging + + +LOG = logging.getLogger(__name__) + +exc_log_opts = [ + cfg.BoolOpt('fatal_exception_format_errors', + default=False, + help='make exception message format errors fatal'), +] + +CONF = cfg.CONF +CONF.register_opts(exc_log_opts) + + +class BrickException(Exception): + """Base Brick Exception + + To correctly use this class, inherit from it and define + a 'msg_fmt' property. That msg_fmt will get printf'd + with the keyword arguments provided to the constructor. + """ + msg_fmt = _("An unknown exception occurred.") + code = 500 + headers = {} + safe = False + + def __init__(self, message=None, **kwargs): + self.kwargs = kwargs + + if 'code' not in self.kwargs: + try: + self.kwargs['code'] = self.code + except AttributeError: + pass + + if not message: + try: + message = self.msg_fmt % kwargs + + except Exception: + exc_info = sys.exc_info() + # kwargs doesn't match a variable in the message + # log the issue and the kwargs + LOG.exception(_('Exception in string format operation')) + for name, value in kwargs.iteritems(): + LOG.error("%s: %s" % (name, value)) + + if CONF.fatal_exception_format_errors: + raise exc_info[0], exc_info[1], exc_info[2] + else: + # at least get the core message out if something happened + message = self.msg_fmt + + super(BrickException, self).__init__(message) + + +class NotFound(BrickException): + message = _("Resource could not be found.") + code = 404 + safe = True + + +class Invalid(BrickException): + message = _("Unacceptable parameters.") + code = 400 + + +# Cannot be templated as the error syntax varies. +# msg needs to be constructed when raised. +class InvalidParameterValue(Invalid): + message = _("%(err)s") + + +class NoFibreChannelHostsFound(BrickException): + message = _("We are unable to locate any Fibre Channel devices.") + + +class NoFibreChannelVolumeDeviceFound(BrickException): + message = _("Unable to find a Fibre Channel volume device.") + + +class VolumeDeviceNotFound(BrickException): + message = _("Volume device not found at %(device)s.") + + +class ISERTargetCreateFailed(BrickException): + message = _("Failed to create iser target for volume %(volume_id)s.") + + +class ISERTargetRemoveFailed(BrickException): + message = _("Failed to remove iser target for volume %(volume_id)s.") diff --git a/cinder/brick/exceptions.py b/cinder/brick/exceptions.py deleted file mode 100644 index d046e3f9c..000000000 --- a/cinder/brick/exceptions.py +++ /dev/null @@ -1,35 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# (c) Copyright 2013 Hewlett-Packard Development Company, L.P. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -"""Exceptions for the Brick library.""" - - -class NoFibreChannelHostsFound(Exception): - def __init__(self): - message = _("We are unable to locate any Fibre Channel devices") - super(NoFibreChannelHostsFound, self).__init__(message) - - -class NoFibreChannelVolumeDeviceFound(Exception): - def __init__(self): - message = _("Unable to find a Fibre Channel volume device") - super(NoFibreChannelVolumeDeviceFound, self).__init__(message) - - -class VolumeDeviceNotFound(Exception): - def __init__(self, device): - message = _("Volume device not found at %s") % device - super(VolumeDeviceNotFound, self).__init__(message) diff --git a/cinder/brick/initiator/connector.py b/cinder/brick/initiator/connector.py index 44d4698f9..26a706755 100644 --- a/cinder/brick/initiator/connector.py +++ b/cinder/brick/initiator/connector.py @@ -25,7 +25,7 @@ import time from oslo.config import cfg -from cinder.brick import exceptions +from cinder.brick import exception from cinder.openstack.common.gettextutils import _ from cinder.openstack.common import lockutils from cinder.openstack.common import log as logging @@ -177,7 +177,7 @@ class ISCSIConnector(InitiatorConnector): tries = 0 while not os.path.exists(host_device): if tries >= CONF.num_iscsi_scan_tries: - raise exceptions.VolumeDeviceNotFound(host_device) + raise exception.VolumeDeviceNotFound(device=host_device) LOG.warn(_("ISCSI volume not yet found at: %(host_device)s. " "Will rescan & retry. Try number: %(tries)s"), @@ -514,7 +514,7 @@ class FibreChannelConnector(InitiatorConnector): # this is empty because we don't have any FC HBAs msg = _("We are unable to locate any Fibre Channel devices") LOG.warn(msg) - raise exceptions.NoFibreChannelHostsFound() + raise exception.NoFibreChannelHostsFound() # The /dev/disk/by-path/... node is not always present immediately # We only need to find the first device. Once we see the first device @@ -534,7 +534,7 @@ class FibreChannelConnector(InitiatorConnector): if self.tries >= CONF.num_iscsi_scan_tries: msg = _("Fibre Channel volume device not found.") LOG.error(msg) - raise exceptions.NoFibreChannelVolumeDeviceFound() + raise exception.NoFibreChannelVolumeDeviceFound() LOG.warn(_("Fibre volume not yet found. " "Will rescan & retry. Try number: %(tries)s"), diff --git a/cinder/brick/iser/iser.py b/cinder/brick/iser/iser.py index cafb2ac95..5c2d8e6c7 100755 --- a/cinder/brick/iser/iser.py +++ b/cinder/brick/iser/iser.py @@ -20,16 +20,13 @@ Helper code for the iSER volume driver. import os -import re from oslo.config import cfg -from cinder import exception +from cinder.brick import exception from cinder.openstack.common import fileutils from cinder.openstack.common import log as logging -from cinder import utils -from cinder.volume import utils as volume_utils - +from cinder.openstack.common import processutils as putils LOG = logging.getLogger(__name__) @@ -98,7 +95,7 @@ class TargetAdmin(object): class TgtAdm(TargetAdmin): """iSER target administration using tgtadm.""" - def __init__(self, execute=utils.execute): + def __init__(self, execute=putils.execute): super(TgtAdm, self).__init__('tgtadm', execute) def _get_target(self, iqn): @@ -154,7 +151,7 @@ class TgtAdm(TargetAdmin): '--update', name, run_as_root=True) - except exception.ProcessExecutionError as e: + except putils.ProcessExecutionError as e: LOG.error(_("Failed to create iser target for volume " "id:%(vol_id)s: %(e)s") % {'vol_id': vol_id, 'e': str(e)}) @@ -194,7 +191,7 @@ class TgtAdm(TargetAdmin): '--delete', iqn, run_as_root=True) - except exception.ProcessExecutionError as e: + except putils.ProcessExecutionError as e: LOG.error(_("Failed to remove iser target for volume " "id:%(vol_id)s: %(e)s") % {'vol_id': vol_id, 'e': str(e)}) diff --git a/cinder/exception.py b/cinder/exception.py index 50b995e46..f9fb69fc7 100644 --- a/cinder/exception.py +++ b/cinder/exception.py @@ -310,22 +310,6 @@ class ISCSITargetRemoveFailed(CinderException): message = _("Failed to remove iscsi target for volume %(volume_id)s.") -class ISERTargetNotFoundForVolume(NotFound): - message = _("No target id found for volume %(volume_id)s.") - - -class ISERTargetCreateFailed(CinderException): - message = _("Failed to create iser target for volume %(volume_id)s.") - - -class ISERTargetAttachFailed(CinderException): - message = _("Failed to attach iser target for volume %(volume_id)s.") - - -class ISERTargetRemoveFailed(CinderException): - message = _("Failed to remove iser target for volume %(volume_id)s.") - - class DiskNotFound(NotFound): message = _("No disk at %(location)s") diff --git a/cinder/tests/brick/test_brick_connector.py b/cinder/tests/brick/test_brick_connector.py index 934c6c618..13eef922c 100644 --- a/cinder/tests/brick/test_brick_connector.py +++ b/cinder/tests/brick/test_brick_connector.py @@ -17,7 +17,7 @@ import os.path import string -from cinder.brick import exceptions +from cinder.brick import exception from cinder.brick.initiator import connector from cinder.brick.initiator import host_driver from cinder.brick.initiator import linuxfc @@ -251,6 +251,6 @@ class FibreChannelConnectorTestCase(ConnectorTestCase): lambda: []) self.stubs.Set(self.connector._linuxfc, 'get_fc_hbas_info', lambda: []) - self.assertRaises(exceptions.NoFibreChannelHostsFound, + self.assertRaises(exception.NoFibreChannelHostsFound, self.connector.connect_volume, connection_info['data'])