hiera-puppet 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,7 @@ class Hiera
12
12
  data_class = "data"
13
13
  end
14
14
 
15
- calling_class = scope.real.resource.name.to_s.downcase
15
+ calling_class = scope.resource.name.to_s.downcase
16
16
  calling_module = calling_class.split("::").first
17
17
 
18
18
  hierarchy = Config[:hierarchy] || [calling_class, calling_module]
@@ -34,31 +34,55 @@ class Hiera
34
34
  end
35
35
 
36
36
  def lookup(key, scope, order_override, resolution_type)
37
- answer = nil
37
+ answer = Backend.empty_answer(resolution_type)
38
38
 
39
39
  Hiera.debug("Looking up #{key} in Puppet backend")
40
40
 
41
41
  include_class = Puppet::Parser::Functions.function(:include)
42
- loaded_classes = scope.real.catalog.classes
42
+ loaded_classes = scope.catalog.classes
43
43
 
44
44
  hierarchy(scope, order_override).each do |klass|
45
- unless answer
46
- Hiera.debug("Looking for data in #{klass}")
45
+ Hiera.debug("Looking for data in #{klass}")
47
46
 
48
- varname = [klass, key].join("::")
49
- unless loaded_classes.include?(klass)
50
- begin
47
+ varname = [klass, key].join("::")
48
+ temp_answer = nil
49
+
50
+ unless loaded_classes.include?(klass)
51
+ begin
52
+ if scope.respond_to?(:function_include)
53
+ scope.function_include(klass)
54
+ else
51
55
  scope.real.function_include(klass)
52
- answer = scope[varname]
53
- Hiera.debug("Found data in class #{klass}")
54
- rescue
55
56
  end
57
+
58
+ temp_answer = scope[varname]
59
+ Hiera.debug("Found data in class #{klass}")
60
+ rescue
61
+ end
62
+ else
63
+ temp_answer = scope[varname]
64
+ end
65
+
66
+ next if temp_answer == :undefined
67
+
68
+ if temp_answer
69
+ # for array resolution we just append to the array whatever
70
+ # we find, we then goes onto the next file and keep adding to
71
+ # the array
72
+ #
73
+ # for priority searches we break after the first found data item
74
+ case resolution_type
75
+ when :array
76
+ answer << Backend.parse_answer(temp_answer, scope)
56
77
  else
57
- answer = scope[varname]
78
+ answer = Backend.parse_answer(temp_answer, scope)
79
+ break
58
80
  end
59
81
  end
60
82
  end
61
83
 
84
+ answer = nil if answer == :undefined
85
+
62
86
  answer
63
87
  end
64
88
  end
@@ -17,5 +17,17 @@ class Hiera
17
17
  def include?(key)
18
18
  @real.lookupvar(key) != ""
19
19
  end
20
+
21
+ def catalog
22
+ @real.catalog
23
+ end
24
+
25
+ def resource
26
+ @real.resource
27
+ end
28
+
29
+ def compiler
30
+ @real.compiler
31
+ end
20
32
  end
21
33
  end
@@ -29,7 +29,12 @@ module Puppet::Parser::Functions
29
29
  config[:logger] = "puppet"
30
30
 
31
31
  hiera = Hiera.new(:config => config)
32
- hiera_scope = Hiera::Scope.new(self)
32
+
33
+ if self.respond_to?("[]")
34
+ hiera_scope = self
35
+ else
36
+ hiera_scope = Hiera::Scope.new(self)
37
+ end
33
38
 
34
39
  answer = hiera.lookup(key, default, hiera_scope, override, :priority)
35
40
 
@@ -0,0 +1,36 @@
1
+ module Puppet::Parser::Functions
2
+ newfunction(:hiera_array, :type => :rvalue) do |*args|
3
+ if args[0].is_a?(Array)
4
+ args = args[0]
5
+ end
6
+
7
+ key = args[0] || nil
8
+ default = args[1] || nil
9
+ override = args[2] || nil
10
+
11
+ configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
12
+
13
+ raise(Puppet::ParseError, "Hiera config file #{configfile} not readable") unless File.exist?(configfile)
14
+ raise(Puppet::ParseError, "You need rubygems to use Hiera") unless Puppet.features.rubygems?
15
+
16
+ require 'hiera'
17
+ require 'hiera/scope'
18
+
19
+ config = YAML.load_file(configfile)
20
+ config[:logger] = "puppet"
21
+
22
+ hiera = Hiera.new(:config => config)
23
+
24
+ if self.respond_to?("[]")
25
+ hiera_scope = self
26
+ else
27
+ hiera_scope = Hiera::Scope.new(self)
28
+ end
29
+
30
+ answer = hiera.lookup(key, default, hiera_scope, override, :array)
31
+
32
+ raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?
33
+
34
+ answer
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+ module Puppet::Parser::Functions
2
+ newfunction(:hiera_include) do |*args|
3
+ if args[0].is_a?(Array)
4
+ args = args[0]
5
+ end
6
+
7
+ key = args[0] || nil
8
+ default = args[1] || nil
9
+ override = args[2] || nil
10
+
11
+ configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
12
+
13
+ raise(Puppet::ParseError, "Hiera config file #{configfile} not readable") unless File.exist?(configfile)
14
+ raise(Puppet::ParseError, "You need rubygems to use Hiera") unless Puppet.features.rubygems?
15
+
16
+ require 'hiera'
17
+ require 'hiera/scope'
18
+
19
+ config = YAML.load_file(configfile)
20
+ config[:logger] = "puppet"
21
+
22
+ hiera = Hiera.new(:config => config)
23
+
24
+ if self.respond_to?("[]")
25
+ hiera_scope = self
26
+ else
27
+ hiera_scope = Hiera::Scope.new(self)
28
+ end
29
+
30
+ answer = hiera.lookup(key, default, hiera_scope, override, :array)
31
+
32
+ raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?
33
+
34
+ method = Puppet::Parser::Functions.function(:include)
35
+ send(method, answer)
36
+ end
37
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - R.I.Pienaar
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-06-06 00:00:00 +01:00
17
+ date: 2011-06-12 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -22,11 +22,13 @@ dependencies:
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ">="
25
+ - - ~>
26
26
  - !ruby/object:Gem::Version
27
27
  segments:
28
28
  - 0
29
- version: "0"
29
+ - 2
30
+ - 0
31
+ version: 0.2.0
30
32
  type: :runtime
31
33
  version_requirements: *id001
32
34
  description: Store and query Hiera data from Puppet
@@ -39,8 +41,9 @@ extra_rdoc_files: []
39
41
 
40
42
  files:
41
43
  - bin/extlookup2hiera
44
+ - lib/puppet/parser/functions/hiera_array.rb
42
45
  - lib/puppet/parser/functions/hiera.rb
43
- - lib/hiera/puppet_logger.rb
46
+ - lib/puppet/parser/functions/hiera_include.rb
44
47
  - lib/hiera/scope.rb
45
48
  - lib/hiera/backend/puppet_backend.rb
46
49
  has_rdoc: true
@@ -1,8 +0,0 @@
1
- class Hiera
2
- module Puppet_logger
3
- class << self
4
- def warn(msg); Puppet.notice("hiera(): #{msg}"); end
5
- def debug(msg); Puppet.debug("hiera(): #{msg}"); end
6
- end
7
- end
8
- end