From: Ivan Kolodyazhny <e0ne@e0ne.info>
Date: Wed, 6 Aug 2014 10:58:43 +0000 (+0300)
Subject: RPC client lazy initialization
X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=d02f79b2043869d98c7ccdcc650206a3b2ed13e6;p=openstack-build%2Fcinder-build.git

RPC client lazy initialization

All *Command classes are instantiating for each cinder-manage command
execution. RPC client should be initialized for VolumeCommands usage.
Some custom RPC clients (oslo.messaging backends) could init RPC
connection after client initialization so it is no need to create RPC
client for every cinder-manage command invoke.

Related-Bug: #1348787
Change-Id: I7b3388dded6eeec972d9949538e1608c3a538734
---

diff --git a/bin/cinder-manage b/bin/cinder-manage
index 0b25ae3ab..064cbdc20 100755
--- a/bin/cinder-manage
+++ b/bin/cinder-manage
@@ -249,11 +249,17 @@ class VersionCommands(object):
 class VolumeCommands(object):
     """Methods for dealing with a cloud in an odd state."""
 
-    def __init__(self, *args, **kwargs):
-        super(VolumeCommands, self).__init__(*args, **kwargs)
-        rpc.init(CONF)
-        target = messaging.Target(topic=CONF.volume_topic)
-        self.client = rpc.get_client(target)
+    def __init__(self):
+        self._client = None
+
+    @property
+    def rpc_client(self):
+        if not rpc.initialized():
+            rpc.init(CONF)
+            target = messaging.Target(topic=CONF.volume_topic)
+            self._client = rpc.get_client(target)
+
+        return self._client
 
     @args('volume_id',
           help='Volume ID to be deleted')
@@ -276,7 +282,7 @@ class VolumeCommands(object):
             print(_("Detach volume from instance and then try again."))
             return
 
-        cctxt = self.client.prepare(server=host)
+        cctxt = self.rpc_client.prepare(server=host)
         cctxt.cast(ctxt, "delete_volume", volume_id=volume['id'])
 
     @args('--currenthost', required=True, help='Existing volume host name')