From b569df9951b36b279438f5881beb813cd3eb39b3 Mon Sep 17 00:00:00 2001
From: "Walter A. Boring IV" <walter.boring@hp.com>
Date: Thu, 15 Aug 2013 10:41:08 -0700
Subject: [PATCH] Update Brick to use executor

modify existing modules in Brick
to make use of the Executor object,
so we don't replicated the same
set_execute() method everywhere.

Change-Id: Ib0363029557253ad86bae90e1db4fefd24a42df7
---
 cinder/brick/{initiator => }/executor.py |  0
 cinder/brick/initiator/connector.py      |  2 +-
 cinder/brick/initiator/linuxscsi.py      |  2 +-
 cinder/brick/iscsi/iscsi.py              | 31 +++++++++++------------
 cinder/brick/iser/iser.py                | 19 ++++++--------
 cinder/brick/local_dev/lvm.py            | 32 ++++++++++++------------
 cinder/tests/test_iscsi.py               |  4 +--
 cinder/tests/test_iser.py                |  2 +-
 cinder/volume/drivers/block_device.py    |  3 ++-
 cinder/volume/drivers/lvm.py             |  6 +++--
 10 files changed, 49 insertions(+), 52 deletions(-)
 rename cinder/brick/{initiator => }/executor.py (100%)

diff --git a/cinder/brick/initiator/executor.py b/cinder/brick/executor.py
similarity index 100%
rename from cinder/brick/initiator/executor.py
rename to cinder/brick/executor.py
diff --git a/cinder/brick/initiator/connector.py b/cinder/brick/initiator/connector.py
index a5a312426..755edffff 100644
--- a/cinder/brick/initiator/connector.py
+++ b/cinder/brick/initiator/connector.py
@@ -22,7 +22,7 @@ import time
 from oslo.config import cfg
 
 from cinder.brick import exception
-from cinder.brick.initiator import executor
+from cinder.brick import executor
 from cinder.brick.initiator import host_driver
 from cinder.brick.initiator import linuxfc
 from cinder.brick.initiator import linuxscsi
