deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [4/4] git commit: Core: Make possible to run multiple frontends using single Deltacloud instance
Date Thu, 07 Jun 2012 11:59:05 GMT
Core: Make possible to run multiple frontends using single Deltacloud instance


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

Branch: refs/heads/master
Commit: 3833cd60f469509995aa4156567bcc842da564e3
Parents: 49d0960
Author: Michal Fojtik <mfojtik@redhat.com>
Authored: Wed Jun 6 17:47:30 2012 +0200
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Wed Jun 6 17:47:30 2012 +0200

----------------------------------------------------------------------
 server/bin/deltacloudd                             |    2 +-
 server/config.ru                                   |   59 ++++++++++-----
 server/lib/cimi/helpers.rb                         |    6 +-
 server/lib/cimi/server.rb                          |    4 +-
 server/lib/deltacloud/drivers/base_driver.rb       |    6 +-
 server/lib/deltacloud/drivers/ec2/ec2_driver.rb    |    2 +-
 server/lib/deltacloud/helpers.rb                   |    5 +-
 server/lib/deltacloud/helpers/deltacloud_helper.rb |    6 +-
 server/lib/deltacloud/helpers/url_helper.rb        |    4 +-
 server/lib/deltacloud/models/hardware_profile.rb   |    2 +-
 server/lib/deltacloud/server.rb                    |   14 ++--
 server/lib/deltacloud_rack.rb                      |   23 +++---
 server/lib/ec2/server.rb                           |    4 +-
 server/lib/sinatra/rack_accept.rb                  |    2 +-
 server/lib/sinatra/rack_driver_select.rb           |    2 +-
 15 files changed, 82 insertions(+), 59 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/server/bin/deltacloudd
----------------------------------------------------------------------
diff --git a/server/bin/deltacloudd b/server/bin/deltacloudd
index 3dbe967..08be61c 100755
--- a/server/bin/deltacloudd
+++ b/server/bin/deltacloudd
@@ -61,7 +61,7 @@ BANNER
   opts.on( '-P', '--provider PROVIDER', 'Use PROVIDER (default is set in the driver)') do
|provider|
     ENV['API_PROVIDER'] = provider
   end
-  opts.on('-f', '--frontend FRONTEND', 'Use the different frontend, not the Deltacloud (cimi
or ec2)') do |frontend|
+  opts.on('-f', '--frontends FRONTENDS', 'Enable different frontends (cimi, ec2, deltacloud)')
do |frontend|
     ENV['API_FRONTEND'] = frontend
   end
   opts.on( '-c', '--config [FILE]', 'Read provider and other config from FILE (default: ~/.deltacloud/config)')
do |config|

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/server/config.ru
----------------------------------------------------------------------
diff --git a/server/config.ru b/server/config.ru
index 582de45..ba3583f 100644
--- a/server/config.ru
+++ b/server/config.ru
@@ -18,6 +18,7 @@
 
 # The default driver is 'mock'
 ENV['API_DRIVER'] ||= 'mock'
+ENV['API_FRONTEND'] ||= 'deltacloud'
 
 load File.join(File.dirname(__FILE__), 'lib', 'deltacloud_rack.rb')
 
@@ -27,38 +28,56 @@ Deltacloud::configure do |server|
   server.klass 'Deltacloud::API'
 end
 
-if ENV['API_FRONTEND'] == 'cimi'
-  Deltacloud::configure do |server|
-    server.root_url '/cimi'
-    server.version '1.0.0'
-    server.klass 'CIMI::API'
-  end
+Deltacloud::configure(:cimi) do |server|
+  server.root_url '/cimi'
+  server.version '1.0.0'
+  server.klass 'CIMI::API'
 end
 
-if ENV['API_FRONTEND'] == 'ec2'
-  Deltacloud::configure do |server|
-    server.root_url '/'
-    server.version '2012-04-01'
-    server.klass 'Deltacloud::EC2::API'
-  end
+Deltacloud::configure(:ec2) do |server|
+  server.root_url '/'
+  server.version '2012-04-01'
+  server.klass 'Deltacloud::EC2::API'
 end
 
-Deltacloud.require_frontend!
+routes = {}
+
+# If user wants to launch multiple frontends withing a single instance of DC API
+# then require them and prepare the routes for Rack
+#
+# NOTE: The '/' will not be generated, since multiple frontends could have
+#       different root_url's
+#
+if ENV['API_FRONTEND'].split(',').size > 1
+
+  ENV['API_FRONTEND'].split(',').each do |frontend|
+    Deltacloud.require_frontend!(frontend)
+    routes.merge!({
+      Deltacloud[frontend].root_url => Deltacloud[frontend].klass
+    })
+  end
 
