From e1fa897535d04137a9f3d6962fe22b77f3d5e4ab Mon Sep 17 00:00:00 2001 From: =?utf8?q?Franti=C5=A1ek=20Dvo=C5=99=C3=A1k?= Date: Fri, 10 Jun 2016 10:22:39 +0200 Subject: [PATCH] Removed peculiar config merge - fixes compatibility with the old rubies. --- .rubocop.yml | 5 +++++ etc/now.yaml | 4 ---- etc/now.yml | 4 ++++ lib/nebula.rb | 37 ++++++++++++------------------------- 4 files changed, 21 insertions(+), 29 deletions(-) delete mode 100644 etc/now.yaml create mode 100644 etc/now.yml diff --git a/.rubocop.yml b/.rubocop.yml index 2d12e50..1ec3868 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -83,3 +83,8 @@ Style/RaiseArgs: # (easy to read) Style/RedundantReturn: Enabled: false + +# Avoid comma after the last item of an array +# (do want) +Style/TrailingCommaInLiteral: + Enabled: false diff --git a/etc/now.yaml b/etc/now.yaml deleted file mode 100644 index 0909b6c..0000000 --- a/etc/now.yaml +++ /dev/null @@ -1,4 +0,0 @@ -opennebula: - admin_user: oneadmin - admin_password: 'good-password' - endpoint: 'http://localhost:2633/RPC2' diff --git a/etc/now.yml b/etc/now.yml new file mode 100644 index 0000000..c14dec3 --- /dev/null +++ b/etc/now.yml @@ -0,0 +1,4 @@ +opennebula: + admin_user: 'nowadmin' + admin_password: 'the-best-strongest-password-ever' + endpoint: http://localhost:2633/RPC2 diff --git a/lib/nebula.rb b/lib/nebula.rb index d0251cb..4c2c1cb 100644 --- a/lib/nebula.rb +++ b/lib/nebula.rb @@ -1,27 +1,14 @@ require 'opennebula' require 'yaml' -# http://stackoverflow.com/questions/9381553/ruby-merge-nested-hash -public def deep_merge(second) - merger = proc do |_key, v1, v2| - if [:undefined, nil, :nil].include?(v2) - v1 - elsif [:undefined, nil, :nil].include?(v1) - v2 - elsif v1.is_a?(Hash) && v2.is_a?(Hash) - v1.merge(v2, &merger) - elsif v1.is_a?(Array) && v2.is_a(Array) - v1 | v2 - else - v2 - end - end - merge(second.to_h, &merger) -end - 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 @@ -72,13 +59,13 @@ module Now logger.info "Starting Network Orchestrator Wrapper (NOW #{VERSION})" @config = {} - c = load_config(::File.expand_path('../../etc/now.yaml', __FILE__)) - @config = @config.deep_merge(c) - #logger.debug "Configuration: #{@config}" - - c = load_config('/etc/now.yaml') - @config = @config.deep_merge(c) - #logger.debug "Configuration: #{@config}" + CONFIG_FILES.each do |path| + if ::File.exist?(path) + @config = load_config(path) + break + end + end + logger.debug "Configuration: #{@config}" @url = @config['opennebula']['endpoint'] end -- 1.8.2.3