Skip to content

Commit

Permalink
(wip) some patches to get things working with puppet-unbound
Browse files Browse the repository at this point in the history
  • Loading branch information
b4ldr committed Apr 29, 2021
1 parent 2340d1b commit f1a1bb8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 32 deletions.
71 changes: 54 additions & 17 deletions lib/puppet-strings/hiera/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -32,30 +34,42 @@ def files
def overrides
@overrides ||= begin
overrides = {}

files.each_key do |file|
data = YAML.load(File.read(file))
data.each do |key, value|
overrides[key] ||= {}
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
Expand All @@ -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']
Expand All @@ -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
Expand Down
19 changes: 8 additions & 11 deletions lib/puppet-strings/hiera/hierarchy_data_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 8 additions & 3 deletions lib/puppet-strings/markdown/puppet_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]] -%>
<details>
<summary>Hiera overrides in a detailed table</summary>

Expand Down

0 comments on commit f1a1bb8

Please sign in to comment.