deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core] Core: Added possibility to configure database location
Date Wed, 09 Jan 2013 11:22:43 GMT
From: Michal Fojtik <mfojtik@redhat.com>

This patch introduce system/user config file support
for Deltacloud instance. The configuration file is a YAML
format file with following structure:

:deltacloud:
  :your_option: Option value
:cimi:
  :database_location: /tmp/cimi.database

The primary 'key' is the Deltacloud frontend the option will
be valid for. Deltacloud will then look for the file in following
locations (in exact order):

$HOME/.deltacloud/config.yaml
/etc/deltacloud/config.yaml

Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/db.rb              |  2 +-
 server/lib/deltacloud_rack.rb | 63 ++++++++++++++++++++++++-------------------
 2 files changed, 36 insertions(+), 29 deletions(-)

diff --git a/server/lib/db.rb b/server/lib/db.rb
index 9564b79..b401329 100644
--- a/server/lib/db.rb
+++ b/server/lib/db.rb
@@ -12,7 +12,7 @@ module Deltacloud
     require_relative './db/address_template'
   end
 
-  DATABASE_LOCATION = ENV['DATABASE_LOCATION'] || File.join('/', 'var', 'tmp', "deltacloud-mock-#{ENV['USER']}",
'db.sqlite')
+  DATABASE_LOCATION = ENV['DATABASE_LOCATION'] || config[:cimi][:database_location] || File.join('/',
'var', 'tmp', "deltacloud-mock-#{ENV['USER']}", 'db.sqlite')
 
   def self.initialize_database
     DataMapper::Logger.new($stdout, :debug) if ENV['API_VERBOSE']
diff --git a/server/lib/deltacloud_rack.rb b/server/lib/deltacloud_rack.rb
index 8ddcb41..296b689 100644
--- a/server/lib/deltacloud_rack.rb
+++ b/server/lib/deltacloud_rack.rb
@@ -22,13 +22,27 @@ require_relative './deltacloud/version'
 
 module Deltacloud
 
+  def self.system_config
+    return @system_config if !@system_config.nil?
+    config_path = File.join(ENV['HOME'], '.deltacloud', 'config.yaml')
+    if File.exists?(config_path)
+      @system_config = YAML.load_file(config_path)
+    end
+    config_path = File.join('/', 'etc', '.deltacloud', 'config.yaml')
+    if File.exists?(config_path)
+      @system_config = YAML.load_file(config_path)
+    end
+    @system_config || {}
+  end
+
   def self.config
     @config ||= {}
   end
 
   def self.configure(frontend=:deltacloud, &block)
     frontend = frontend.to_sym
-    config[frontend] ||= Server.new(frontend, &block)
+    system_options = system_config[frontend] || {}
+    config[frontend] ||= Server.new(frontend, system_options, &block)
     self
   end
 
@@ -78,20 +92,17 @@ module Deltacloud
 
   class Server
 
-    attr_reader :name
-    attr_reader :root_url
-    attr_reader :version
-    attr_reader :klass
-    attr_reader :logger
-    attr_reader :default_driver
+    attr_accessor :config
 
     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.setup(ENV['API_LOG'], ENV['API_VERBOSE'])
-      @default_driver = opts[:default_driver] || ENV['API_DRIVER'] || :mock
+      @config = {}
+      root_url(opts.delete(:root_url))
+      version(opts.delete(:version))
+      klass(opts.delete(:klass))
+      logger(opts.delete(:logger) || Rack::DeltacloudLogger.setup(ENV['API_LOG'], ENV['API_VERBOSE']))
+      default_driver(opts.delete(:default_driver) || ENV['API_DRIVER'] || :mock)
+      @config.merge!(:name => frontend.to_sym)
+      @config.merge!(opts)
       instance_eval(&block)
     end
 
@@ -101,28 +112,24 @@ module Deltacloud
       @root_url = url
     end
 
-    def version(version=nil)
-      return @version if version.nil?
-      @version = version
-    end
-
-    def klass(k=nil)
-      return @klass if k.nil?
-      @klass = k
+    [:version, :klass, :default_driver, :logger].each do |option|
+      define_method option do |value=nil|
+        @config[option] = value if !value.nil?
+        @config[option]
+      end
     end
 
-    def default_driver(drv=nil)
-      return @default_driver if drv.nil?
-      @default_driver = drv
+    def method_missing(name, *args)
+      return config[name.to_sym] if config.has_key?(name.to_sym)
+      super
     end
 
-    def logger(logger=nil)
-      return @logger if logger.nil?
-      @logger = logger
+    def [](value)
+      config[value]
     end
 
     def require!
-      Deltacloud.require_frontend!(@name)
+      Deltacloud.require_frontend!(name)
     end
 
     def default_frontend!
-- 
1.8.0.2


Mime
View raw message