deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [11/11] git commit: Core: Improved frontend loading and usage of ENV vars
Date Tue, 07 Aug 2012 17:17:48 GMT
Core: Improved frontend loading and usage of ENV vars

* Added Deltacloud[:ec2].require! to load frontend class
* Added Deltacloud.default_frontend instead of ENV['API_FRONTEND']


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/2eb5d1a7
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/2eb5d1a7
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/2eb5d1a7

Branch: refs/heads/master
Commit: 2eb5d1a7e8967de891b833a4ad237460bec05e1c
Parents: ad770cf
Author: Michal Fojtik <mfojtik@redhat.com>
Authored: Fri Aug 3 15:03:03 2012 +0200
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Tue Aug 7 19:14:02 2012 +0200

----------------------------------------------------------------------
 server/config.ru                                |   12 ++++--
 server/lib/deltacloud/drivers/ec2/ec2_driver.rb |    2 +-
 server/lib/deltacloud/helpers/driver_helper.rb  |    2 +-
 server/lib/deltacloud/helpers/url_helper.rb     |    2 +-
 server/lib/deltacloud_rack.rb                   |   32 ++++++++++++++++-
 server/lib/sinatra/rack_accept.rb               |    2 +-
 6 files changed, 42 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/2eb5d1a7/server/config.ru
----------------------------------------------------------------------
diff --git a/server/config.ru b/server/config.ru
index 254baf2..e36ad64 100644
--- a/server/config.ru
+++ b/server/config.ru
@@ -27,6 +27,7 @@ Deltacloud::configure do |server|
   server.version '1.0.0'
   server.klass 'Deltacloud::API'
   server.logger Rack::DeltacloudLogger.setup(ENV['API_LOG'], ENV['API_VERBOSE'])
+  server.default_driver ENV['API_DRIVER']
 end
 
 Deltacloud::configure(:cimi) do |server|
@@ -34,6 +35,7 @@ Deltacloud::configure(:cimi) do |server|
   server.version '1.0.0'
   server.klass 'CIMI::API'
   server.logger Rack::DeltacloudLogger.setup(ENV['API_LOG'], ENV['API_VERBOSE'])
+  server.default_driver ENV['API_DRIVER']
 end
 
 Deltacloud::configure(:ec2) do |server|
@@ -41,6 +43,7 @@ Deltacloud::configure(:ec2) do |server|
   server.version '2012-04-01'
   server.klass 'Deltacloud::EC2::API'
   server.logger Rack::DeltacloudLogger.setup(ENV['API_LOG'], ENV['API_VERBOSE'])
+  server.default_driver ENV['API_DRIVER']
 end
 
 routes = {}
@@ -54,21 +57,22 @@ routes = {}
 if ENV['API_FRONTEND'].split(',').size > 1
 
   ENV['API_FRONTEND'].split(',').each do |frontend|
-    Deltacloud.require_frontend!(frontend)
+    Deltacloud[frontend.to_sym].require!
     routes.merge!({
       Deltacloud[frontend].root_url => Deltacloud[frontend].klass
     })
   end
 
 else
-  Deltacloud.require_frontend!(ENV['API_FRONTEND'])
+  Deltacloud[ENV['API_FRONTEND'].to_sym].require!
+  Deltacloud[ENV['API_FRONTEND'].to_sym].default_frontend!
   class IndexEntrypoint < Sinatra::Base
     get "/" do
-      redirect Deltacloud[ENV['API_FRONTEND']].root_url, 301
+      redirect Deltacloudd.default_frontend.root_url, 301
     end
   end
   routes['/'] = IndexEntrypoint.new
-  routes[Deltacloud[ENV['API_FRONTEND']].root_url] = Deltacloud[ENV['API_FRONTEND']].klass
+  routes[Deltacloud.default_frontend.root_url] = Deltacloud.default_frontend.klass
 end
 
 

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/2eb5d1a7/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
index 714b3e8..79dd9d2 100644
--- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
+++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
@@ -776,7 +776,7 @@ module Deltacloud
         def providers(credentials, opts={})
           ec2 = new_client(credentials)
           @providers ||= ec2.describe_regions.map{|r| Provider.new( {:id=>r, :name=>r,
-           :url=>"#{ENV['API_HOST']}:#{ENV['API_PORT']}#{Deltacloud[ENV['API_FRONTEND']
|| :deltacloud].root_url}\;provider=#{r}" }) }
+           :url=>"#{ENV['API_HOST']}:#{ENV['API_PORT']}#{Deltacloud.default_frontend.root_url}\;provider=#{r}"
}) }
         end
 
         def configured_providers

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/2eb5d1a7/server/lib/deltacloud/helpers/driver_helper.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/helpers/driver_helper.rb b/server/lib/deltacloud/helpers/driver_helper.rb
index ab0e3c6..009effd 100644
--- a/server/lib/deltacloud/helpers/driver_helper.rb
+++ b/server/lib/deltacloud/helpers/driver_helper.rb
@@ -22,7 +22,7 @@ module Deltacloud::Helpers
     end
 
     def driver_name
