You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by lu...@redhat.com on 2011/01/22 01:02:54 UTC

[PATCH 3/5] Only make available/access drivers within a request

From: David Lutterkort <lu...@redhat.com>

There is no point talking about a driver outside the context of a request
anymore, since we don't know until request time what driver the user wants.

Because of the lazy instantiation of drivers, accessing a driver outside of
a request can lead to nasty races.

  * lib/drivers.rb: do not include module Deltacloud in Object
  * lib/deltacloud/helpers/application_helper.rb: include module Deltacloud
---
 .../lib/deltacloud/helpers/application_helper.rb   |    2 ++
 server/lib/drivers.rb                              |   17 ++++++-----------
 2 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/server/lib/deltacloud/helpers/application_helper.rb b/server/lib/deltacloud/helpers/application_helper.rb
index 8607163..206903f 100644
--- a/server/lib/deltacloud/helpers/application_helper.rb
+++ b/server/lib/deltacloud/helpers/application_helper.rb
@@ -19,6 +19,8 @@
 
 module ApplicationHelper
 
+  include Deltacloud
+
   def bread_crumb
     s = "<ul class='breadcrumb'><li class='first'><a href='#{url_for('/')}'>&#948</a></li>"
     url = request.path_info.split('?')  #remove extra query string parameters
diff --git a/server/lib/drivers.rb b/server/lib/drivers.rb
index 420621c..4b6e4f2 100644
--- a/server/lib/drivers.rb
+++ b/server/lib/drivers.rb
@@ -15,6 +15,8 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
+# FIXME: This should be moved into lib/ and be called Deltacloud::Drivers
+# or some such
 module Deltacloud
   DRIVERS = {
     :ec2 => { :name => "EC2" },
@@ -48,9 +50,9 @@ module Deltacloud
     DRIVERS[:"#{driver_symbol}"][:name]
   end
 
-  def driver_class_name
+  def driver_class
     basename = DRIVERS[:"#{driver_symbol}"][:class] || "#{driver_name}Driver"
-    "Deltacloud::Drivers::#{driver_name}::#{basename}"
+    Deltacloud::Drivers.const_get(driver_name).const_get(basename)
   end
 
   def driver_source_name
@@ -58,20 +60,13 @@ module Deltacloud
   end
 
   def driver_mock_source_name
-    return File.join('deltacloud', 'drivers', "#{driver_symbol}", 
+    return File.join('deltacloud', 'drivers', "#{driver_symbol}",
                      "#{driver_symbol}_driver.rb") if driver_name.eql? 'Mock'
   end
 
   def driver
     require driver_source_name
-    #require 'deltacloud/base_driver/mock_driver.rb'
 
-    if Sinatra::Application.environment.eql? :test
-      require driver_mock_source_name if driver_mock_source_name
-    end
-
-    @driver ||= eval( driver_class_name ).new
+    @driver ||= driver_class.new
   end
 end
-
-include Deltacloud
-- 
1.7.3.4