hiera-puppet 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'csv'
5
+
6
+ options = {:in => nil, :out => nil, :format => :yaml}
7
+
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Converter for extlookup CSV files into Hiera JSON and YAML files"
10
+
11
+ opts.on("--in FILE", "-i", "Input CSV file") do |v|
12
+ options[:in] = v
13
+ end
14
+
15
+ opts.on("--out FILE", "-o", "Output Hiera file") do |v|
16
+ options[:out] = v
17
+ end
18
+
19
+ opts.on("--json", "-j", "Create JSON format file") do |v|
20
+ options[:format] = :json
21
+ end
22
+ end.parse!
23
+
24
+ if options[:in].nil? || options[:out].nil?
25
+ STDERR.puts "Please specify an input and output file with --in and --out"
26
+ exit 1
27
+ end
28
+
29
+ unless File.exist?(options[:in])
30
+ STDERR.puts "Cannot find input file #{options[:in]}"
31
+ exit 1
32
+ end
33
+
34
+ csvdata = CSV.read(options[:in])
35
+ hieradata = {}
36
+
37
+ csvdata.each do |d|
38
+ d = d.map{|item| item.to_s}
39
+
40
+ if d.size > 2
41
+ hieradata[d[0]] = d[1, d.size].flatten
42
+ else
43
+ hieradata[d[0]] = d[1]
44
+ end
45
+ end
46
+
47
+ case options[:format]
48
+ when :yaml
49
+ require 'yaml'
50
+ File.open(options[:out], "w") {|f| f.write hieradata.to_yaml}
51
+
52
+ when :json
53
+ require 'rubygems'
54
+ require 'json'
55
+ File.open(options[:out], "w") {|f| f.write JSON.pretty_generate hieradata}
56
+ end
@@ -0,0 +1,66 @@
1
+ class Hiera
2
+ module Backend
3
+ class Puppet_backend
4
+ def initialize
5
+ Hiera.debug("Hiera Puppet backend starting")
6
+ end
7
+
8
+ def hierarchy(scope, override)
9
+ begin
10
+ data_class = Config[:puppet][:datasource] || "data"
11
+ rescue
12
+ data_class = "data"
13
+ end
14
+
15
+ calling_class = scope.real.resource.name.to_s.downcase
16
+ calling_module = calling_class.split("::").first
17
+
18
+ hierarchy = Config[:hierarchy] || [calling_class, calling_module]
19
+
20
+ hierarchy = [hierarchy].flatten.map do |klass|
21
+ klass = Backend.parse_string(klass, scope, {"calling_class" => calling_class, "calling_module" => calling_module})
22
+
23
+ next if klass == ""
24
+
25
+ [data_class, klass].join("::")
26
+ end.compact
27
+
28
+ hierarchy << [calling_class, data_class].join("::")
29
+ hierarchy << [calling_module, data_class].join("::") unless calling_module == calling_class
30
+
31
+ hierarchy.insert(0, [data_class, override].join("::")) if override
32
+
33
+ hierarchy
34
+ end
35
+
36
+ def lookup(key, scope, order_override, resolution_type)
37
+ answer = nil
38
+
39
+ Hiera.debug("Looking up #{key} in Puppet backend")
40
+
41
+ include_class = Puppet::Parser::Functions.function(:include)
42
+ loaded_classes = scope.real.catalog.classes
43
+
44
+ hierarchy(scope, order_override).each do |klass|
45
+ unless answer
46
+ Hiera.debug("Looking for data in #{klass}")
47
+
48
+ varname = [klass, key].join("::")
49
+ unless loaded_classes.include?(klass)
50
+ begin
51
+ scope.real.function_include(klass)
52
+ answer = scope[varname]
53
+ Hiera.debug("Found data in class #{klass}")
54
+ rescue
55
+ end
56
+ else
57
+ answer = scope[varname]
58
+ end
59
+ end
60
+ end
61
+
62
+ answer
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,21 @@
1
+ class Hiera
2
+ class Scope
3
+ attr_reader :real
4
+
5
+ def initialize(real)
6
+ @real = real
7
+ end
8
+
9
+ def [](key)
10
+ ans = @real.lookupvar(key)
11
+
12
+ # damn you puppet visual basic style variables.
13
+ return nil if ans == ""
14
+ return ans
15
+ end
16
+
17
+ def include?(key)
18
+ @real.lookupvar(key) != ""
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,40 @@
1
+ module Puppet::Parser::Functions
2
+ newfunction(:hiera, :type => :rvalue) do |*args|
3
+ # Functions called from puppet manifests that look like this:
4
+ # lookup("foo", "bar")
5
+ # internally in puppet are invoked: func(["foo", "bar"])
6
+ #
7
+ # where as calling from templates should work like this:
8
+ # scope.function_lookup("foo", "bar")
9
+ #
10
+ # Therefore, declare this function with args '*args' to accept any number
11
+ # of arguments and deal with puppet's special calling mechanism now:
12
+ if args[0].is_a?(Array)
13
+ args = args[0]
14
+ end
15
+
16
+ key = args[0] || nil
17
+ default = args[1] || nil
18
+ override = args[2] || nil
19
+
20
+ configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
21
+
22
+ raise(Puppet::ParseError, "Hiera config file #{configfile} not readable") unless File.exist?(configfile)
23
+ raise(Puppet::ParseError, "You need rubygems to use Hiera") unless Puppet.features.rubygems?
24
+
25
+ require 'hiera'
26
+ require 'hiera/scope'
27
+
28
+ config = YAML.load_file(configfile)
29
+ config[:logger] = "puppet"
30
+
31
+ hiera = Hiera.new(:config => config)
32
+ hiera_scope = Hiera::Scope.new(self)
33
+
34
+ answer = hiera.lookup(key, default, hiera_scope, override, :priority)
35
+
36
+ raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") unless answer
37
+
38
+ return answer
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hiera-puppet
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - R.I.Pienaar
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-06-06 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: hiera
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ description: Store and query Hiera data from Puppet
33
+ email: rip@devco.net
34
+ executables:
35
+ - extlookup2hiera
36
+ extensions: []
37
+
38
+ extra_rdoc_files: []
39
+
40
+ files:
41
+ - bin/extlookup2hiera
42
+ - lib/puppet/parser/functions/hiera.rb
43
+ - lib/hiera/puppet_logger.rb
44
+ - lib/hiera/scope.rb
45
+ - lib/hiera/backend/puppet_backend.rb
46
+ has_rdoc: true
47
+ homepage: https://github.com/ripienaar/hiera-puppet
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.6
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Puppet query interface and backend for Hiera
76
+ test_files: []
77
+