diff options
Diffstat (limited to 'lib/leap_cli/config/manager.rb')
-rw-r--r-- | lib/leap_cli/config/manager.rb | 83 |
1 files changed, 32 insertions, 51 deletions
diff --git a/lib/leap_cli/config/manager.rb b/lib/leap_cli/config/manager.rb index 6a7c1e9..55575cf 100644 --- a/lib/leap_cli/config/manager.rb +++ b/lib/leap_cli/config/manager.rb @@ -1,11 +1,14 @@ -require 'oj' +require 'json/pure' require 'yaml' module LeapCli module Config + # + # A class to manage all the objects in all the configuration files. + # class Manager - attr_reader :services, :tags, :nodes + attr_reader :services, :tags, :nodes, :provider ## ## IMPORT EXPORT @@ -15,10 +18,11 @@ module LeapCli # load .json configuration files # def load(dir) - @services = load_all_json("#{dir}/services/*.json", :tag) - @tags = load_all_json("#{dir}/tags/*.json", :tag) - @common = load_all_json("#{dir}/common.json", :tag)['common'] - @nodes = load_all_json("#{dir}/nodes/*.json", :node) + @services = load_all_json("#{dir}/services/*.json") + @tags = load_all_json("#{dir}/tags/*.json") + @common = load_all_json("#{dir}/common.json")['common'] + @provider = load_all_json("#{dir}/provider.json")['provider'] + @nodes = load_all_json("#{dir}/nodes/*.json") @nodes.each do |name, node| @nodes[name] = apply_inheritance(node) end @@ -61,7 +65,7 @@ module LeapCli filters[0] = filters[0][1..-1] end - node_list = Config::List.new + node_list = Config::ObjectList.new filters.each do |filter| if filter =~ /^\+/ keep_list = nodes_for_name(filter[1..-1]) @@ -73,38 +77,21 @@ module LeapCli end end else - node_list << nodes_for_name(filter) + node_list.merge!(nodes_for_name(filter)) end end return node_list end - ## - ## CLASS METHODS - ## - - #def self.manager - # @manager ||= begin - # manager = ConfigManager.new - # manager.load(Path.provider) - # manager - # end - #end - - #def self.filter(filters); manager.filter(filters); end - #def self.nodes; manager.nodes; end - #def self.services; manager.services; end - #def self.tags; manager.tags; end - private def load_all_json(pattern, config_type = :class) - results = Config::List.new + results = Config::ObjectList.new Dir.glob(pattern).each do |filename| obj = load_json(filename, config_type) if obj name = File.basename(filename).sub(/\.json$/,'') - obj['name'] = name + obj['name'] ||= name results[name] = obj end end @@ -128,15 +115,16 @@ module LeapCli # parse json, and flatten hash begin - hash = Oj.load(buffer.string) || {} + #hash = Oj.load(buffer.string) || {} + hash = JSON.parse(buffer.string, :object_class => Hash, :array_class => Array) || {} rescue SyntaxError => exc log0 'Error in file "%s":' % filename log0 exc.to_s return nil end - config = config_type == :node ? Node.new(self) : Tag.new(self) - config.deep_merge!(hash) - return config + object = Config::Object.new(self) + object.deep_merge!(hash) + return object end # @@ -156,17 +144,14 @@ module LeapCli # end # - # makes this node inherit options from the common, service, and tag json files. - # - # - takes a hash - # - returns a Node object. + # makes a node inherit options from appropriate the common, service, and tag json files. # def apply_inheritance(node) - new_hash = Node.new(self) - #new_node = Node.new(self) + new_node = Config::Object.new(self) + name = node.name # inherit from common - new_hash.deep_merge!(@common) + new_node.deep_merge!(@common) # inherit from services if node['services'] @@ -175,8 +160,8 @@ module LeapCli if service.nil? log0('Error in node "%s": the service "%s" does not exist.' % [node['name'], node_service]) else - new_hash.deep_merge!(service) - service.nodes << new_hash + new_node.deep_merge!(service) + service.node_list.add(name, new_node) end end end @@ -188,19 +173,15 @@ module LeapCli if tag.nil? log0('Error in node "%s": the tag "%s" does not exist.' % [node['name'], node_tag]) else - new_hash.deep_merge!(tag) - tag.nodes << new_hash + new_node.deep_merge!(tag) + tag.node_list.add(name, new_node) end end end # inherit from node - new_hash.deep_merge!(node) - - # typecast full hash tree to type Node - #new_node.clone_from_plain_hash!(new_hash) - - return new_hash + new_node.deep_merge!(node) + return new_node end # @@ -208,11 +189,11 @@ module LeapCli # def nodes_for_name(name) if node = self.nodes[name] - Config::List.new(node) + Config::ObjectList.new(node) elsif service = self.services[name] - service.nodes + service.node_list elsif tag = self.tags[name] - tag.nodes + tag.node_list end end |