Class: HieraTemplate
- Inherits:
-
Object
- Object
- HieraTemplate
- Defined in:
- lib/hiera_template.rb
Overview
Templating handler object used by ERBReflective
Constant Summary collapse
- DEFAULT_CONFIG =
'/etc/puppet/hiera.yaml'
Instance Method Summary collapse
- #custom_file(path, owner: nil, group: nil, mode: nil) ⇒ Object
-
#custom_out(&block) ⇒ Object
Methods used by ERBReflective to access hiera data See above, full content is not available until the template is rendered so we only stash the block references that we are given and call it later.
-
#hiera(key, default = nil) ⇒ Object
If hiera does not exist we will try facter data in scope to unify the API.
- #hiera_array(key, default = nil) ⇒ Object
- #hiera_hash(key, default = nil) ⇒ Object
-
#initialize(hiera_config = DEFAULT_CONFIG) ⇒ HieraTemplate
constructor
A new instance of HieraTemplate.
- #mkdir_p(path, owner: nil, group: nil, mode: nil) ⇒ Object
-
#render(content) ⇒ Object
Template render.
Constructor Details
#initialize(hiera_config = DEFAULT_CONFIG) ⇒ HieraTemplate
Returns a new instance of HieraTemplate.
13 14 15 16 17 18 19 |
# File 'lib/hiera_template.rb', line 13 def initialize(hiera_config = DEFAULT_CONFIG) @scope = Facter.to_hash # Time consuming, do it once if possible! # Just so we can handle old facter references starting :: legacy = @scope.map { |k, v| ["::#{k}", v] } @scope.merge!(Hash[legacy]) @hiera = Hiera.new(config: hiera_config) end |
Instance Method Details
#custom_file(path, owner: nil, group: nil, mode: nil) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/hiera_template.rb', line 64 def custom_file(path, owner: nil, group: nil, mode: nil) custom_out do |content| File.write(path, content) FileUtils.chown(owner, group, path) if owner || group FileUtils.chmod(mode, path) if mode end end |
#custom_out(&block) ⇒ Object
Methods used by ERBReflective to access hiera data See above, full content is not available until the template is rendered so we only stash the block references that we are given and call it later
35 36 37 38 39 |
# File 'lib/hiera_template.rb', line 35 def custom_out(&block) raise StandardError, 'custom_out requires a block argument' unless block_given? @blocks << block end |
#hiera(key, default = nil) ⇒ Object
If hiera does not exist we will try facter data in scope to unify the API
44 45 46 |
# File 'lib/hiera_template.rb', line 44 def hiera(key, default = nil) @hiera.lookup(key, nil, @scope) || @scope.dig(*key.split('.')) || default end |
#hiera_array(key, default = nil) ⇒ Object
52 53 54 |
# File 'lib/hiera_template.rb', line 52 def hiera_array(key, default = nil) @hiera.lookup(key, default, @scope, nil, :array) end |
#hiera_hash(key, default = nil) ⇒ Object
48 49 50 |
# File 'lib/hiera_template.rb', line 48 def hiera_hash(key, default = nil) @hiera.lookup(key, default, @scope, nil, :hash) end |
#mkdir_p(path, owner: nil, group: nil, mode: nil) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/hiera_template.rb', line 56 def mkdir_p(path, owner: nil, group: nil, mode: nil) custom_out do |_| FileUtils.mkdir_p(path) FileUtils.chown_R(owner, group, path) if owner || group FileUtils.chmod_R(mode, path) if mode end end |
#render(content) ⇒ Object
Template render
22 23 24 25 26 27 28 29 30 |
# File 'lib/hiera_template.rb', line 22 def render(content) @blocks = [] rendered = ERBReflective.new(content, nil, '-').result(self) return rendered unless @blocks.any? # See custom_out @blocks.each { |block| block.call(rendered) } true end |