-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
310 additions
and
106 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
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
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 |
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 |
---|---|---|
@@ -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 |
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,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 |
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,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 |
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,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 |
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,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 |
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
Oops, something went wrong.