Skip to content

Commit

Permalink
Merge branch 'main' into ruby-3.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaffen authored Apr 25, 2024
2 parents fe6a42b + 7c2ec12 commit 855d811
Show file tree
Hide file tree
Showing 25 changed files with 310 additions and 106 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ production:

global_hiera_yaml: /etc/puppetlabs/puppet/hiera.yaml

base_module_path: "/etc/puppetlabs/puppet/code:/opt/puppetlabs/puppet/modules" # optional, in case you overwrite `basemodulepath` in puppet.conf

ldap: # LDAP User auth
host: 'localhost'
port: 389
Expand Down
4 changes: 2 additions & 2 deletions app/models/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def self.find(name)
all.find { |e| e.name == name }
end

def layers
Layer.all(environment: self)
def layers(key: nil)
Layer.all(environment: self, key:)
end

def find_layer(name:)
Expand Down
26 changes: 11 additions & 15 deletions app/models/hiera_data.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class HieraData
attr_reader :environment, :layers
attr_reader :environment

def self.environments(config_dir:)
Pathname.new(config_dir)
Expand All @@ -10,24 +10,19 @@ def self.environments(config_dir:)

def initialize(environment:)
@environment = environment
@layers = HieraData::Layer.for(environment:)
end

def find_layer(layer_name:)
@layers.find { |l| l.name == layer_name }
end

def find_hierarchy(layer_name:, hierarchy_name:)
find_layer(layer_name:).find_hierarchy(hierarchy_name:)
def layers(key: nil)
HieraData::Layer.for(environment:, key:)
end

def all_keys(facts:)
@layers.flat_map { |l| l.all_keys(facts:) }
.sort.uniq
layers.flat_map { |l| l.all_keys(facts:) }
.sort.uniq
end

def lookup_options_for(key:, facts: {}, decrypt: false)
candidates = lookup_for(facts:, decrypt:)
candidates = lookup_for(key:, facts:, decrypt:)
.lookup("lookup_options", merge_strategy: :hash)
merge = extract_merge_value(key, candidates)
case merge
Expand All @@ -42,15 +37,16 @@ def lookup_options_for(key:, facts: {}, decrypt: false)

def lookup(key:, facts: {}, decrypt: false)
merge_strategy = lookup_options_for(key:, facts:, decrypt:).to_sym
lookup_for(facts:).lookup(key, merge_strategy:)
lookup_for(key:, facts:).lookup(key, merge_strategy:)
end

private

def lookup_for(facts:, decrypt: false)
def lookup_for(key:, facts:, decrypt: false)
@cached_lookups ||= {}
@cached_lookups[facts] ||= begin
hashes = @layers.flat_map { |l| l.file_contents(facts:, decrypt:) }
@cached_lookups[facts] ||= {}
@cached_lookups[facts][key] ||= begin
hashes = layers(key:).flat_map { |l| l.file_contents(facts:, decrypt:) }
Lookup.new(hashes.compact)
end
end
Expand Down
51 changes: 51 additions & 0 deletions app/models/hiera_data/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class HieraData
class Environment
attr_reader :name

def initialize(name:)
@name = name
end

def base_path
@base_path ||=
Pathname.new(config_dir)
.join("environments", @name)
end

def module_path(module_name:)
module_paths
.map { |p| p.join(module_name) }
.find(&:exist?)
end

private

def module_paths
full_modulepath_setting = replace_basemodulepath_in(modulepath_from_config)
full_modulepath_setting.split(":").map do |path|
pathname = Pathname.new(path)
pathname = base_path.join(pathname) if pathname.relative?
pathname
end
end

def modulepath_from_config
config_file = base_path.join("environment.conf")
modulepath_config =
if config_file.exist?
File.read(config_file)
.match(/^modulepath\s*=\s*(.+)$/)&.captures&.first
end
modulepath_config || "modules:$basemodulepath"
end

def replace_basemodulepath_in(string)
basemodulepath = Rails.configuration.hdm.base_module_path || "#{config_dir}/modules:/opt/puppetlabs/puppet/modules"
string.gsub("$basemodulepath", basemodulepath)
end

def config_dir
@config_dir ||= Rails.configuration.hdm.config_dir
end
end
end
81 changes: 2 additions & 79 deletions app/models/hiera_data/layer.rb
Original file line number Diff line number Diff line change
@@ -1,84 +1,7 @@
class HieraData
module Layer
class Base
delegate :hierarchies, to: :config

def present?
File.exist?(hiera_yaml)
end

def all_keys(facts:)
hierarchies.flat_map { |h| h.all_keys(facts:) }.sort.uniq
end

def file_contents(facts:, decrypt: false)
hierarchies.flat_map { |h| h.file_contents(facts:, decrypt:) }
end

private

def config
@config ||= Config.new(hiera_yaml)
end

def find_hierarchy(hierarchy_name:)
config.hierarchies.find { |h| h.name == hierarchy_name }
end

def hiera_yaml
filename = Rails.configuration.hdm.hiera_config_file || "hiera.yaml"
base_path.join(filename)
end
end