-class IndexEntrypoint < Sinatra::Base
-  get "/" do
-    redirect Deltacloud[:root_url], 301
+else
+  Deltacloud.require_frontend!(ENV['API_FRONTEND'])
+  class IndexEntrypoint < Sinatra::Base
+    get "/" do
+      redirect Deltacloud[ENV['API_FRONTEND']].root_url, 301
+    end
   end
+  routes['/'] = IndexEntrypoint.new
+  routes[Deltacloud[ENV['API_FRONTEND']].root_url] = Deltacloud[ENV['API_FRONTEND']].klass
 end
 
+
 run Rack::Builder.new {
   use Rack::MatrixParams
   use Rack::DriverSelect
 
-  run Rack::URLMap.new(
-    "/" => IndexEntrypoint.new,
-    Deltacloud[:root_url] => Deltacloud[:klass],
+  routes.merge!({
     "/stylesheets" =>  Rack::Directory.new( "public/stylesheets" ),
     "/javascripts" =>  Rack::Directory.new( "public/javascripts" )
-  )
+  })
+
+  run Rack::URLMap.new(routes)
+
 } if respond_to? :run

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/server/lib/cimi/helpers.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/helpers.rb b/server/lib/cimi/helpers.rb
index 5fe8cf8..1b91423 100644
--- a/server/lib/cimi/helpers.rb
+++ b/server/lib/cimi/helpers.rb
@@ -62,8 +62,8 @@ module CIMI::Collections
     enable :show_errors
     disable :show_exceptions
 
-    set :root_url, Deltacloud[:root_url]
-    set :version, Deltacloud[:version]
+    set :root_url, Deltacloud[:cimi].root_url
+    set :version, Deltacloud[:cimi].version
     set :root, File.join(File.dirname(__FILE__), '..', '..')
     set :views, root + '/views/cimi'
     set :public_folder, root + '/public'
@@ -82,7 +82,7 @@ module CIMI::Collections
     end
 
     after do
-      headers 'X-CIMI-Specification-Version' => Deltacloud[:version]
+      headers 'X-CIMI-Specification-Version' => Deltacloud[:cimi].version
     end
 
     def self.new_route_for(route, &block)

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/server/lib/cimi/server.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index 58c1688..4e612f4 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -45,8 +45,8 @@ module CIMI
     include CIMI::Collections
     include CIMI::Model
 
-    get Deltacloud[:root_url] do
-      redirect Deltacloud[:root_url] + '/cloudEntryPoint', 301
+    get Deltacloud[:cimi].root_url do
+      redirect Deltacloud[:cimi].root_url + '/cloudEntryPoint', 301
     end
 
   end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/server/lib/deltacloud/drivers/base_driver.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/drivers/base_driver.rb b/server/lib/deltacloud/drivers/base_driver.rb
index a360807..555a244 100644
--- a/server/lib/deltacloud/drivers/base_driver.rb
+++ b/server/lib/deltacloud/drivers/base_driver.rb
@@ -23,7 +23,7 @@ module Deltacloud
     STATE_MACHINE_OPTS = {
       :all_states => [:start, :pending, :running, :stopping, :stopped, :finish],
       :all_actions => [:create, :reboot, :stop, :start, :destroy]
-    }
+    } unless defined?(STATE_MACHINE_OPTS)
 
     def self.driver_name
       name.split('::').last.gsub('Driver', '').downcase
@@ -243,8 +243,8 @@ module Deltacloud
       firewalls(credentials, opts).first if has_capability?(:firewalls)
     end
 
-    MEMBER_SHOW_METHODS =
-      [ :realm, :image, :instance, :storage_volume, :bucket, :blob, :key, :firewall ]
+    MEMBER_SHOW_METHODS = [ :realm, :image, :instance, :storage_volume, :bucket, :blob,
+                            :key, :firewall ] unless defined?(MEMBER_SHOW_METHODS)
 
     def filter_on(collection, attribute, opts)
       return collection if opts.nil?

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/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 8a61b9c..cca995d 100644
--- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
+++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
@@ -752,7 +752,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[:root_url]}\;provider=#{r}"
}) }
+           :url=>"#{ENV['API_HOST']}:#{ENV['API_PORT']}#{Deltacloud[ENV['API_FRONTEND']
|| :deltacloud].root_url}\;provider=#{r}" }) }
         end
 
         def configured_providers

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/server/lib/deltacloud/helpers.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/helpers.rb b/server/lib/deltacloud/helpers.rb
index f2a9ebb..8d5774c 100644
--- a/server/lib/deltacloud/helpers.rb
+++ b/server/lib/deltacloud/helpers.rb
@@ -40,8 +40,9 @@ module Deltacloud::Collections
     enable :method_override
     disable :show_exceptions
 
