deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 1/4] Core: Added 'lib/initializers' directory
Date Fri, 15 Feb 2013 10:36:29 GMT
From: Michal Fojtik <mfojtik@redhat.com>

Before this patch, we used to initialize stuff like database
or mock directories in random places in source code.

This patch should make all initialization happen in one place.

Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/config.ru                                  | 65 +++++------------------
 server/lib/cimi/dependencies.rb                   |  2 -
 server/lib/cimi/models.rb                         | 18 +++----
 server/lib/db.rb                                  | 17 ------
 server/lib/deltacloud/drivers/mock/mock_driver.rb |  9 +---
 server/lib/deltacloud_rack.rb                     | 13 +++++
 server/lib/initialize.rb                          | 25 +++++++++
 server/lib/initializers/database_initialize.rb    | 28 ++++++++++
 server/lib/initializers/frontend_initialize.rb    | 49 +++++++++++++++++
 server/lib/initializers/mock_initialize.rb        | 15 ++++++
 server/tests/deltacloud/common.rb                 | 14 +----
 server/tests/drivers/base/common.rb               | 14 +----
 12 files changed, 155 insertions(+), 114 deletions(-)
 create mode 100644 server/lib/initialize.rb
 create mode 100644 server/lib/initializers/database_initialize.rb
 create mode 100644 server/lib/initializers/frontend_initialize.rb
 create mode 100644 server/lib/initializers/mock_initialize.rb

diff --git a/server/config.ru b/server/config.ru
index 406c769..305ab4b 100644
--- a/server/config.ru
+++ b/server/config.ru
@@ -14,56 +14,7 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
-# The default URL prefix (where to mount Deltacloud API)
-
-# The default driver is 'mock'
-ENV['API_DRIVER'] ||= 'mock'
-ENV['API_FRONTEND'] ||= 'deltacloud'
-
-load File.join(File.dirname(__FILE__), 'lib', 'deltacloud_rack.rb')
-
-Deltacloud::configure do |server|
-  server.root_url '/api'
-  server.version Deltacloud::API_VERSION
-  server.klass 'Deltacloud::API'
-end
-
-Deltacloud::configure(:cimi) do |server|
-  server.root_url '/cimi'
-  server.version Deltacloud::CIMI_API_VERSION
-  server.klass 'CIMI::API'
-end
-
-Deltacloud::configure(:ec2) do |server|
-  server.root_url '/ec2'
-  server.version '2012-04-01'
-  server.klass 'Deltacloud::EC2::API'
-end
-
-routes = {}
-
-def frontends
-  ENV['API_FRONTEND'].split(',').size > 1 ?
-    ENV['API_FRONTEND'].split(',') : [ENV['API_FRONTEND']]
-end
-
-# 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
-#
-frontends.each do |frontend|
-  frontend = frontend.strip
-  if Deltacloud[frontend.to_sym].nil?
-    puts "ERROR: Unknown frontend (#{frontend}). Valid values are 'deltacloud,cimi,ec2'"
-    exit(1)
-  end
-  Deltacloud[frontend.to_sym].require!
-  routes.merge!({
-    Deltacloud[frontend].root_url => Deltacloud[frontend].klass
-  })
-end
+load File.join(File.dirname(__FILE__), 'lib', 'initialize.rb')
 
 def static_dir_for(name)
   Rack::Directory.new( File.join(File.dirname(__FILE__), "public", name))
@@ -71,14 +22,22 @@ end
 
 # Mount static assets directories and index entrypoint
 #
-routes.merge!({
+# The 'IndexApp' is small Sinatra::Base application that
+# sits on the '/' route and display list of available frontends.
+#
+static_files = {
   '/' => Deltacloud::IndexApp,
   '/stylesheets' =>  static_dir_for('stylesheets'),
   '/javascripts' =>  static_dir_for('javascripts'),
   '/images' =>  static_dir_for('images')
-})
+}
+
+# The 'generate_routes_for' also require the frontend
+# servers and all dependencies.
+#
+routes = Deltacloud.generate_routes_for(frontends)
 
 run Rack::Builder.new {
   use Rack::MatrixParams
-  run Rack::URLMap.new(routes)
+  run Rack::URLMap.new(routes.merge(static_files))
 }