diff --git a/cinder/brick/initiator/linuxscsi.py b/cinder/brick/initiator/linuxscsi.py
index bb0f56f99..70bd4d190 100644
--- a/cinder/brick/initiator/linuxscsi.py
+++ b/cinder/brick/initiator/linuxscsi.py
@@ -20,7 +20,7 @@
 """
 import os
 
-from cinder.brick.initiator import executor
+from cinder.brick import executor
 from cinder.openstack.common.gettextutils import _
 from cinder.openstack.common import log as logging
 from cinder.openstack.common import processutils as putils
diff --git a/cinder/brick/iscsi/iscsi.py b/cinder/brick/iscsi/iscsi.py
index 5a3b8cc66..80800032f 100644
--- a/cinder/brick/iscsi/iscsi.py
+++ b/cinder/brick/iscsi/iscsi.py
@@ -28,6 +28,7 @@ import stat
 from oslo.config import cfg
 
 from cinder.brick import exception
+from cinder.brick import executor
 from cinder.openstack.common import fileutils
 from cinder.openstack.common.gettextutils import _
 from cinder.openstack.common import log as logging
@@ -67,19 +68,15 @@ CONF.register_opts(iscsi_helper_opt)
 CONF.import_opt('volume_name_template', 'cinder.db')
 
 
-class TargetAdmin(object):
+class TargetAdmin(executor.Executor):
     """iSCSI target administration.
 
     Base class for iSCSI target admin helpers.
     """
 
-    def __init__(self, cmd, execute):
+    def __init__(self, cmd, root_helper, execute):
+        super(TargetAdmin, self).__init__(root_helper, execute=execute)
         self._cmd = cmd
-        self.set_execute(execute)
-
-    def set_execute(self, execute):
-        """Set the function to be used to execute commands."""
-        self._execute = execute
 
     def _run(self, *args, **kwargs):
         self._execute(self._cmd, *args, run_as_root=True, **kwargs)
@@ -117,8 +114,8 @@ class TargetAdmin(object):
 class TgtAdm(TargetAdmin):
     """iSCSI target administration using tgtadm."""
 
-    def __init__(self, execute=putils.execute):
-        super(TgtAdm, self).__init__('tgtadm', execute)
+    def __init__(self, root_helper, execute=putils.execute):
+        super(TgtAdm, self).__init__('tgtadm', root_helper, execute)
 
     def _get_target(self, iqn):
         (out, err) = self._execute('tgt-admin', '--show', run_as_root=True)
@@ -234,8 +231,8 @@ class TgtAdm(TargetAdmin):
 class IetAdm(TargetAdmin):
     """iSCSI target administration using ietadm."""
 
-    def __init__(self, execute=putils.execute):
-        super(IetAdm, self).__init__('ietadm', execute)
+    def __init__(self, root_helper, execute=putils.execute):
+        super(IetAdm, self).__init__('ietadm', root_helper, execute)
 
     def _is_block(self, path):
         mode = os.stat(path).st_mode
@@ -381,8 +378,8 @@ class FakeIscsiHelper(object):
 
 class LioAdm(TargetAdmin):
     """iSCSI target administration for LIO using python-rtslib."""
-    def __init__(self, execute=putils.execute):
-        super(LioAdm, self).__init__('rtstool', execute)
+    def __init__(self, root_helper, execute=putils.execute):
+        super(LioAdm, self).__init__('rtstool', root_helper, execute)
 
         try:
             self._execute('rtstool', 'verify')
@@ -491,12 +488,12 @@ class LioAdm(TargetAdmin):
             raise exception.ISCSITargetAttachFailed(volume_id=volume['id'])
 
 
-def get_target_admin():
+def get_target_admin(root_helper):
     if CONF.iscsi_helper == 'tgtadm':
-        return TgtAdm()
+        return TgtAdm(root_helper)
     elif CONF.iscsi_helper == 'fake':
         return FakeIscsiHelper()
     elif CONF.iscsi_helper == 'lioadm':
-        return LioAdm()
+        return LioAdm(root_helper)
     else:
-        return IetAdm()
+        return IetAdm(root_helper)
diff --git a/cinder/brick/iser/iser.py b/cinder/brick/iser/iser.py
index e6afa4531..43f66b792 100644
--- a/cinder/brick/iser/iser.py
+++ b/cinder/brick/iser/iser.py
@@ -24,6 +24,7 @@ import os
 from oslo.config import cfg
 
 from cinder.brick import exception
+from cinder.brick import executor
 from cinder.openstack.common import fileutils
 from cinder.openstack.common.gettextutils import _
 from cinder.openstack.common import log as logging
@@ -46,19 +47,15 @@ CONF.register_opts(iser_helper_opt)
 CONF.import_opt('volume_name_template', 'cinder.db')
 
 
-class TargetAdmin(object):
+class TargetAdmin(executor.Executor):
     """iSER target administration.
 
     Base class for iSER target admin helpers.
     """
 
-    def __init__(self, cmd, execute):
+    def __init__(self, cmd, root_helper, execute):
+        super(TargetAdmin, self).__init__(root_helper, execute=execute)
         self._cmd = cmd
-        self.set_execute(execute)
-
-    def set_execute(self, execute):
-        """Set the function to be used to execute commands."""
-        self._execute = execute
 
     def _run(self, *args, **kwargs):
         self._execute(self._cmd, *args, run_as_root=True, **kwargs)
@@ -96,8 +93,8 @@ class TargetAdmin(object):
 class TgtAdm(TargetAdmin):
     """iSER target administration using tgtadm."""
 
-    def __init__(self, execute=putils.execute):
-        super(TgtAdm, self).__init__('tgtadm', execute)
+    def __init__(self, root_helper, execute=putils.execute):
+        super(TgtAdm, self).__init__('tgtadm', root_helper, execute)
 
     def _get_target(self, iqn):
         (out, err) = self._execute('tgt-admin', '--show', run_as_root=True)
@@ -223,8 +220,8 @@ class FakeIserHelper(object):
         return self.tid
 
 
-def get_target_admin():
+def get_target_admin(root_helper):
     if CONF.iser_helper == 'fake':
         return FakeIserHelper()
     else:
-        return TgtAdm()
+        return TgtAdm(root_helper)
diff --git a/cinder/brick/local_dev/lvm.py b/cinder/brick/local_dev/lvm.py
index 2543a8735..dbd5d474c 100644
--- a/cinder/brick/local_dev/lvm.py
+++ b/cinder/brick/local_dev/lvm.py
@@ -25,6 +25,7 @@ import re
 from itertools import izip
 
 from cinder.brick import exception
+from cinder.brick import executor
 from cinder.openstack.common.gettextutils import _
 from cinder.openstack.common import log as logging
 from cinder.openstack.common import processutils as putils
@@ -32,7 +33,7 @@ from cinder.openstack.common import processutils as putils
 LOG = logging.getLogger(__name__)
 
 
-class LVM(object):
+class LVM(executor.Executor):
     """LVM object to enable various LVM related operations."""
 
     def __init__(self, vg_name, root_helper, create_vg=False,
@@ -53,6 +54,7 @@ class LVM(object):
         :param executor: Execute method to use, None uses common/processutils
 
         """
