Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: hiera/puppet-node-classifier.rb

Issue 4810150141493248: Issue 122 - Puppet ENC via Hiera (Closed)
Patch Set: Puppet ENC via Hiera Created March 4, 2015, 5:59 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env ruby
2 # This script is a node classifier for Puppet that operates on top of Hiera
3 # and uses a custom hosts.yaml config to map host roles.
4
5 require 'getoptlong'
6 require 'hiera'
7 require 'socket'
8 require 'yaml'
9
10 # Where to search for the Hiera configuration
11 HIERA_CONFIG = ENV.fetch('PUPPET_HIERA_CONFIG', '/etc/puppet/hiera.yaml')
12 # Where to search for the Hosts configuration
13 HOSTS_CONFIG = ENV.fetch('PUPPET_HOSTS_CONFIG', '/etc/puppet/infrastructure/hier a/private/hosts.yaml')
14
15 # The current script name, used for logging and usage hints
Felix Dahlke 2015/03/09 23:14:51 That seems a bit too obvious for a comment IMO. Co
mathias 2015/03/16 12:07:11 Done.
16 BASENAME = File.basename($0)
17
18 # Although there's no need for any options in particular yet, the script
Felix Dahlke 2015/03/09 23:14:51 IMO also too obvious. It's a no-brainer that --hel
mathias 2015/03/16 12:07:11 Done.
19 # still recognizes a --help option, which is considered the bare minimum by
20 # convention. Nonetheless, future options may become integrated here (see
21 # also http://ruby-doc.org/stdlib/libdoc/getoptlong/rdoc/GetoptLong.html):
22 GetoptLong.new(
23 ['--help', '-h', GetoptLong::NO_ARGUMENT]
24 ).each do |opt, arg|
25 case opt
26
27 when '--help'
28 puts <<-END
29 Usage: #{BASENAME} [hostname]
30 #{BASENAME} example.com
31 #{BASENAME} --help
32
33 Options:
34
35 --help, -h
36 Display this help message and exit gracefully.
37
38 Environment:
39
40 PUPPET_HIERA_CONFIG=#{HIERA_CONFIG}
41 Where to find the hiera configuration file.
42 PUPPET_HOSTS_CONFIG=#{HOSTS_CONFIG}
43 Where to find the hosts configuration file.
44
45 END
46 exit 0
47
48 end
49 end
50
51 # Only one additional non-option argument is allowed, in order to explicitly
Felix Dahlke 2015/03/09 23:14:51 Also pretty obvious from the code IMO.
mathias 2015/03/16 12:07:11 Although I've improved some of the comments you've
52 # specify a hostname to use instead of the default:
53 case ARGV.length
54 when 0
55 hostname = Socket.gethostname
56 when 1
57 hostname = ARGV[0][/^[^.]+/]
58 else
59 STDERR.puts <<-END
60 #{BASENAME}: unknown option: #{ARGV[0]}
61 #{BASENAME}: try #{BASENAME} --help
62 END
63 exit 1
64 end
65
66 # Extract the server -> hostname -> role information from the hosts
67 # configuration file:
68 begin
69 config = YAML.load_file(HOSTS_CONFIG)
70 servers = config.fetch("servers", {})
71 host = servers.fetch(hostname, {})
72 role = host.fetch("role", "default")
73 rescue Exception => error
74 STDERR.puts "#{BASENAME}: #{error.message}: #{HOSTS_CONFIG}"
75 exit 1
76 end
77
78 # Map Hiera data into the structure Puppet expects an ENC to generate (see
79 # https://docs.puppetlabs.com/guides/external_nodes.html for more info):
80 begin
81 hiera = Hiera.new(:config => HIERA_CONFIG)
82 scope = {'::hostname' => hostname, '::role' => role}
83 classes = hiera.lookup('classes', {}, scope, nil, :hash)
84 parameters = hiera.lookup('parameters', {}, scope, nil, :hash)
85 parameters['role'] = role
86 result = { 'classes' => classes, 'parameters' => parameters }
87 rescue Exception => error
88 STDERR.puts "#{BASENAME}: #{error.message}: #{HIERA_CONFIG}"
89 exit 1
90 end
91
92 puts result.to_yaml
93
OLDNEW

Powered by Google App Engine
This is Rietveld