diff --git a/server/lib/cimi/dependencies.rb b/server/lib/cimi/dependencies.rb
index 6f0000c..9b7dcf2 100644
--- a/server/lib/cimi/dependencies.rb
+++ b/server/lib/cimi/dependencies.rb
@@ -58,5 +58,3 @@ require 'sinatra/rack_date'
 require 'sinatra/rack_matrix_params'
 require 'sinatra/rack_syslog'
 require 'sinatra/sinatra_verbose'
-
-CMWG_NAMESPACE = "http://schemas.dmtf.org/cimi/1"
diff --git a/server/lib/cimi/models.rb b/server/lib/cimi/models.rb
index 20c0ef1..39beae4 100644
--- a/server/lib/cimi/models.rb
+++ b/server/lib/cimi/models.rb
@@ -36,17 +36,15 @@ module CIMI
 end
 
 require 'require_relative' if RUBY_VERSION < '1.9'
-require_relative '../db'
 
-unless Deltacloud.test_environment?
-  Deltacloud::initialize_database
-  require_relative './../db/provider'
-  require_relative './../db/entity'
-  require_relative './../db/machine_template'
-  require_relative './../db/address_template'
-  require_relative './../db/volume_configuration'
-  require_relative './../db/volume_template'
-end
+# Database entities
+#
+require_relative './../db/provider'
+require_relative './../db/entity'
+require_relative './../db/machine_template'
+require_relative './../db/address_template'
+require_relative './../db/volume_configuration'
+require_relative './../db/volume_template'
 
 require_relative './models/schema'
 require_relative './models/resource'
diff --git a/server/lib/db.rb b/server/lib/db.rb
index 827a54c..9bf7ead 100644
--- a/server/lib/db.rb
+++ b/server/lib/db.rb
@@ -15,23 +15,6 @@
 
 module Deltacloud
 
-  def self.test_environment?
-    ENV['RACK_ENV'] == 'test'
-  end
-
-  unless test_environment?
-    require 'sequel'
-    require 'logger'
-    Sequel::Model.plugin :validation_class_methods
-  end
-
-  if RUBY_PLATFORM == 'java'
-    DATABASE_LOCATION = ENV['DATABASE_LOCATION'] ||
-      'jdbc:sqlite:'+File.join('/', 'var', 'tmp', "deltacloud-mock-#{ENV['USER']}", 'db.sqlite')
-  else
-    DATABASE_LOCATION = ENV['DATABASE_LOCATION'] ||
-      'sqlite://'+File.join('/', 'var', 'tmp', "deltacloud-mock-#{ENV['USER']}", 'db.sqlite')
-  end
 
   def self.database(opts={})
     opts[:logger] = ::Logger.new($stdout) if ENV['API_VERBOSE']
diff --git a/server/lib/deltacloud/drivers/mock/mock_driver.rb b/server/lib/deltacloud/drivers/mock/mock_driver.rb
index cb18e92..fe930c2 100644
--- a/server/lib/deltacloud/drivers/mock/mock_driver.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_driver.rb
@@ -94,14 +94,7 @@ module Deltacloud::Drivers::Mock
 
 
     def initialize
-      if ENV["DELTACLOUD_MOCK_STORAGE"]
-        storage_root = ENV["DELTACLOUD_MOCK_STORAGE"]
-      elsif Etc.getlogin
-        storage_root = File::join("/var/tmp", "deltacloud-mock-#{ENV["USER"]}")
-      else
-        raise "Please set either the DELTACLOUD_MOCK_STORAGE or USER environment variable"
-      end
-      @client = Client.new(storage_root)
+      @client = Client.new(MOCK_STORAGE_DIR)
     end
 
     def realms(credentials, opts={})