-    set :root_url, Deltacloud[:root_url]
-    set :version, Deltacloud[:version]
+    set :config, Deltacloud[:deltacloud]
+    set :root_url, config.root_url
+    set :version, config.version
     set :root, File.join(File.dirname(__FILE__), '..', '..')
     set :views, root + '/views'
     set :public_folder, root + '/public'

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/server/lib/deltacloud/helpers/deltacloud_helper.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/helpers/deltacloud_helper.rb b/server/lib/deltacloud/helpers/deltacloud_helper.rb
index 1d0df0e..843e45f 100644
--- a/server/lib/deltacloud/helpers/deltacloud_helper.rb
+++ b/server/lib/deltacloud/helpers/deltacloud_helper.rb
@@ -20,9 +20,9 @@ module Deltacloud::Helpers
 
     def self.included(klass)
       klass.class_eval do
-        set :root_url, Deltacloud[:root_url]
+        set :config, Deltacloud[:deltacloud]
         include Sinatra::Rabbit
-        Sinatra::Rabbit.set :root_path, root_url+'/'
+        Sinatra::Rabbit.set :root_path, "#{config.root_url}/"
       end
     end
 
@@ -236,7 +236,7 @@ module Deltacloud::Helpers
       end
     end
 
-    NEW_BLOB_FORM_ID = 'new_blob_form_d15cfd90'
+    NEW_BLOB_FORM_ID = 'new_blob_form_d15cfd90' unless defined?(NEW_BLOB_FORM_ID)
 
     def new_blob_form_url(bucket)
       bucket_url(@bucket.name) + "/" + NEW_BLOB_FORM_ID

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/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 fd4d9bc..eead293 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[:root_url] : request.script_name
+        base = request.script_name.empty? ? Deltacloud[ENV['API_FRONTEND'] || :deltacloud].root_url
: request.script_name
       when :full
         scheme = request.scheme
         port = request.port
@@ -96,7 +96,7 @@ module Sinatra
         else
           port = ":#{port}"
         end
-        base = "#{scheme}://#{request_host}#{port}#{request.script_name.empty? ? Deltacloud[:root_url]
: request.script_name}"
+        base = "#{scheme}://#{request_host}#{port}#{request.script_name.empty? ? Deltacloud[ENV['API_FRONTEND']
|| :deltacloud].root_url : request.script_name}"
       else
         raise TypeError, "Unknown url_for mode #{mode}"
       end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/server/lib/deltacloud/models/hardware_profile.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/models/hardware_profile.rb b/server/lib/deltacloud/models/hardware_profile.rb
index 45e77a1..0b36c4b 100644
--- a/server/lib/deltacloud/models/hardware_profile.rb
+++ b/server/lib/deltacloud/models/hardware_profile.rb
@@ -22,7 +22,7 @@ module Deltacloud
       :storage => "GB",
       :architecture => "label",
       :cpu => "count"
-    }
+    } unless defined?(UNITS)
 
     def self.unit(name)
       UNITS[name]

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/server/lib/deltacloud/server.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/server.rb b/server/lib/deltacloud/server.rb
index e26f427..cdb7150 100644
--- a/server/lib/deltacloud/server.rb
+++ b/server/lib/deltacloud/server.rb
@@ -42,7 +42,9 @@ module Deltacloud
     include Deltacloud::Helpers
     include Deltacloud::Collections
 
-    get Deltacloud[:root_url] + '/?' do
+    set :config, Deltacloud[:deltacloud]
+
+    get config.root_url + '/?' do
       if params[:force_auth]
         return [401, 'Authentication failed'] unless driver.valid_credentials?(credentials)
       end
@@ -53,20 +55,20 @@ module Deltacloud
       end
     end
 
-    post Deltacloud[:root_url] + '/?' do
+    post config.root_url + '/?' do
       param_driver, param_provider = params["driver"], params["provider"]
       if param_driver
-        redirect "#{Deltacloud[:root_url]}\;driver=#{param_driver}", 301
+        redirect "#{config.root_url}\;driver=#{param_driver}", 301
       elsif param_provider && param_provider != "default"
 #FIXME NEEDS A BETTER WAY OF GRABBING CURRENT DRIVER FROM MATRIX PARAMS...
         current_matrix_driver = env["HTTP_REFERER"].match(/\;(driver)=(\w*).*$/i)
         if current_matrix_driver
