Removed peculiar config merge - fixes compatibility with the old rubies.
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Fri, 10 Jun 2016 08:22:39 +0000 (10:22 +0200)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Fri, 10 Jun 2016 08:22:39 +0000 (10:22 +0200)
.rubocop.yml
etc/now.yaml [deleted file]
etc/now.yml [new file with mode: 0644]
lib/nebula.rb

index 2d12e50..1ec3868 100644 (file)
@@ -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 (file)
index 0909b6c..0000000
+++ /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 (file)
index 0000000..c14dec3
--- /dev/null
@@ -0,0 +1,4 @@
+opennebula:
+  admin_user: 'nowadmin'
+  admin_password: 'the-best-strongest-password-ever'
+  endpoint: http://localhost:2633/RPC2
index d0251cb..4c2c1cb 100644 (file)
@@ -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