deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michal Fojtik <mfoj...@redhat.com>
Subject Re: [PATCH core 6/8] Core: Improved frontend loading and usage of ENV vars
Date Fri, 03 Aug 2012 14:13:36 GMT
Hi,

I just found a small bug in config.ru on this.

You need to have:

  server.default_driver ENV['API_DRIVER']

in each frontend config to make deltacloudd -i work. 

Michal Fojtik
http://deltacloud.org
mfojtik@redhat.com



On Aug 3, 2012, at 3:15 PM, mfojtik@redhat.com wrote:

> From: Michal Fojtik <mfojtik@redhat.com>
> 
> * Added Deltacloud[:ec2].require! to load frontend class
> * Added Deltacloud.default_frontend instead of ENV['API_FRONTEND']
> 
> Signed-off-by: Michal fojtik <mfojtik@redhat.com>
> ---
> server/config.ru                                |    9 ++++---
> 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, 39 insertions(+), 10 deletions(-)
> 
> diff --git a/server/config.ru b/server/config.ru
> index 254baf2..16b025a 100644
> --- a/server/config.ru
> +++ b/server/config.ru
> @@ -54,21 +54,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
> 
> 
> 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
> 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
> 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
> 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
> 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|
> -- 
> 1.7.10.2
> 


Mime
View raw message