-          redirect "#{Deltacloud[:root_url]}\;driver=#{$2}\;provider=#{param_provider}",
301
+          redirect "#{config.root_url}\;driver=#{$2}\;provider=#{param_provider}", 301
         else
-          redirect "#{Deltacloud[:root_url]}\;provider=#{param_provider}", 301
+          redirect "#{config.root_url}\;provider=#{param_provider}", 301
         end
       else
-        redirect "#{Deltacloud[:root_url]}", 301
+        redirect "#{config.root_url}", 301
       end
     end
 

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/server/lib/deltacloud_rack.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud_rack.rb b/server/lib/deltacloud_rack.rb
index e59b11a..2c55b29 100644
--- a/server/lib/deltacloud_rack.rb
+++ b/server/lib/deltacloud_rack.rb
@@ -25,23 +25,24 @@ require_relative './deltacloud/core_ext'
 
 module Deltacloud
 
-  def self.config(conf=nil)
-    @config ||= conf
+  def self.config
+    @config ||= {}
   end
 
-  def self.configure(&block)
-    @config = Server.new(&block)
+  def self.configure(frontend=:deltacloud, &block)
+    frontend = frontend.to_sym
+    config[frontend] = Server.new(&block)
     self
   end
 
-  def self.[](item)
-    config.send(item)
+  def self.[](frontend=:deltacloud)
+    config[frontend.to_sym]
   end
 
-  def self.require_frontend!
-    ENV['API_FRONTEND'] ||= 'deltacloud'
-    require File.join(File.dirname(__FILE__), ENV['API_FRONTEND'], 'server.rb')
-    config.klass eval('::'+self[:klass])
+  def self.require_frontend!(frontend=:deltacloud)
+    frontend = frontend.to_sym
+    require File.join(File.dirname(__FILE__), frontend.to_s, 'server.rb')
+    Deltacloud[frontend].klass eval('::'+Deltacloud[frontend].klass)
   end
 
   class Server
@@ -59,7 +60,7 @@ module Deltacloud
 
     def root_url(url=nil)
       return @root_url if url.nil?
-      raise '[Core] The server URL must start with /' unless url =~ /^\//
+      raise "[Core] The server URL must start with / (#{url})" unless url =~ /^\//
       @root_url = url
     end
 

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/server/lib/ec2/server.rb
----------------------------------------------------------------------
diff --git a/server/lib/ec2/server.rb b/server/lib/ec2/server.rb
index 9b045e4..52f3993 100644
--- a/server/lib/ec2/server.rb
+++ b/server/lib/ec2/server.rb
@@ -41,8 +41,8 @@ module Deltacloud::EC2
     enable :method_override
     disable :show_exceptions
 
-    set :version, Deltacloud[:version]
-    set :root_url, Deltacloud[:root_url]
+    set :version, Deltacloud[:ec2].version
+    set :root_url, Deltacloud[:ec2].root_url
     set :root, File.join(File.dirname(__FILE__), '..', '..')
     set :views, File.join(File.dirname(__FILE__), 'views')
 

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/server/lib/sinatra/rack_accept.rb
----------------------------------------------------------------------
diff --git a/server/lib/sinatra/rack_accept.rb b/server/lib/sinatra/rack_accept.rb
index 4286e93..cef287f 100644
--- a/server/lib/sinatra/rack_accept.rb
+++ b/server/lib/sinatra/rack_accept.rb
@@ -113,7 +113,7 @@ module Rack
       :html => { :return => 'text/html', :match => ['application/xhtml+xml', 'text/html',
'*/*'] },
       :png => { :return => 'image/png', :match => ['image/png'] },
       :gv => { :return => 'application/ghostscript', :match => ['application/ghostscript']
}
-    }
+    } unless defined?(ACCEPTED_MEDIA_TYPES)
 
     def call(env)
       accept, index = env['rack-accept.request'], {}

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3833cd60/server/lib/sinatra/rack_driver_select.rb
----------------------------------------------------------------------
diff --git a/server/lib/sinatra/rack_driver_select.rb b/server/lib/sinatra/rack_driver_select.rb
index 829ce8c..141f399 100644
--- a/server/lib/sinatra/rack_driver_select.rb
+++ b/server/lib/sinatra/rack_driver_select.rb
@@ -25,7 +25,7 @@ module Rack
     HEADER_TO_ENV_MAP = {
       'HTTP_X_DELTACLOUD_DRIVER' => :driver,
       'HTTP_X_DELTACLOUD_PROVIDER' => :provider
-    }
+    } unless defined?(HEADER_TO_ENV_MAP)
 
     def call(env)
       original_settings = { }


Mime
View raw message