diff --git a/server/lib/deltacloud_rack.rb b/server/lib/deltacloud_rack.rb
index b8e605b..f656d1d 100644
--- a/server/lib/deltacloud_rack.rb
+++ b/server/lib/deltacloud_rack.rb
@@ -57,6 +57,19 @@ module Deltacloud
     @default_frontend || config[:deltacloud]
   end
 
+  def self.generate_routes_for(frontends)
+    frontends.inject({}) do |result, frontend|
+      frontend = frontend.strip
+      if Deltacloud[frontend.to_sym].nil?
+        puts "ERROR: Unknown frontend (#{frontend}). Valid values are 'deltacloud,cimi,ec2'"
+        exit(1)
+      end
+      Deltacloud[frontend.to_sym].require!
+      result[Deltacloud[frontend].root_url] = Deltacloud[frontend].klass
+      result
+    end
+  end
+
   require 'sinatra/base'
   require_relative './deltacloud/helpers/deltacloud_helper'
   require_relative './sinatra/rack_accept'
diff --git a/server/lib/initialize.rb b/server/lib/initialize.rb
new file mode 100644
index 0000000..54060a7
--- /dev/null
+++ b/server/lib/initialize.rb
@@ -0,0 +1,25 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless require_relatived by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+# Deltacloud server initialization scripts:
+
+# Initialize storage for mock and CIMI database
+require_relative './initializers/mock_initialize'
+
+# Configure available frontends
+require_relative './initializers/frontend_initialize'
+
+# If CIMI is enabled, then initialize database stuff
+require_relative './initializers/database_initialize' if frontends.include? 'cimi'
diff --git a/server/lib/initializers/database_initialize.rb b/server/lib/initializers/database_initialize.rb
new file mode 100644
index 0000000..32badb2
--- /dev/null
+++ b/server/lib/initializers/database_initialize.rb
@@ -0,0 +1,28 @@
+# Initialize the storage layer we use to persist some CIMI entities
+# and attributes.
+#
+# By default the database backend is sqlite3
+
+require 'sequel'
+require 'logger'
+
+require_relative '../db'
+
+# We want to enable validation plugin for all database models
+#
+Sequel::Model.plugin :validation_class_methods
+
+# For JRuby we need to different Sequel driver
+#
+sequel_driver = (RUBY_PLATFORM=='java') ? 'jdbc:sqlite:' : 'sqlite://'
+
+# The default sqlite3 database could be override by 'DATABASE_LOCATION'
+# environment variable.
+#
+# For more details about possible values see:
+# http://sequel.rubyforge.org/rdoc/files/doc/opening_databases_rdoc.html
+#
+DATABASE_LOCATION = ENV['DATABASE_LOCATION'] ||
+  "#{sequel_driver}#{File.join(BASE_STORAGE_DIR, 'db.sqlite')}"
+
+Deltacloud::initialize_database
diff --git a/server/lib/initializers/frontend_initialize.rb b/server/lib/initializers/frontend_initialize.rb
new file mode 100644
index 0000000..bfa1871
--- /dev/null
+++ b/server/lib/initializers/frontend_initialize.rb
@@ -0,0 +1,49 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless require_relatived by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+# The default driver if the API_DRIVER environment variable is not set:
+ENV['API_DRIVER'] ||= 'mock'
+
+# The default frontend if the API_FRONTEND environment variable is not set:
+ENV['API_FRONTEND'] ||= 'deltacloud'
+
+require_relative './../deltacloud_rack'
+
+# This will configure the URL mapping, verions and different servers
+# that multiple frontends require. You might not need to change this :)
+
+def frontends
+  ENV['API_FRONTEND'] ||= 'deltacloud'
+  ENV['API_FRONTEND'].split(',').size > 1 ?
+    ENV['API_FRONTEND'].split(',') : [ENV['API_FRONTEND']]
+end
+
+Deltacloud::configure do |server|
+  server.root_url '/api'
+  server.version Deltacloud::API_VERSION
+  server.klass 'Deltacloud::API'
+end
+
+Deltacloud::configure(:cimi) do |server|
+  server.root_url '/cimi'
+  server.version Deltacloud::CIMI_API_VERSION
+  server.klass 'CIMI::API'
+end
+
+Deltacloud::configure(:ec2) do |server|
+  server.root_url '/ec2'
+  server.version '2012-04-01'
+  server.klass 'Deltacloud::EC2::API'
+end
diff --git a/server/lib/initializers/mock_initialize.rb b/server/lib/initializers/mock_initialize.rb
new file mode 100644
index 0000000..c8c24e9
--- /dev/null
+++ b/server/lib/initializers/mock_initialize.rb
@@ -0,0 +1,15 @@
+# This will create the directory we use for storing Mock driver
+# data and also CIMI database file
+#
+
+# By default the location is /var/tmp/deltacloud-{USER}/
+#
+BASE_STORAGE_DIR = File.join('/', 'var', 'tmp', "deltacloud-#{ENV['USER']}")
+
+# The mock driver YAML files are stored in BASE_STORAGE_DIR/mock
+# You can overide this by setting 'DELTACLOUD_MOCK_STORAGE' environment variable
+#
+MOCK_STORAGE_DIR = ENV['DELTACLOUD_MOCK_STORAGE'] || File.join(BASE_STORAGE_DIR, 'mock')
+
+FileUtils.mkpath(BASE_STORAGE_DIR, :mode => 0750) unless File.directory?(BASE_STORAGE_DIR)
+FileUtils.mkpath(BASE_STORAGE_DIR, :mode => 0750) unless File.directory?(MOCK_STORAGE_DIR)
diff --git a/server/tests/deltacloud/common.rb b/server/tests/deltacloud/common.rb
index 1c3f45c..2b304d6 100644
--- a/server/tests/deltacloud/common.rb
+++ b/server/tests/deltacloud/common.rb
@@ -1,17 +1,7 @@
 require_relative '../test_helper.rb'
