From f1a1bb85d09aba8657ffe99c820f37bbc7f78140 Mon Sep 17 00:00:00 2001 From: John Bond Date: Wed, 28 Apr 2021 21:31:14 +0200 Subject: [PATCH] (wip) some patches to get things working with puppet-unbound --- lib/puppet-strings/hiera/data.rb | 71 ++++++++++++++----- .../hiera/hierarchy_data_path.rb | 19 +++-- lib/puppet-strings/markdown/puppet_class.rb | 11 ++- .../templates/classes_and_defines.erb | 2 +- 4 files changed, 71 insertions(+), 32 deletions(-) diff --git a/lib/puppet-strings/hiera/data.rb b/lib/puppet-strings/hiera/data.rb index 3ce9a75f6..b4e6b9b70 100644 --- a/lib/puppet-strings/hiera/data.rb +++ b/lib/puppet-strings/hiera/data.rb @@ -2,11 +2,13 @@ module PuppetStrings::Hiera class Data - attr_reader :config_path, :data_paths + attr_reader :config_path, :interpolated_paths, :uninterpolated_paths, :defaults def initialize(config_path) @config_path = config_path - @data_paths = [] + @interpolated_paths = [] + # This will probably always be ['common.yaml'] jut make it an array just incase + @uninterpolated_paths = [] load_config end @@ -15,7 +17,7 @@ def files @files ||= begin result = {} - data_paths.each do |dp| + interpolated_paths.each do |dp| dp.matches.each do |file, interpolations| unless result.key?(file) result[file] = interpolations @@ -32,7 +34,6 @@ def files def overrides @overrides ||= begin overrides = {} - files.each_key do |file| data = YAML.load(File.read(file)) data.each do |key, value| @@ -40,22 +41,35 @@ def overrides overrides[key][file] = value end end - overrides end end + # @return [Hash[String, Hash[String, Any]]] + # Full variable (class::var) -> filename: value + def defaults + @defaults ||= begin + defaults = {} + uninterpolated_paths.each do |file| + data = YAML.load(File.read(file)) + data.each do |key, value| + defaults[key] = value.nil? ? 'undef' : value.inspect + end + end + defaults + end + end + # @return [Hash[String, Hash[String, Any]]] # variable -> filename: value - def for_class(class_name) - result = {} - overrides.each do |key, value| - override_class_name, _, variable = key.rpartition('::') - if override_class_name == class_name - result[variable] = value - end - end - result + def overrides_for_class(class_name) + filter_mappings(class_name, overrides) + end + + # @return [Hash[String, Hash[String, Any]]] + # variable -> filename: value + def defaults_for_class(class_name) + filter_mappings(class_name, defaults) end def to_s @@ -64,13 +78,28 @@ def to_s private + # @return [Hash[String, Hash[String, Any]]] + # variable -> filename: value + def filter_mappings(class_name, mappings) + result = {} + mappings.each do |key, value| + mapped_class_name, _, variable = key.rpartition('::') + if mapped_class_name == class_name + result[variable] = value + end + end + result + end + + # TODO: this should be a class method not an instance method def load_config return unless File.exist?(config_path) config = YAML.load(File.read(config_path)) unless config['version'] == 5 - raise "Unsupported version '#{config['version']}'" + log.warn("Unsupported version '#{config['version']}'") + return end hierarchy = config['hierarchy'] @@ -83,10 +112,18 @@ def load_config datadir = level['datadir'] || config['defaults']['datadir'] if level['path'] - data_paths << PuppetStrings::Hiera::HierarchyDataPath.new(datadir, level['path']) + if level['path'] =~ /%{[^}]+}/ + interpolated_paths << PuppetStrings::Hiera::HierarchyDataPath.new(datadir, level['path']) + else + uninterpolated_paths << File.join(datadir, level['path']) + end elsif level['paths'] level['paths'].each do |path| - data_paths << PuppetStrings::Hiera::HierarchyDataPath.new(datadir, path) + if path =~ /%{[^}]+}/ + interpolated_paths << PuppetStrings::Hiera::HierarchyDataPath.new(datadir, path) + else + uninterpolated_paths << File.join(datadir, path) + end end end end diff --git a/lib/puppet-strings/hiera/hierarchy_data_path.rb b/lib/puppet-strings/hiera/hierarchy_data_path.rb index 93b7eaa56..2639e23af 100644 --- a/lib/puppet-strings/hiera/hierarchy_data_path.rb +++ b/lib/puppet-strings/hiera/hierarchy_data_path.rb @@ -13,20 +13,17 @@ def initialize(datadir, path) def matches result = {} - Dir.chdir(datadir) do - Dir['**'].each do |entry| - next unless File.file?(entry) + Dir.glob(File.join(datadir, '**', '*.yaml')).each do |entry| + next unless File.file?(entry) - regex.match(entry) do |match| - full_path = File.join(datadir, entry) - interpolations = {} + regex.match(entry) do |match| + interpolations = {} - mapping.each do |name, interpolation| - interpolations[interpolation] = match.named_captures[name] - end - - result[full_path] = interpolations + mapping.each do |name, interpolation| + interpolations[interpolation] = match.named_captures[name] end + + result[entry] = interpolations end end diff --git a/lib/puppet-strings/markdown/puppet_class.rb b/lib/puppet-strings/markdown/puppet_class.rb index 7b36c06b0..33b866cd1 100644 --- a/lib/puppet-strings/markdown/puppet_class.rb +++ b/lib/puppet-strings/markdown/puppet_class.rb @@ -7,18 +7,19 @@ class PuppetClass < Base def initialize(registry) @template = 'classes_and_defines.erb' super(registry, 'class') + @hiera = PuppetStrings::Hiera.load_config + update_defauls end def hiera_overrides @hiera_overrides ||= begin - hiera = PuppetStrings::Hiera.load_config - overrides = hiera.for_class(name) + overrides = @hiera.overrides_for_class(name) result = {} overrides.each do |variable, files| result[variable] = files.map do |filename, value| - interpolations = hiera.files[filename] + interpolations = @hiera.files[filename] [filename, interpolations, value] end end @@ -27,6 +28,10 @@ def hiera_overrides end end + def update_defauls + @registry[:defaults].merge!(@hiera.defaults_for_class(name)) + end + def render super(@template) end diff --git a/lib/puppet-strings/markdown/templates/classes_and_defines.erb b/lib/puppet-strings/markdown/templates/classes_and_defines.erb index 57998235d..180fea9bb 100644 --- a/lib/puppet-strings/markdown/templates/classes_and_defines.erb +++ b/lib/puppet-strings/markdown/templates/classes_and_defines.erb @@ -82,7 +82,7 @@ Options: Default value: `<%= value_string(defaults[param[:name]]) %>` <% end -%> -<% if hiera_overrides[param[:name]] -%> +<% if !hiera_overrides.empty? && hiera_overrides[param[:name]] -%>
Hiera overrides in a detailed table