From 80616540b907a397764b2c23b65a5f8c44bc56ff Mon Sep 17 00:00:00 2001 From: =?utf8?q?Franti=C5=A1ek=20Dvo=C5=99=C3=A1k?= Date: Tue, 14 Jun 2016 23:47:17 +0200 Subject: [PATCH] Refactoring: main wrapper class 'nebula' not globally, configuration to separated class. --- .rubocop.yml | 5 +---- application.rb | 3 ++- lib/api.rb | 10 +++++----- lib/config.rb | 41 +++++++++++++++++++++++++++++++++++++++++ lib/nebula.rb | 38 +++++++++----------------------------- 5 files changed, 58 insertions(+), 39 deletions(-) create mode 100644 lib/config.rb diff --git a/.rubocop.yml b/.rubocop.yml index e1913b5..af1f2e2 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -28,10 +28,7 @@ Style/FormatString: # Do not introduce global variables # (do want) Style/GlobalVars: - Exclude: - - application.rb - - lib/api.rb - - lib/nebula.rb + Enabled: false # Extra empty line detected at class body end # (easy to read) diff --git a/application.rb b/application.rb index 62df71d..bd44200 100644 --- a/application.rb +++ b/application.rb @@ -6,6 +6,7 @@ end require './version' require './lib/error' require './lib/server_cipher_auth' +require './lib/config' require './lib/nebula' require './lib/api' @@ -14,4 +15,4 @@ $logger.formatter = proc do |severity, datetime, _progname, msg| date_format = datetime.strftime('%Y-%m-%dT%H:%M:%S%z') sprintf "[#{date_format}] %5s: #{msg}\n", severity end -$nebula = Now::Nebula.new() +$config = Now::Config.new() diff --git a/lib/api.rb b/lib/api.rb index ec05fb8..05735a4 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -11,7 +11,7 @@ module Now def initialize super - @nebula = $nebula + @nebula = Now::Nebula.new($config) end configure do @@ -27,9 +27,9 @@ module Now helpers do def switch_user(user) if user.nil? - @nebula.switch_server() + nebula.switch_server() else - @nebula.switch_user(user) + nebula.switch_user(user) end end end @@ -43,7 +43,7 @@ module Now cross_origin begin switch_user(params['user']) - networks = @nebula.list_networks + networks = nebula.list_networks JSON.pretty_generate(networks) rescue NowError => e logger.error "[HTTP #{e.code}] #{e.message}" @@ -55,7 +55,7 @@ module Now cross_origin begin switch_user(params['user']) - network = @nebula.get(params['id']) + network = nebula.get(params['id']) JSON.pretty_generate(network) rescue NowError => e logger.error "[HTTP #{e.code}] #{e.message}" diff --git a/lib/config.rb b/lib/config.rb new file mode 100644 index 0000000..86b4b3f --- /dev/null +++ b/lib/config.rb @@ -0,0 +1,41 @@ +require 'logger' +require 'yaml' + +module Now + + CONFIG_FILES = [ + ::File.expand_path('~/.config/now.yml'), + '/etc/now.yml', + ::File.expand_path('../../etc/now.yml', __FILE__), + ] + + # Config class for NOW + class Config < Hash + attr_accessor :logger + + def load_config(file) + c = YAML.load_file(file) + logger.debug "Config file '#{file}' loaded" + return c + rescue Errno::ENOENT + logger.debug "Config file '#{file}' not found" + return {} + end + + def initialize + @logger = $logger + config = {} + + CONFIG_FILES.each do |path| + if ::File.exist?(path) + config = load_config(path) + break + end + end + #logger.debug "[config] Configuration: #{config}" + + replace config + end + + end +end diff --git a/lib/nebula.rb b/lib/nebula.rb index a871d9c..af4ea3c 100644 --- a/lib/nebula.rb +++ b/lib/nebula.rb @@ -4,36 +4,22 @@ require 'yaml' module Now EXPIRE_LENGTH = 8 * 60 * 60 - CONFIG_FILES = [ - ::File.expand_path('~/.config/now.yml'), - '/etc/now.yml', - ::File.expand_path('../../etc/now.yml', __FILE__), - ] # NOW core class for communication with OpenNebula class Nebula - attr_accessor :logger + attr_accessor :logger, :config @ctx = nil @server_ctx = nil @user_ctx = nil - def load_config(file) - c = YAML.load_file(file) - logger.debug "Config file '#{file}' loaded" - return c - rescue Errno::ENOENT - logger.debug "Config file '#{file}' not found" - return {} - end - def one_connect(url, credentials) logger.debug "Connecting to #{url} ..." return OpenNebula::Client.new(credentials, url) end def switch_user(user) - admin_user = @config['opennebula']['admin_user'] - admin_password = @config['opennebula']['admin_password'] + admin_user = config['opennebula']['admin_user'] + admin_password = config['opennebula']['admin_password'] logger.debug "Authentication from #{admin_user} to #{user}" server_auth = ServerCipherAuth.new(admin_user, admin_password) @@ -45,8 +31,8 @@ module Now end def switch_server - admin_user = @config['opennebula']['admin_user'] - admin_password = @config['opennebula']['admin_password'] + admin_user = config['opennebula']['admin_user'] + admin_password = config['opennebula']['admin_password'] logger.debug "Authentication to #{admin_user}" direct_token = "#{admin_user}:#{admin_password}" @@ -54,20 +40,14 @@ module Now @ctx = @server_ctx end - def initialize + def initialize(config) @logger = $logger logger.info "Starting Network Orchestrator Wrapper (NOW #{VERSION})" - @config = {} - CONFIG_FILES.each do |path| - if ::File.exist?(path) - @config = load_config(path) - break - end - end - logger.debug "Configuration: #{@config}" + @config = config + #logger.debug "[nebula] Configuration: #{config}" - @url = @config['opennebula']['endpoint'] + @url = config['opennebula']['endpoint'] end def list_networks -- 1.8.2.3