From: Walter A. Boring IV Date: Fri, 16 Aug 2013 15:18:09 +0000 (-0700) Subject: Add root_helper param to get_connector_properties X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=2584b3038239b6f48f540c17b36e5c891e303c58;p=openstack-build%2Fcinder-build.git Add root_helper param to get_connector_properties This patch adds the ability to pass in a custom root_helper for executing commands. This is needed for other projects that need a custom root-wrapper, such as nova. fixes Bug #1213135 Change-Id: Ie18276ae2e736bb2276d4e9c57385d2588441428 --- diff --git a/cinder/brick/initiator/connector.py b/cinder/brick/initiator/connector.py index 35f3775b7..a5a312426 100644 --- a/cinder/brick/initiator/connector.py +++ b/cinder/brick/initiator/connector.py @@ -48,11 +48,11 @@ CONF.register_opts(connector_opts) synchronized = lockutils.synchronized_with_prefix('brick-') -def get_connector_properties(): +def get_connector_properties(root_helper): """Get the connection properties for all protocols.""" - iscsi = ISCSIConnector() - fc = linuxfc.LinuxFibreChannel() + iscsi = ISCSIConnector(root_helper=root_helper) + fc = linuxfc.LinuxFibreChannel(root_helper=root_helper) props = {} props['ip'] = CONF.my_ip @@ -71,9 +71,9 @@ def get_connector_properties(): class InitiatorConnector(executor.Executor): - def __init__(self, driver=None, execute=putils.execute, - root_helper="sudo", *args, **kwargs): - super(InitiatorConnector, self).__init__(execute, root_helper, + def __init__(self, root_helper, driver=None, + execute=putils.execute, *args, **kwargs): + super(InitiatorConnector, self).__init__(root_helper, execute, *args, **kwargs) if not driver: driver = host_driver.HostDriver() @@ -85,8 +85,8 @@ class InitiatorConnector(executor.Executor): self.driver = driver @staticmethod - def factory(protocol, driver=None, execute=putils.execute, - root_helper="sudo", use_multipath=False): + def factory(protocol, root_helper, driver=None, + execute=putils.execute, use_multipath=False): """Build a Connector object based upon protocol.""" LOG.debug("Factory for %s" % protocol) protocol = protocol.upper() @@ -148,11 +148,11 @@ class InitiatorConnector(executor.Executor): class ISCSIConnector(InitiatorConnector): """Connector class to attach/detach iSCSI volumes.""" - def __init__(self, driver=None, execute=putils.execute, - root_helper="sudo", use_multipath=False, + def __init__(self, root_helper, driver=None, + execute=putils.execute, use_multipath=False, *args, **kwargs): - self._linuxscsi = linuxscsi.LinuxSCSI(execute, root_helper) - super(ISCSIConnector, self).__init__(driver, execute, root_helper, + self._linuxscsi = linuxscsi.LinuxSCSI(root_helper, execute) + super(ISCSIConnector, self).__init__(root_helper, driver, execute, *args, **kwargs) self.use_multipath = use_multipath @@ -486,14 +486,13 @@ class ISCSIConnector(InitiatorConnector): class FibreChannelConnector(InitiatorConnector): """Connector class to attach/detach Fibre Channel volumes.""" - def __init__(self, driver=None, execute=putils.execute, - root_helper="sudo", use_multipath=False, + def __init__(self, root_helper, driver=None, + execute=putils.execute, use_multipath=False, *args, **kwargs): - self._linuxscsi = linuxscsi.LinuxSCSI(execute, root_helper) - self._linuxfc = linuxfc.LinuxFibreChannel(execute, root_helper) - super(FibreChannelConnector, self).__init__(driver, execute, - root_helper, - *args, **kwargs) + self._linuxscsi = linuxscsi.LinuxSCSI(root_helper, execute) + self._linuxfc = linuxfc.LinuxFibreChannel(root_helper, execute) + super(FibreChannelConnector, self).__init__(root_helper, driver, + execute, *args, **kwargs) self.use_multipath = use_multipath def set_execute(self, execute): @@ -658,9 +657,9 @@ class FibreChannelConnector(InitiatorConnector): class AoEConnector(InitiatorConnector): """Connector class to attach/detach AoE volumes.""" - def __init__(self, driver=None, execute=putils.execute, - root_helper="sudo", *args, **kwargs): - super(AoEConnector, self).__init__(driver, execute, root_helper, + def __init__(self, root_helper, driver=None, + execute=putils.execute, *args, **kwargs): + super(AoEConnector, self).__init__(root_helper, driver, execute, *args, **kwargs) def _get_aoe_info(self, connection_properties): diff --git a/cinder/brick/initiator/executor.py b/cinder/brick/initiator/executor.py index f70fea670..f7a44c09c 100644 --- a/cinder/brick/initiator/executor.py +++ b/cinder/brick/initiator/executor.py @@ -24,7 +24,7 @@ from cinder.openstack.common import processutils as putils class Executor(object): - def __init__(self, execute=putils.execute, root_helper="sudo", + def __init__(self, root_helper, execute=putils.execute, *args, **kwargs): self.set_execute(execute) self.set_root_helper(root_helper) diff --git a/cinder/brick/initiator/linuxfc.py b/cinder/brick/initiator/linuxfc.py index 5878bf1f5..fd53dd259 100644 --- a/cinder/brick/initiator/linuxfc.py +++ b/cinder/brick/initiator/linuxfc.py @@ -27,9 +27,9 @@ LOG = logging.getLogger(__name__) class LinuxFibreChannel(linuxscsi.LinuxSCSI): - def __init__(self, execute=putils.execute, root_helper="sudo", + def __init__(self, root_helper, execute=putils.execute, *args, **kwargs): - super(LinuxFibreChannel, self).__init__(execute, root_helper, + super(LinuxFibreChannel, self).__init__(root_helper, execute, *args, **kwargs) def rescan_hosts(self, hbas): diff --git a/cinder/brick/initiator/linuxscsi.py b/cinder/brick/initiator/linuxscsi.py index 5a4a2e65b..bb0f56f99 100644 --- a/cinder/brick/initiator/linuxscsi.py +++ b/cinder/brick/initiator/linuxscsi.py @@ -29,9 +29,9 @@ LOG = logging.getLogger(__name__) class LinuxSCSI(executor.Executor): - def __init__(self, execute=putils.execute, root_helper="sudo", + def __init__(self, root_helper, execute=putils.execute, *args, **kwargs): - super(LinuxSCSI, self).__init__(execute, root_helper, + super(LinuxSCSI, self).__init__(root_helper, execute, *args, **kwargs) def echo_scsi_command(self, path, content): diff --git a/cinder/tests/brick/test_brick_connector.py b/cinder/tests/brick/test_brick_connector.py index 5ec9e657e..f40fa25b3 100644 --- a/cinder/tests/brick/test_brick_connector.py +++ b/cinder/tests/brick/test_brick_connector.py @@ -44,40 +44,40 @@ class ConnectorTestCase(test.TestCase): return "", None def test_connect_volume(self): - self.connector = connector.InitiatorConnector() + self.connector = connector.InitiatorConnector(None) self.assertRaises(NotImplementedError, self.connector.connect_volume, None) def test_disconnect_volume(self): - self.connector = connector.InitiatorConnector() + self.connector = connector.InitiatorConnector(None) self.assertRaises(NotImplementedError, self.connector.disconnect_volume, None, None) def test_factory(self): - obj = connector.InitiatorConnector.factory('iscsi') + obj = connector.InitiatorConnector.factory('iscsi', None) self.assertTrue(obj.__class__.__name__, "ISCSIConnector") - obj = connector.InitiatorConnector.factory('fibre_channel') + obj = connector.InitiatorConnector.factory('fibre_channel', None) self.assertTrue(obj.__class__.__name__, "FibreChannelConnector") - obj = connector.InitiatorConnector.factory('aoe') + obj = connector.InitiatorConnector.factory('aoe', None) self.assertTrue(obj.__class__.__name__, "AoEConnector") self.assertRaises(ValueError, connector.InitiatorConnector.factory, - "bogus") + "bogus", None) def test_check_valid_device_with_wrong_path(self): - self.connector = connector.InitiatorConnector() + self.connector = connector.InitiatorConnector(None) self.stubs.Set(self.connector, '_execute', lambda *args, **kwargs: ("", None)) self.assertFalse(self.connector.check_valid_device('/d0v')) def test_check_valid_device(self): - self.connector = connector.InitiatorConnector() + self.connector = connector.InitiatorConnector(None) self.stubs.Set(self.connector, '_execute', lambda *args, **kwargs: ("", "")) self.assertTrue(self.connector.check_valid_device('/dev')) @@ -85,7 +85,7 @@ class ConnectorTestCase(test.TestCase): def test_check_valid_device_with_cmd_error(self): def raise_except(*args, **kwargs): raise putils.ProcessExecutionError - self.connector = connector.InitiatorConnector() + self.connector = connector.InitiatorConnector(None) self.stubs.Set(self.connector, '_execute', raise_except) self.assertFalse(self.connector.check_valid_device('/dev')) @@ -109,8 +109,8 @@ class ISCSIConnectorTestCase(ConnectorTestCase): def setUp(self): super(ISCSIConnectorTestCase, self).setUp() - self.connector = connector.ISCSIConnector(execute=self.fake_execute, - use_multipath=False) + self.connector = connector.ISCSIConnector( + None, execute=self.fake_execute, use_multipath=False) self.stubs.Set(self.connector._linuxscsi, 'get_name_from_path', lambda x: "/dev/sdb") @@ -196,7 +196,7 @@ class ISCSIConnectorTestCase(ConnectorTestCase): connection_properties = self.iscsi_connection(vol, location, iqn) self.connector_with_multipath =\ - connector.ISCSIConnector(use_multipath=True) + connector.ISCSIConnector(None, use_multipath=True) self.stubs.Set(self.connector_with_multipath, '_run_iscsiadm_bare', lambda *args, **kwargs: "%s %s" % (location, iqn)) @@ -239,7 +239,7 @@ class FibreChannelConnectorTestCase(ConnectorTestCase): def setUp(self): super(FibreChannelConnectorTestCase, self).setUp() self.connector = connector.FibreChannelConnector( - execute=self.fake_execute, use_multipath=False) + None, execute=self.fake_execute, use_multipath=False) self.assertIsNotNone(self.connector) self.assertIsNotNone(self.connector._linuxfc) self.assertIsNotNone(self.connector._linuxscsi) @@ -335,7 +335,7 @@ class AoEConnectorTestCase(ConnectorTestCase): def setUp(self): super(AoEConnectorTestCase, self).setUp() self.mox = mox.Mox() - self.connector = connector.AoEConnector() + self.connector = connector.AoEConnector('sudo') self.connection_properties = {'target_shelf': 'fake_shelf', 'target_lun': 'fake_lun'} diff --git a/cinder/tests/brick/test_brick_linuxfc.py b/cinder/tests/brick/test_brick_linuxfc.py index 9091dacc5..03864f8d1 100644 --- a/cinder/tests/brick/test_brick_linuxfc.py +++ b/cinder/tests/brick/test_brick_linuxfc.py @@ -30,7 +30,7 @@ class LinuxFCTestCase(test.TestCase): super(LinuxFCTestCase, self).setUp() self.cmds = [] self.stubs.Set(os.path, 'exists', lambda x: True) - self.lfc = linuxfc.LinuxFibreChannel(execute=self.fake_execute) + self.lfc = linuxfc.LinuxFibreChannel(None, execute=self.fake_execute) def fake_execute(self, *cmd, **kwargs): self.cmds.append(string.join(cmd)) diff --git a/cinder/tests/brick/test_brick_linuxscsi.py b/cinder/tests/brick/test_brick_linuxscsi.py index c1bc072e5..9b7b0db68 100644 --- a/cinder/tests/brick/test_brick_linuxscsi.py +++ b/cinder/tests/brick/test_brick_linuxscsi.py @@ -30,7 +30,7 @@ class LinuxSCSITestCase(test.TestCase): super(LinuxSCSITestCase, self).setUp() self.cmds = [] self.stubs.Set(os.path, 'realpath', lambda x: '/dev/sdc') - self.linuxscsi = linuxscsi.LinuxSCSI(execute=self.fake_execute) + self.linuxscsi = linuxscsi.LinuxSCSI(None, execute=self.fake_execute) def fake_execute(self, *cmd, **kwargs): self.cmds.append(string.join(cmd)) diff --git a/cinder/tests/test_coraid.py b/cinder/tests/test_coraid.py index f31b85084..ab3a849c5 100644 --- a/cinder/tests/test_coraid.py +++ b/cinder/tests/test_coraid.py @@ -773,15 +773,19 @@ class CoraidDriverImageTestCases(CoraidDriverTestCase): self.driver.terminate_connection(fake_volume, mox.IgnoreArg())\ .AndReturn(None) + root_helper = 'sudo cinder-rootwrap None' + self.mox.StubOutWithMock(connector, 'get_connector_properties') - connector.get_connector_properties().AndReturn({}) + connector.get_connector_properties(root_helper).\ + AndReturn({}) self.mox.StubOutWithMock(connector.InitiatorConnector, 'factory') aoe_initiator = self.mox.CreateMockAnything() - connector.InitiatorConnector.factory('aoe', use_multipath=False)\ - .AndReturn(aoe_initiator) + connector.InitiatorConnector.factory('aoe', root_helper, + use_multipath=False).\ + AndReturn(aoe_initiator) aoe_initiator\ .connect_volume(self.fake_connection['data'])\ diff --git a/cinder/volume/driver.py b/cinder/volume/driver.py index 0a0cd5f36..587ca578b 100644 --- a/cinder/volume/driver.py +++ b/cinder/volume/driver.py @@ -236,7 +236,8 @@ class VolumeDriver(object): LOG.debug(_('copy_data_between_volumes %(src)s -> %(dest)s.') % {'src': src_vol['name'], 'dest': dest_vol['name']}) - properties = initiator.get_connector_properties() + root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config + properties = initiator.get_connector_properties(root_helper) dest_remote = True if remote in ['dest', 'both'] else False dest_orig_status = dest_vol['status'] try: @@ -290,7 +291,8 @@ class VolumeDriver(object): """Fetch the image from image_service and write it to the volume.""" LOG.debug(_('copy_image_to_volume %s.') % volume['name']) - properties = initiator.get_connector_properties() + root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config + properties = initiator.get_connector_properties(root_helper) attach_info = self._attach_volume(context, volume, properties) try: @@ -306,7 +308,8 @@ class VolumeDriver(object): """Copy the volume to the specified image.""" LOG.debug(_('copy_volume_to_image %s.') % volume['name']) - properties = initiator.get_connector_properties() + root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config + properties = initiator.get_connector_properties(root_helper) attach_info = self._attach_volume(context, volume, properties) try: @@ -329,8 +332,9 @@ 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, use_multipath=use_multipath) + protocol, root_helper, use_multipath=use_multipath) device = connector.connect_volume(conn['data']) host_device = device['path']