Merge "Make RBDImageMetadata and RBDVolumeIOWrapper re-usable"

This commit is contained in:
Jenkins 2016-06-15 18:32:40 +00:00 committed by Gerrit Code Review
commit 0fbaad5730
4 changed files with 42 additions and 4 deletions

View File

@ -1965,13 +1965,15 @@ class RBDConnector(BaseLinuxConnector):
try: try:
user = connection_properties['auth_username'] user = connection_properties['auth_username']
pool, volume = connection_properties['name'].split('/') pool, volume = connection_properties['name'].split('/')
conf = connection_properties.get('conffile')
except IndexError: except IndexError:
msg = _("Connect volume failed, malformed connection properties") msg = _("Connect volume failed, malformed connection properties")
raise exception.BrickException(msg=msg) raise exception.BrickException(msg=msg)
rbd_client = linuxrbd.RBDClient(user, pool) rbd_client = linuxrbd.RBDClient(user, pool)
rbd_volume = linuxrbd.RBDVolume(rbd_client, volume) rbd_volume = linuxrbd.RBDVolume(rbd_client, volume)
rbd_handle = linuxrbd.RBDVolumeIOWrapper(rbd_volume) rbd_handle = linuxrbd.RBDVolumeIOWrapper(
linuxrbd.RBDImageMetadata(rbd_volume, pool, user, conf))
return rbd_handle return rbd_handle
def connect_volume(self, connection_properties): def connect_volume(self, connection_properties):

View File

@ -54,6 +54,8 @@ class RBDClient(object):
err=_('rbd module required')) err=_('rbd module required'))
self.rbd_conf = kwargs.get('conffile', '/etc/ceph/ceph.conf') self.rbd_conf = kwargs.get('conffile', '/etc/ceph/ceph.conf')
self.rbd_cluster_name = kwargs.get('rbd_cluster_name', 'ceph')
self.rados_connect_timeout = kwargs.get('rados_connect_timeout', -1)
self.client, self.ioctx = self.connect() self.client, self.ioctx = self.connect()
@ -64,10 +66,17 @@ class RBDClient(object):
self.disconnect() self.disconnect()
def connect(self): def connect(self):
LOG.debug("opening connection to ceph cluster (timeout=%s).",
self.rados_connect_timeout)
client = self.rados.Rados(rados_id=self.rbd_user, client = self.rados.Rados(rados_id=self.rbd_user,
clustername=self.rbd_cluster_name,
conffile=self.rbd_conf) conffile=self.rbd_conf)
try: try:
if self.rados_connect_timeout >= 0:
client.connect(
timeout=self.configuration.rados_connect_timeout)
else:
client.connect() client.connect()
ioctx = client.open_ioctx(self.rbd_pool) ioctx = client.open_ioctx(self.rbd_pool)
return client, ioctx return client, ioctx
@ -116,6 +125,15 @@ class RBDVolume(object):
return getattr(self.image, attrib) return getattr(self.image, attrib)
class RBDImageMetadata(object):
"""RBD image metadata to be used with RBDVolumeIOWrapper."""
def __init__(self, image, pool, user, conf):
self.image = image
self.pool = encodeutils.safe_encode(pool or '')
self.user = encodeutils.safe_encode(user or '')
self.conf = encodeutils.safe_encode(conf or '')
class RBDVolumeIOWrapper(io.RawIOBase): class RBDVolumeIOWrapper(io.RawIOBase):
"""Enables LibRBD.Image objects to be treated as Python IO objects. """Enables LibRBD.Image objects to be treated as Python IO objects.
@ -130,6 +148,22 @@ class RBDVolumeIOWrapper(io.RawIOBase):
def _inc_offset(self, length): def _inc_offset(self, length):
self._offset += length self._offset += length
@property
def rbd_image(self):
return self._rbd_volume.image
@property
def rbd_user(self):
return self._rbd_volume.user
@property
def rbd_pool(self):
return self._rbd_volume.pool
@property
def rbd_conf(self):
return self._rbd_volume.conf
def read(self, length=None): def read(self, length=None):
offset = self._offset offset = self._offset
total = self._rbd_volume.image.size() total = self._rbd_volume.image.size()
@ -138,7 +172,7 @@ class RBDVolumeIOWrapper(io.RawIOBase):
# length (they just return nothing) but rbd images do so we need to # length (they just return nothing) but rbd images do so we need to
# return empty string if we have reached the end of the image. # return empty string if we have reached the end of the image.
if (offset >= total): if (offset >= total):
return '' return b''
if length is None: if length is None:
length = total length = total

View File

@ -2303,6 +2303,7 @@ class RBDConnectorTestCase(ConnectorTestCase):
# Ensure rados is instantiated correctly # Ensure rados is instantiated correctly
mock_rados.Rados.assert_called_once_with( mock_rados.Rados.assert_called_once_with(
clustername='ceph',
rados_id=encodeutils.safe_encode(self.user), rados_id=encodeutils.safe_encode(self.user),
conffile='/etc/ceph/ceph.conf') conffile='/etc/ceph/ceph.conf')

View File

@ -36,6 +36,7 @@ class RBDClientTestCase(base.TestCase):
# Assert connect is called with correct paramaters # Assert connect is called with correct paramaters
mock_rados.Rados.assert_called_once_with( mock_rados.Rados.assert_called_once_with(
clustername='ceph',
rados_id=encodeutils.safe_encode('test_user'), rados_id=encodeutils.safe_encode('test_user'),
conffile='/etc/ceph/ceph.conf') conffile='/etc/ceph/ceph.conf')
@ -80,7 +81,7 @@ class RBDVolumeIOWrapperTestCase(base.TestCase):
self.assertEqual(self.full_data, data) self.assertEqual(self.full_data, data)
data = self.mock_volume_wrapper.read() data = self.mock_volume_wrapper.read()
self.assertEqual('', data) self.assertEqual(b'', data)
self.mock_volume_wrapper.seek(0) self.mock_volume_wrapper.seek(0)
data = self.mock_volume_wrapper.read() data = self.mock_volume_wrapper.read()