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 - Without Arrow Alignment Created March 16, 2015, 2:04 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
« no previous file with comments | « hiera/install_precise.py ('k') | hiera/roles/codereviewserver.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: hiera/puppet_node_classifier.rb
===================================================================
new file mode 100755
--- /dev/null
+++ b/hiera/puppet_node_classifier.rb
@@ -0,0 +1,90 @@
+#!/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')
+
+# For logging and usage hints
+BASENAME = File.basename($0)
+
+# There's no need for any options beside the commonly exepected ones yet
+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
+# 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
+
« no previous file with comments | « hiera/install_precise.py ('k') | hiera/roles/codereviewserver.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld