diff --git a/Puppetfile b/Puppetfile index 03873332b..3d79d9218 100644 --- a/Puppetfile +++ b/Puppetfile @@ -67,7 +67,7 @@ mod 'memcached', :git => 'https://github.com/saz/puppet-memcached.git' mod 'module-data', - :commit => '159fc5e0e21ce9df96c777f0064b5eca88e29cae', + :commit => '010c9eb3496f515582a4d4868154e5fb246ee9b8', :git => 'https://github.com/ripienaar/puppet-module-data.git' mod 'mongodb', diff --git a/module-data/.gitignore b/module-data/.gitignore new file mode 100644 index 000000000..d8fe4fa70 --- /dev/null +++ b/module-data/.gitignore @@ -0,0 +1 @@ +/.project diff --git a/module-data/Modulefile b/module-data/Modulefile index 5c05ad258..976df9ccb 100644 --- a/module-data/Modulefile +++ b/module-data/Modulefile @@ -1,5 +1,5 @@ name 'ripienaar-module_data' -version '0.0.3' +version '0.0.4' description 'A hiera backend to allow the use of data while writing sharable modules' project_page 'https://github.com/ripienaar/puppet-module-data' license 'ASL 2.0' diff --git a/module-data/lib/hiera/backend/module_data_backend.rb b/module-data/lib/hiera/backend/module_data_backend.rb index 5e8ff3a37..a865bb8cf 100644 --- a/module-data/lib/hiera/backend/module_data_backend.rb +++ b/module-data/lib/hiera/backend/module_data_backend.rb @@ -14,9 +14,9 @@ def load_module_config(module_name, environment) default_config = {:hierarchy => ["common"]} mod = Puppet::Module.find(module_name, environment) - + return default_config unless mod - + path = mod.path module_config = File.join(path, "data", "hiera.yaml") config = {} @@ -25,7 +25,7 @@ def load_module_config(module_name, environment) Hiera.debug("Reading config from %s file" % module_config) config = load_data(module_config) end - + config["path"] = path default_config.merge(config) @@ -44,15 +44,19 @@ def lookup(key, scope, order_override, resolution_type) Hiera.debug("Looking up %s in Module Data backend" % key) - unless scope["module_name"] + module_name = begin + scope["module_name"] + rescue Puppet::ParseError # Gets thrown if not in a module and strict_variables = true + end + + unless module_name Hiera.debug("Skipping Module Data backend as this does not look like a module") return answer end - config = load_module_config(scope["module_name"], scope["environment"]) - + config = load_module_config(scope["module_name"], scope["::environment"]) unless config["path"] - Hiera.debug("Could not find a path to the module '%s' in environment '%s'" % [scope["module_name"], scope["environment"]]) + Hiera.debug("Could not find a path to the module '%s' in environment '%s'" % [scope["module_name"], scope["::environment"]]) return answer end @@ -67,19 +71,19 @@ def lookup(key, scope, order_override, resolution_type) next if data.empty? next unless data.include?(key) - found = data[key] - + new_answer = Backend.parse_answer(data[key], scope) case resolution_type when :array - raise("Hiera type mismatch: expected Array or String and got %s" % found.class) unless [Array, String].include?(found.class) + raise("Hiera type mismatch: expected Array and got %s" % new_answer.class) unless (new_answer.kind_of?(Array) || new_answer.kind_of?(String)) answer ||= [] - answer << Backend.parse_answer(found, scope) + answer << new_answer when :hash - raise("Hiera type mismatch: expected Hash and got %s" % found.class) unless found.is_a?(Hash) - answer = Backend.parse_answer(found, scope).merge(answer || {}) + raise("Hiera type mismatch: expected Hash and got %s" % new_answer.class) unless new_answer.kind_of?(Hash) + answer ||= {} + answer = Backend.merge_answer(new_answer, answer) else - answer = Backend.parse_answer(found, scope) + answer = new_answer break end end diff --git a/module-data/metadata.json b/module-data/metadata.json new file mode 100644 index 000000000..44df666d8 --- /dev/null +++ b/module-data/metadata.json @@ -0,0 +1,10 @@ +{ + "author": "R.I.Pienaar ", + "license": "ASL 2.0", + "name": "ripienaar-module_data", + "project_page": "https://github.com/ripienaar/puppet-module-data", + "source": "https://github.com/ripienaar/puppet-module-data.git", + "summary": "A hiera backend to allow the use of data while writing sharable modules", + "version": "0.4.0", + "dependencies": [] +}