From d581c8655e876e86cbd7357eb3ba686017949e97 Mon Sep 17 00:00:00 2001 From: "Walter A. Boring IV" Date: Wed, 21 Aug 2013 14:00:13 -0700 Subject: [PATCH] Adds brick helpers to cinder utils This patch adds some cinder utils functions that wrap calls into brick to automatically populate cinder's root wrap helper. This prevents propogating the recreation of the sudo cinder-rootwrap string. Change-Id: I3ae2add4f38a40c57ca197b231e09a9856d7e809 --- cinder/tests/test_coraid.py | 7 +++-- cinder/tests/test_utils.py | 61 +++++++++++++++++++++++++++++++++++++ cinder/utils.py | 35 ++++++++++++++++++--- cinder/volume/driver.py | 14 +++------ 4 files changed, 101 insertions(+), 16 deletions(-) diff --git a/cinder/tests/test_coraid.py b/cinder/tests/test_coraid.py index ab3a849c5..3797498f3 100644 --- a/cinder/tests/test_coraid.py +++ b/cinder/tests/test_coraid.py @@ -26,6 +26,7 @@ from cinder.openstack.common import jsonutils from cinder.openstack.common import log as logging from cinder import test from cinder import units +from cinder import utils from cinder.volume import configuration as conf from cinder.volume.drivers import coraid from cinder.volume import volume_types @@ -779,12 +780,12 @@ class CoraidDriverImageTestCases(CoraidDriverTestCase): connector.get_connector_properties(root_helper).\ AndReturn({}) - self.mox.StubOutWithMock(connector.InitiatorConnector, 'factory') + self.mox.StubOutWithMock(utils, 'brick_get_connector') aoe_initiator = self.mox.CreateMockAnything() - connector.InitiatorConnector.factory('aoe', root_helper, - use_multipath=False).\ + utils.brick_get_connector('aoe', + use_multipath=False).\ AndReturn(aoe_initiator) aoe_initiator\ diff --git a/cinder/tests/test_utils.py b/cinder/tests/test_utils.py index 4468ce916..0a50d3f5e 100644 --- a/cinder/tests/test_utils.py +++ b/cinder/tests/test_utils.py @@ -20,6 +20,7 @@ import datetime import hashlib import os import paramiko +import socket import StringIO import tempfile import uuid @@ -28,7 +29,10 @@ import mox from oslo.config import cfg import cinder +from cinder.brick.initiator import connector +from cinder.brick.initiator import linuxfc from cinder import exception +from cinder.openstack.common import processutils as putils from cinder.openstack.common import timeutils from cinder import test from cinder import utils @@ -721,3 +725,60 @@ class SSHPoolTestCase(test.TestCase): third_id = ssh.id self.assertNotEqual(first_id, third_id) + + +class BrickUtils(test.TestCase): + """Unit test to test the brick utility + wrapper functions. + """ + + def test_brick_get_connector_properties(self): + + self.mox.StubOutWithMock(socket, 'gethostname') + socket.gethostname().AndReturn('fakehost') + + self.mox.StubOutWithMock(connector.ISCSIConnector, 'get_initiator') + connector.ISCSIConnector.get_initiator().AndReturn('fakeinitiator') + + self.mox.StubOutWithMock(linuxfc.LinuxFibreChannel, 'get_fc_wwpns') + linuxfc.LinuxFibreChannel.get_fc_wwpns().AndReturn(None) + + self.mox.StubOutWithMock(linuxfc.LinuxFibreChannel, 'get_fc_wwnns') + linuxfc.LinuxFibreChannel.get_fc_wwnns().AndReturn(None) + + props = {'initiator': 'fakeinitiator', + 'host': 'fakehost', + 'ip': CONF.my_ip, + } + + self.mox.ReplayAll() + props_actual = utils.brick_get_connector_properties() + self.assertEqual(props, props_actual) + self.mox.VerifyAll() + + def test_brick_get_connector(self): + + root_helper = utils.get_root_helper() + + self.mox.StubOutClassWithMocks(connector, 'ISCSIConnector') + connector.ISCSIConnector(execute=putils.execute, + driver=None, + root_helper=root_helper, + use_multipath=False) + + self.mox.StubOutClassWithMocks(connector, 'FibreChannelConnector') + connector.FibreChannelConnector(execute=putils.execute, + driver=None, + root_helper=root_helper, + use_multipath=False) + + self.mox.StubOutClassWithMocks(connector, 'AoEConnector') + connector.AoEConnector(execute=putils.execute, + driver=None, + root_helper=root_helper) + + self.mox.ReplayAll() + utils.brick_get_connector('iscsi') + utils.brick_get_connector('fibre_channel') + utils.brick_get_connector('aoe') + self.mox.VerifyAll() diff --git a/cinder/utils.py b/cinder/utils.py index f1aac5729..9068d9256 100644 --- a/cinder/utils.py +++ b/cinder/utils.py @@ -46,6 +46,7 @@ from eventlet import pools from oslo.config import cfg +from cinder.brick.initiator import connector from cinder import exception from cinder.openstack.common import excutils from cinder.openstack.common import gettextutils @@ -143,8 +144,7 @@ def fetchfile(url, target): def execute(*cmd, **kwargs): """Convenience wrapper around oslo's execute() method.""" if 'run_as_root' in kwargs and not 'root_helper' in kwargs: - kwargs['root_helper'] =\ - 'sudo cinder-rootwrap %s' % CONF.rootwrap_config + kwargs['root_helper'] = get_root_helper() try: (stdout, stderr) = processutils.execute(*cmd, **kwargs) except processutils.ProcessExecutionError as ex: @@ -162,8 +162,7 @@ def execute(*cmd, **kwargs): def trycmd(*args, **kwargs): """Convenience wrapper around oslo's trycmd() method.""" if 'run_as_root' in kwargs and not 'root_helper' in kwargs: - kwargs['root_helper'] =\ - 'sudo cinder-rootwrap %s' % CONF.rootwrap_config + kwargs['root_helper'] = get_root_helper() try: (stdout, stderr) = processutils.trycmd(*args, **kwargs) except processutils.ProcessExecutionError as ex: @@ -1013,3 +1012,31 @@ class UndoManager(object): LOG.exception(msg, **kwargs) self._rollback() + + +def get_root_helper(): + return 'sudo cinder-rootwrap %s' % CONF.rootwrap_config + + +def brick_get_connector_properties(): + """wrapper for the brick calls to automatically set + the root_helper needed for cinder. + """ + + root_helper = get_root_helper() + return connector.get_connector_properties(root_helper) + + +def brick_get_connector(protocol, driver=None, + execute=processutils.execute, + use_multipath=False): + """Wrapper to get a brick connector object. + This automatically populates the required protocol as well + as the root_helper needed to execute commands. + """ + + root_helper = get_root_helper() + return connector.InitiatorConnector.factory(protocol, root_helper, + driver=driver, + execute=execute, + use_multipath=use_multipath) diff --git a/cinder/volume/driver.py b/cinder/volume/driver.py index 7d0837943..6620f584d 100644 --- a/cinder/volume/driver.py +++ b/cinder/volume/driver.py @@ -236,8 +236,7 @@ class VolumeDriver(object): LOG.debug(_('copy_data_between_volumes %(src)s -> %(dest)s.') % {'src': src_vol['name'], 'dest': dest_vol['name']}) - root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config - properties = initiator.get_connector_properties(root_helper) + properties = utils.brick_get_connector_properties() dest_remote = True if remote in ['dest', 'both'] else False dest_orig_status = dest_vol['status'] try: @@ -291,8 +290,7 @@ class VolumeDriver(object): """Fetch the image from image_service and write it to the volume.""" LOG.debug(_('copy_image_to_volume %s.') % volume['name']) - root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config - properties = initiator.get_connector_properties(root_helper) + properties = utils.brick_get_connector_properties() attach_info = self._attach_volume(context, volume, properties) try: @@ -308,8 +306,7 @@ class VolumeDriver(object): """Copy the volume to the specified image.""" LOG.debug(_('copy_volume_to_image %s.') % volume['name']) - root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config - properties = initiator.get_connector_properties(root_helper) + properties = utils.brick_get_connector_properties() attach_info = self._attach_volume(context, volume, properties) try: @@ -332,9 +329,8 @@ class VolumeDriver(object): # Use Brick's code to do attach/detach use_multipath = self.configuration.use_multipath_for_image_xfer protocol = conn['driver_volume_type'] - root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config - connector = initiator.InitiatorConnector.factory( - protocol, root_helper, use_multipath=use_multipath) + connector = utils.brick_get_connector(protocol, + use_multipath=use_multipath) device = connector.connect_volume(conn['data']) host_device = device['path'] -- 2.45.2