-require_relative File.join('..', '..', 'lib', 'deltacloud_rack.rb')
+require_relative File.join('..', '..', 'lib', 'initializers', 'frontend_initialize')
 
-# Setup Deltacloud::API Sinatra instance
-
-Deltacloud::configure do |server|
-  server.root_url '/api'
-  server.version Deltacloud::API_VERSION
-  server.klass 'Deltacloud::API'
-  server.logger Rack::DeltacloudLogger.setup(ENV['API_LOG'], ENV['API_VERBOSE'])
-  server.default_driver :mock
-end
-
-Deltacloud.require_frontend!
+Deltacloud.require_frontend!(:deltacloud)
 
 def check_json_serialization_for(model, sample_id, optional_attrs=[])
   header 'Accept', 'application/json'
diff --git a/server/tests/drivers/base/common.rb b/server/tests/drivers/base/common.rb
index 163a715..02d8673 100644
--- a/server/tests/drivers/base/common.rb
+++ b/server/tests/drivers/base/common.rb
@@ -1,15 +1,5 @@
 require_relative '../../test_helper'
 
-require_relative File.join('..', '..', '..', 'lib', 'deltacloud_rack.rb')
+require_relative File.join('..', '..', '..', 'lib', 'initializers', 'frontend_initialize')
 
-# Setup Deltacloud::API Sinatra instance
-
-Deltacloud::configure do |server|
-  server.root_url '/api'
-  server.version Deltacloud::API_VERSION
-  server.klass 'Deltacloud::API'
-  server.logger Rack::DeltacloudLogger.setup(ENV['API_LOG'], ENV['API_VERBOSE'])
-  server.default_driver :mock
-end
-
-Deltacloud.require_frontend!
+Deltacloud.require_frontend!(:deltacloud)
-- 
1.8.1.2


Mime
View raw message