Class: Puppet::Util::Hiera::ModuleLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/util/hiera/module_loader.rb

Constant Summary collapse

@@undef_new =
1

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.compilerObject (readonly)

Returns the value of attribute compiler.



18
19
20
# File 'lib/puppet/util/hiera/module_loader.rb', line 18

def compiler
  @compiler
end

.hiera_areas_nameObject (readonly)

Returns the value of attribute hiera_areas_name.



21
22
23
# File 'lib/puppet/util/hiera/module_loader.rb', line 21

def hiera_areas_name
  @hiera_areas_name
end

.hiera_class_params_nameObject (readonly)

Returns the value of attribute hiera_class_params_name.



24
25
26
# File 'lib/puppet/util/hiera/module_loader.rb', line 24

def hiera_class_params_name
  @hiera_class_params_name
end

.hiera_collection_nameObject (readonly)

Returns the value of attribute hiera_collection_name.



29
30
31
# File 'lib/puppet/util/hiera/module_loader.rb', line 29

def hiera_collection_name
  @hiera_collection_name
end

.hiera_default_mapping_nameObject (readonly)

Returns the value of attribute hiera_default_mapping_name.



26
27
28
# File 'lib/puppet/util/hiera/module_loader.rb', line 26

def hiera_default_mapping_name
  @hiera_default_mapping_name
end

.hiera_default_subresources_nameObject (readonly)

Returns the value of attribute hiera_default_subresources_name.



28
29
30
# File 'lib/puppet/util/hiera/module_loader.rb', line 28

def hiera_default_subresources_name
  @hiera_default_subresources_name
end

.hiera_defaults_nameObject (readonly)

Returns the value of attribute hiera_defaults_name.



25
26
27
# File 'lib/puppet/util/hiera/module_loader.rb', line 25

def hiera_defaults_name
  @hiera_defaults_name
end

.hiera_require_nameObject (readonly)

Returns the value of attribute hiera_require_name.



22
23
24
# File 'lib/puppet/util/hiera/module_loader.rb', line 22

def hiera_require_name
  @hiera_require_name
end

.hiera_resources_nameObject (readonly)

Returns the value of attribute hiera_resources_name.



23
24
25
# File 'lib/puppet/util/hiera/module_loader.rb', line 23

def hiera_resources_name
  @hiera_resources_name
end

.hiera_scopeObject (readonly)

Returns the value of attribute hiera_scope.



19
20
21
# File 'lib/puppet/util/hiera/module_loader.rb', line 19

def hiera_scope
  @hiera_scope
end

.hiera_subresources_nameObject (readonly)

Returns the value of attribute hiera_subresources_name.



27
28
29
# File 'lib/puppet/util/hiera/module_loader.rb', line 27

def hiera_subresources_name
  @hiera_subresources_name
end

.scopeObject (readonly)

Returns the value of attribute scope.



17
18
19
# File 'lib/puppet/util/hiera/module_loader.rb', line 17

def scope
  @scope
end

Class Method Details

.create_anchor(name) ⇒ Object



157
158
159
160
161
# File 'lib/puppet/util/hiera/module_loader.rb', line 157

def create_anchor(name)
  debug("create_anchor: #{name}")
  @scope.function_create_resources(['anchor', { name => {} }])
  return "Anchor[#{name}]"
end

.create_resource(res_type, res_name, res_data, res_defaults) ⇒ Object



106
107
108
109
# File 'lib/puppet/util/hiera/module_loader.rb', line 106

def create_resource(res_type, res_name, res_data, res_defaults)
  debug("create_resource: #{res_type}[#{res_name}] (#{res_data.inspect}) / #{res_defaults.inspect}")
  @scope.function_create_resources([res_type, { res_name => res_data }, res_defaults])
end

.debug(msg) ⇒ Object



128
129
130
# File 'lib/puppet/util/hiera/module_loader.rb', line 128

def debug(msg)
  Puppet.debug("hiera_module() #{@hiera_scope.get_hiera_name()}: #{msg}")
end

.function_hiera(key, default, override = nil, type = :priority) ⇒ Object



51
52
53
# File 'lib/puppet/util/hiera/module_loader.rb', line 51

def function_hiera(key, default, override=nil, type=:priority)
  HieraPuppet.lookup(key, default, @hiera_scope, override, type)
end

.get_stages(modulestoload = {}) ⇒ Object



163
164
165
# File 'lib/puppet/util/hiera/module_loader.rb', line 163

def get_stages(modulestoload={})
  hiera_lookup_array(@hiera_stages_name, ['default'])
end

.hiera_lookup(lookup, default, override, type, add_sub) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puppet/util/hiera/module_loader.rb', line 68

def hiera_lookup(lookup, default, override, type, add_sub)
  if add_sub and type == :hash
    res = {}
    
    @hiera_scope.get_hiera_lookup_names(lookup).reverse.each do |l|
      data = HieraPuppet.lookup(l, default, @hiera_scope,
          override, type)
      debug("lookup parents #{l}: #{data.inspect}")
      merge_answer(res, data)
    end
    return res
  else
    HieraPuppet.lookup(@hiera_scope.get_hiera_lookup_name(lookup),
        default, @hiera_scope, override, type)
  end
end

.hiera_lookup_array(lookup, default, override = nil) ⇒ Object



85
86
87
# File 'lib/puppet/util/hiera/module_loader.rb', line 85

def hiera_lookup_array(lookup, default, override=nil)
  hiera_lookup(lookup, default, override, :array, false)