-      Thread.current[:driver] ||= ENV['API_DRIVER']
+      Thread.current[:driver] ||= Deltacloud.default_frontend.default_driver.to_s
     end
 
     def provider_name

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/2eb5d1a7/server/lib/deltacloud/helpers/url_helper.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/helpers/url_helper.rb b/server/lib/deltacloud/helpers/url_helper.rb
index bf82867..1b084d4 100644
--- a/server/lib/deltacloud/helpers/url_helper.rb
+++ b/server/lib/deltacloud/helpers/url_helper.rb
@@ -79,7 +79,7 @@ module Sinatra
     def url_for url_fragment, mode=:path_only
       case mode
       when :path_only
-        base = request.script_name.empty? ? Deltacloud[ENV['API_FRONTEND'] || :deltacloud].root_url
: request.script_name
+        base = request.script_name.empty? ? Deltacloud.default_frontend.root_url : request.script_name
       when :full
         scheme = request.scheme
         port = request.port

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/2eb5d1a7/server/lib/deltacloud_rack.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud_rack.rb b/server/lib/deltacloud_rack.rb
index 28b3045..54474c3 100644
--- a/server/lib/deltacloud_rack.rb
+++ b/server/lib/deltacloud_rack.rb
@@ -26,7 +26,7 @@ module Deltacloud
 
   def self.configure(frontend=:deltacloud, &block)
     frontend = frontend.to_sym
-    config[frontend] = Server.new(&block)
+    config[frontend] ||= Server.new(frontend, &block)
     self
   end
 
@@ -36,22 +36,37 @@ module Deltacloud
 
   def self.require_frontend!(frontend=:deltacloud)
     frontend = frontend.to_sym
+    return false if frontend_required?(frontend)
     require_relative File.join(frontend.to_s, 'server.rb')
     Deltacloud[frontend].klass eval('::'+Deltacloud[frontend].klass)
   end
 
+  def self.frontend_required?(frontend)
+    true unless Deltacloud[frontend].klass.kind_of? String
+  end
+
+  def self.default_frontend(frontend=nil)
+    @default_frontend = frontend unless frontend.nil?
+    raise "Could not determine default API frontend" if @default_frontend.nil? and !config[:deltacloud]
+    @default_frontend || config[:deltacloud]
+  end
+
   class Server
 
+    attr_reader :name
     attr_reader :root_url
     attr_reader :version
     attr_reader :klass
     attr_reader :logger
+    attr_reader :default_driver
 
-    def initialize(opts={}, &block)
+    def initialize(frontend, opts={}, &block)
+      @name=frontend.to_sym
       @root_url = opts[:root_url]
       @version = opts[:version]
       @klass = opts[:klass]
       @logger = opts[:logger] || Rack::DeltacloudLogger
+      @default_driver = opts[:default_driver] || :mock
       instance_eval(&block)
     end
 
@@ -71,11 +86,24 @@ module Deltacloud
       @klass = k
     end
 
+    def default_driver(drv=nil)
+      return @default_driver if drv.nil?
+      @default_driver = drv
+    end
+
     def logger(logger=nil)
       return @logger if logger.nil?
       @logger = logger
     end
 
+    def require!
+      Deltacloud.require_frontend!(@name)
+    end
+
+    def default_frontend!
+      Deltacloud.default_frontend(self)
+    end
+
   end
 
 end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/2eb5d1a7/server/lib/sinatra/rack_accept.rb
----------------------------------------------------------------------
diff --git a/server/lib/sinatra/rack_accept.rb b/server/lib/sinatra/rack_accept.rb
index cef287f..bcfb4d0 100644
--- a/server/lib/sinatra/rack_accept.rb
+++ b/server/lib/sinatra/rack_accept.rb
@@ -73,7 +73,7 @@ module Rack
             self[type] = handler
           end
           yield wants
-          if ENV['API_FRONTEND'] == "cimi"
+          if Deltacloud.default_frontend.name == :cimi
             @media_type = (accepting_formats.has_key?(:xml) ? [:xml, accepting_formats[:xml]]
: nil)
           end
           @media_type ||= accepting_formats.to_a.sort { |a,b| a[1]<=>b[1] }.reverse.select
do |format, priority|


Mime
View raw message