Please review pull request #25: (#13684) Modify hiera function to fall back to default if config absent opened by (eshamow)
Description:
As heira gets integrated into the workflow for Puppet, we should
expect users to be able to write modules using the hiera call and
upload those to the forge. At the same time we should expect that
a large number of hosts won't have the hiera library installed yet.
To enable this behavior, the hiera method call should allow module
writers to specify a default value. We then expect the following
behavior:
- If no hiera.yaml is present and the default value is not set, fail.
- If no hiera.yaml is present but there is a default value, opt for the default, and warn to log about the hiera config.
- If the hiera.yaml is present but not readable, throw an error and stop. We only want hiera to stop if the user has attempted to configure hiera, at which point we can see that there is a config failure and that we are not in our default install state.
- Opened: Thu May 10 16:54:53 UTC 2012
- Based on: puppetlabs:master (59b8bf64abaa89472f8b0aa593e439f05258843a)
- Requested merge: eshamow:tickets/feature/13684_graceful_hiera_function_failure (b87afbe0892ae8166cb60030f1f702628eabd60d)
Diff follows:
diff --git a/lib/puppet/parser/functions/hiera.rb b/lib/puppet/parser/functions/hiera.rb
index ed5404f..b86c3b4 100644
--- a/lib/puppet/parser/functions/hiera.rb
+++ b/lib/puppet/parser/functions/hiera.rb
@@ -19,10 +19,34 @@ module Puppet::Parser::Functions
configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
- raise(Puppet::ParseError, "Hiera config file #{configfile} not readable") unless File.exist?(configfile)
+ # As heira gets integrated into the workflow for Puppet, we should
+ # expect users to be able to write modules using the hiera call and
+ # upload those to the forge. At the same time we should expect that
+ # a large number of hosts won't have the hiera library installed yet.
+ # To enable this behavior, the hiera method call should allow module
+ # writers to specify a default value. We then expect the following
+ # behavior:
+ # * If no hiera.yaml is present and the default value is not set, fail.
+ # * If no hiera.yaml is present but there is a default value, opt for
+ # the default, and warn to log about the hiera config.
+ # * If the hiera.yaml is present but not readable, throw an error and
+ # stop.
+ # We only want hiera to stop if the user has attempted to configure
+ # hiera, at which point we can see that there is a config failure
+ # and that we are not in our default install state.
+ if default.nil? && !File.exist?(configfile)
+ raise(Puppet::ParseError, "Hiera config file #{configfile} not readable")
+ elsif !File.exist?(configfile)
+ Puppet.warning "Hiera config file #{configfile} not readable, using default value"
+ return default
+ end
- require 'hiera'
- require 'hiera/scope'
+ begin
+ require 'hiera'
+ require 'hiera/scope'
+ rescue LoadError
+ raise(Puppet::ParseError, "Hiera lookup not supported without hiera library")
+ end
config = YAML.load_file(configfile)
config[:logger] = "puppet"
-- You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.
