diff options
author | Luke Kanies <luke@madstop.com> | 2009-10-14 17:12:56 -0700 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-10-24 08:36:58 +1100 |
commit | 41b7c3c70082a03563a6b59f23e72ba3735e6efe (patch) | |
tree | 6536278a2e0c27736ab40e4c3b18db93e81ddcfb | |
parent | 66a44ddc3032654109036371a5f3a60dd2ab4a9f (diff) | |
download | puppet-41b7c3c70082a03563a6b59f23e72ba3735e6efe.tar.gz puppet-41b7c3c70082a03563a6b59f23e72ba3735e6efe.tar.xz puppet-41b7c3c70082a03563a6b59f23e72ba3735e6efe.zip |
Adding an example yaml node script
Signed-off-by: Luke Kanies <luke@madstop.com>
-rwxr-xr-x | ext/yaml_nodes.rb | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/ext/yaml_nodes.rb b/ext/yaml_nodes.rb new file mode 100755 index 000000000..4715b16de --- /dev/null +++ b/ext/yaml_nodes.rb @@ -0,0 +1,99 @@ +#!/usr/bin/ruby +# +# = Synopsis +# +# Use YAML files to provide external node support. +# +# = Usage +# +# yaml-nodes <host> +# +# = Description +# +# This is a simple example external node script. It allows you to maintain your +# node information in yaml files, and it will find a given node's file and produce +# it on stdout. It has simple inheritance, in that a node can specify a parent +# node, and the node will inherit that parent's classes and parameters. +# +# = Options +# +# help:: +# Print this help message +# +# yamldir:: +# Specify where the yaml is found. Defaults to 'yaml' in the current directory. +# +# = Author +# +# Luke Kanies +# +# = Copyright +# +# Copyright (c) 2009 Reductive Labs, Inc. +# Licensed under the GPL2 + +require 'yaml' +require 'optparse' + +BASEDIR = Dir.chdir(File.dirname(__FILE__) + "/..") { Dir.getwd } + +options = {:yamldir => File.join(BASEDIR, "yaml")} +OptionParser.new do |opts| + opts.banner = "Usage: yaml-nodes [options] <host>" + + opts.on("-y dir", "--yamldir dir", "Specify the directory with the YAML files") do |arg| + raise "YAML directory #{arg} does not exist or is not a directory" unless FileTest.directory?(arg) + options[:yamldir] = arg + end + + opts.on("-h", "--help", "Print this help") do + puts opts.help + exit(0) + end +end.parse! + +# Read in a pure yaml representation of our node. +def read_node(node) + nodefile = File.join(YAMLDIR, "#{node}.yaml") + if FileTest.exist?(nodefile) + return YAML.load_file(nodefile) + else + raise "Could not find information for %s" % node + end +end + +node = ARGV[0] + +info = read_node(node) + +# Iterate over any provided parents, merging in there information. +parents_seen = [] +while parent = info["parent"] + raise "Found inheritance loop with parent %s" % parent if parents_seen.include?(parent) + + parents_seen << parent + + info.delete("parent") + + parent_info = read_node(parent) + + # Include any parent classes in our list. + if pclasses = parent_info["classes"] + info["classes"] += pclasses + info["classes"].uniq! + end + + # And inherit parameters from our parent, while preferring our own values. + if pparams = parent_info["parameters"] + # When using Hash#merge, the hash being merged in wins, and we + # want the subnode parameters to be the parent node parameters. + info["parameters"] = pparams.merge(info["parameters"]) + end + + # Copy over any parent node name. + if pparent = parent_info["parent"] + info["parent"] = pparent + end +end + +puts YAML.dump(info) |