+        super(LVM, self).__init__(execute=executor, root_helper=root_helper)
         self.vg_name = vg_name
         self.pv_list = []
         self.lv_list = []
@@ -62,8 +64,6 @@ class LVM(object):
         self.vg_uuid = None
         self.vg_thin_pool = None
         self.vg_thin_pool_size = 0
-        self.root_helper = root_helper
-        self._set_execute(executor)
 
         if create_vg and physical_volumes is not None:
             self.pv_list = physical_volumes
@@ -109,7 +109,7 @@ class LVM(object):
         exists = False
         cmd = ['vgs', '--noheadings', '-o', 'name']
         (out, err) = self._execute(*cmd,
-                                   root_helper=self.root_helper,
+                                   root_helper=self._root_helper,
                                    run_as_root=True)
 
         if out is not None:
@@ -121,7 +121,7 @@ class LVM(object):
 
     def _create_vg(self, pv_list):
         cmd = ['vgcreate', self.vg_name, ','.join(pv_list)]
-        self._execute(*cmd, root_helper=self.root_helper, run_as_root=True)
+        self._execute(*cmd, root_helper=self._root_helper, run_as_root=True)
 
     def _get_vg_uuid(self):
         (out, err) = self._execute('vgs', '--noheadings',
@@ -192,7 +192,7 @@ class LVM(object):
         :returns: List of Dictionaries with LV info
 
         """
-        self.lv_list = self.get_all_volumes(self.root_helper, self.vg_name)
+        self.lv_list = self.get_all_volumes(self._root_helper, self.vg_name)
         return self.lv_list
 
     def get_volume(self, name):
@@ -248,7 +248,7 @@ class LVM(object):
         :returns: List of Dictionaries with PV info
 
         """
-        self.pv_list = self.get_all_physical_volumes(self.root_helper,
+        self.pv_list = self.get_all_physical_volumes(self._root_helper,
                                                      self.vg_name)
         return self.pv_list
 
@@ -299,7 +299,7 @@ class LVM(object):
         :returns: Dictionaries of VG info
 
         """
-        vg_list = self.get_all_volume_groups(self.root_helper, self.vg_name)
+        vg_list = self.get_all_volume_groups(self._root_helper, self.vg_name)
 
         if len(vg_list) != 1:
             LOG.error(_('Unable to find VG: %s') % self.vg_name)
@@ -311,7 +311,7 @@ class LVM(object):
         self.vg_uuid = vg_list[0]['uuid']
 
         if self.vg_thin_pool is not None:
-            for lv in self.get_all_volumes(self.root_helper, self.vg_name):
+            for lv in self.get_all_volumes(self._root_helper, self.vg_name):
                 if lv['name'] == self.vg_thin_pool:
                     self.vg_thin_pool_size = lv['size']
 
@@ -327,7 +327,7 @@ class LVM(object):
 
         """
 
-        if not self.supports_thin_provisioning(self.root_helper):
+        if not self.supports_thin_provisioning(self._root_helper):
             LOG.error(_('Requested to setup thin provisioning, '
                         'however current LVM version does not '
                         'support it.'))
@@ -347,7 +347,7 @@ class LVM(object):
         cmd = ['lvcreate', '-T', '-L', size_str, pool_path]
 
         self._execute(*cmd,
-                      root_helper=self.root_helper,
+                      root_helper=self._root_helper,
                       run_as_root=True)
         self.vg_thin_pool = pool_path
 
@@ -378,7 +378,7 @@ class LVM(object):
 
         try:
             self._execute(*cmd,
-                          root_helper=self.root_helper,
+                          root_helper=self._root_helper,
                           run_as_root=True)
         except putils.ProcessExecutionError as err:
             LOG.exception(_('Error creating Volume'))
@@ -407,7 +407,7 @@ class LVM(object):
 
         try:
             self._execute(*cmd,
-                          root_helper=self.root_helper,
+                          root_helper=self._root_helper,
                           run_as_root=True)
         except putils.ProcessExecutionError as err:
             LOG.exception(_('Error creating snapshot'))
@@ -425,7 +425,7 @@ class LVM(object):
         self._execute('lvremove',
                       '-f',
                       '%s/%s' % (self.vg_name, name),
-                      root_helper=self.root_helper, run_as_root=True)
+                      root_helper=self._root_helper, run_as_root=True)
 
     def revert(self, snapshot_name):
         """Revert an LV from snapshot.
@@ -434,14 +434,14 @@ class LVM(object):
 
         """
         self._execute('lvconvert', '--merge',
-                      snapshot_name, root_helper=self.root_helper,
+                      snapshot_name, root_helper=self._root_helper,
                       run_as_root=True)
 
     def lv_has_snapshot(self, name):
         out, err = self._execute('lvdisplay', '--noheading',
                                  '-C', '-o', 'Attr',
                                  '%s/%s' % (self.vg_name, name),
-                                 root_helper=self.root_helper,
+                                 root_helper=self._root_helper,
                                  run_as_root=True)
         if out:
             out = out.strip()
diff --git a/cinder/tests/test_iscsi.py b/cinder/tests/test_iscsi.py
index d10354a72..2247465e0 100644
--- a/cinder/tests/test_iscsi.py
+++ b/cinder/tests/test_iscsi.py
@@ -42,7 +42,7 @@ class TargetAdminTestCase(object):
         self.stubs.Set(iscsi.LioAdm, '_get_target', self.fake_get_target)
         self.stubs.Set(iscsi.LioAdm, '__init__', self.fake_init)
 
-    def fake_init(obj):
+    def fake_init(obj, root_helper):
         return
 
     def fake_get_target(obj, iqn):
@@ -79,7 +79,7 @@ class TargetAdminTestCase(object):
         self.verify_cmds(cmds)
 
     def run_commands(self):
-        tgtadm = iscsi.get_target_admin()
+        tgtadm = iscsi.get_target_admin(None)
         tgtadm.set_execute(self.fake_execute)
         tgtadm.create_iscsi_target(self.target_name, self.tid,
                                    self.lun, self.path)
diff --git a/cinder/tests/test_iser.py b/cinder/tests/test_iser.py
index 789fbe360..aa621bda4 100644
--- a/cinder/tests/test_iser.py
+++ b/cinder/tests/test_iser.py
@@ -77,7 +77,7 @@ class TargetAdminTestCase(object):
         self.verify_cmds(cmds)
 
     def run_commands(self):
-        tgtadm = iser.get_target_admin()
+        tgtadm = iser.get_target_admin(None)
         tgtadm.set_execute(self.fake_execute)
         tgtadm.create_iser_target(self.target_name, self.tid,
                                   self.lun, self.path)
diff --git a/cinder/volume/drivers/block_device.py b/cinder/volume/drivers/block_device.py
index 1b3031add..0272d4d08 100644
--- a/cinder/volume/drivers/block_device.py
+++ b/cinder/volume/drivers/block_device.py
@@ -44,7 +44,8 @@ class BlockDeviceDriver(driver.ISCSIDriver):
     VERSION = '1.0.0'
 
     def __init__(self, *args, **kwargs):
-        self.tgtadm = iscsi.get_target_admin()
+        root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config
+        self.tgtadm = iscsi.get_target_admin(root_helper)
 
         super(BlockDeviceDriver, self).__init__(*args, **kwargs)
         self.configuration.append_config_values(volume_opts)
diff --git a/cinder/volume/drivers/lvm.py b/cinder/volume/drivers/lvm.py
index 2b01cc031..6e6ace625 100644
--- a/cinder/volume/drivers/lvm.py
+++ b/cinder/volume/drivers/lvm.py
@@ -372,7 +372,8 @@ class LVMISCSIDriver(LVMVolumeDriver, driver.ISCSIDriver):
     """
 
     def __init__(self, *args, **kwargs):
-        self.tgtadm = iscsi.get_target_admin()
+        root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config
+        self.tgtadm = iscsi.get_target_admin(root_helper)
         super(LVMISCSIDriver, self).__init__(*args, **kwargs)
 
     def set_execute(self, execute):
@@ -723,7 +724,8 @@ class LVMISERDriver(LVMISCSIDriver, driver.ISERDriver):
     """
 
     def __init__(self, *args, **kwargs):
-        self.tgtadm = iser.get_target_admin()
+        root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config
+        self.tgtadm = iser.get_target_admin(root_helper)
         LVMVolumeDriver.__init__(self, *args, **kwargs)
 
     def set_execute(self, execute):
-- 
2.45.2