class Global < Base
def base_path
hiera_yaml.dirname
end

def name
"global"
end

def to_param
name
end

private

def hiera_yaml
Pathname.new(
Rails.configuration.hdm.global_hiera_yaml || "/etc/puppetlabs/puppet/hiera.yaml"
)
end
end

class Environment < Base
def initialize(environment:)
super()
@environment = environment
end

def base_path
Pathname.new(Rails.configuration.hdm.config_dir)
.join("environments", @environment)
end

def name
"environment"
end

def to_param
name
end
end

class Module < Base
# TODO
end

def self.for(environment:)
[Global.new, Environment.new(environment:)].select(&:present?)
def self.for(environment:, key: nil)
[Global.new, Environment.new(environment:), Module.new(environment:, key:)].select(&:present?)
end
end
end
36 changes: 36 additions & 0 deletions app/models/hiera_data/layer/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class HieraData
module Layer
class Base
delegate :hierarchies, to: :config

def present?
File.exist?(hiera_yaml)
end

def all_keys(facts:)
hierarchies.flat_map { |h| h.all_keys(facts:) }.sort.uniq
end

def file_contents(facts:, decrypt: false)
hierarchies
.flat_map { |h| h.file_contents(facts:, decrypt:) }
.compact
end

def to_param
name
end

private

def config
@config ||= Config.new(hiera_yaml)
end

def hiera_yaml
filename = Rails.configuration.hdm.hiera_config_file || "hiera.yaml"
base_path&.join(filename) || ""
end
end
end
end
16 changes: 16 additions & 0 deletions app/models/hiera_data/layer/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class HieraData
module Layer
class Environment < Base
def initialize(environment:)
super()
@environment = HieraData::Environment.new(name: environment)
end

def base_path = @environment.base_path

def name = "environment"

def description = @environment.name
end
end
end
21 changes: 21 additions & 0 deletions app/models/hiera_data/layer/global.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class HieraData
module Layer
class Global < Base
def base_path
hiera_yaml.dirname
end

def name = "global"

def description = nil

private

def hiera_yaml
Pathname.new(
Rails.configuration.hdm.global_hiera_yaml || "/etc/puppetlabs/puppet/hiera.yaml"
)
end
end
end
end
39 changes: 39 additions & 0 deletions app/models/hiera_data/layer/module.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class HieraData
module Layer
class Module < Base
def initialize(environment:, key:)
super()
@environment = HieraData::Environment.new(name: environment)
@namespace = key&.match(/^(.+?)::/)&.captures&.first
end

def name = "module"

def description = @namespace

def base_path
@environment.module_path(module_name: @namespace)
end

def present?
@namespace && super
end

def all_keys(facts:)
super.select { |k| key_matches_module?(k) }
end

def file_contents(facts:, decrypt: false)
super.map do |hash|
hash.select { |k, _v| key_matches_module?(k) }
end
end

private

def key_matches_module?(key)
key.match(/^#{@namespace}::/)
end
end
end
end
2 changes: 1 addition & 1 deletion app/models/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def ==(other)

def search(environment:)
result = {}
environment.layers.each do |layer|
environment.layers(key: self).each do |layer|
result[layer] = {}
layer.hierarchies.each do |hierarchy|
result[layer][hierarchy] = {}
Expand Down
13 changes: 9 additions & 4 deletions app/models/layer.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
class Layer < HieraModel
attribute :name
attribute :description
attribute :environment
attribute :hiera_layer

def self.all(environment: nil)
hiera_data = environment ? environment.hiera_data : HieraData.new
hiera_data.layers.map do |layer|
def self.all(environment:, key: nil)
environment.hiera_data.layers(key: key&.name).map do |layer|
new(
name: layer.name,
hiera_data:,
description: layer.description,
hiera_data: environment.hiera_data,
hiera_layer: layer,
environment: layer.name == "environment" ? environment : nil
)
Expand All @@ -19,6 +20,10 @@ def hierarchies
Hierarchy.all(layer: self)
end

def name_and_description
"#{name.upcase_first} Layer#{" (#{description})" if description}"
end

def to_param
name
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/files/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<% file_index = 0 %>
<% @files_and_values.each do |layer, hierarchies| %>
<div class="card mb-2">
<div class="card-header">Layer: <%= layer.name %></div>
<div class="card-header"><%= layer.name_and_description %></div>
<div class="card-body">
<% hierarchies.each do |hierarchy, files_and_values| %>
<% hierarchy_index += 1 %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/keys/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
</button>
</div>
<% index = 0 %>
<% @environment.layers.each do |layer| %>
<% @environment.layers(key: @key).each do |layer| %>
<div class="card mb-2">
<div class="card-header">Layer: <%= layer.name %></div>
<div class="card-header"><%= layer.name_and_description %></div>
<div class="card-body">
<% layer.hierarchies.each do |hierarchy| %>
<div id="<%= hierarchy.name.parameterize %>" class="accordion">
Expand Down
Loading

0 comments on commit 855d811

Please sign in to comment.