end

.hiera_lookup_hash(lookup, default, add_sub = false, override = nil) ⇒ Object



89
90
91
# File 'lib/puppet/util/hiera/module_loader.rb', line 89

def hiera_lookup_hash(lookup, default, add_sub=false, override=nil)
  hiera_lookup(lookup, default, override, :hash, add_sub)
end

.init(scope, compiler) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/puppet/util/hiera/module_loader.rb', line 31

def init(scope, compiler)
  @modules = {}
  @to_load_modules = []
  @scope = scope
  @compiler = compiler
  @hiera_scope = Scope.new(scope)

  @hiera_modules_name = function_hiera('hiera::modules_name', 'modules')
  @hiera_require_name = function_hiera('hiera::require_name', 'require')
  @hiera_areas_name = function_hiera('hiera::areas_name', 'areas')
  @hiera_stages_name = function_hiera('hiera::stages_name', 'stages')
  @hiera_resources_name = function_hiera('hiera::resources_name', 'resources')
  @hiera_subresources_name = function_hiera('hiera::subresources_name', 'subresources')
  @hiera_default_subresources_name = function_hiera('hiera::default_subresources_name', 'default_subresources')
  @hiera_defaults_name = function_hiera('hiera::defaults_name', 'defaults')
  @hiera_class_params_name = function_hiera('hiera::class_params_name', 'class_params')
  @hiera_default_mapping_name = function_hiera('hiera::default_mapping_name', 'default_mapping')
  @hiera_collection_name = function_hiera('hiera::collection_name', 'collection')
end

.load(modulestoload = {}) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/puppet/util/hiera/module_loader.rb', line 176

def load(modulestoload={})
  start_anchor = create_anchor('start')
  get_stages().each do |stage|
    debug("Load stage: #{stage}.")
    start_anchor = load_stage(stage, start_anchor, modulestoload[stage])
  end
  res = {}
  @modules.each do |k,v|
    res[k] = 1
  end
  debug("Loaded modules: #{res.inspect}")
  return res
end

.load_class(name, params, lazy = false) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/puppet/util/hiera/module_loader.rb', line 93

def load_class(name, params, lazy=false)
  classes = @compiler.evaluate_classes({name => params}, @scope, lazy)
  if classes.empty?
    # Throw an error if we didn't evaluate all of the classes.
    str = "Could not find class #{name}"

    if n = @scope.namespaces and ! n.empty? and n != [""]
      str += " in namespaces #{n.join(", ")}"
    end
    self.fail Puppet::ParseError, str
  end
end

.load_modules(to_load_modules, start_anchor, end_anchor) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/puppet/util/hiera/module_loader.rb', line 137

def load_modules(to_load_modules, start_anchor, end_anchor)
  to_load_modules.map!{|name| modules(name)}.reject!{|ob| ob.loaded?}
  while not to_load_modules.empty?
    moduleob = to_load_modules.shift
    next if moduleob.loaded?
    @hiera_scope.hiera_module = moduleob
    debug("Trying to load module: #{moduleob.name}")
    required = moduleob.get_required_unloaded_modules()
    if not required.empty?
      debug("Unmet required modules: #{required.map{|ob| ob.name}.inspect}, stoping loading #{moduleob.name}")
      @hiera_scope.hiera_module = nil
      to_load_modules.unshift(moduleob)
      to_load_modules.unshift(*required)
      next
    end
    moduleob.load(start_anchor, end_anchor)
    @hiera_scope.hiera_module = nil
  end
end

.load_stage(name, start_anchor, modulestoload = []) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/puppet/util/hiera/module_loader.rb', line 167

def load_stage(name, start_anchor, modulestoload = [])
  modulestoload ||= []
  @hiera_scope.stage_name = name
  end_anchor = create_anchor("stage_#{name}_end")
  load_modules(modulestoload + hiera_lookup_array(@hiera_modules_name, []), start_anchor, end_anchor)
  @hiera_scope.stage_name = ''
  return end_anchor
end

.merge_answer(left, right) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet/util/hiera/module_loader.rb', line 57

def merge_answer(left,right)
  case Hiera::Config[:merge_behavior]
  when :deeper,'deeper'
    left.deep_merge!(right)
  when :deep,'deep'
    left.deep_merge(right)
  else # Native and undefined
    left.merge(right)
  end
end

.modules(name) ⇒ Object



132
133
134
135
# File 'lib/puppet/util/hiera/module_loader.rb', line 132

def modules(name)
  return @modules[name] if @modules[name]
  @modules[name] = Module.new(self, name)
end

.override_resource(res_type, res_name, res_data) ⇒ Object



111
112
113
114
# File 'lib/puppet/util/hiera/module_loader.rb', line 111

def override_resource(res_type, res_name, res_data)
  debug("override_resource: #{res_type}[#{res_name}] (#{res_data.inspect})")
  @scope.function_override_resource([res_type, res_name, res_data])
end

.realize_resources(query, res_type, type) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/puppet/util/hiera/module_loader.rb', line 116

def realize_resources(query, res_type, type)
  case type
  when "exported"
    debug("realize_resources: #{res_type.split("::").collect { |s| s.capitalize }.join("::")} <<| #{query} |>>")
  when "virtual"
    debug("realize_resources: #{res_type.split("::").collect { |s| s.capitalize }.join("::")} <| #{query} |>")
  else
    debug("realize_resources: Unknown Collection query type #{type}")
  end
  @scope.function_realize_resources([query, res_type, type])
end