-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It is possible to explode the Hiera overrides and generate a table in the reference.
- Loading branch information
Showing
8 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module PuppetStrings | ||
module Hiera | ||
require_relative 'hiera/hierarchy_data_path' | ||
require_relative 'hiera/data' | ||
|
||
def self.load_config | ||
PuppetStrings::Hiera::Data.new('hiera.yaml') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# frozen_string_literal: true | ||
|
||
module PuppetStrings::Hiera | ||
class Data | ||
attr_reader :config_path, :data_paths | ||
|
||
def initialize(config_path) | ||
@config_path = config_path | ||
@data_paths = [] | ||
|
||
load_config | ||
end | ||
|
||
def files | ||
@files ||= begin | ||
result = {} | ||
|
||
data_paths.each do |dp| | ||
dp.matches.each do |file, interpolations| | ||
unless result.key?(file) | ||
result[file] = interpolations | ||
end | ||
end | ||
end | ||
|
||
result | ||
end | ||
end | ||
|
||
# @return [Hash[String, Hash[String, Any]]] | ||
# Full variable (class::var) -> filename: value | ||
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]]] | ||
# 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 | ||
end | ||
|
||
def to_s | ||
config_path | ||
end | ||
|
||
private | ||
|
||
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']}'" | ||
end | ||
|
||
hierarchy = config['hierarchy'] | ||
return unless hierarchy | ||
|
||
hierarchy.each do |level| | ||
data_hash = level['data_hash'] || config['defaults']['data_hash'] | ||
next unless data_hash == 'yaml_data' | ||
|
||
datadir = level['datadir'] || config['defaults']['datadir'] | ||
|
||
if level['path'] | ||
data_paths << PuppetStrings::Hiera::HierarchyDataPath.new(datadir, level['path']) | ||
elsif level['paths'] | ||
level['paths'].each do |path| | ||
data_paths << PuppetStrings::Hiera::HierarchyDataPath.new(datadir, path) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
module PuppetStrings::Hiera | ||
class HierarchyDataPath | ||
attr_reader :datadir, :path, :regex, :mapping | ||
|
||
def initialize(datadir, path) | ||
@datadir = datadir | ||
@path = path | ||
@regex, @mapping = HierarchyDataPath.path2regex(path) | ||
end | ||
|
||
def matches | ||
result = {} | ||
|
||
Dir.chdir(datadir) do | ||
Dir['**'].each do |entry| | ||
next unless File.file?(entry) | ||
|
||
regex.match(entry) do |match| | ||
full_path = File.join(datadir, entry) | ||
interpolations = {} | ||
|
||
mapping.each do |name, interpolation| | ||
interpolations[interpolation] = match.named_captures[name] | ||
end | ||
|
||
result[full_path] = interpolations | ||
end | ||
end | ||
end | ||
|
||
result | ||
end | ||
|
||
def self.path2regex(path) | ||
mapping = {} | ||
|
||
intermediate_result = path | ||
|
||
# First pass - intermediate replacements | ||
path.scan(/%{[^}]+}/).each_with_index do |interpolation, i| | ||
replacement = "X_INTERPOLATION_#{i}_X" | ||
mapping[replacement] = interpolation[2..-2] | ||
intermediate_result = intermediate_result.sub(interpolation, replacement) | ||
end | ||
|
||
# Second pass - escape any special chars | ||
escaped = Regexp.escape(intermediate_result) | ||
|
||
# Third pass - replacement intermediates with regex | ||
mapping.each_key do |replacement| | ||
escaped = escaped.sub(replacement, "(?<#{replacement}>.+)") | ||
end | ||
|
||
[Regexp.new(escaped), mapping] | ||
end | ||
|
||
def to_s | ||
File.join(datadir, path) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters