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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: hiera/puppet-node-classifier.rb
===================================================================
new file mode 100755
--- /dev/null
+++ b/hiera/puppet-node-classifier.rb
@@ -0,0 +1,93 @@
+#!/usr/bin/env ruby
+# This script is a node classifier for Puppet that operates on top of Hiera
+# and uses a custom hosts.yaml config to map host roles.
+
+require 'getoptlong'
+require 'hiera'
+require 'socket'
+require 'yaml'
+
+# Where to search for the Hiera configuration
+HIERA_CONFIG = ENV.fetch('PUPPET_HIERA_CONFIG', '/etc/puppet/hiera.yaml')
+# Where to search for the Hosts configuration
+HOSTS_CONFIG = ENV.fetch('PUPPET_HOSTS_CONFIG', '/etc/puppet/infrastructure/hiera/private/hosts.yaml')
+
+# 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.
+BASENAME = File.basename($0)
+
+# 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.
+# still recognizes a --help option, which is considered the bare minimum by
+# convention. Nonetheless, future options may become integrated here (see
+# also http://ruby-doc.org/stdlib/libdoc/getoptlong/rdoc/GetoptLong.html):
+GetoptLong.new(
+ ['--help', '-h', GetoptLong::NO_ARGUMENT]
+).each do |opt, arg|
+ case opt
+
+ when '--help'
+ puts <<-END
+Usage: #{BASENAME} [hostname]
+ #{BASENAME} example.com
+ #{BASENAME} --help
+
+Options:
+
+ --help, -h
+ Display this help message and exit gracefully.
+
+Environment:
+
+ PUPPET_HIERA_CONFIG=#{HIERA_CONFIG}
+ Where to find the hiera configuration file.
+ PUPPET_HOSTS_CONFIG=#{HOSTS_CONFIG}
+ Where to find the hosts configuration file.
+
+END
+ exit 0
+
+ end
+end
+
+# 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
+# specify a hostname to use instead of the default:
+case ARGV.length
+ when 0
+ hostname = Socket.gethostname
+ when 1
+ hostname = ARGV[0][/^[^.]+/]
+ else
+ STDERR.puts <<-END
+#{BASENAME}: unknown option: #{ARGV[0]}
+#{BASENAME}: try #{BASENAME} --help
+END
+ exit 1
+end
+
+# Extract the server -> hostname -> role information from the hosts
+# configuration file:
+begin
+ config = YAML.load_file(HOSTS_CONFIG)
+ servers = config.fetch("servers", {})
+ host = servers.fetch(hostname, {})
+ role = host.fetch("role", "default")
+rescue Exception => error
+ STDERR.puts "#{BASENAME}: #{error.message}: #{HOSTS_CONFIG}"
+ exit 1
+end
+
+# Map Hiera data into the structure Puppet expects an ENC to generate (see
+# https://docs.puppetlabs.com/guides/external_nodes.html for more info):
+begin
+ hiera = Hiera.new(:config => HIERA_CONFIG)
+ scope = {'::hostname' => hostname, '::role' => role}
+ classes = hiera.lookup('classes', {}, scope, nil, :hash)
+ parameters = hiera.lookup('parameters', {}, scope, nil, :hash)
+ parameters['role'] = role
+ result = { 'classes' => classes, 'parameters' => parameters }
+rescue Exception => error
+ STDERR.puts "#{BASENAME}: #{error.message}: #{HIERA_CONFIG}"
+ exit 1
+end
+
+puts result.to_yaml
+

Powered by Google App Engine
This is Rietveld